| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?php
- include('settings.php');
- include('reformat-blocks.php');
- include('related-items.php');
- include('formats.php');
-
- class Make_Endpoint_For extends WP_REST_Controller {
- private $post_type;
- function __construct($post_type) {
- $this->post_type = $post_type;
- }
- /**
- * Register the routes for the objects of the controller.
- */
- public function register_custom_route($route) {
- $version = '2';
- $namespace = 'craft/v' . $version;
-
- register_rest_route( $namespace, '/' . $route, [
- array(
- 'methods' => WP_REST_Server::READABLE,
- 'callback' => array( $this, 'get_all_items' )
- ),
- ]);
-
- register_rest_route( $namespace, '/' . $route . '/(?P<id>[\d]+)', [
- array(
- 'methods' => WP_REST_Server::READABLE,
- 'callback' => array( $this, 'get_single_item' )
- ),
- ]);
- }
- public function get_single_item( $request ) {
- $args = array(
- 'numberposts' => 1,
- 'post_type' => $this->post_type
- );
-
- $params = $request->get_params();
- if(intval($params['limit']) > 0) { $args['numberposts'] = intval($params['limit']); }
- if(intval($params['id']) > 0) { $args['include'] = array($params['id']); }
-
- return new WP_REST_Response( $this->prepare_single_item_for_response($args), 200 );
- }
- public function get_all_items( $request ) {
- $args = array(
- 'post_type' => $this->post_type,
- 'post_status' => ['publish'],
- 'posts_per_page' => -1,
- 'page' => 1,
- 'orderby' => 'date',
- 'order' => 'DESC'
- );
-
- // Get parameters from request,
- // /?limit=<num>&?orderby=<str>&?order=<str>
- $params = $request->get_params();
- if(intval($params['limit']) > 0) {
- $args['posts_per_page'] = intval($params['limit']);
- }
- if(intval($params['id']) > 0) {
- $args['include'] = array($params['id']);
- }
- if(intval($params['p']) > 1) {
- $args['page'] = intval($params['p']);
- $args['offset'] = ($args['page'] * $args['posts_per_page']) - $args['posts_per_page'];
- }
- if($params['orderby']) {
- $args['orderby'] = $params['orderby'];
- }
- if($params['order']) {
- $args['order'] = $params['order'];
- }
-
- return new WP_REST_Response( $this->prepare_all_items_for_response($args), 200 );
- }
- public function prepare_single_item_for_response( $args ) {
- $collection = array();
-
- // https://developer.wordpress.org/reference/functions/get_posts/
- foreach( get_posts($args) as $item ) {
- $filtered = default_post_format( $item, false );
-
- $filtered[galleries] = get_ids_from_gallery_block(
- parse_blocks($item->post_content)
- );
-
- $filtered[attached] = get_images_from_content($item->post_content);
-
- // For your hero URL
- $filtered[hero] = get_post_meta( $item->ID, 'hero_header', true );
-
- $filtered[relatedto] = p2p_related_to( $item->ID, $item->post_type );
-
- $content = $item->post_content;
- $content = apply_filters('the_content', $content);
- $filtered[content] = str_replace(']]>', ']]>', $content);
-
- if($item->post_type === 'episode') $filtered[credits] = get_post_meta( $item->ID, 'credits', true );
-
- array_push($collection, $filtered);
- }
- wp_reset_postdata();
-
- return $collection;
- }
- public function prepare_all_items_for_response( $args ) {
- $collection = [];
- $alphabet_section = 'a';
-
- $q = new WP_Query( $args );
-
- $args['max'] = $q->max_num_pages;
- $found_posts = $q->get_posts();
-
- foreach( $found_posts as $post ) {
- // print_r($args);
- // print_r($post);
- $formatted = default_post_format( $post, false );
-
- // For your hero URL
- $formatted[hero] = get_post_meta( $post->ID, 'hero_header', true );
-
- // Sticky
- $formatted[sticky] = get_post_meta( $post->ID, 'is_sticky', true );
-
- // Don't use the BLOCK system for pages for some reason
- if($post->post_type == 'page') $formatted[content] = $post->post_content;
-
- // This is where you add to your letter counter
- if($post->post_type == 'artist') {}
-
- array_push($collection, $formatted);
- }
- wp_reset_postdata();
- // Uncomment to include pagination and query information
- // $collection['_meta'] = $q;
- return $collection;
- }
- }
-
- ?>
|