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

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