|
|
@@ -21,6 +21,7 @@ class Make_Sort_By extends WP_REST_Controller {
|
|
21
|
21
|
}
|
|
22
|
22
|
function _get_args($params) {
|
|
23
|
23
|
$args = [
|
|
|
24
|
+ 'post_type' => $this->post_type,
|
|
24
|
25
|
'posts_per_page' => 99,
|
|
25
|
26
|
'page' => 1
|
|
26
|
27
|
];
|
|
|
@@ -32,11 +33,9 @@ class Make_Sort_By extends WP_REST_Controller {
|
|
32
|
33
|
}
|
|
33
|
34
|
return $args;
|
|
34
|
35
|
}
|
|
35
|
|
- function _get_page_items($res, $request) {
|
|
|
36
|
+ function _get_page_items($res, $args) {
|
|
36
|
37
|
// Get parameters from request,
|
|
37
|
38
|
// /?limit=<num>&?orderby=<str>&?order=<str>
|
|
38
|
|
- $args = $this->_get_args($request->get_params());
|
|
39
|
|
-
|
|
40
|
39
|
$index_from_page = $args['page'] - 1;
|
|
41
|
40
|
$offset = array_chunk($res, $args['posts_per_page']);
|
|
42
|
41
|
return $offset[$index_from_page];
|
|
|
@@ -54,8 +53,9 @@ class Make_Sort_By extends WP_REST_Controller {
|
|
54
|
53
|
$this->post_type
|
|
55
|
54
|
));
|
|
56
|
55
|
wp_reset_postdata();
|
|
57
|
|
-
|
|
58
|
|
- return new WP_REST_Response( $this->prepare_items_for_reponse($this->_get_page_items($res, $request)), 200 );
|
|
|
56
|
+ $args = $this->_get_args($request->get_params());
|
|
|
57
|
+ $found_posts = $this->_get_page_items($res, $args);
|
|
|
58
|
+ return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts), 200 );
|
|
59
|
59
|
}
|
|
60
|
60
|
|
|
61
|
61
|
public function by_material( $request ) {
|
|
|
@@ -88,7 +88,9 @@ class Make_Sort_By extends WP_REST_Controller {
|
|
88
|
88
|
$end, $time, $this->post_type
|
|
89
|
89
|
));
|
|
90
|
90
|
wp_reset_postdata();
|
|
91
|
|
- return new WP_REST_Response( $this->prepare_items_for_reponse($this->_get_page_items($res, $request)), 200 );
|
|
|
91
|
+ $args = $this->_get_args($request->get_params());
|
|
|
92
|
+ $found_posts = $this->_get_page_items($res, $args);
|
|
|
93
|
+ return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts), 200 );
|
|
92
|
94
|
}
|
|
93
|
95
|
|
|
94
|
96
|
public function by_current( $request ) {
|
|
|
@@ -115,7 +117,6 @@ class Make_Sort_By extends WP_REST_Controller {
|
|
115
|
117
|
}
|
|
116
|
118
|
|
|
117
|
119
|
public function by_current_and_upcoming( $request ) {
|
|
118
|
|
- global $wpdb;
|
|
119
|
120
|
$time = strval(time());
|
|
120
|
121
|
$start = 'exhibit-start-date';
|
|
121
|
122
|
$end = 'exhibit-end-date';
|
|
|
@@ -123,18 +124,22 @@ class Make_Sort_By extends WP_REST_Controller {
|
|
123
|
124
|
$start = 'event-start-time';
|
|
124
|
125
|
$end = 'event-end-time';
|
|
125
|
126
|
}
|
|
126
|
|
- $res = $wpdb->get_results($wpdb->prepare(
|
|
127
|
|
- "SELECT DISTINCT wp_posts.* FROM wp_posts
|
|
128
|
|
- JOIN wp_postmeta AS starts
|
|
129
|
|
- ON (starts.post_id = wp_posts.ID AND starts.meta_key = %s AND starts.meta_value <= %s OR starts.meta_value > %s)
|
|
130
|
|
- JOIN wp_postmeta AS ends
|
|
131
|
|
- ON (ends.post_id = wp_posts.ID AND ends.meta_key = %s AND ends.meta_value >= %s)
|
|
132
|
|
- WHERE post_type = %s AND post_status = 'publish'
|
|
133
|
|
- ORDER BY ends.meta_value ASC",
|
|
134
|
|
- $start, $time, $time, $end, $time, $this->post_type
|
|
135
|
|
- ));
|
|
|
127
|
+ $args = $this->_get_args($request->get_params());
|
|
|
128
|
+ $args['meta_query'] = [
|
|
|
129
|
+ 'relation' => 'AND',
|
|
|
130
|
+ [
|
|
|
131
|
+ 'relation' => 'OR',
|
|
|
132
|
+ ['key' => $start, 'value' => $time, 'compare' => '>'],
|
|
|
133
|
+ ['key' => $start, 'value' => $time, 'compare' => '<='],
|
|
|
134
|
+ ],
|
|
|
135
|
+ ['key' => $end, 'value' => $time, 'compare' => '>=']
|
|
|
136
|
+ ];
|
|
|
137
|
+ $q = new WP_Query($args);
|
|
|
138
|
+ $found_posts = $q->get_posts();
|
|
|
139
|
+
|
|
136
|
140
|
wp_reset_postdata();
|
|
137
|
|
- return new WP_REST_Response( $this->prepare_items_for_reponse($this->_get_page_items($res, $request)), 200 );
|
|
|
141
|
+ // return new WP_REST_Response( $this->prepare_items_for_reponse($this->_get_page_items($res, $request)), 200 );
|
|
|
142
|
+ return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts), 200 );
|
|
138
|
143
|
}
|
|
139
|
144
|
|
|
140
|
145
|
public function by_upcoming( $request ) {
|