瀏覽代碼

:recycle: adding the terms endpoint controller

tags/0.9.0
J 4 年之前
父節點
當前提交
6ba539f572
共有 1 個檔案被更改,包括 46 行新增0 行删除
  1. 46
    0
      plugins/cia-endpoints/includes/class.make-terms.php

+ 46
- 0
plugins/cia-endpoints/includes/class.make-terms.php 查看文件

@@ -0,0 +1,46 @@
1
+<?php
2
+// include('formats.php');
3
+
4
+class Make_Terms_Endpoint extends WP_REST_Controller {
5
+    /**
6
+     * Register the routes for the objects of the controller.
7
+     */
8
+    public function register_custom_route($route) {
9
+        $version = '2';
10
+        $namespace = 'craft/v' . $version;
11
+
12
+        register_rest_route( $namespace, '/' . $route, [
13
+            array(
14
+                'methods'  => WP_REST_Server::READABLE,
15
+                'callback' => array( $this, 'get_all_terms' )
16
+            ),
17
+        ]);
18
+    }
19
+    public function get_all_terms( $request ) {
20
+
21
+        $terms = $categories = get_terms($post->post_type . '_type', array(
22
+            'orderby'    => 'count',
23
+            'hide_empty' => 0,
24
+        ));
25
+        $taxonomies = get_object_taxonomies($post->post_type);
26
+        $formatted[terms] = $terms;
27
+        $formatted[taxonomies] = $taxonomies;
28
+
29
+        return new WP_REST_Response( $this->prepare_all_items_for_response($args), 200 );
30
+    }
31
+
32
+    public function prepare_all_items_for_response( $args ) {
33
+        $collection = [];
34
+        
35
+        // https://developer.wordpress.org/reference/functions/get_posts/
36
+        foreach( get_posts($args) as $item ) {
37
+            $formatted = default_post_format( $item, true );
38
+            array_push($collection, $formatted);
39
+        }
40
+        wp_reset_postdata();
41
+
42
+        return $collection;
43
+    }
44
+}
45
+
46
+?>

Loading…
取消
儲存