NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

class.make-sortby.php 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. $args['tax_query'] = array([
  94. 'taxonomy' => $taxonomy,
  95. 'operator' => 'EXISTS'
  96. ]);
  97. $q = new WP_Query($args);
  98. $found_posts = $q->get_posts();
  99. wp_reset_postdata();
  100. return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts), 200 );
  101. }
  102. public function by_material( $request ) {
  103. $args = $this->make_args($request, null);
  104. $params = $request->get_params();
  105. // Default to all materials
  106. $taxonomy = 'material';
  107. $tax_id_array = $this->make_terms_array($taxonomy, 'ids');
  108. // Filter with a materials array
  109. // 2: clay
  110. // 3: fiber
  111. // 4: glass
  112. // 5: metals
  113. // 6: other
  114. // 7: paper
  115. // 8: wood
  116. if ($params['materials']) $tax_id_array = explode( ',', $params['materials'] );
  117. $args['tax_query'] = array([
  118. 'taxonomy' => $taxonomy,
  119. 'field' => 'term_id',
  120. 'terms' => $tax_id_array
  121. ]);
  122. $q = new WP_Query($args);
  123. $found_posts = $q->get_posts();
  124. wp_reset_postdata();
  125. return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts), 200 );
  126. }
  127. public function by_past( $request ) {
  128. $q = new WP_Query($this->make_args($request, '<='));
  129. $found_posts = $q->get_posts();
  130. wp_reset_postdata();
  131. return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts), 200 );
  132. }
  133. public function by_current_and_upcoming( $request ) {
  134. $q = new WP_Query($this->make_args($request, '>='));
  135. $found_posts = $q->get_posts();
  136. wp_reset_postdata();
  137. return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts), 200 );
  138. }
  139. public function by_episode( $request ) {
  140. $q = new WP_Query($this->make_args($request, null));
  141. global $wpdb;
  142. $type = $q->query['post_type'];
  143. // TODO: optimize this query
  144. $items = $wpdb->get_results($wpdb->prepare(
  145. "SELECT DISTINCT wp_posts.*,
  146. header.post_title AS related_episode
  147. FROM wp_posts LEFT JOIN wp_p2p
  148. ON (wp_p2p.p2p_from = wp_posts.ID OR wp_p2p.p2p_to = wp_posts.ID)
  149. LEFT JOIN wp_posts AS header
  150. ON header.ID = IF(wp_posts.ID = wp_p2p.p2p_to, wp_p2p.p2p_from, wp_p2p.p2p_to)
  151. WHERE wp_posts.post_type = %s AND wp_posts.post_status = 'publish'
  152. AND (wp_p2p.p2p_type = %s OR wp_p2p.p2p_type = %s)
  153. ORDER BY header.post_date DESC, related_episode, wp_posts.post_date DESC",
  154. $type, $type . 's_to_episodes' , 'episodes_to_' . $type . 's',
  155. ));
  156. wp_reset_postdata();
  157. return new WP_REST_Response( $this->prepare_items_for_reponse($items), 200 );
  158. }
  159. public function prepare_items_for_reponse( $items ) {
  160. $collection = [];
  161. forEach( $items as $item ) {
  162. $formatted = default_post_format( $item, false );
  163. $formatted[hero] = get_post_meta( $item->ID, 'hero_header', true );
  164. array_push($collection, $formatted);
  165. }
  166. return $collection;
  167. }
  168. }
  169. ?>