| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?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_items' )
- ),
- ]);
- register_rest_route( $namespace, '/' . $route . '/(?P<id>[\d]+)', [
- array(
- 'methods' => WP_REST_Server::READABLE,
- 'callback' => array( $this, 'get_items' )
- ),
- ]);
- }
-
- public function get_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
- // /<id>?limit=<num>
- $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_item_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[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_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[relatedto] = p2p_related_to($item->ID, $item->post_type);
-
- $collection[$item->ID] = $filtered;
- }
- wp_reset_postdata();
-
- return $collection;
- }
- }
-
- ?>
|