PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.9.1
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.9.1
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / Core / BaseAPI.php

BaseAPI.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.9.1, at includes/Core/BaseAPI.php

147 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace WPDeveloper\BetterDocs\Core;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8
9 use WP_Error;
10 use WP_REST_Server;
11 use WP_REST_Response;
12 use WPDeveloper\BetterDocs\Utils\Base;
13 use WPDeveloper\BetterDocs\Dependencies\DI\Container;
14
15 abstract class BaseAPI extends Base {
16 protected $namespace = 'betterdocs';
17 protected $version = 'v1';
18 /**
19 * Summary of settings
20 * @var Settings
21 */
22 protected $settings;
23
24 /**
25 * Summary of container
26 * @var Container
27 */
28 protected $container;
29
30 public function __construct( Settings $settings, Container $container ) {
31 $this->settings = $settings;
32 $this->container = $container;
33
34 add_filter( 'rest_prepare_doc_category', [ $this, 'rest_prepare_doc_category' ], 10, 3 );
35 }
36
37 public function get_namespace() {
38 return $this->namespace . '/' . $this->version;
39 }
40
41 public function get( $endpoint, $callback, $args = [] ) {
42 return $this->register_endpoint( $endpoint, $callback, $args, WP_REST_Server::READABLE );
43 }
44
45 public function post( $endpoint, $callback, $args = [] ) {
46 return $this->register_endpoint( $endpoint, $callback, $args );
47 }
48
49 /**
50 * Default permission callback for every route registered via
51 * register_endpoint().
52 *
53 * This used to `return true`, which made the *default* for a new REST class
54 * "world-readable and world-writable". Forgetting to override it was silent —
55 * nothing failed, the endpoint simply shipped open — and that is exactly how
56 * /knowledge_base and /plugin_info ended up anonymous.
57 *
58 * It now fails closed. A genuinely public endpoint must say so explicitly by
59 * overriding this method (see REST\InstantAnswer and REST\PopularKeywords) or
60 * by passing its own permission_callback to register_rest_route(). Making
61 * "public" a deliberate, greppable act is the whole point.
62 *
63 * Note this governs routes only; register_field() does not use it, so classes
64 * that only register REST fields are unaffected — their access is governed by
65 * the parent controller.
66 *
67 * @return bool
68 */
69 public function permission_check() {
70 return current_user_can( 'edit_posts' );
71 }
72
73 protected function register_endpoint( $endpoint, $callback, $args = [], $methods = WP_REST_Server::CREATABLE ) {
74 return register_rest_route(
75 $this->get_namespace(),
76 $endpoint,
77 [
78 'methods' => $methods,
79 'callback' => $callback,
80 'permission_callback' => [ $this, 'permission_check' ],
81 'args' => $args
82 ]
83 );
84 }
85
86 public function register_field( $type, $attribute, $args = [] ) {
87 $args = wp_parse_args(
88 $args,
89 [
90 'update_callback' => null,
91 'schema' => [
92 'description' => 'Holds the thumbnail URL of doc category',
93 'type' => 'string',
94 'format' => 'url'
95 ]
96 ]
97 );
98
99 return register_rest_field( $type, $attribute, $args );
100 }
101
102 /**
103 * @param $data
104 *
105 * @return WP_REST_Response
106 */
107 public function success( $data ) {
108 $_data = [
109 'success' => true,
110 'data' => $data
111 ];
112
113 if ( is_string( $data ) ) {
114 $_data['message'] = $data;
115
116 unset( $_data['data'] );
117 }
118
119 return new WP_REST_Response( $_data, 200 );
120 }
121
122 /**
123 * @param $error_code string
124 * @param $error_message string|array
125 * @param $endpoint string
126 * @param $status int
127 * @param $additional_data array
128 *
129 * @return WP_Error
130 */
131 public function error( $error_code, $error_message, $status = 500, $additional_data = [] ) {
132 $additional_data['status'] = $status;
133 return new WP_Error( $error_code, $error_message, $additional_data );
134 }
135
136 abstract public function register();
137
138 public function rest_prepare_doc_category( $data, $category, $request ) {
139 $nested_subcategory = $request->get_param( 'nested_subcategory', false );
140 $_counts = betterdocs()->query->get_docs_count( $category, $nested_subcategory );
141
142 // Add the custom data to the response
143 $data->data['count'] = $_counts;
144 return $data;
145 }
146 }
147