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

class.make-sortby.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
  35. }
  36. public function by_material( $request ) {
  37. global $wpdb;
  38. // !: Make this a real query
  39. $res = $wpdb->get_results($wpdb->prepare(
  40. "SELECT * FROM wp_posts
  41. WHERE post_type = %s
  42. AND post_status = 'publish'",
  43. $this->post_type
  44. ));
  45. wp_reset_postdata();
  46. return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
  47. }
  48. public function by_past( $request ) {
  49. global $wpdb;
  50. $time = strval(time());
  51. $end = 'exhibit-end-date';
  52. if($this->post_type == 'event') {
  53. $end = 'event-end-time';
  54. }
  55. $res = $wpdb->get_results($wpdb->prepare(
  56. "SELECT DISTINCT wp_posts.* FROM wp_posts
  57. JOIN wp_postmeta AS ends
  58. ON (ends.post_id = wp_posts.ID AND ends.meta_key = %s AND ends.meta_value <= %s)
  59. WHERE post_type = %s AND post_status = 'publish'
  60. ORDER BY ends.meta_value DESC",
  61. $end, $time, $this->post_type
  62. ));
  63. wp_reset_postdata();
  64. return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
  65. }
  66. public function by_current( $request ) {
  67. global $wpdb;
  68. $time = strval(time());
  69. $start = 'exhibit-start-date';
  70. $end = 'exhibit-end-date';
  71. if($this->post_type == 'event') {
  72. $start = 'event-start-time';
  73. $end = 'event-end-time';
  74. }
  75. $res = $wpdb->get_results($wpdb->prepare(
  76. "SELECT DISTINCT wp_posts.* FROM wp_posts
  77. JOIN wp_postmeta AS starts
  78. ON (starts.post_id = wp_posts.ID AND starts.meta_key = %s AND starts.meta_value <= %s)
  79. JOIN wp_postmeta AS ends
  80. ON (ends.post_id = wp_posts.ID AND ends.meta_key = %s AND ends.meta_value >= %s)
  81. WHERE post_type = %s AND post_status = 'publish'
  82. ORDER BY starts.meta_value DESC",
  83. $start, $time, $end, $time, $this->post_type
  84. ));
  85. wp_reset_postdata();
  86. return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
  87. }
  88. public function by_current_and_upcoming( $request ) {
  89. global $wpdb;
  90. $time = strval(time());
  91. $start = 'exhibit-start-date';
  92. $end = 'exhibit-end-date';
  93. if($this->post_type == 'event') {
  94. $start = 'event-start-time';
  95. $end = 'event-end-time';
  96. }
  97. $res = $wpdb->get_results($wpdb->prepare(
  98. "SELECT DISTINCT wp_posts.* FROM wp_posts
  99. JOIN wp_postmeta AS starts
  100. ON (starts.post_id = wp_posts.ID AND starts.meta_key = %s AND starts.meta_value <= %s OR starts.meta_value > %s)
  101. JOIN wp_postmeta AS ends
  102. ON (ends.post_id = wp_posts.ID AND ends.meta_key = %s AND ends.meta_value >= %s)
  103. WHERE post_type = %s AND post_status = 'publish'
  104. ORDER BY ends.meta_value ASC",
  105. $start, $time, $time, $end, $time, $this->post_type
  106. ));
  107. wp_reset_postdata();
  108. return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
  109. }
  110. public function by_upcoming( $request ) {
  111. global $wpdb;
  112. $time = strval(time());
  113. $start = 'exhibit-start-date';
  114. if($this->post_type == 'event') {
  115. $start = 'event-start-time';
  116. }
  117. $res = $wpdb->get_results($wpdb->prepare(
  118. "SELECT DISTINCT wp_posts.* FROM wp_posts
  119. JOIN wp_postmeta AS starts
  120. ON (starts.post_id = wp_posts.ID AND starts.meta_key = %s AND starts.meta_value >= %s)
  121. WHERE post_type = %s AND post_status = 'publish'
  122. ORDER BY ends.meta_value ASC",
  123. $start, $time, $this->post_type
  124. ));
  125. wp_reset_postdata();
  126. return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
  127. }
  128. public function prepare_items_for_reponse( $items ) {
  129. $collection = array();
  130. forEach( $items as $key => $item ) $collection[$key] = default_post_format($item, true);
  131. return $collection;
  132. }
  133. }
  134. ?>