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[\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( 'numberposts' => -1, 'post_type' => $this->post_type, // Order DOES NOT WORK because you're // returning the posts by id - DUH // 'orderby' => 'date', // 'order' => 'DESC' ); // Get parameters from request // /?limit= $params = $request->get_params(); if(intval($params['limit']) > 0) { $args['numberposts'] = intval($params['limit']); } if(intval($params['id']) > 0) { $args['include'] = array($params['id']); } // !: Add order asc/desc // !: Add orderby return new WP_REST_Response( $this->prepare_all_items_for_response($args), 200 ); } private function _getAttachments($item, $galleries) { // Get media $desiredSizes = ['thumbnail', 'medium', 'large', 'full']; $all_image_ids_in_gallery = array(); $all_image_ids = array(); foreach ( $galleries as $gallery ) { array_push( $all_image_ids_in_gallery, $gallery[attrs][ids] ); } foreach ( $all_image_ids_in_gallery as $ids ) { foreach ( $ids as $id ) { array_push($all_image_ids, $id); } } $attachment_map = array(); foreach ( $all_image_ids as $id ) { $imageSizes = array(); foreach ( $desiredSizes as $size ) { $imageSizes[$size] = wp_get_attachment_image_url( $id, $size ); } // store size:url map under image id $attachment_map[$id] = $imageSizes; } return $attachment_map; } 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 ); // Get those Block! $filtered[blocks] = get_rearrange_blocks( parse_blocks( $item->post_content ) ); // Galleries From blocks $media_ids = array(); foreach ( get_attached_media( '', $item->ID ) as $attached_media ): array_push($media_ids, $attached_media->ID); endforeach; $filtered[galleries] = get_ids_from_gallery_block( parse_blocks( $item->post_content ) ); // Find all image info $filtered[attached] = $this->_getAttachments( $item, $filtered[galleries] ); // For your hero URL $filtered[hero] = get_post_meta( $item->ID, 'hero_header', true ); $filtered[credits] = get_post_meta( $item->ID, 'credits', true ); $filtered[relatedto] = p2p_related_to( $item->ID, $item->post_type ); $collection[$item->ID] = $filtered; } wp_reset_postdata(); return $collection; } public function prepare_all_items_for_response( $args ) { $collection = array(); // https://developer.wordpress.org/reference/functions/get_posts/ foreach( get_posts($args) as $item ) { $filtered = default_post_format( $item ); // Find all image info $filtered[attached] = $this->_getAttachments( $item, $filtered[galleries] ); // For your hero URL $filtered[hero] = get_post_meta( $item->ID, 'hero_header', true ); // Sticky $filtered[sticky] = get_post_meta( $item->ID, 'is_sticky', true ); // $filtered[relatedto] = p2p_related_to( $item->ID, $item->post_type ); $collection[$item->ID] = $filtered; } wp_reset_postdata(); return $collection; } } ?>