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

class.make-sortby.php 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. function make_args($request, $comparitor) {
  10. $args = [
  11. 'post_type' => $this->post_type,
  12. 'post_status' => ['publish'],
  13. 'posts_per_page' => -1,
  14. 'page' => 1
  15. ];
  16. // Get parameters from request,
  17. // /?limit=<num>&?orderby=<str>&?order=<str>
  18. $params = $request->get_params();
  19. if(intval($params['limit']) > 0) {
  20. $args['posts_per_page'] = intval($params['limit']);
  21. }
  22. if(intval($params['p']) > 1) {
  23. $args['page'] = intval($params['p']);
  24. $args['offset'] = ($args['page'] * $args['posts_per_page']) - $args['posts_per_page'];
  25. }
  26. if($this->post_type == 'event' || $this->post_type == 'exhibition') {
  27. $time = strval(time());
  28. $key = $this->post_type == 'exhibition' ? 'exhibit-end-date' : 'event-end-time';
  29. $args['meta_query'] = array(
  30. ['key' => $key, 'value' => $time, 'compare' => $comparitor]
  31. );
  32. $args['orderby'] = 'ends';
  33. }
  34. return $args;
  35. }
  36. function make_terms_array($taxonomy, $field) {
  37. $terms = get_terms([
  38. 'taxonomy' => $taxonomy,
  39. 'hide_empty' => false,
  40. 'fields' => $field
  41. ]);
  42. return $terms;
  43. }
  44. /**
  45. * Register the routes for the objects of the controller.
  46. */
  47. public function register_custom_route($route) {
  48. $version = '2';
  49. $namespace = 'craft/v' . $version;
  50. register_rest_route( $namespace, '/sort/' . $route, [
  51. array(
  52. 'methods' => WP_REST_Server::READABLE,
  53. 'callback' => array( $this, $this->sort_type )
  54. ),
  55. ]);
  56. }
  57. public function by_alpha( $request ) {
  58. global $wpdb;
  59. $res = $wpdb->get_results($wpdb->prepare(
  60. "SELECT DISTINCT wp_posts.*,
  61. IFNULL(wp_postmeta.meta_value, SUBSTRING_INDEX(wp_posts.post_title, ' ', -1)) AS sort_name
  62. FROM wp_posts LEFT JOIN wp_postmeta
  63. ON (wp_postmeta.post_id = wp_posts.ID AND wp_postmeta.meta_key='artist-sort-name')
  64. WHERE wp_posts.post_type='artist' AND wp_posts.post_status='publish'
  65. ORDER BY sort_name",
  66. $this->post_type
  67. ));
  68. wp_reset_postdata();
  69. // Get parameters from request,
  70. // /?limit=<num>&?orderby=<str>&?order=<str>
  71. $args = array(
  72. 'posts_per_page' => -1,
  73. 'page' => 1
  74. );
  75. $params = $request->get_params();
  76. if(intval($params['limit']) > 0) {
  77. $args['posts_per_page'] = intval($params['limit']);
  78. }
  79. if(intval($params['p']) > 1) {
  80. $args['page'] = intval($params['p']);
  81. }
  82. $index_from_page = $args['page'] - 1;
  83. $offset = array_chunk($res, $args['posts_per_page']);
  84. $page = $offset[$index_from_page];
  85. return new WP_REST_Response( $this->prepare_items_for_reponse($page, false), 200 );
  86. }
  87. public function by_type( $request ) {
  88. $args = $this->make_args($request, null);
  89. $params = $request->get_params();
  90. // Get all term ID's in a given taxonomy
  91. $taxonomy = $this->post_type . '_type';
  92. if($this->post_type == 'post') $taxonomy = 'category';
  93. $tax_id_array = $this->make_terms_array($taxonomy, 'ids');
  94. $args['tax_query'] = array([
  95. 'taxonomy' => $taxonomy,
  96. 'field' => 'term_id',
  97. 'terms' => $tax_id_array,
  98. ]);
  99. $q = new WP_Query($args);
  100. $found_posts = $q->get_posts();
  101. wp_reset_postdata();
  102. return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts), 200 );
  103. }
  104. public function by_material( $request ) {
  105. $args = $this->make_args($request, null);
  106. $params = $request->get_params();
  107. // Default to all materials
  108. $taxonomy = 'material';
  109. $tax_id_array = $this->make_terms_array($taxonomy, 'ids');
  110. // Filter with a materials array
  111. // 2: clay
  112. // 3: fiber
  113. // 4: glass
  114. // 5: metals
  115. // 6: other
  116. // 7: paper
  117. // 8: wood
  118. if ($params['materials']) $tax_id_array = explode( ',', $params['materials'] );
  119. $args['tax_query'] = array([
  120. 'taxonomy' => $taxonomy,
  121. 'field' => 'term_id',
  122. 'terms' => $tax_id_array
  123. ]);
  124. $q = new WP_Query($args);
  125. $found_posts = $q->get_posts();
  126. wp_reset_postdata();
  127. return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts), 200 );
  128. }
  129. public function by_past( $request ) {
  130. $q = new WP_Query($this->make_args($request, '<='));
  131. $found_posts = $q->get_posts();
  132. wp_reset_postdata();
  133. return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts), 200 );
  134. }
  135. public function by_current_and_upcoming( $request ) {
  136. $q = new WP_Query($this->make_args($request, '>='));
  137. $found_posts = $q->get_posts();
  138. wp_reset_postdata();
  139. return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts), 200 );
  140. }
  141. public function by_episode( $request ) {
  142. $q = new WP_Query($this->make_args($request, null));
  143. global $wpdb;
  144. $type = $q->query['post_type'] . 's';
  145. // TODO: optimize this query
  146. // $items = $q->get_posts();
  147. $items = $wpdb->get_results($wpdb->prepare(
  148. "SELECT DISTINCT wp_posts.*,
  149. header.post_title AS related_episode
  150. FROM wp_posts LEFT JOIN wp_p2p
  151. ON (wp_p2p.p2p_from = wp_posts.ID OR wp_p2p.p2p_to = wp_posts.ID)
  152. LEFT JOIN wp_posts AS header
  153. ON header.ID = IF(wp_posts.ID = wp_p2p.p2p_to, wp_p2p.p2p_from, wp_p2p.p2p_to)
  154. WHERE wp_posts.post_type = %s AND wp_posts.post_status = 'publish'
  155. AND (wp_p2p.p2p_type = %s OR wp_p2p.p2p_type = %s)
  156. ORDER BY header.post_date DESC, related_episode, wp_posts.post_date DESC",
  157. trim($type, 's'), $type . "_to_episodes" , "episodes_to_" . $type,
  158. ));
  159. wp_reset_postdata();
  160. return new WP_REST_Response( $this->prepare_items_for_reponse($items), 200 );
  161. }
  162. public function prepare_items_for_reponse( $items ) {
  163. $collection = [];
  164. forEach( $items as $item ) {
  165. $formatted = default_post_format( $item, false );
  166. $formatted[hero] = get_post_meta( $item->ID, 'hero_header', true );
  167. array_push($collection, $formatted);
  168. }
  169. return $collection;
  170. }
  171. }
  172. ?>