Parcourir la source

:sparkles: paginating alpha sort | simplifying sort pagination

tags/0.9.0
J il y a 4 ans
Parent
révision
041a57e3a2
1 fichiers modifiés avec 26 ajouts et 21 suppressions
  1. 26
    21
      plugins/cia-endpoints/includes/class.make-sortby.php

+ 26
- 21
plugins/cia-endpoints/includes/class.make-sortby.php Voir le fichier

@@ -19,6 +19,28 @@ class Make_Sort_By extends WP_REST_Controller {
19 19
             ),
20 20
         ]);
21 21
     }
22
+    function _get_args($params) {
23
+        $args = [
24
+            'posts_per_page' => 99,
25
+            'page' => 1
26
+        ];
27
+        if(intval($params['limit']) > 0) {
28
+            $args['posts_per_page'] = intval($params['limit']);
29
+        }
30
+        if(intval($params['p']) > 1) {
31
+            $args['page'] = intval($params['p']);
32
+        }
33
+        return $args;
34
+    }
35
+    function _get_page_items($res, $request) {
36
+        // Get parameters from request,
37
+        // /?limit=<num>&?orderby=<str>&?order=<str>
38
+        $args = $this->_get_args($request->get_params());
39
+        
40
+        $index_from_page = $args['page'] - 1;
41
+        $offset = array_chunk($res, $args['posts_per_page']);
42
+        return $offset[$index_from_page];
43
+    }
22 44
 
23 45
     public function by_alpha( $request ) {
24 46
         global $wpdb;
@@ -33,7 +55,7 @@ class Make_Sort_By extends WP_REST_Controller {
33 55
         ));
34 56
         wp_reset_postdata();
35 57
 
36
-        return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
58
+        return new WP_REST_Response( $this->prepare_items_for_reponse($this->_get_page_items($res, $request)), 200 );
37 59
     }
38 60
 
39 61
     public function by_material( $request ) {
@@ -66,7 +88,7 @@ class Make_Sort_By extends WP_REST_Controller {
66 88
             $end, $time, $this->post_type
67 89
         ));
68 90
         wp_reset_postdata();
69
-        return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
91
+        return new WP_REST_Response( $this->prepare_items_for_reponse($this->_get_page_items($res, $request)), 200 );
70 92
     }
71 93
 
72 94
     public function by_current( $request ) {
@@ -112,24 +134,7 @@ class Make_Sort_By extends WP_REST_Controller {
112 134
             $start, $time, $time, $end, $time, $this->post_type
113 135
         ));
114 136
         wp_reset_postdata();
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 );
137
+        return new WP_REST_Response( $this->prepare_items_for_reponse($this->_get_page_items($res, $request)), 200 );
133 138
     }
134 139
     
135 140
     public function by_upcoming( $request ) {
@@ -152,7 +157,7 @@ class Make_Sort_By extends WP_REST_Controller {
152 157
     }
153 158
 
154 159
     public function prepare_items_for_reponse( $items ) {
155
-        $collection = array();
160
+        $collection = [];
156 161
         forEach( $items as $key => $item ) $collection[$key] = default_post_format($item, false);
157 162
         return $collection;
158 163
     }

Chargement…
Annuler
Enregistrer