Bladeren bron

:recycle: reverting to old sort queries

tags/0.9.0
J 4 jaren geleden
bovenliggende
commit
42d912c69f
2 gewijzigde bestanden met toevoegingen van 27 en 60 verwijderingen
  1. 0
    4
      plugins/cia-endpoints/cia-end-points.php
  2. 27
    56
      plugins/cia-endpoints/includes/class.make-sortby.php

+ 0
- 4
plugins/cia-endpoints/cia-end-points.php Bestand weergeven

@@ -66,10 +66,6 @@ add_action( 'rest_api_init', function () {
66 66
     $sort_controller->register_custom_route('event/by-current');
67 67
     $sort_controller = new Make_Sort_By('exhibition', 'by_current');
68 68
     $sort_controller->register_custom_route('exhibition/by-current');
69
-    $sort_controller = new Make_Sort_By('event', 'by_upcoming');
70
-    $sort_controller->register_custom_route('event/by-upcoming');
71
-    $sort_controller = new Make_Sort_By('exhibition', 'by_upcoming');
72
-    $sort_controller->register_custom_route('exhibition/by-upcoming');
73 69
     $sort_controller = new Make_Sort_By('event', 'by_current_and_upcoming');
74 70
     $sort_controller->register_custom_route('event/by-current-and-upcoming');
75 71
     $sort_controller = new Make_Sort_By('exhibition', 'by_current_and_upcoming');

+ 27
- 56
plugins/cia-endpoints/includes/class.make-sortby.php Bestand weergeven

@@ -19,27 +19,6 @@ class Make_Sort_By extends WP_REST_Controller {
19 19
             ),
20 20
         ]);
21 21
     }
22
-    function _get_args($params) {
23
-        $args = [
24
-            'post_type' => $this->post_type,
25
-            'posts_per_page' => 99,
26
-            'page' => 1
27
-        ];
28
-        if(intval($params['limit']) > 0) {
29
-            $args['posts_per_page'] = intval($params['limit']);
30
-        }
31
-        if(intval($params['p']) > 1) {
32
-            $args['page'] = intval($params['p']);
33
-        }
34
-        return $args;
35
-    }
36
-    function _get_page_items($res, $args) {
37
-        // Get parameters from request,
38
-        // /?limit=<num>&?orderby=<str>&?order=<str>
39
-        $index_from_page = $args['page'] - 1;
40
-        $offset = array_chunk($res, $args['posts_per_page']);
41
-        return $offset[$index_from_page];
42
-    }
43 22
 
44 23
     public function by_alpha( $request ) {
45 24
         global $wpdb;
@@ -53,9 +32,8 @@ class Make_Sort_By extends WP_REST_Controller {
53 32
             $this->post_type
54 33
         ));
55 34
         wp_reset_postdata();
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 );
35
+
36
+        return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
59 37
     }
60 38
 
61 39
     public function by_material( $request ) {
@@ -88,9 +66,7 @@ class Make_Sort_By extends WP_REST_Controller {
88 66
             $end, $time, $this->post_type
89 67
         ));
90 68
         wp_reset_postdata();
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 );
69
+        return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
94 70
     }
95 71
 
96 72
     public function by_current( $request ) {
@@ -117,6 +93,7 @@ class Make_Sort_By extends WP_REST_Controller {
117 93
     }
118 94
     
119 95
     public function by_current_and_upcoming( $request ) {
96
+        global $wpdb;
120 97
         $time = strval(time());
121 98
         $start = 'exhibit-start-date';
122 99
         $end = 'exhibit-end-date';
@@ -124,45 +101,39 @@ class Make_Sort_By extends WP_REST_Controller {
124 101
             $start = 'event-start-time';
125 102
             $end = 'event-end-time';
126 103
         }
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
-        
140
-        wp_reset_postdata();
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 );
143
-    }
144
-    
145
-    public function by_upcoming( $request ) {
146
-        global $wpdb;
147
-        $time = strval(time());
148
-        $start = 'exhibit-start-date';
149
-        if($this->post_type == 'event') {
150
-            $start = 'event-start-time';
151
-        }
152 104
         $res = $wpdb->get_results($wpdb->prepare(
153 105
             "SELECT DISTINCT wp_posts.* FROM wp_posts 
154 106
             JOIN wp_postmeta AS starts
155
-            ON (starts.post_id = wp_posts.ID AND starts.meta_key = %s AND starts.meta_value >= %s)
107
+            ON (starts.post_id = wp_posts.ID AND starts.meta_key = %s AND starts.meta_value <= %s OR starts.meta_value > %s)
108
+            JOIN wp_postmeta AS ends
109
+            ON (ends.post_id = wp_posts.ID AND ends.meta_key = %s AND ends.meta_value >= %s)
156 110
             WHERE post_type = %s AND post_status = 'publish'
157 111
             ORDER BY ends.meta_value ASC",
158
-            $start, $time, $this->post_type
112
+            $start, $time, $time, $end, $time, $this->post_type
159 113
         ));
160 114
         wp_reset_postdata();
161
-        return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
115
+
116
+        // Get parameters from request,
117
+        // /?limit=<num>&?orderby=<str>&?order=<str>
118
+        $args = array(
119
+            'posts_per_page' => -1,
120
+            'page' => 1
121
+        );
122
+        $params = $request->get_params();
123
+        if(intval($params['limit']) > 0) {
124
+            $args['posts_per_page'] = intval($params['limit']);
125
+        }
126
+        if(intval($params['p']) > 1) {
127
+            $args['page'] = intval($params['p']);
128
+        }
129
+        $index_from_page = $args['page'] - 1;
130
+        $offset = array_chunk($res, $args['posts_per_page']);
131
+        $page = $offset[$index_from_page];
132
+        return new WP_REST_Response( $this->prepare_items_for_reponse($page), 200 );
162 133
     }
163 134
 
164 135
     public function prepare_items_for_reponse( $items ) {
165
-        $collection = [];
136
+        $collection = array();
166 137
         forEach( $items as $key => $item ) $collection[$key] = default_post_format($item, false);
167 138
         return $collection;
168 139
     }

Laden…
Annuleren
Opslaan