NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

class.make-sortby.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. class Make_Sort_By extends WP_REST_Controller {
  3. private $post_type;
  4. private $sort_type;
  5. function __construct($post_type, $sort_type) {
  6. $this->post_type = $post_type;
  7. $this->sort_type = $sort_type;
  8. }
  9. /**
  10. * Register the routes for the objects of the controller.
  11. */
  12. public function register_custom_route($route) {
  13. $version = '2';
  14. $namespace = 'craft/v' . $version;
  15. register_rest_route( $namespace, '/sort/' . $route, [
  16. array(
  17. 'methods' => WP_REST_Server::READABLE,
  18. 'callback' => array( $this, $this->sort_type )
  19. ),
  20. ]);
  21. }
  22. function _get_args($params) {
  23. $args = [
  24. 'post_type' => $this->post_type,
  25. 'posts_per_page' => 99,
  26. 'page' => 1
  27. ];
  28. if(intval($params['limit']) > 0) {
  29. $args['posts_per_page'] = intval($params['limit']);
  30. }
  31. if(intval($params['p']) > 1) {
  32. $args['page'] = intval($params['p']);
  33. }
  34. return $args;
  35. }
  36. function _get_page_items($res, $args) {
  37. // Get parameters from request,
  38. // /?limit=<num>&?orderby=<str>&?order=<str>
  39. $index_from_page = $args['page'] - 1;
  40. $offset = array_chunk($res, $args['posts_per_page']);
  41. return $offset[$index_from_page];
  42. }
  43. public function by_alpha( $request ) {
  44. global $wpdb;
  45. $res = $wpdb->get_results($wpdb->prepare(
  46. "SELECT DISTINCT wp_posts.*,
  47. IFNULL(wp_postmeta.meta_value, SUBSTRING_INDEX(wp_posts.post_title, ' ', -1)) AS sort_name
  48. FROM wp_posts LEFT JOIN wp_postmeta
  49. ON (wp_postmeta.post_id = wp_posts.ID AND wp_postmeta.meta_key='artist-sort-name')
  50. WHERE wp_posts.post_type='artist' AND wp_posts.post_status='publish'
  51. ORDER BY sort_name",
  52. $this->post_type
  53. ));
  54. wp_reset_postdata();
  55. $args = $this->_get_args($request->get_params());
  56. $found_posts = $this->_get_page_items($res, $args);
  57. return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts), 200 );
  58. }
  59. public function by_material( $request ) {
  60. global $wpdb;
  61. // !: Make this a real query
  62. $res = $wpdb->get_results($wpdb->prepare(
  63. "SELECT * FROM wp_posts
  64. WHERE post_type = %s
  65. AND post_status = 'publish'",
  66. $this->post_type
  67. ));
  68. wp_reset_postdata();
  69. return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
  70. }
  71. public function by_past( $request ) {
  72. global $wpdb;
  73. $time = strval(time());
  74. $end = 'exhibit-end-date';
  75. if($this->post_type == 'event') {
  76. $end = 'event-end-time';
  77. }
  78. $res = $wpdb->get_results($wpdb->prepare(
  79. "SELECT DISTINCT wp_posts.* FROM wp_posts
  80. JOIN wp_postmeta AS ends
  81. ON (ends.post_id = wp_posts.ID AND ends.meta_key = %s AND ends.meta_value <= %s)
  82. WHERE post_type = %s AND post_status = 'publish'
  83. ORDER BY ends.meta_value DESC",
  84. $end, $time, $this->post_type
  85. ));
  86. wp_reset_postdata();
  87. $args = $this->_get_args($request->get_params());
  88. $found_posts = $this->_get_page_items($res, $args);
  89. return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts), 200 );
  90. }
  91. public function by_current( $request ) {
  92. global $wpdb;
  93. $time = strval(time());
  94. $start = 'exhibit-start-date';
  95. $end = 'exhibit-end-date';
  96. if($this->post_type == 'event') {
  97. $start = 'event-start-time';
  98. $end = 'event-end-time';
  99. }
  100. $res = $wpdb->get_results($wpdb->prepare(
  101. "SELECT DISTINCT wp_posts.* FROM wp_posts
  102. JOIN wp_postmeta AS starts
  103. ON (starts.post_id = wp_posts.ID AND starts.meta_key = %s AND starts.meta_value <= %s)
  104. JOIN wp_postmeta AS ends
  105. ON (ends.post_id = wp_posts.ID AND ends.meta_key = %s AND ends.meta_value >= %s)
  106. WHERE post_type = %s AND post_status = 'publish'
  107. ORDER BY starts.meta_value DESC",
  108. $start, $time, $end, $time, $this->post_type
  109. ));
  110. wp_reset_postdata();
  111. return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
  112. }
  113. public function by_current_and_upcoming( $request ) {
  114. $time = strval(time());
  115. $start = 'exhibit-start-date';
  116. $end = 'exhibit-end-date';
  117. if($this->post_type == 'event') {
  118. $start = 'event-start-time';
  119. $end = 'event-end-time';
  120. }
  121. $args = $this->_get_args($request->get_params());
  122. $args['meta_query'] = [
  123. 'relation' => 'AND',
  124. [
  125. 'relation' => 'OR',
  126. ['key' => $start, 'value' => $time, 'compare' => '>'],
  127. ['key' => $start, 'value' => $time, 'compare' => '<='],
  128. ],
  129. ['key' => $end, 'value' => $time, 'compare' => '>=']
  130. ];
  131. $q = new WP_Query($args);
  132. $found_posts = $q->get_posts();
  133. wp_reset_postdata();
  134. // return new WP_REST_Response( $this->prepare_items_for_reponse($this->_get_page_items($res, $request)), 200 );
  135. return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts), 200 );
  136. }
  137. public function by_upcoming( $request ) {
  138. global $wpdb;
  139. $time = strval(time());
  140. $start = 'exhibit-start-date';
  141. if($this->post_type == 'event') {
  142. $start = 'event-start-time';
  143. }
  144. $res = $wpdb->get_results($wpdb->prepare(
  145. "SELECT DISTINCT wp_posts.* FROM wp_posts
  146. JOIN wp_postmeta AS starts
  147. ON (starts.post_id = wp_posts.ID AND starts.meta_key = %s AND starts.meta_value >= %s)
  148. WHERE post_type = %s AND post_status = 'publish'
  149. ORDER BY ends.meta_value ASC",
  150. $start, $time, $this->post_type
  151. ));
  152. wp_reset_postdata();
  153. return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
  154. }
  155. public function prepare_items_for_reponse( $items ) {
  156. $collection = [];
  157. forEach( $items as $key => $item ) $collection[$key] = default_post_format($item, false);
  158. return $collection;
  159. }
  160. }
  161. ?>