| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- include('formats.php');
- include('reformat-blocks.php');
- include('related-items.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'];
- }
- // Unify interface for post categories and <type> subtypes
- // See:custom-taxonomies.php for a list of custom subtypes
- if($params['type']) {
- $taxonomy_name = $this->post_type . '_type';
- if($this->post_type == 'post') { $taxonomy_name = 'category'; }
- $args['tax_query'] = array(
- [
- 'taxonomy' => $taxonomy_name,
- 'field' => 'slug',
- 'terms' => $params['type']
- ]
- );
- }
-
- 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, true );
- $filtered[galleries] = get_ids_from_gallery_block(
- parse_blocks($item->post_content)
- );
- $filtered[blocks] = parse_blocks($item->post_content);
- $filtered[attached] = get_images_from_content($item->post_content);
- $filtered[relatedto] = p2p_related_to( $item->ID, false );
- array_push($collection, $filtered);
- }
- wp_reset_postdata();
-
- return $collection;
- }
- public function prepare_all_items_for_response( $args ) {
- $collection = [];
-
- $q = new WP_Query( $args );
-
- $args['max'] = $q->max_num_pages;
- $found_posts = $q->get_posts();
-
- foreach( $found_posts as $post ) {
- $formatted = default_post_format( $post, false );
- $formatted[sticky] = get_post_meta( $post->ID, 'is_sticky', true );
- if($post->post_type == 'page') $formatted[content] = $post->post_content;
-
- array_push($collection, $formatted);
- }
- wp_reset_postdata();
- // Uncomment to include pagination and query information
- // $collection['_meta'] = $q;
- return $collection;
- }
- }
-
- ?>
|