PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.8.2
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.8.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 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.8.2, at includes/Core/BaseAPI.php

127 lines 2.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 public function permission_check() {
50 return true;
51 }
52
53 protected function register_endpoint( $endpoint, $callback, $args = [], $methods = WP_REST_Server::CREATABLE ) {
54 return register_rest_route(
55 $this->get_namespace(),
56 $endpoint,
57 [
58 'methods' => $methods,
59 'callback' => $callback,
60 'permission_callback' => [ $this, 'permission_check' ],
61 'args' => $args
62 ]
63 );
64 }
65
66 public function register_field( $type, $attribute, $args = [] ) {
67 $args = wp_parse_args(
68 $args,
69 [
70 'update_callback' => null,
71 'schema' => [
72 'description' => 'Holds the thumbnail URL of doc category',
73 'type' => 'string',
74 'format' => 'url'
75 ]
76 ]
77 );
78
79 return register_rest_field( $type, $attribute, $args );
80 }
81
82 /**
83 * @param $data
84 *
85 * @return WP_REST_Response
86 */
87 public function success( $data ) {
88 $_data = [
89 'success' => true,
90 'data' => $data
91 ];
92
93 if ( is_string( $data ) ) {
94 $_data['message'] = $data;
95
96 unset( $_data['data'] );
97 }
98
99 return new WP_REST_Response( $_data, 200 );
100 }
101
102 /**
103 * @param $error_code string
104 * @param $error_message string|array
105 * @param $endpoint string
106 * @param $status int
107 * @param $additional_data array
108 *
109 * @return WP_Error
110 */
111 public function error( $error_code, $error_message, $status = 500, $additional_data = [] ) {
112 $additional_data['status'] = $status;
113 return new WP_Error( $error_code, $error_message, $additional_data );
114 }
115
116 abstract public function register();
117
118 public function rest_prepare_doc_category( $data, $category, $request ) {
119 $nested_subcategory = $request->get_param( 'nested_subcategory', false );
120 $_counts = betterdocs()->query->get_docs_count( $category, $nested_subcategory );
121
122 // Add the custom data to the response
123 $data->data['count'] = $_counts;
124 return $data;
125 }
126 }
127