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 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. public function by_alpha( $request ) {
  23. global $wpdb;
  24. $res = $wpdb->get_results($wpdb->prepare(
  25. "SELECT DISTINCT wp_posts.*,
  26. IFNULL(wp_postmeta.meta_value, SUBSTRING_INDEX(wp_posts.post_title, ' ', -1)) AS sort_name
  27. FROM wp_posts LEFT JOIN wp_postmeta
  28. ON (wp_postmeta.post_id = wp_posts.ID AND wp_postmeta.meta_key='artist-sort-name')
  29. WHERE wp_posts.post_type='artist' AND wp_posts.post_status='publish'
  30. ORDER BY sort_name",
  31. $this->post_type
  32. ));
  33. wp_reset_postdata();
  34. // Get parameters from request,
  35. // /?limit=<num>&?orderby=<str>&?order=<str>
  36. $args = array(
  37. 'posts_per_page' => -1,
  38. 'page' => 1
  39. );
  40. $params = $request->get_params();
  41. if(intval($params['limit']) > 0) {
  42. $args['posts_per_page'] = intval($params['limit']);
  43. }
  44. if(intval($params['p']) > 1) {
  45. $args['page'] = intval($params['p']);
  46. }
  47. $index_from_page = $args['page'] - 1;
  48. $offset = array_chunk($res, $args['posts_per_page']);
  49. $page = $offset[$index_from_page];
  50. return new WP_REST_Response( $this->prepare_items_for_reponse($page), 200 );
  51. }
  52. public function by_material( $request ) {
  53. global $wpdb;
  54. // !: Make this a real query
  55. $res = $wpdb->get_results($wpdb->prepare(
  56. "SELECT * FROM wp_posts
  57. WHERE post_type = %s
  58. AND post_status = 'publish'",
  59. $this->post_type
  60. ));
  61. wp_reset_postdata();
  62. return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
  63. }
  64. public function by_past( $request ) {
  65. global $wpdb;
  66. $time = strval(time());
  67. $end = 'exhibit-end-date';
  68. if($this->post_type == 'event') {
  69. $end = 'event-end-time';
  70. }
  71. $res = $wpdb->get_results($wpdb->prepare(
  72. "SELECT DISTINCT wp_posts.* FROM wp_posts
  73. JOIN wp_postmeta AS ends
  74. ON (ends.post_id = wp_posts.ID AND ends.meta_key = %s AND ends.meta_value <= %s)
  75. WHERE post_type = %s AND post_status = 'publish'
  76. ORDER BY ends.meta_value DESC",
  77. $end, $time, $this->post_type
  78. ));
  79. wp_reset_postdata();
  80. return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
  81. }
  82. public function by_current( $request ) {
  83. global $wpdb;
  84. $time = strval(time());
  85. $start = 'exhibit-start-date';
  86. $end = 'exhibit-end-date';
  87. if($this->post_type == 'event') {
  88. $start = 'event-start-time';
  89. $end = 'event-end-time';
  90. }
  91. $res = $wpdb->get_results($wpdb->prepare(
  92. "SELECT DISTINCT wp_posts.* FROM wp_posts
  93. JOIN wp_postmeta AS starts
  94. ON (starts.post_id = wp_posts.ID AND starts.meta_key = %s AND starts.meta_value <= %s)
  95. JOIN wp_postmeta AS ends
  96. ON (ends.post_id = wp_posts.ID AND ends.meta_key = %s AND ends.meta_value >= %s)
  97. WHERE post_type = %s AND post_status = 'publish'
  98. ORDER BY starts.meta_value DESC",
  99. $start, $time, $end, $time, $this->post_type
  100. ));
  101. wp_reset_postdata();
  102. return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
  103. }
  104. public function by_current_and_upcoming( $request ) {
  105. global $wpdb;
  106. $time = strval(time());
  107. $start = 'exhibit-start-date';
  108. $end = 'exhibit-end-date';
  109. if($this->post_type == 'event') {
  110. $start = 'event-start-time';
  111. $end = 'event-end-time';
  112. }
  113. $res = $wpdb->get_results($wpdb->prepare(
  114. "SELECT DISTINCT wp_posts.* FROM wp_posts
  115. JOIN wp_postmeta AS starts
  116. ON (starts.post_id = wp_posts.ID AND starts.meta_key = %s AND starts.meta_value <= %s OR starts.meta_value > %s)
  117. JOIN wp_postmeta AS ends
  118. ON (ends.post_id = wp_posts.ID AND ends.meta_key = %s AND ends.meta_value >= %s)
  119. WHERE post_type = %s AND post_status = 'publish'
  120. ORDER BY ends.meta_value ASC",
  121. $start, $time, $time, $end, $time, $this->post_type
  122. ));
  123. wp_reset_postdata();
  124. // Get parameters from request,
  125. // /?limit=<num>&?orderby=<str>&?order=<str>
  126. $args = array(
  127. 'posts_per_page' => -1,
  128. 'page' => 1
  129. );
  130. $params = $request->get_params();
  131. if(intval($params['limit']) > 0) {
  132. $args['posts_per_page'] = intval($params['limit']);
  133. }
  134. if(intval($params['p']) > 1) {
  135. $args['page'] = intval($params['p']);
  136. }
  137. $index_from_page = $args['page'] - 1;
  138. $offset = array_chunk($res, $args['posts_per_page']);
  139. $page = $offset[$index_from_page];
  140. return new WP_REST_Response( $this->prepare_items_for_reponse($page), 200 );
  141. }
  142. public function prepare_items_for_reponse( $items ) {
  143. $collection = array();
  144. $formatted = default_post_format($item, false);
  145. $formatted[hero] = get_post_meta( $post->ID, 'hero_header', true );
  146. forEach( $items as $key => $item ) $collection[$key] = $formatted;
  147. return $collection;
  148. }
  149. }
  150. ?>