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

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

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