| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- <?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;
- }
- /**
- * 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();
-
- return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
- }
-
- public function by_material( $request ) {
- global $wpdb;
- // !: Make this a real query
- $res = $wpdb->get_results($wpdb->prepare(
- "SELECT * FROM wp_posts
- WHERE post_type = %s
- AND post_status='publish'",
- $this->post_type
- ));
- wp_reset_postdata();
-
- return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
- }
-
- public function by_upcoming_event( $request ) {
- global $wpdb;
- $time = new DateTime();
- $timestamp = $time->getTimestamp();
- $res = $wpdb->get_results($wpdb->prepare(
- "SELECT DISTINCT wp_posts.*
- FROM wp_posts LEFT JOIN wp_postmeta AS ends
- ON (ends.post_id = wp_posts.ID AND ends.meta_key='event-end-time' AND ends.meta_value <= %d)
- WHERE post_type = %s
- AND CONVERT('event-end-time', DATETIME) >= CONVERT('2021-10-31', DATETIME)
- AND post_status='publish'
- ORDER BY ends.meta_value DESC",
- $time, $this->post_type
- ));
- wp_reset_postdata();
-
- return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
- }
- public function by_upcoming_exhibition( $request ) {
- global $wpdb;
- $res = $wpdb->get_results($wpdb->prepare(
- "SELECT DISTINCT wp_posts.*
- FROM wp_posts LEFT JOIN wp_postmeta
- ON (wp_postmeta.post_id = wp_posts.ID AND wp_postmeta.meta_key='exhibit-end-date')
- WHERE post_type = 'exhibition'
- AND post_status='publish'
- ORDER BY 'exhibit-end-date'",
- $this->post_type
- ));
- wp_reset_postdata();
-
- return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
- }
-
- public function prepare_items_for_reponse( $items ) {
- $collection = array();
- forEach( $items as $key => $item ) $collection[$key] = default_post_format($item, true);
- return $collection;
- }
- }
-
- ?>
|