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

class.make-sortby.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. include('related-items.php');
  3. class Make_Sort_By extends WP_REST_Controller {
  4. private $post_type;
  5. private $sort_type;
  6. function __construct($post_type, $sort_type) {
  7. $this->post_type = $post_type;
  8. $this->sort_type = $sort_type;
  9. }
  10. function make_args($request, $comparitor) {
  11. $args = [
  12. 'post_type' => $this->post_type,
  13. 'post_status' => ['publish'],
  14. 'posts_per_page' => -1,
  15. 'page' => 1
  16. ];
  17. // Get parameters from request,
  18. // /?limit=<num>&?orderby=<str>&?order=<str>
  19. $params = $request->get_params();
  20. if(intval($params['limit']) > 0) {
  21. $args['posts_per_page'] = intval($params['limit']);
  22. }
  23. if(intval($params['p']) > 1) {
  24. $args['page'] = intval($params['p']);
  25. $args['offset'] = ($args['page'] * $args['posts_per_page']) - $args['posts_per_page'];
  26. }
  27. if($this->post_type == 'event' || $this->post_type == 'exhibition') {
  28. $time = strval(time());
  29. $key = $this->post_type == 'exhibition' ? 'exhibit-end-date' : 'event-end-time';
  30. $args['meta_query'] = array(
  31. ['key' => $key, 'value' => $time, 'compare' => $comparitor]
  32. );
  33. $args['orderby'] = 'ends';
  34. }
  35. if($this->post_type == 'artist') {
  36. // Default to all materials
  37. $tax_id_array = [3, 4, 5, 6, 7, 8];
  38. if ($params['materials']) {
  39. $tax_id_array = explode( ',', $params['materials'] );
  40. }
  41. // 3 clay
  42. // 4 fiber
  43. // 5 glass
  44. // 6 metals
  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['by'] = $params['episode'];
  57. }
  58. return $args;
  59. }
  60. /**
  61. * Register the routes for the objects of the controller.
  62. */
  63. public function register_custom_route($route) {
  64. $version = '2';
  65. $namespace = 'craft/v' . $version;
  66. register_rest_route( $namespace, '/sort/' . $route, [
  67. array(
  68. 'methods' => WP_REST_Server::READABLE,
  69. 'callback' => array( $this, $this->sort_type )
  70. ),
  71. ]);
  72. }
  73. public function by_alpha( $request ) {
  74. global $wpdb;
  75. $res = $wpdb->get_results($wpdb->prepare(
  76. "SELECT DISTINCT wp_posts.*,
  77. IFNULL(wp_postmeta.meta_value, SUBSTRING_INDEX(wp_posts.post_title, ' ', -1)) AS sort_name
  78. FROM wp_posts LEFT JOIN wp_postmeta
  79. ON (wp_postmeta.post_id = wp_posts.ID AND wp_postmeta.meta_key='artist-sort-name')
  80. WHERE wp_posts.post_type='artist' AND wp_posts.post_status='publish'
  81. ORDER BY sort_name",
  82. $this->post_type
  83. ));
  84. wp_reset_postdata();
  85. // Get parameters from request,
  86. // /?limit=<num>&?orderby=<str>&?order=<str>
  87. $args = array(
  88. 'posts_per_page' => -1,
  89. 'page' => 1
  90. );
  91. $params = $request->get_params();
  92. if(intval($params['limit']) > 0) {
  93. $args['posts_per_page'] = intval($params['limit']);
  94. }
  95. if(intval($params['p']) > 1) {
  96. $args['page'] = intval($params['p']);
  97. }
  98. $index_from_page = $args['page'] - 1;
  99. $offset = array_chunk($res, $args['posts_per_page']);
  100. $page = $offset[$index_from_page];
  101. return new WP_REST_Response( $this->prepare_items_for_reponse($page, false), 200 );
  102. }
  103. public function by_material( $request ) {
  104. $q = new WP_Query($this->make_args($request, null));
  105. // $args['max'] $q->max_num_pages;
  106. $found_posts = $q->get_posts();
  107. wp_reset_postdata();
  108. return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts, false), 200 );
  109. }
  110. public function by_past( $request ) {
  111. $q = new WP_Query($this->make_args($request, '<='));
  112. // $args['max'] $q->max_num_pages;
  113. $found_posts = $q->get_posts();
  114. wp_reset_postdata();
  115. return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts), 200 );
  116. }
  117. public function by_current_and_upcoming( $request ) {
  118. $q = new WP_Query($this->make_args($request, '>='));
  119. // $args['max'] $q->max_num_pages;
  120. $found_posts = $q->get_posts();
  121. wp_reset_postdata();
  122. return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts, false), 200 );
  123. }
  124. public function by_episode( $request ) {
  125. $q = new WP_Query($this->make_args($request, null));
  126. // $args['max'] $q->max_num_pages;
  127. $found_posts = $q->get_posts();
  128. wp_reset_postdata();
  129. return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts, true), 200 );
  130. }
  131. public function prepare_items_for_reponse( $items, $p2p ) {
  132. $collection = [];
  133. forEach( $items as $item ) {
  134. $formatted = default_post_format( $item, false );
  135. $formatted[hero] = get_post_meta( $item->ID, 'hero_header', true );
  136. if($p2p) {
  137. $formatted[relatedto] = p2p_related_to( $item->ID );
  138. }
  139. array_push($collection, $formatted);
  140. }
  141. return $collection;
  142. }
  143. }
  144. ?>