Przeglądaj źródła

created custom api endpoint plugin

tags/0.9.0
J 6 lat temu
rodzic
commit
09c7b29a0e

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

@@ -0,0 +1,43 @@
1
+<?php
2
+/**
3
+ * Craft in America Custom Endpoints Plugin
4
+ *
5
+ * @since             1.0.0
6
+ * @package           cia_endpoints
7
+ *
8
+ * @wordpress-plugin
9
+ * Plugin Name:       Craft in America - API Endpoints
10
+ * Plugin URI:
11
+ * Description:       The test plugin that adds rest functionality
12
+ * Version:           1.0.0
13
+ * Author:            TOJ <john@yvvas.com>
14
+ */
15
+
16
+// If this file is called directly, abort.
17
+if ( ! defined( 'WPINC' ) ) { die; }
18
+
19
+require_once('includes/class.make-endpoint.php');
20
+
21
+/**
22
+ * The standard wordpress post_types
23
+ */
24
+add_action( 'rest_api_init', function () {
25
+    $post_controller = new Make_Endpoint_For('post');
26
+    $post_controller->register_routes('posts', true);
27
+});
28
+add_action( 'rest_api_init', function () {
29
+    $page_controller = new Make_Endpoint_For('page');
30
+    $page_controller->register_routes('pages', true);
31
+});
32
+add_action( 'rest_api_init', function () {
33
+    $media_controller = new Make_Endpoint_For('media');
34
+    $media_controller->register_routes('media', false);
35
+});
36
+
37
+/**
38
+ * Craft in America custom post_types
39
+ */
40
+add_action( 'rest_api_init', function () {
41
+    $media_controller = new Make_Endpoint_For('customtype');
42
+    $media_controller->register_routes('custometypes', false);
43
+});

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

@@ -0,0 +1,95 @@
1
+<?php
2
+
3
+include('reformat-blocks.php');
4
+
5
+class Make_Endpoint_For extends WP_REST_Controller {
6
+    private $post_type;
7
+    function __construct($post_type) {
8
+        $this->post_type = $post_type;
9
+    }
10
+    /**
11
+     * Register the routes for the objects of the controller.
12
+     */
13
+    public function register_routes($route) {
14
+        $version = '2';
15
+        $namespace = 'craft/v' . $version;
16
+
17
+        register_rest_route( $namespace, '/' . $route, [
18
+            array(
19
+                'methods'  => WP_REST_Server::READABLE,
20
+                'callback' => array( $this, 'get_items' ),
21
+                'args'     => array(),
22
+            ),   
23
+        ]);
24
+        register_rest_route( $namespace, '/' . $route . '/(?P<id>[\d]+)', [
25
+            array(
26
+                'methods'  => WP_REST_Server::READABLE,
27
+                'callback' => array( $this, 'get_item' ),
28
+                'args'     => array(
29
+                    'context' => array(
30
+                        'default' => 'view',
31
+                    ),
32
+                ),
33
+            ),   
34
+        ]);
35
+    }
36
+
37
+    public function get_items( $request ) {
38
+        $args = array(
39
+            'post_type' => $this->post_type,
40
+        );
41
+
42
+        //do a query, call another class, etc
43
+        $items = new WP_Query($args);
44
+        
45
+        $data = array();
46
+        foreach( $items as $item ) {
47
+            // Get those Block!
48
+            $item->blocks = parse_blocks( $item->post_content );;
49
+            $itemdata = $this->prepare_item_for_response( $item, $request );
50
+            if($item->ID > 0) {
51
+                $data[$item->ID] = $this->prepare_response_for_collection( $itemdata );
52
+            }
53
+        }
54
+        wp_reset_postdata();
55
+        return new WP_REST_Response( $data, 200 );
56
+    }
57
+    public function get_item( $request ) {
58
+        $params = $request->get_params();
59
+
60
+        $args = array(
61
+            'numberposts' => 1,
62
+            'orderby'     => 'date',
63
+            'order'       => 'DESC',
64
+            'post_type'    => $this->post_type,
65
+        );
66
+        
67
+        //do a query, call another class, etc
68
+        $item = new WP_Query($args);
69
+        
70
+        $data = $this->prepare_item_for_response( $item, $request );
71
+        wp_reset_postdata();
72
+        //return a response or error based on some conditional
73
+        if ( 1 == 1 ) {
74
+            return new WP_REST_Response( $data, 200 );
75
+        } else {
76
+            return new WP_Error( 'code', __( 'message', 'text-domain' ) );
77
+        }
78
+    }
79
+
80
+    public function prepare_item_for_response( $item, $request ) {
81
+        $filtered = array();
82
+        $filtered[id] = $item->ID;
83
+        $filtered[type] = $item->post_type;
84
+        $filtered[title] = $item->post_title;
85
+        $filtered[excerpt] = $item->post_excerpt;
86
+        $filtered[date] = $item->post_date;
87
+        $filtered[content] = $item->post_content;
88
+        $filtered[blocks] = get_rearrange_blocks($item->blocks);
89
+        
90
+        // return $item;
91
+        if($item->post_status === 'publish') return $filtered;
92
+    }
93
+}
94
+
95
+?>

+ 15
- 0
plugins/cia-endpoints/includes/reformat-blocks.php Wyświetl plik

@@ -0,0 +1,15 @@
1
+<?php
2
+    function get_rearrange_blocks($blocks) {
3
+        $parsed_blocks = array();
4
+        foreach( $blocks as $block ) {
5
+            if(sizeof($block[innerBlocks]) < 1 && $block[innerHTML] !== "\n\n") {
6
+                array_push($parsed_blocks, $block[innerHTML]);
7
+            } elseif ($block[innerHTML] === "\n\n") {
8
+                array_push($parsed_blocks, null);
9
+            } else {
10
+                array_push($parsed_blocks, $block[innerBlocks]);
11
+            }
12
+        }
13
+        return $parsed_blocks;
14
+    }
15
+?>

+ 1
- 1
plugins/includes/custom-types.php Wyświetl plik

@@ -1,7 +1,7 @@
1 1
 <?php
2 2
     function get_all_custom_types() {
3 3
         return array(
4
-            'custom type',
4
+            'customtype',
5 5
             // 'artist',
6 6
             // 'episode',
7 7
             // 'short',

+ 8
- 7
vue-theme/src/app.vue Wyświetl plik

@@ -32,13 +32,14 @@ html > body
32 32
     :--headings
33 33
         color: red
34 34
 
35
-    nav.main, footer.main
36
-        background-color: #ccc
37
-        margin: 0 0 $ms
38
-        padding: $ms
39
-        width: 100%
40
-        ul > li
41
-            padding: 0 $ms
35
+    nav, footer
36
+        &.main
37
+            background-color: #ccc
38
+            margin: 0 0 $ms
39
+            padding: $ms
40
+            width: 100%
41
+            ul > li
42
+                padding: 0 $ms
42 43
     footer.main
43 44
         margin: $ms 0 0
44 45
 </style>

+ 5
- 1
vue-theme/src/pages/index.vue Wyświetl plik

@@ -6,7 +6,11 @@ article.page--index
6 6
         p {{ somePages(10) }}
7 7
     section(v-if="allPostsLoaded")
8 8
         h4 posts
9
-        p {{ allPosts }}
9
+        p {{ somePages(10) }}
10
+        hr
11
+        div(v-for="post in allPosts")
12
+            p {{ post.slug }}
13
+            p Sticky? {{ post.sticky }}
10 14
 </template>
11 15
 
12 16
 <script>

Ładowanie…
Anuluj
Zapisz