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
betterdocs / includes / Abilities / Settings / GetSettings.php

GetSettings.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.9.2, at includes/Abilities/Settings/GetSettings.php

178 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Read settings values ability.
4 *
5 * @package BetterDocs
6 * @since 4.9.0
7 */
8
9 namespace WPDeveloper\BetterDocs\Abilities\Settings;
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit; // Exit if accessed directly.
13 }
14
15 use WPDeveloper\BetterDocs\Abilities\AbilityBase;
16 use WPDeveloper\BetterDocs\Abilities\AbilityError;
17
18 /**
19 * What the settings are set to right now.
20 *
21 * Values come from `Core\Settings::get_all( false )` — stored values merged
22 * over the defaults, which is what the plugin itself reads — and are mapped to
23 * the JSON types the schema advertises, so a toggle reads `true`, not `"1"`.
24 *
25 * API keys are **never** returned. They are listed under `masked` with a
26 * constant `********` in their place; an agent can write a new key and cannot
27 * read the one that is there.
28 *
29 * @since 4.9.0
30 */
31 class GetSettings extends AbilityBase {
32
33 /**
34 * @since 4.9.0
35 */
36 public function __construct() {
37 $this->id = 'betterdocs/get-settings';
38 $this->label = __( 'Get settings', 'betterdocs' );
39 $this->description = __( 'Read BetterDocs settings as they are now, merged with the defaults. Ask for specific keys or a whole tab; with neither, every setting comes back. API keys are masked and can never be read.', 'betterdocs' );
40 $this->capability = 'edit_docs_settings';
41 }
42
43 /**
44 * @since 4.9.0
45 *
46 * @return array
47 */
48 public function get_annotations() {
49 return [
50 'readonly' => true,
51 'destructive' => false,
52 'idempotent' => true,
53 'priority' => 1.5,
54 'openWorldHint' => false
55 ];
56 }
57
58 /**
59 * @since 4.9.0
60 *
61 * @return array
62 */
63 public function get_input_schema() {
64 return [
65 'type' => 'object',
66 'additionalProperties' => false,
67 'properties' => [
68 'keys' => [
69 'type' => 'array',
70 'items' => [ 'type' => 'string' ],
71 'description' => __( 'Only these setting keys. Unknown keys are reported in unknown rather than failing the call.', 'betterdocs' )
72 ],
73 'tab' => [
74 'type' => 'string',
75 'description' => __( 'Only the settings on this tab, by tab id.', 'betterdocs' )
76 ]
77 ],
78 'default' => []
79 ];
80 }
81
82 /**
83 * @since 4.9.0
84 *
85 * @return array
86 */
87 public function get_output_schema() {
88 return [
89 'type' => 'object',
90 'properties' => [
91 'settings' => [ 'type' => 'object' ],
92 'masked' => [
93 'type' => 'array',
94 'items' => [ 'type' => 'string' ]
95 ],
96 'unknown' => [
97 'type' => 'array',
98 'items' => [ 'type' => 'string' ]
99 ],
100 'count' => [ 'type' => 'integer' ]
101 ]
102 ];
103 }
104
105 /**
106 * @since 4.9.0
107 *
108 * @param array $input Validated input.
109 * @return array|\WP_Error
110 */
111 public function execute( $input ) {
112 $schema = SettingsSchema::resolve();
113 $tab = isset( $input['tab'] ) ? (string) $input['tab'] : '';
114
115 if ( '' !== $tab ) {
116 $known = wp_list_pluck( SettingsSchema::tabs(), 'id' );
117
118 if ( ! in_array( $tab, $known, true ) ) {
119 return AbilityError::invalid_input(
120 'tab',
121 sprintf(
122 /* translators: %s: tab id. */
123 __( 'There is no settings tab called "%s" on this site.', 'betterdocs' ),
124 $tab
125 ),
126 $known
127 );
128 }
129 }
130
131 $unknown = [];
132 $wanted = [];
133
134 if ( ! empty( $input['keys'] ) ) {
135 foreach ( (array) $input['keys'] as $key ) {
136 $key = (string) $key;
137
138 if ( isset( $schema[ $key ] ) ) {
139 $wanted[] = $key;
140 continue;
141 }
142
143 $unknown[] = $key;
144 }
145 } else {
146 $wanted = array_keys( $schema );
147 }
148
149 // `false`, not `true`: the merged view is what the plugin's own readers
150 // see, so a setting that has never been saved reports its default
151 // rather than nothing at all.
152 $stored = (array) betterdocs()->settings->get_all( false );
153 $settings = [];
154 $masked = [];
155
156 foreach ( $wanted as $key ) {
157 if ( '' !== $tab && $schema[ $key ]['tab'] !== $tab ) {
158 continue;
159 }
160
161 $value = array_key_exists( $key, $stored ) ? $stored[ $key ] : $schema[ $key ]['default'];
162
163 $settings[ $key ] = SettingsSchema::to_public( $key, $value );
164
165 if ( ! $schema[ $key ]['readable'] ) {
166 $masked[] = $key;
167 }
168 }
169
170 return [
171 'settings' => $settings,
172 'masked' => $masked,
173 'unknown' => $unknown,
174 'count' => count( $settings )
175 ];
176 }
177 }
178