| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?php
-
- class Make_Sort_By extends WP_REST_Controller {
- private $post_type;
- private $sort_type;
- function __construct($post_type, $sort_type) {
- $this->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=<num>&?orderby=<str>&?order=<str>
- $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';
- }
-
- if($this->post_type == 'artist') {
- // Default to all materials
- $tax_id_array = [2, 3, 4, 5, 6, 7, 8];
-
- if ($params['materials']) {
- $tax_id_array = explode( ',', $params['materials'] );
- }
- // 2 clay
- // 3 fiber
- // 4 glass
- // 5 metals
- // 6 other
- // 7 paper
- // 8 wood
- $args['tax_query'] = array(
- [
- 'taxonomy' => 'material',
- 'field' => 'term_id',
- 'terms' => $tax_id_array
- ]
- );
- }
-
- return $args;
- }
- /**
- * 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=<num>&?orderby=<str>&?order=<str>
- $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_material( $request ) {
- $q = new WP_Query($this->make_args($request, null));
- // $args['max'] $q->max_num_pages;
- $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, '<='));
- // $args['max'] $q->max_num_pages;
- $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, '>='));
- // $args['max'] $q->max_num_pages;
- $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'] . 's';
- // TODO: optimize this query
- // $items = $q->get_posts();
- $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",
- trim($type, 's'), $type . "_to_episodes" , "episodes_to_" . $type,
- ));
- 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;
- }
- }
-
- ?>
|