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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. if($this->post_type == 'artist') {
  35. // Default to all materials
  36. $tax_id_array = [2, 3, 4, 5, 6, 7, 8];
  37. if ($params['materials']) {
  38. $tax_id_array = explode( ',', $params['materials'] );
  39. }
  40. // 2 clay
  41. // 3 fiber
  42. // 4 glass
  43. // 5 metals
  44. // 6 other
  45. // 7 paper
  46. // 8 wood
  47. $args['tax_query'] = array(
  48. [
  49. 'taxonomy' => 'material',
  50. 'field' => 'term_id',
  51. 'terms' => $tax_id_array
  52. ]
  53. );
  54. }
  55. return $args;
  56. }
  57. /**
  58. * Register the routes for the objects of the controller.
  59. */
  60. public function register_custom_route($route) {
  61. $version = '2';
  62. $namespace = 'craft/v' . $version;
  63. register_rest_route( $namespace, '/sort/' . $route, [
  64. array(
  65. 'methods' => WP_REST_Server::READABLE,
  66. 'callback' => array( $this, $this->sort_type )
  67. ),
  68. ]);
  69. }
  70. public function by_alpha( $request ) {
  71. global $wpdb;
  72. $res = $wpdb->get_results($wpdb->prepare(
  73. "SELECT DISTINCT wp_posts.*,
  74. IFNULL(wp_postmeta.meta_value, SUBSTRING_INDEX(wp_posts.post_title, ' ', -1)) AS sort_name
  75. FROM wp_posts LEFT JOIN wp_postmeta
  76. ON (wp_postmeta.post_id = wp_posts.ID AND wp_postmeta.meta_key='artist-sort-name')
  77. WHERE wp_posts.post_type='artist' AND wp_posts.post_status='publish'
  78. ORDER BY sort_name",
  79. $this->post_type
  80. ));
  81. wp_reset_postdata();
  82. // Get parameters from request,
  83. // /?limit=<num>&?orderby=<str>&?order=<str>
  84. $args = array(
  85. 'posts_per_page' => -1,
  86. 'page' => 1
  87. );
  88. $params = $request->get_params();
  89. if(intval($params['limit']) > 0) {
  90. $args['posts_per_page'] = intval($params['limit']);
  91. }
  92. if(intval($params['p']) > 1) {
  93. $args['page'] = intval($params['p']);
  94. }
  95. $index_from_page = $args['page'] - 1;
  96. $offset = array_chunk($res, $args['posts_per_page']);
  97. $page = $offset[$index_from_page];
  98. return new WP_REST_Response( $this->prepare_items_for_reponse($page, false), 200 );
  99. }
  100. public function by_material( $request ) {
  101. $q = new WP_Query($this->make_args($request, null));
  102. // $args['max'] $q->max_num_pages;
  103. $found_posts = $q->get_posts();
  104. wp_reset_postdata();
  105. return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts), 200 );
  106. }
  107. public function by_past( $request ) {
  108. $q = new WP_Query($this->make_args($request, '<='));
  109. // $args['max'] $q->max_num_pages;
  110. $found_posts = $q->get_posts();
  111. wp_reset_postdata();
  112. return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts), 200 );
  113. }
  114. public function by_current_and_upcoming( $request ) {
  115. $q = new WP_Query($this->make_args($request, '>='));
  116. // $args['max'] $q->max_num_pages;
  117. $found_posts = $q->get_posts();
  118. wp_reset_postdata();
  119. return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts), 200 );
  120. }
  121. public function by_episode( $request ) {
  122. $q = new WP_Query($this->make_args($request, null));
  123. global $wpdb;
  124. $type = $q->query['post_type'] . 's';
  125. // TODO: optimize this query
  126. // $items = $q->get_posts();
  127. $items = $wpdb->get_results($wpdb->prepare(
  128. "SELECT DISTINCT wp_posts.*,
  129. header.post_title AS related_episode
  130. FROM wp_posts LEFT JOIN wp_p2p
  131. ON (wp_p2p.p2p_from = wp_posts.ID OR wp_p2p.p2p_to = wp_posts.ID)
  132. LEFT JOIN wp_posts AS header
  133. ON header.ID = IF(wp_posts.ID = wp_p2p.p2p_to, wp_p2p.p2p_from, wp_p2p.p2p_to)
  134. WHERE wp_posts.post_type = %s AND wp_posts.post_status = 'publish'
  135. AND (wp_p2p.p2p_type = %s OR wp_p2p.p2p_type = %s)
  136. ORDER BY header.post_date DESC, related_episode, wp_posts.post_date DESC",
  137. trim($type, 's'), $type . "_to_episodes" , "episodes_to_" . $type,
  138. ));
  139. wp_reset_postdata();
  140. return new WP_REST_Response( $this->prepare_items_for_reponse($items), 200 );
  141. }
  142. public function prepare_items_for_reponse( $items ) {
  143. $collection = [];
  144. forEach( $items as $item ) {
  145. $formatted = default_post_format( $item, false );
  146. $formatted[hero] = get_post_meta( $item->ID, 'hero_header', true );
  147. array_push($collection, $formatted);
  148. }
  149. return $collection;
  150. }
  151. }
  152. ?>