Przeglądaj źródła

:recycle: updating materials sort to find terms | updating subtype sort to paginate

tags/0.9.0
J 4 lat temu
rodzic
commit
567263fef4

+ 6
- 0
plugins/cia-endpoints/cia-end-points.php Wyświetl plik

@@ -81,6 +81,12 @@ add_action( 'rest_api_init', function () {
81 81
         'object', 'publication',
82 82
     ];
83 83
     _make_sorts($by_material_types, $material_sorts);
84
+    
85
+    $subtype_sorts = ['by_type'];
86
+    $by_subtype_types = [
87
+        'artist', 'event', 'post'
88
+    ];
89
+    _make_sorts($by_subtype_types, $subtype_sorts);
84 90
 });
85 91
 
86 92
 /**

+ 52
- 29
plugins/cia-endpoints/includes/class.make-sortby.php Wyświetl plik

@@ -34,31 +34,16 @@ class Make_Sort_By extends WP_REST_Controller {
34 34
             $args['orderby'] = 'ends';
35 35
         }
36 36
         
37
-        if($this->post_type == 'artist') {
38
-            //  Default to all materials
39
-            $tax_id_array = [2, 3, 4, 5, 6, 7, 8];
40
-
41
-            if ($params['materials']) {
42
-                $tax_id_array = explode( ',', $params['materials'] );
43
-            }
44
-            // 2 clay
45
-            // 3 fiber
46
-            // 4 glass
47
-            // 5 metals
48
-            // 6 other
49
-            // 7 paper
50
-            // 8 wood
51
-            $args['tax_query'] = array(
52
-                [
53
-                    'taxonomy' => 'material',
54
-                    'field'    => 'term_id',
55
-                    'terms'    => $tax_id_array
56
-                ]
57
-            );
58
-        }
59
-
60 37
         return $args;
61 38
     }
39
+    function make_terms_array($taxonomy, $field) {
40
+        $terms = get_terms([
41
+            'taxonomy'   => $taxonomy,
42
+            'hide_empty' => true,
43
+            'fields'     => $field
44
+        ]);
45
+        return $terms;
46
+    }
62 47
     /**
63 48
      * Register the routes for the objects of the controller.
64 49
      */
@@ -105,9 +90,51 @@ class Make_Sort_By extends WP_REST_Controller {
105 90
         return new WP_REST_Response( $this->prepare_items_for_reponse($page, false), 200 );
106 91
     }
107 92
 
93
+    public function by_type( $request ) {
94
+        $args = $this->make_args($request, null);
95
+        $params = $request->get_params();
96
+        
97
+        // Get all term ID's in a given taxonomy
98
+        $taxonomy = $this->post_type . '_type';
99
+        if($this->post_type == 'post') $taxonomy = 'category';
100
+
101
+        $tax_id_array = $this->make_terms_array($taxonomy, 'ids');
102
+        $args['tax_query'] = array([
103
+            'taxonomy' => $taxonomy,
104
+            'field'    => 'term_id',
105
+            'terms'    => $tax_id_array,
106
+        ]);
107
+
108
+        $q = new WP_Query($args);
109
+        $found_posts = $q->get_posts();
110
+        wp_reset_postdata();
111
+
112
+        return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts), 200 );
113
+    }
114
+    
108 115
     public function by_material( $request ) {
109
-        $q = new WP_Query($this->make_args($request, null));
110
-        // $args['max'] $q->max_num_pages;
116
+        $args = $this->make_args($request, null);
117
+        $params = $request->get_params();
118
+        
119
+        //  Default to all materials
120
+        $taxonomy = 'material';
121
+        $tax_id_array = $this->make_terms_array($taxonomy, 'ids');
122
+
123
+        // Filter with a materials array
124
+        // 2: clay
125
+        // 3: fiber
126
+        // 4: glass
127
+        // 5: metals
128
+        // 6: other
129
+        // 7: paper
130
+        // 8: wood
131
+        if ($params['materials']) $tax_id_array = explode( ',', $params['materials'] );
132
+        $args['tax_query'] = array([
133
+            'taxonomy' => $taxonomy,
134
+            'field'    => 'term_id',
135
+            'terms'    => $tax_id_array
136
+        ]);
137
+        $q = new WP_Query($args);
111 138
         $found_posts = $q->get_posts();
112 139
         wp_reset_postdata();
113 140
 
@@ -116,19 +143,15 @@ class Make_Sort_By extends WP_REST_Controller {
116 143
     
117 144
     public function by_past( $request ) {
118 145
         $q = new WP_Query($this->make_args($request, '<='));
119
-        // $args['max'] $q->max_num_pages;
120 146
         $found_posts = $q->get_posts();
121 147
         wp_reset_postdata();
122
-
123 148
         return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts), 200 );
124 149
     }
125 150
     
126 151
     public function by_current_and_upcoming( $request ) {
127 152
         $q = new WP_Query($this->make_args($request, '>='));
128
-        // $args['max'] $q->max_num_pages;
129 153
         $found_posts = $q->get_posts();
130 154
         wp_reset_postdata();
131
-
132 155
         return new WP_REST_Response( $this->prepare_items_for_reponse($found_posts), 200 );
133 156
     }
134 157
 

+ 8
- 0
plugins/cia-endpoints/includes/class.make-terms.php Wyświetl plik

@@ -2,6 +2,14 @@
2 2
 // include('formats.php');
3 3
 
4 4
 class Make_Terms_Endpoint extends WP_REST_Controller {
5
+    function make_args($request, $post_type) {
6
+        $args = [
7
+            'post_type'       => $post_type,
8
+            'post_status'     => ['publish'],
9
+            'posts_per_page'  => -1,
10
+            'page'            => 1
11
+        ];
12
+    }
5 13
     /**
6 14
      * Register the routes for the objects of the controller.
7 15
      */

+ 3
- 8
plugins/cia-post-types/includes/custom-taxonomies.php Wyświetl plik

@@ -1,18 +1,13 @@
1 1
 <?php
2 2
     function create_materials_taxonomy() {
3 3
         $post_types_that_show_materials = [
4
-            'artist',
5
-            'short',
6
-            'guide',
7
-            'exhibition',
8
-            'event',
9
-            'object',
10
-            'publication',
4
+            'artist', 'guide', 'short',
5
+            'object', 'publication',
11 6
         ];
12 7
         register_taxonomy('material', $post_types_that_show_materials, ['label' => 'Materials']);
13 8
     }
14 9
     function create_types_taxonomy() {
15
-        $post_types = [ 'artist', 'exhibition', 'event' ];
10
+        $post_types = [ 'artist', 'event' ];
16 11
         foreach ( $post_types as $post_type ) {
17 12
             register_taxonomy($post_type . '_type', $post_type, ['label' => ucfirst($post_type) . ' Type']);
18 13
         }

Ładowanie…
Anuluj
Zapisz