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

class.make-sortby.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. if($params['episode']) {
  56. $args['p2p_query'] = $params['episode'];
  57. $args['tax_query'] = [];
  58. }
  59. return $args;
  60. }
  61. /**
  62. * Register the routes for the objects of the controller.
  63. */
  64. public function register_custom_route($route) {
  65. $version = '2';
  66. $namespace = 'craft/v' . $version;
  67. register_rest_route( $namespace, '/sort/' . $route, [
  68. array(
  69. 'methods' => WP_REST_Server::READABLE,
  70. 'callback' => array( $this, $this->sort_type )
  71. ),
  72. ]);
  73. }
  74. public function by_alpha( $request ) {
  75. global $wpdb;
  76. $res = $wpdb->get_results($wpdb->prepare(
  77. "SELECT DISTINCT wp_posts.*,
  78. IFNULL(wp_postmeta.meta_value, SUBSTRING_INDEX(wp_posts.post_title, ' ', -1)) AS sort_name
  79. FROM wp_posts LEFT JOIN wp_postmeta
  80. ON (wp_postmeta.post_id = wp_posts.ID AND wp_postmeta.meta_key='artist-sort-name')
  81. WHERE wp_posts.post_type='artist' AND wp_posts.post_status='publish'
  82. ORDER BY sort_name",
  83. $this->post_type
  84. ));
  85. wp_reset_postdata();
  86. // Get parameters from request,
  87. // /?limit=<num>&?orderby=<str>&?order=<str>
  88. $args = array(
  89. 'posts_per_page' => -1,
  90. 'page' => 1
  91. );
  92. $params = $request->get_params();
  93. if(intval($params['limit']) > 0) {
  94. $args['posts_per_page'] = intval($params['limit']);
  95. }
  96. if(intval($params['p']) > 1) {
  97. $args['page'] = intval($params['p']);
  98. }
  99. $index_from_page = $args['page'] - 1;
  100. $offset = array_chunk($res, $args['posts_per_page']);
  101. $page = $offset[$index_from_page];
  102. return new WP_REST_Response( $this->prepare_items_for_reponse($page, false), 200 );
  103. }
  104. public function by_material( $request ) {
  105. $q = new WP_Query($this->make_args($request, null));
  106. // $args['max'] $q->max_num_pages;
  107. $found_posts = $q->get_posts();
  108. wp_reset_postdata();
  109. return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts), 200 );
  110. }
  111. public function by_past( $request ) {
  112. $q = new WP_Query($this->make_args($request, '<='));
  113. // $args['max'] $q->max_num_pages;
  114. $found_posts = $q->get_posts();
  115. wp_reset_postdata();
  116. return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts), 200 );
  117. }
  118. public function by_current_and_upcoming( $request ) {
  119. $q = new WP_Query($this->make_args($request, '>='));
  120. // $args['max'] $q->max_num_pages;
  121. $found_posts = $q->get_posts();
  122. wp_reset_postdata();
  123. return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts), 200 );
  124. }
  125. public function by_episode( $request ) {
  126. $q = new WP_Query($this->make_args($request, null));
  127. $episode_name = $q->query->p2p_query;
  128. $items = $this->prepare_items_for_reponse($q->get_posts());
  129. wp_reset_postdata();
  130. return new WP_REST_Response( $items, 200 );
  131. }
  132. public function prepare_items_for_reponse( $items ) {
  133. $collection = [];
  134. forEach( $items as $item ) {
  135. $formatted = default_post_format( $item, false );
  136. $formatted[hero] = get_post_meta( $item->ID, 'hero_header', true );
  137. $formatted[relatedto] = p2p_related_to( $item->ID, false );
  138. array_push($collection, $formatted);
  139. }
  140. return $collection;
  141. }
  142. }
  143. ?>