PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.6.2
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.6.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 / REST / Settings.php

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

337 lines 10.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPDeveloper\BetterDocs\REST;
4
5 use WP_Query;
6 use WP_REST_Request;
7 use WPDeveloper\BetterDocs\Admin\CSVExporter;
8 use WPDeveloper\BetterDocs\Admin\Importer\Parsers\CSV_Parser;
9 use WPDeveloper\BetterDocs\Admin\Importer\WPImport;
10 use WPDeveloper\BetterDocs\Admin\ReportEmail;
11 use WPDeveloper\BetterDocs\Admin\XMLExporter;
12 use WPDeveloper\BetterDocs\Core\BaseAPI;
13 use WPDeveloper\BetterDocs\Dependencies\DI\DependencyException;
14 use WPDeveloper\BetterDocs\Dependencies\DI\NotFoundException;
15 use WPDeveloper\BetterDocsChatbot\Core\AIChatbot;
16
17 class Settings extends BaseAPI {
18
19 public function permission_check(): bool {
20 return current_user_can( 'edit_docs_settings' );
21 }
22
23 public function register() {
24 $this->get( 'settings', [ $this, 'get_settings' ] );
25 $this->post( 'dark-mode', [ $this, 'dark_mode' ] );
26 $this->post( 'settings', [ $this, 'save_settings' ] );
27 $this->post( 'plugin_insights', [ $this, 'plugin_insights' ] );
28 $this->post( 'create-sample-docs', [$this, 'sample_docs'] );
29 $this->post( 'reporting-test', [ $this, 'test_reporting' ] );
30 $this->post( 'export-docs', [ $this, 'export_docs' ] );
31 $this->post( 'export-settings', [ $this, 'export_settings' ] );
32 $this->post( 'import-docs', [ $this, 'import_docs' ] );
33 $this->post( 'import-settings', [ $this, 'import_settings' ] );
34 $this->post( 'parse-xml', [ $this, 'parse_xml' ] );
35 $this->post( 'parse-csv', [ $this, 'parse_csv' ] );
36 $this->post( 'migrate', [ $this, 'migrate_plugins' ] );
37 $this->post( 'helpscout-migration', [$this, 'helpscout_migration'] );
38 $this->post( 'dashboard-mode', [$this, 'dashboard_mode'] );
39 }
40
41 public function dashboard_mode( WP_REST_Request $request ) {
42 $mode = $request->get_param( 'dashboard_mode' );
43 return update_option( 'dashboard_mode', $mode );
44 }
45
46 public function dark_mode( WP_REST_Request $request ): bool {
47 $dark_mode = rest_sanitize_boolean( $request->get_param( 'mode' ) );
48
49 if ( betterdocs()->settings->save( 'dark_mode', $dark_mode ) ) {
50 return true;
51 }
52
53 return false;
54 }
55
56 public function sample_docs( WP_REST_Request $request ) {
57 $action = $request->get_param( 'action' );
58
59 if ( $action == 'create-dummy-data' ) {
60 $file = BETTERDOCS_ABSPATH . 'assets/admin/images/BetterDocs-sample-data.csv';
61 $args = [
62 'fetch_attachments' => true,
63 'action' => '',
64 'existing_slug' => '',
65 'file_type' => 'text/csv'
66 ];
67 $wp_import_object = new WPImport( $file, $args );
68 return $wp_import_object->run();
69 }
70 }
71
72 public function export_docs( WP_REST_Request $request ) {
73 $data = $request->get_params();
74 $args = [
75 'include_post_featured_image_as_attachment' => true,
76 'status' => 'publish',
77 'content' => $data['export_type'],
78 'selected_docs' => $data['export_docs'],
79 'include_faq' => $data['enable_export_faq'],
80 ];
81
82 if ( $data['export_type'] == 'docs' && $data['export_docs'][0] != 'all' ) {
83 $args['post__in'] = $data['export_docs'];
84 }
85
86 if ( $data['export_type'] == 'doc_category' || $data['export_type'] == 'knowledge_base' ) {
87 $args['content'] = 'docs';
88 }
89
90 if ( $data['export_type'] == 'doc_category' && $data['export_categories'][0] != 'all' ) {
91 $args['category_terms'] = $data['export_categories'];
92 } else if ( $data['export_type'] == 'knowledge_base' && $data['export_kbs'][0] != 'all' ) {
93 $args['kb_terms'] = $data['export_kbs'];
94 }
95
96 if ( $data['export_type'] == 'glossaries' && $data['export_glossaries'][0] != 'all' ) {
97 $args['glossary_terms'] = $data['export_glossaries'];
98 unset( $args['selected_docs'] );
99 }
100
101 if ( $data['file_type'] == 'xml' ) {
102 $exporter = new XMLExporter( $args );
103 } else if ( $data['file_type'] == 'csv' ) {
104 $exporter = new CSVExporter( $args );
105 }
106
107 /**
108 * @var XMLExporter|CSVExporter $exporter
109 */
110
111 return $exporter->run();
112 }
113
114 public function export_settings( WP_REST_Request $request ): array {
115 $betterdocs_settings = get_option( 'betterdocs_settings' );
116 $json_str = json_encode( $betterdocs_settings, JSON_PRETTY_PRINT );
117 $file_name = 'betterdocs-settings.json';
118
119 return [
120 'success' => true,
121 'data' => [
122 'filename' => $file_name,
123 'filetype' => 'text/json',
124 'download' => $json_str,
125 ]
126 ];
127 }
128
129 public function import_settings( WP_REST_Request $request ): array {
130 $settings = $request->get_param( 'settings' );
131 // Decode the JSON data into a PHP array
132 $settings = json_decode( $settings, true );
133 $saved = update_option( 'betterdocs_settings', $settings );
134
135 if ( ! $saved ) {
136 return [
137 'status' => 'failed'
138 ];
139 }
140
141 return [
142 'status' => 'success'
143 ];
144 }
145
146 public function import_docs( WP_REST_Request $request ): array {
147 $existing_slug = $request->get_param( 'existing_slug' );
148 $action = $request->get_param( 'action' );
149
150 $files = $request->get_file_params();
151
152 $file = $files['file']['tmp_name'];
153
154 $args = [
155 'fetch_attachments' => true,
156 'existing_slug' => $existing_slug,
157 'action' => $action,
158 'file_type' => $files['file']['type']
159 ];
160
161 $wp_importer = new WPImport( $file, $args );
162
163 return $wp_importer->run();
164 }
165
166 public function parse_xml( WP_REST_Request $request ): array {
167 $data = $request->get_params();
168 $existing_post_slugs = $this->get_existing_slugs( $data['posts'] );
169
170 // Output the array of existing post slugs
171 return [
172 'status' => 'success',
173 'data' => $existing_post_slugs,
174 ];
175 }
176
177 public function parse_csv( WP_REST_Request $request ): array {
178 $files = $request->get_file_params();
179
180 $file = $files['file']['tmp_name'];
181
182 $parser = new CSV_Parser();
183 $parsed = $parser->parse( $file );
184
185 $existing_post_slugs = [];
186
187 if ( isset( $parsed['posts'] ) && is_array( $parsed['posts'] ) ) {
188 $post_names = array_map(
189 function ( $post ) {
190 return $post['post_name'];
191 },
192 $parsed['posts']
193 );
194
195 $existing_post_slugs = $this->get_existing_slugs( $post_names );
196 }
197
198 return [
199 'status' => 'success',
200 'data' => $existing_post_slugs,
201 ];
202 }
203
204 public function migrate_plugins( WP_REST_Request $request ): array {
205 betterdocs()->kbmigration->migrate();
206
207 return [
208 'status' => 'success'
209 ];
210 }
211
212 public function helpscout_migration( WP_REST_Request $request ) {
213 $api_key = $request->get_param( 'helpscout_api_key' );
214 $collection_id = $request->get_param( 'helpscout_collection_id' );
215
216 if ( ! $api_key || ! $collection_id ) {
217 return [
218 'status' => 'error',
219 'message' => esc_html__( 'Please provide both API Key and Collection ID', 'betterdocs' )
220 ];
221 }
222
223 $api_endpoint = 'https://docsapi.helpscout.net/v1/collections/' . $collection_id . '/articles?pageSize=1';
224
225 $headers = array(
226 'Authorization' => 'Basic ' . base64_encode( $api_key . ':X' ),
227 'Content-Type' => 'application/json',
228 );
229
230 $initial_response = wp_remote_get( $api_endpoint, array( 'headers' => $headers ) );
231
232 if ( $initial_response['response']['code'] == '404' || $initial_response['response']['code'] == 401 ) {
233 return [
234 'status' => 'error',
235 'message' => esc_html__( 'Unauthorized API Key or Collection ID', 'betterdocs' )
236 ];
237 }
238
239 // Enqueue migration task
240 betterdocs()->backgroundProccessor->push_to_queue(
241 array(
242 'headers' => $headers,
243 'initial_response' => $initial_response,
244 'api_key' => $api_key,
245 'collection_id' => $collection_id
246 )
247 )->save();
248
249 // Start processing the queue
250 betterdocs()->backgroundProccessor->dispatch();
251
252 return [
253 'status' => 'success',
254 'message' => esc_html__( 'Migration process has started in the background.', 'betterdocs' )
255 ];
256 }
257
258 public function get_existing_slugs( $slugs ) {
259 // Initialize the array for existing post slugs
260 $existing_post_slugs = [];
261
262 // Initialize the query arguments
263 $args = [
264 'post_type' => [ 'docs', 'betterdocs_faq' ], // Change 'post' to your custom post type if applicable
265 'post_status' => 'publish',
266 'posts_per_page' => -1, // Retrieve all posts
267 'fields' => 'ids', // Retrieve only post IDs to reduce memory usage
268 ];
269
270 // Create a new instance of WP_Query
271 $query = new WP_Query( $args );
272
273 // Get an array of post slugs from the query
274 $all_post_slugs = array_map(
275 function ( $post_id ) {
276 return get_post_field( 'post_name', $post_id );
277 },
278 $query->posts
279 );
280
281 // Restore original post data
282 wp_reset_postdata();
283
284 // Find the intersection of the two arrays to get existing post slugs
285 return array_intersect( $slugs, $all_post_slugs );
286 }
287
288 public function insights(): bool {
289 return true;
290 }
291
292 public function get_settings(): array {
293 return betterdocs()->settings->get_all( true );
294 }
295
296 public function save_settings( WP_REST_Request $request ) {
297 $result = betterdocs()->settings->save_settings( $request->get_params() );
298 if ( is_wp_error( $result ) ) {
299 return $this->error( $result->get_error_code(), $result->get_error_message(), 400 );
300 }
301 if ( $result ) {
302 return $this->success( __( 'Settings Saved!', 'betterdocs' ) );
303 }
304
305 return $this->error( 'nothing_changed', __( 'There are no changes to be saved.', 'betterdocs' ), 200 );
306 }
307
308 /**
309 * @throws NotFoundException
310 * @throws DependencyException
311 */
312 public function test_reporting( $request ) {
313 return $this->container->get( ReportEmail::class )->test_email_report( $request );
314 }
315
316 public function do_wizard_tracking(): bool {
317 $insights = betterdocs()->admin->plugin_insights( true );
318 // Get our data
319 $insights->schedule_tracking();
320 $insights->set_is_tracking_allowed( true, 'betterdocs' );
321 if ( $insights->do_tracking( true ) ) {
322 $insights->update_block_notice( 'betterdocs' );
323 }
324 if ( ! isset( $_COOKIE['betterdocs_insights_allowed'] ) ) {
325 setcookie( 'betterdocs_insights_allowed', true, time() + ( 30 * 24 * 60 * 60 ), '/' );
326 }
327 return true;
328 }
329
330 public function plugin_insights( $request ) {
331 if ( $this->do_wizard_tracking() ) {
332 wp_send_json_success( 'done' );
333 }
334 wp_send_json_error( 'Something went wrong.' );
335 }
336 }
337