NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

class.make-sortby.php 6.2KB

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