Переглянути джерело

tweaking that api to return right

tags/0.9.0
J 6 роки тому
джерело
коміт
f0ce9da28d

+ 4
- 4
plugins/cia-endpoints/cia-end-points.php Переглянути файл

@@ -23,15 +23,15 @@ require_once('includes/class.make-endpoint.php');
23 23
  */
24 24
 add_action( 'rest_api_init', function () {
25 25
     $post_controller = new Make_Endpoint_For('post');
26
-    $post_controller->register_routes('posts', true);
26
+    $post_controller->register_custom_route('posts');
27 27
 });
28 28
 add_action( 'rest_api_init', function () {
29 29
     $page_controller = new Make_Endpoint_For('page');
30
-    $page_controller->register_routes('pages', true);
30
+    $page_controller->register_custom_route('pages');
31 31
 });
32 32
 add_action( 'rest_api_init', function () {
33 33
     $media_controller = new Make_Endpoint_For('media');
34
-    $media_controller->register_routes('media', false);
34
+    $media_controller->register_custom_route('media');
35 35
 });
36 36
 
37 37
 /**
@@ -39,5 +39,5 @@ add_action( 'rest_api_init', function () {
39 39
  */
40 40
 add_action( 'rest_api_init', function () {
41 41
     $media_controller = new Make_Endpoint_For('customtype');
42
-    $media_controller->register_routes('custometypes', false);
42
+    $media_controller->register_custom_route('custometypes');
43 43
 });

+ 20
- 47
plugins/cia-endpoints/includes/class.make-endpoint.php Переглянути файл

