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

class.make-sortby.php 4.4KB

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