瀏覽代碼

:sparkles: added sticky endpoint to API

tags/0.9.0
j 4 年之前
父節點
當前提交
102c000d08

+ 4
- 0
plugins/cia-endpoints/cia-end-points.php 查看文件

@@ -17,6 +17,7 @@
17 17
 if ( ! defined( 'WPINC' ) ) { die; }
18 18
 
19 19
 require_once('includes/class.make-endpoint.php');
20
+require_once('includes/class.make-sticky.php');
20 21
 require_once('includes/class.make-sortby.php');
21 22
 
22 23
 add_action( 'rest_api_init', function () {
@@ -47,6 +48,9 @@ add_action( 'rest_api_init', function () {
47 48
     $exhibition_controller = new Make_Endpoint_For('exhibition');
48 49
     $exhibition_controller->register_custom_route('exhibitions');
49 50
 
51
+    $sticky_controller = new Make_Sticky_Endpoint();
52
+    $sticky_controller->register_custom_route('sticky');
53
+
50 54
     /**
51 55
      * Craft in America custom sort_types
52 56
      */

+ 1
- 1
plugins/cia-endpoints/includes/class.make-endpoint.php 查看文件

@@ -44,7 +44,7 @@ class Make_Endpoint_For extends WP_REST_Controller {
44 44
     }
45 45
     public function get_all_items( $request ) {
46 46
         $args = array(
47
-            'numberposts' => -1,
47
+            'numberposts' => 999999,
48 48
             'post_type' => $this->post_type,
49 49
             // Order DOES NOT WORK because you're
50 50
             // returning the posts by id - DUH

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

@@ -0,0 +1,52 @@
1
+<?php
2
+// include('formats.php');
3
+
4
+class Make_Sticky_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_items' )
16
+            ),
17
+        ]);
18
+    }
19
+    public function get_all_items( $request ) {
20
+        $args = array(
21
+            'numberposts' => 20,
22
+            'post_type' => [
23
+                'artist',
24
+                'episode',
25
+                'event',
26
+                'exhibition',
27
+                'post',
28
+                'page'
29
+            ],
30
+            'meta_query' => [array(
31
+                'key' => 'is_sticky',
32
+                'value' => 'on',
33
+                'compare' => 'LIKE'
34
+            )]
35
+        );
36
+
37
+        return new WP_REST_Response( $this->prepare_all_items_for_response($args), 200 );
38
+    }
39
+    public function prepare_all_items_for_response( $args ) {
40
+        $collection = array();
41
+        
42
+        // https://developer.wordpress.org/reference/functions/get_posts/
43
+        foreach( get_posts($args) as $item ) {
44
+            $collection[$item->ID] = default_post_format( $item, true );
45
+        }
46
+        wp_reset_postdata();
47
+
48
+        return $collection;
49
+    }
50
+}
51
+
52
+?>

+ 1
- 1
plugins/cia-endpoints/includes/formats.php 查看文件

@@ -8,7 +8,7 @@ function make_taxonomy_endpoint_for( $terms ) {
8 8
 }
9 9
 
10 10
 
11
-function default_post_format( $item, $omit_content ) {
11
+function default_post_format( $item, $include_content ) {
12 12
     $filtered = [];
13 13
     $filtered[id] = $item->ID;
14 14
     $filtered[slug] = $item->post_name;

+ 1
- 1
plugins/cia-post-types/cia-post-types.php 查看文件

@@ -54,7 +54,7 @@ class PostType {
54 54
             'supports'           => ['title', 'editor', 'revisions', 'thumbnail'],
55 55
             'has_archive'        => false,
56 56
             'show_in_rest'       => true,
57
-            'publicly_queryable' => true, // Needed for slug edit
57
+            'publicly_queryable' => true, // Needed for slug
58 58
         ];
59 59
 
60 60
         register_post_type( $this->post_type, $args );

Loading…
取消
儲存