post_type = $post_type; $this->sort_type = $sort_type; } function make_args($request, $comparitor) { $args = [ 'post_type' => $this->post_type, 'post_status' => ['publish'], 'posts_per_page' => -1, 'page' => 1 ]; // Get parameters from request, // /?limit=&?orderby=&?order= $params = $request->get_params(); if(intval($params['limit']) > 0) { $args['posts_per_page'] = intval($params['limit']); } if(intval($params['p']) > 1) { $args['page'] = intval($params['p']); $args['offset'] = ($args['page'] * $args['posts_per_page']) - $args['posts_per_page']; } if($this->post_type == 'event' || $this->post_type == 'exhibition') { $time = strval(time()); $key = $this->post_type == 'exhibition' ? 'exhibit-end-date' : 'event-end-time'; $args['meta_query'] = array( ['key' => $key, 'value' => $time, 'compare' => $comparitor] ); $args['orderby'] = 'ends'; } return $args; } function make_terms_array($taxonomy, $field) { $terms = get_terms([ 'taxonomy' => $taxonomy, 'hide_empty' => false, 'fields' => $field ]); return $terms; } /** * 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, '/sort/' . $route, [ array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, $this->sort_type ) ), ]); } public function by_alpha( $request ) { global $wpdb; $res = $wpdb->get_results($wpdb->prepare( "SELECT DISTINCT wp_posts.*, IFNULL(wp_postmeta.meta_value, SUBSTRING_INDEX(wp_posts.post_title, ' ', -1)) AS sort_name FROM wp_posts LEFT JOIN wp_postmeta ON (wp_postmeta.post_id = wp_posts.ID AND wp_postmeta.meta_key='artist-sort-name') WHERE wp_posts.post_type='artist' AND wp_posts.post_status='publish' ORDER BY sort_name", $this->post_type )); wp_reset_postdata(); // Get parameters from request, // /?limit=&?orderby=&?order= $args = array( 'posts_per_page' => -1, 'page' => 1 ); $params = $request->get_params(); if(intval($params['limit']) > 0) { $args['posts_per_page'] = intval($params['limit']); } if(intval($params['p']) > 1) { $args['page'] = intval($params['p']); } $index_from_page = $args['page'] - 1; $offset = array_chunk($res, $args['posts_per_page']); $page = $offset[$index_from_page]; return new WP_REST_Response( $this->prepare_items_for_reponse($page, false), 200 ); } public function by_type( $request ) { $args = $this->make_args($request, null); $params = $request->get_params(); // Get all term ID's in a given taxonomy $taxonomy = $this->post_type . '_type'; if($this->post_type == 'post') $taxonomy = 'category'; $args['tax_query'] = array([ 'taxonomy' => $taxonomy, 'operator' => 'EXISTS' ]); $q = new WP_Query($args); $found_posts = $q->get_posts(); wp_reset_postdata(); return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts), 200 ); } public function by_material( $request ) { $args = $this->make_args($request, null); $params = $request->get_params(); // Default to all materials $taxonomy = 'material'; $tax_id_array = $this->make_terms_array($taxonomy, 'ids'); // Filter with a materials array // 2: clay // 3: fiber // 4: glass // 5: metals // 6: other // 7: paper // 8: wood if ($params['materials']) $tax_id_array = explode( ',', $params['materials'] ); $args['tax_query'] = array([ 'taxonomy' => $taxonomy, 'field' => 'term_id', 'terms' => $tax_id_array ]); $q = new WP_Query($args); $found_posts = $q->get_posts(); wp_reset_postdata(); return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts), 200 ); } public function by_past( $request ) { $q = new WP_Query($this->make_args($request, '<=')); $found_posts = $q->get_posts(); wp_reset_postdata(); return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts), 200 ); } public function by_current_and_upcoming( $request ) { $q = new WP_Query($this->make_args($request, '>=')); $found_posts = $q->get_posts(); wp_reset_postdata(); return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts), 200 ); } public function by_episode( $request ) { $q = new WP_Query($this->make_args($request, null)); global $wpdb; $type = $q->query['post_type']; // TODO: optimize this query $items = $wpdb->get_results($wpdb->prepare( "SELECT DISTINCT wp_posts.*, header.post_title AS related_episode FROM wp_posts LEFT JOIN wp_p2p ON (wp_p2p.p2p_from = wp_posts.ID OR wp_p2p.p2p_to = wp_posts.ID) LEFT JOIN wp_posts AS header ON header.ID = IF(wp_posts.ID = wp_p2p.p2p_to, wp_p2p.p2p_from, wp_p2p.p2p_to) WHERE wp_posts.post_type = %s AND wp_posts.post_status = 'publish' AND (wp_p2p.p2p_type = %s OR wp_p2p.p2p_type = %s) ORDER BY header.post_date DESC, related_episode, wp_posts.post_date DESC", $type, $type . 's_to_episodes' , 'episodes_to_' . $type . 's', )); wp_reset_postdata(); return new WP_REST_Response( $this->prepare_items_for_reponse($items), 200 ); } public function prepare_items_for_reponse( $items ) { $collection = []; forEach( $items as $item ) { $formatted = default_post_format( $item, false ); $formatted[hero] = get_post_meta( $item->ID, 'hero_header', true ); array_push($collection, $formatted); } return $collection; } } ?>