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

class.make-sortby.php 4.8KB

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