PluginProbe
weDocs: AI Powered Knowledge Base, Docs, Documentation, Wiki & AI Chatbot / trunk
weDocs: AI Powered Knowledge Base, Docs, Documentation, Wiki & AI Chatbot vtrunk
2.5.0 2.4.1 2.4.0 2.3.1 2.3.0 2.2.5 2.2.6 2.2.7 2.2.2 2.2.3 2.2.4 1.3.0 1.3.1 1.3.2 1.3.3 1.4 1.4.1 1.5 1.6 1.6.1 1.6.2 1.6.3 1.7 1.7.1 1.7.2 All 54 releases
wedocs / includes / API.php

API.php in weDocs: AI Powered Knowledge Base, Docs, Documentation, Wiki & AI Chatbot trunk, at includes/API.php

152 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WeDevs\WeDocs;
4
5 /**
6 * API Class
7 */
8 class API {
9
10 /**
11 * Initialize the class
12 */
13 public function __construct() {
14 add_action( 'rest_api_init', [ $this, 'init_api' ] );
15
16 add_filter( 'rest_prepare_docs', [ $this, 'set_pagination' ], 10, 3 );
17 add_filter( 'rest_prepare_docs', [ $this, 'set_comment_count_to_docs_response' ], 10, 3 );
18 add_filter( 'rest_prepare_docs', [ $this, 'set_caps' ], 10, 3 );
19 add_filter( 'rest_delete_docs', [ $this, 'delete_child_docs' ], 10 );
20 }
21
22 /**
23 * Initialize the API
24 *
25 * @return void
26 */
27 public function init_api() {
28 $api = new API\API( $this );
29 $api->register_routes();
30 }
31
32 /**
33 * Set next and previous pagination.
34 *
35 * @param WP_REST_Response $response
36 * @param WP_Post $post
37 * @param WP_REST_Request $request full data about the request
38 */
39 public function set_pagination( $response, $post, $request ) {
40 global $wpdb;
41
42 // we don't want this for edit context
43 if ( 'edit' == $request['context'] ) {
44 return $response;
45 }
46
47 // is it a single request?
48 $single = false;
49
50 if ( isset( $request['id'] ) || $request['slug'] || 'sidebar' == $request['context'] ) {
51 $single = true;
52 }
53
54 if ( !$single ) {
55 return $response;
56 }
57
58 $next_query = "SELECT ID, post_title FROM {$wpdb->posts}
59 WHERE post_parent = {$post->post_parent} and post_type = 'docs' and post_status = 'publish' and menu_order > {$post->menu_order}
60 ORDER BY menu_order ASC
61 LIMIT 0, 1";
62
63 $prev_query = "SELECT ID, post_title FROM {$wpdb->posts}
64 WHERE post_parent = {$post->post_parent} and post_type = 'docs' and post_status = 'publish' and menu_order < {$post->menu_order}
65 ORDER BY menu_order DESC
66 LIMIT 0, 1";
67
68 $next_post = $wpdb->get_row( $next_query );
69 $prev_post = $wpdb->get_row( $prev_query );
70
71 if ( $next_post ) {
72 $response->add_link( 'next', rest_url( "/wp/v2/docs/{$next_post->ID}" ), [
73 'title' => $next_post->post_title,
74 'link' => get_permalink( $next_post->ID ),
75 ] );
76 }
77
78 if ( $prev_post ) {
79 $response->add_link( 'prev', rest_url( "/wp/v2/docs/{$prev_post->ID}" ), [
80 'title' => $prev_post->post_title,
81 'link' => get_permalink( $prev_post->ID ),
82 ] );
83 }
84
85 return $response;
86 }
87
88 /**
89 * Set next and previous pagination.
90 *
91 * @since 2.0.0
92 *
93 * @param \WP_REST_Response $response
94 * @param \WP_Post $post
95 * @param \WP_REST_Request $request full data about the request
96 *
97 * @return \WP_REST_Response
98 */
99 public function set_comment_count_to_docs_response( $response, $post, $request ) {
100 // WordPress already keeps `posts.comment_count` in sync with the number of
101 // approved comments (see wp_update_comment_count_now()), and the column is
102 // loaded with the post itself. Reading it costs nothing, whereas
103 // wp_count_comments() fires five COUNT queries per post and is only cached
104 // in-request, so on a large doc tree it dominated the response time.
105 $response->data['comment_count'] = (int) $post->comment_count;
106
107 return $response;
108 }
109
110 /**
111 * Set capabilities.
112 *
113 * @param WP_REST_Response $response
114 * @param WP_Post $post
115 * @param WP_REST_Request $request full data about the request
116 */
117 public function set_caps( $response, $post, $request ) {
118 if ( 'edit' !== $request['context'] ) {
119 return $response;
120 }
121
122 $response->data['caps'] = [
123 'edit' => current_user_can( 'edit_post', $post->ID ),
124 'delete' => current_user_can( 'delete_post', $post->ID ),
125 ];
126
127 $response->data['children'] = [];
128
129 return $response;
130 }
131
132 /**
133 * Delete child posts.
134 *
135 * @param \WP_Post $doc
136 *
137 * @return void
138 */
139 public function delete_child_docs( $doc ) {
140 $childrens = get_children( [ 'post_parent' => $doc->ID ] );
141
142 if ( $childrens ) {
143 foreach ( $childrens as $child_post ) {
144 // recursively delete
145 $this->delete_child_docs( $child_post->ID );
146
147 wp_delete_post( $child_post->ID );
148 }
149 }
150 }
151 }
152