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

347 lines 12.3 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 WPDeveloper\BetterDocs\Admin\ReportEmail;
6 use WP_Query;
7 use WP_User;
8 use WP_REST_Request;
9 use WPDeveloper\BetterDocs\Core\BaseAPI;
10 use WPDeveloper\BetterDocs\Admin\WPExporter;
11 use WPDeveloper\BetterDocs\Admin\CSVExporter;
12 use WPDeveloper\BetterDocs\Admin\Importer\WPImport;
13 use WPDeveloper\BetterDocs\Admin\Importer\Parsers\CSV_Parser;
14
15 class Settings extends BaseAPI {
16
17 public function permission_check() {
18 return current_user_can( 'edit_docs_settings' );
19 }
20
21 public function register() {
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'] );
34 $this->post( 'create-sample-docs', [$this, 'sample_docs'] );
35 $this->post( 'helpscout-migration', [$this, 'helpscout_migration'] );
36 }
37
38 public function sample_docs( WP_REST_Request $request ) {
39 $action = $request->get_param( 'action' );
40
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 }
53
54 public function export_docs( WP_REST_Request $request ) {
55 $args = [
56 'include_post_featured_image_as_attachment' => true,
57 'status' => 'publish',
58 'content' => 'docs'
59 ];
60
61 $data = $request->get_params();
62
63 if ( $data['export_type'] == 'docs' && $data['export_docs'][0] != 'all' ) {
64 $args['post__in'] = $data['export_docs'];
65 }
66
67 if ( $data['export_type'] == 'doc_category' && $data['export_categories'][0] != 'all' ) {
68 $args['category_terms'] = $data['export_categories'];
69 }
70
71 if ( $data['export_type'] == 'knowledge_base' && $data['export_kbs'][0] != 'all' ) {
72 $args['kb_terms'] = $data['export_kbs'];
73 }
74
75 if ($data['file_type'] == 'xml') {
76 $exporter = new WPExporter( $args );
77 } else if ($data['file_type'] == 'csv') {
78 $exporter = new CSVExporter( $args );
79 }
80
81 return $exporter->run();
82 }
83
84 public function export_settings( WP_REST_Request $request ) {
85 $betterdocs_settings = get_option( 'betterdocs_settings' );
86 $json_str = json_encode( $betterdocs_settings, JSON_PRETTY_PRINT );
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 }
97
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 );
103
104 if ( $save == true ) {
105 return [
106 'status' => 'success'
107 ];
108 } else {
109 return [
110 'status' => 'failed'
111 ];
112 }
113 }
114
115 public function import_docs( WP_REST_Request $request ) {
116 $existing_slug = $request->get_param( 'existing_slug' );
117 $action = $request->get_param( 'action' );
118
119 $files = $request->get_file_params();
120
121 $file = $files['file']['tmp_name'];
122
123 $args = [
124 'fetch_attachments' => true,
125 'existing_slug' => $existing_slug,
126 'action' => $action,
127 'file_type' => $files['file']['type']
128 ];
129
130 $wp_importer = new WPImport( $file, $args );
131
132 return $wp_importer->run();
133 }
134
135 public function parse_xml( WP_REST_Request $request ) {
136 $data = $request->get_params();
137 $existing_post_slugs = $this->get_existing_slugs($data['posts']);
138
139 // Output the array of existing post slugs
140 return [
141 'status' => 'success',
142 'data' => $existing_post_slugs,
143 ];
144 }
145
146 public function parse_csv( WP_REST_Request $request ) {
147 $files = $request->get_file_params();
148
149 $file = $files['file']['tmp_name'];
150
151 $parser = new CSV_Parser();
152 $parsed = $parser->parse( $file );
153
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 }
159
160 $existing_post_slugs = $this->get_existing_slugs( $post_names) ;
161
162 return [
163 'status' => 'success',
164 'data' => $existing_post_slugs,
165 ];
166 }
167
168 public function migrate_plugins( WP_REST_Request $request ) {
169 betterdocs()->kbmigration->migrate();
170
171 return [
172 'status' => 'success'
173 ];
174 }
175
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');
179
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 }
186
187 $api_endpoint = 'https://docsapi.helpscout.net/v1/collections/' . $collection_id . '/articles';
188
189 $headers = array(
190 'Authorization' => 'Basic ' . base64_encode($api_key . ':X'),
191 'Content-Type' => 'application/json',
192 );
193
194 $response = wp_remote_get($api_endpoint, array('headers' => $headers));
195
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);
204
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'];
209
210 // Fetch detailed information for each article
211 $detailedArticles = [];
212
213 foreach ($articles as $article) {
214 $articleId = $article['id'];
215 $articleNumber = $article['number'];
216
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));
220
221 if (!is_wp_error($articleResponse)) {
222 $articleBody = wp_remote_retrieve_body($articleResponse);
223 $articleData = json_decode($articleBody, true);
224
225 if (isset($articleData['article']) && isset($articleData['article']['text'])) {
226 // Extract the article text
227 $article['text'] = $articleData['article']['text'];
228
229 // Extract category details
230 $categories = $articleData['article']['categories'];
231 $categoryDetails = [];
232
233 foreach ($categories as $categoryId) {
234 $categoryEndpoint = "https://docsapi.helpscout.net/v1/categories/{$categoryId}";
235 $categoryResponse = wp_remote_get($categoryEndpoint, array('headers' => $headers));
236
237 if (!is_wp_error($categoryResponse)) {
238 $categoryBody = wp_remote_retrieve_body($categoryResponse);
239 $categoryData = json_decode($categoryBody, true);
240
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 }
250
251 // Add category details to the article
252 $article['categories'] = $categoryDetails;
253
254 // Add the article to the detailed articles array
255 $detailedArticles[] = $article;
256 }
257 }
258 }
259
260 $wp_importer = new WPImport('');
261
262 $result = $wp_importer->import_helpscout_data( $detailedArticles );
263
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 }
276
277
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 }
346 }
347