PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.9.2
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.9.2
4.9.2 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 All 200 releases
← All changes | includes/Core/BaseAPI.php +117 -86 3.4.14.9.2 View file →
@@ -1,8 +1,12 @@
1 1 <?php
2 +namespace WPDeveloper\BetterDocs\Core;
2 3
3 -namespace WPDeveloper\BetterDocs\Core;
4 +if ( ! defined( 'ABSPATH' ) ) {
5 + exit;
6 +}
4 7
8 +
5 9 use WP_Error;
6 10 use WP_REST_Server;
7 11 use WP_REST_Response;
8 12 use WPDeveloper\BetterDocs\Utils\Base;
@@ -8,108 +12,135 @@
8 12 use WPDeveloper\BetterDocs\Utils\Base;
9 13 use WPDeveloper\BetterDocs\Dependencies\DI\Container;
10 14
11 15 abstract class BaseAPI extends Base {
12 - protected $namespace = 'betterdocs';
13 - protected $version = 'v1';
14 - /**
15 - * Summary of settings
16 - * @var Settings
17 - */
18 - protected $settings;
16 + protected $namespace = 'betterdocs';
17 + protected $version = 'v1';
18 + /**
19 + * Summary of settings
20 + * @var Settings
21 + */
22 + protected $settings;
19 23
20 - /**
21 - * Summary of container
22 - * @var Container
23 - */
24 - protected $container;
24 + /**
25 + * Summary of container
26 + * @var Container
27 + */
28 + protected $container;
25 29
26 - public function __construct( Settings $settings, Container $container ) {
27 - $this->settings = $settings;
28 - $this->container = $container;
30 + public function __construct( Settings $settings, Container $container ) {
31 + $this->settings = $settings;
32 + $this->container = $container;
29 33
30 - add_filter( 'rest_prepare_doc_category', [$this, 'rest_prepare_doc_category'], 10, 3 );
31 - }
34 + add_filter( 'rest_prepare_doc_category', [ $this, 'rest_prepare_doc_category' ], 10, 3 );
35 + }
32 36
33 - public function get_namespace() {
34 - return $this->namespace . '/' . $this->version;
35 - }
37 + public function get_namespace() {
38 + return $this->namespace . '/' . $this->version;
39 + }
36 40
37 - public function get( $endpoint, $callback, $args = [] ) {
38 - return $this->register_endpoint( $endpoint, $callback, $args, WP_REST_Server::READABLE );
39 - }
41 + public function get( $endpoint, $callback, $args = [] ) {
42 + return $this->register_endpoint( $endpoint, $callback, $args, WP_REST_Server::READABLE );
43 + }
40 44
41 - public function post( $endpoint, $callback, $args = [] ) {
42 - return $this->register_endpoint( $endpoint, $callback, $args );
43 - }
45 + public function post( $endpoint, $callback, $args = [] ) {
46 + return $this->register_endpoint( $endpoint, $callback, $args );
47 + }
44 48
45 - public function permission_check() {
46 - return true;
47 - }
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 + }
48 72
49 - protected function register_endpoint( $endpoint, $callback, $args = [], $methods = WP_REST_Server::CREATABLE ) {
50 - return register_rest_route( $this->get_namespace(), $endpoint, [
51 - 'methods' => $methods,
52 - 'callback' => $callback,
53 - 'permission_callback' => [$this, 'permission_check'],
54 - 'args' => $args
55 - ] );
56 - }
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 + }
57 85
58 - public function register_field( $type, $attribute, $args = [] ) {
59 - $args = wp_parse_args( $args, [
60 - 'update_callback' => null,
61 - 'schema' => [
62 - 'description' => 'Holds the thumbnail URL of doc category',
63 - 'type' => 'string',
64 - 'format' => 'url'
65 - ]
66 - ] );
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 + );
67 98
68 - return register_rest_field( $type, $attribute, $args );
69 - }
99 + return register_rest_field( $type, $attribute, $args );
100 + }
70 101
71 - /**
72 - * @param $data
73 - *
74 - * @return WP_REST_Response
75 - */
76 - public function success( $data ) {
77 - $_data = [
78 - 'success' => true,
79 - 'data' => $data
80 - ];
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 + ];
81 112
82 - if ( is_string( $data ) ) {
83 - $_data['message'] = $data;
113 + if ( is_string( $data ) ) {
114 + $_data['message'] = $data;
84 115
85 - unset( $_data['data'] );
86 - }
116 + unset( $_data['data'] );
117 + }
87 118
88 - return new WP_REST_Response( $_data, 200 );
89 - }
119 + return new WP_REST_Response( $_data, 200 );
120 + }
90 121
91 - /**
92 - * @param $error_code string
93 - * @param $error_message string|array
94 - * @param $endpoint string
95 - * @param $status int
96 - * @param $additional_data array
97 - *
98 - * @return WP_Error
99 - */
100 - public function error( $error_code, $error_message, $status = 500, $additional_data = [] ) {
101 - $additional_data['status'] = $status;
102 - return new WP_Error( $error_code, $error_message, $additional_data );
103 - }
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 + }
104 135
105 - abstract public function register();
136 + abstract public function register();
106 137
107 - public function rest_prepare_doc_category( $data, $category, $request ) {
108 - $nested_subcategory = $request->get_param('nested_subcategory', false);
109 - $_counts = betterdocs()->query->get_docs_count( $category, $nested_subcategory );
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 );
110 141
111 - // Add the custom data to the response
112 - $data->data['count'] = $_counts;
113 - return $data;
114 - }
142 + // Add the custom data to the response
143 + $data->data['count'] = $_counts;
144 + return $data;
145 + }
115 146 }