@@ -10,76 +10,50 @@ class Make_Endpoint_For extends WP_REST_Controller {
10 10
     /**
11 11
      * Register the routes for the objects of the controller.
12 12
      */
13
-    public function register_routes($route) {
13
+    public function register_custom_route($route) {
14 14
         $version = '2';
15 15
         $namespace = 'craft/v' . $version;
16 16
 
17 17
         register_rest_route( $namespace, '/' . $route, [
18 18
             array(
19 19
                 'methods'  => WP_REST_Server::READABLE,
20
-                'callback' => array( $this, 'get_items' ),
21
-                'args'     => array(),
20
+                'callback' => array( $this, 'get_items' )
22 21
             ),   
23 22
         ]);
24 23
         register_rest_route( $namespace, '/' . $route . '/(?P<id>[\d]+)', [
25 24
             array(
26 25
                 'methods'  => WP_REST_Server::READABLE,
27
-                'callback' => array( $this, 'get_item' ),
28
-                'args'     => array(
29
-                    'context' => array(
30
-                        'default' => 'view',
31
-                    ),
32
-                ),
26
+                'callback' => array( $this, 'get_items' )
33 27
             ),   
34 28
         ]);
35 29
     }
36 30
 
37 31
     public function get_items( $request ) {
38
-        $args = array(
39
-            'post_type' => $this->post_type,
40
-        );
32
+        // Get parameters from request
33
+        // /<id>?limit=<num>
34
+        $params = $request->get_params();
41 35
 
42
-        //do a query, call another class, etc
43
-        $items = new WP_Query($args);
44
-        
45
-        $data = array();
46
-        foreach( $items as $item ) {
36
+        $args = array( 'numberposts' => -1, 'post_type' => $this->post_type );
37
+        if(intval($params['limit']) > 0) { $args['numberposts'] = intval($params['limit']); }
38
+        if(intval($params['id']) > 0) { $args['include'] = array($params['id']); }
39
+        // !: Add order asc/desc
40
+        // !: Add orderby
41
+
42
+        $collection = array();
43
+        foreach( get_posts($args) as $item ) {
47 44
             // 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
-            }
45
+            $item->blocks = parse_blocks( $item->post_content );
46
+            $data = $this->prepare_item_for_response( $item, $request );
47
+            $collection[$item->ID] = $this->prepare_response_for_collection( $data );
53 48
         }
54 49
         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
-        }
50
+        return new WP_REST_Response( $collection, 200 );
78 51
     }
79 52
 
80 53
     public function prepare_item_for_response( $item, $request ) {
81 54
         $filtered = array();
82 55
         $filtered[id] = $item->ID;
56
+        $filtered[slug] = $item->post_name;
83 57
         $filtered[type] = $item->post_type;
84 58
         $filtered[title] = $item->post_title;
85 59
         $filtered[excerpt] = $item->post_excerpt;
@@ -87,8 +61,7 @@ class Make_Endpoint_For extends WP_REST_Controller {
87 61
         $filtered[content] = $item->post_content;
88 62
         $filtered[blocks] = get_rearrange_blocks($item->blocks);
89 63
         
90
-        // return $item;
91
-        if($item->post_status === 'publish') return $filtered;
64
+        return $filtered;
92 65
     }
93 66
 }
94 67
 

+ 4
- 1
vue-theme/functions.php Переглянути файл

@@ -139,4 +139,7 @@ function remove_cssjs_ver( $src ) {
139 139
     return $src;
140 140
 }
141 141
 add_filter( 'style_loader_src', 'remove_cssjs_ver', 10, 2 );
142
-add_filter( 'script_loader_src', 'remove_cssjs_ver', 10, 2 );
142
+add_filter( 'script_loader_src', 'remove_cssjs_ver', 10, 2 );
143
+
144
+// header( 'Access-Control-Allow-Origin: http://localhost:8080' );
145
+header( 'Content-Type: application/json' );

+ 4
- 4
vue-theme/src/pages/index.vue Переглянути файл

@@ -3,14 +3,14 @@ article.page--index
3 3
     h1 {{ site }}: index
4 4
     section(v-if="allPagesLoaded")
5 5
         h4 pages
6
-        p {{ somePages(10) }}
6
+        p {{ allPages.length }}
7 7
     section(v-if="allPostsLoaded")
8 8
         h4 posts
9
-        p {{ somePages(10) }}
9
+        p {{ allPosts.length }}
10 10
         hr
11 11
         div(v-for="post in allPosts")
12
-            p {{ post.slug }}
13
-            p Sticky? {{ post.sticky }}
12
+            h4 {{ post.slug }}
13
+            p(v-for="block in post.blocks" v-html="block")
14 14
 </template>
15 15
 
16 16
 <script>

+ 5
- 3
vue-theme/src/store/modules/page.js Переглянути файл

@@ -2,14 +2,14 @@ import api from '../../utils/api'
2 2
 
3 3
 // initial state
4 4
 const state = {
5
-    all: [],
5
+    all: {},
6 6
     loaded: false,
7 7
     page: null,
8 8
 }
9 9
 
10 10
 // getters
11 11
 const getters = {
12
-    allPages: state => state.all,
12
+    allPages: state => Object.values((state.all)),
13 13
     allPagesLoaded: state => state.loaded,
14 14
     page: state => id => {
15 15
         let field = typeof id === 'number' ? 'id' : 'slug'
@@ -43,7 +43,9 @@ const actions = {
43 43
 
44 44
 // mutations
45 45
 const mutations = {
46
-    STORE_FETCHED_PAGES(state, { pages }) { state.all = pages },
46
+    STORE_FETCHED_PAGES(state, { pages }) {
47
+        state.all = pages
48
+    },
47 49
     PAGES_LOADED(state, val) { state.loaded = val },
48 50
 }
49 51
 

+ 5
- 5
vue-theme/src/store/modules/post.js Переглянути файл

@@ -1,19 +1,19 @@
1 1
 import api from '../../utils/api'
2 2
 
3 3
 const createPostSlug = post => {
4
-    let slug = post.link.replace(window.location.protocol + '//' + window.location.host, '')
5
-    post.slug = slug
4
+    // let slug = post.link.replace(window.location.protocol + '//' + window.location.host, '')
5
+    // post.slug = slug
6 6
     return post
7 7
 }
8 8
 
9 9
 const state = {
10
-    all: [],
10
+    all: {},
11 11
     loaded: false,
12 12
     post: null,
13 13
 }
14 14
 
15 15
 const getters = {
16
-    allPosts: state => state.all,
16
+    allPosts: state => Object.values(state.all),
17 17
     allPostsLoaded: state => state.loaded,
18 18
     post: state => id => {
19 19
         let field = typeof id === 'number' ? 'id' : 'slug'
@@ -38,7 +38,7 @@ const getters = {
38 38
 const actions = {
39 39
     getAllPosts({ commit }) {
40 40
         api.getByType('posts', posts => {
41
-            posts.map((post, i) => {
41
+            Object.values(posts).map((post, i) => {
42 42
                 posts[i] = createPostSlug(post)
43 43
             })
44 44
             commit('STORE_FETCHED_POSTS', { posts })

+ 3
- 3
vue-theme/src/utils/api.js Переглянути файл

@@ -2,12 +2,12 @@ import axios from 'axios'
2 2
 
3 3
 const SETTINGS = { 
4 4
     LOADING_SEGMENTS: 2,
5
-    API_BASE_PATH: '/wp-json/wp/v2/'
5
+    API_BASE_PATH: '/wp-json/craft/v2/',
6 6
 }
7 7
 
8 8
 export default {
9 9
     getByType(type, cb) {
10
-        axios.get(SETTINGS.API_BASE_PATH + `${type}?per_page=10`)
10
+        axios.get(SETTINGS.API_BASE_PATH + `${type}`)
11 11
         .then(response => {
12 12
             cb(response.data)
13 13
         })
@@ -17,7 +17,7 @@ export default {
17 17
     },
18 18
     getPosts(limit = 5, cb) {
19 19
         axios
20
-        .get(SETTINGS.API_BASE_PATH + 'posts?per_page=' + limit)
20
+        .get(SETTINGS.API_BASE_PATH + 'posts')
21 21
         .then(response => {
22 22
             cb(response.data)
23 23
         })

Завантаження…
Відмінити
Зберегти