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