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

231 lines 7.1 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_REST_Request;
7 use WPDeveloper\BetterDocs\Core\BaseAPI;
8 use WPDeveloper\BetterDocs\Admin\WPExporter;
9 use WPDeveloper\BetterDocs\Admin\CSVExporter;
10 use WPDeveloper\BetterDocs\Admin\Importer\WPImport;
11 use WPDeveloper\BetterDocs\Admin\Importer\Parsers\CSV_Parser;
12 use WP_Query;
13
14 class Settings extends BaseAPI {
15
16 public function permission_check() {
17 return current_user_can( 'edit_docs_settings' );
18 }
19
20 public function register() {
21 $this->get( 'settings', [$this, 'get_settings'] );
22 $this->post( 'settings', [$this, 'save_settings'] );
23 $this->post( 'plugin_insights', [$this, 'plugin_insights'] );
24
25 $this->post( 'reporting-test', [$this, 'test_reporting'] );
26 $this->post( 'export-docs', [$this, 'export_docs'] );
27 $this->post( 'export-settings', [$this, 'export_settings'] );
28 $this->post( 'import-docs', [$this, 'import_docs'] );
29 $this->post( 'import-settings', [$this, 'import_settings'] );
30 $this->post( 'parse-xml', [$this, 'parse_xml'] );
31 $this->post( 'parse-csv', [$this, 'parse_csv'] );
32 $this->post( 'migrate', [$this, 'migrate_plugins'] );
33 }
34
35 public function export_docs( WP_REST_Request $request ) {
36 $args = [
37 'include_post_featured_image_as_attachment' => true,
38 'status' => 'publish',
39 'content' => 'docs'
40 ];
41
42 $data = $request->get_params();
43
44 if ( $data['export_type'] == 'docs' && $data['export_docs'][0] != 'all' ) {
45 $args['post__in'] = $data['export_docs'];
46 }
47
48 if ( $data['export_type'] == 'doc_category' && $data['export_categories'][0] != 'all' ) {
49 $args['category_terms'] = $data['export_categories'];
50 }
51
52 if ( $data['export_type'] == 'knowledge_base' && $data['export_kbs'][0] != 'all' ) {
53 $args['kb_terms'] = $data['export_kbs'];
54 }
55
56 if ($data['file_type'] == 'xml') {
57 $exporter = new WPExporter( $args );
58 } else if ($data['file_type'] == 'csv') {
59 $exporter = new CSVExporter( $args );
60 }
61
62 return $exporter->run();
63 }
64
65 public function export_settings( WP_REST_Request $request ) {
66 $betterdocs_settings = get_option('betterdocs_settings');
67 $json_str = json_encode($betterdocs_settings, JSON_PRETTY_PRINT);
68 $file_name = 'betterdocs-settings.json';
69 return [
70 'success' => true,
71 'data' => [
72 'filename' => $file_name,
73 'filetype' => 'text/json',
74 'download' => $json_str,
75 ]
76 ];
77 }
78
79 public function import_settings( WP_REST_Request $request ) {
80 $settings = $request->get_param('settings');
81 // Decode the JSON data into a PHP array
82 $settings = json_decode($settings, true);
83 $save = update_option( 'betterdocs_settings', $settings );
84
85 if ( $save == true ) {
86 return [
87 'status' => 'success'
88 ];
89 } else {
90 return [
91 'status' => 'failed'
92 ];
93 }
94 }
95
96 public function import_docs( WP_REST_Request $request ) {
97 $existing_slug = $request->get_param('existing_slug');
98 $action = $request->get_param('action');
99
100 $files = $request->get_file_params();
101
102 $file = $files['file']['tmp_name'];
103
104 $args = [
105 'fetch_attachments' => true,
106 'existing_slug' => $existing_slug,
107 'action' => $action,
108 'file_type' => $files['file']['type']
109 ];
110
111 $wp_importer = new WPImport( $file, $args );
112
113 return $wp_importer->run();
114 }
115
116 public function parse_xml( WP_REST_Request $request ) {
117 $data = $request->get_params();
118 $existing_post_slugs = $this->get_existing_slugs($data['posts']);
119
120 // Output the array of existing post slugs
121 return [
122 'status' => 'success',
123 'data' => $existing_post_slugs,
124 ];
125 }
126
127 public function parse_csv( WP_REST_Request $request ) {
128 $files = $request->get_file_params();
129
130 $file = $files['file']['tmp_name'];
131
132 $parser = new CSV_Parser();
133 $parsed = $parser->parse( $file );
134
135 // if ( $parsed['type'] == 'sample/csv' ) {
136 // return [
137 // 'status' => 'success'
138 // ];
139 // }
140
141 if ( isset( $parsed['posts'] ) && is_array( $parsed['posts'] ) ) {
142 $post_names = array_map(function ($post) {
143 return $post['post_name'];
144 }, $parsed['posts']);
145 }
146
147 $existing_post_slugs = $this->get_existing_slugs( $post_names) ;
148
149 return [
150 'status' => 'success',
151 'data' => $existing_post_slugs,
152 ];
153 }
154
155 public function migrate_plugins( WP_REST_Request $request ) {
156 betterdocs()->kbmigration->migrate();
157
158 return [
159 'status' => 'success'
160 ];
161 }
162
163 public function get_existing_slugs( $slugs ) {
164 // Initialize the array for existing post slugs
165 $existing_post_slugs = [];
166
167 // Initialize the query arguments
168 $args = [
169 'post_type' => 'docs', // Change 'post' to your custom post type if applicable
170 'post_status' => 'publish',
171 'posts_per_page' => -1, // Retrieve all posts
172 'fields' => 'ids', // Retrieve only post IDs to reduce memory usage
173 ];
174
175 // Create a new instance of WP_Query
176 $query = new WP_Query($args);
177
178 // Get an array of post slugs from the query
179 $all_post_slugs = array_map(function ($post_id) {
180 return get_post_field('post_name', $post_id);
181 }, $query->posts);
182
183 // Restore original post data
184 wp_reset_postdata();
185
186 // Find the intersection of the two arrays to get existing post slugs
187 $existing_post_slugs = array_intersect($slugs, $all_post_slugs);
188
189 return $existing_post_slugs;
190
191 }
192 public function insights(){
193 return true;
194 }
195
196 public function get_settings() {
197 return betterdocs()->settings->get_all( true );
198 }
199
200 public function save_settings( WP_REST_Request $request ) {
201 if ( betterdocs()->settings->save_settings( $request->get_params() ) ) {
202 return $this->success( __( 'Settings Saved!', 'betterdocs' ) );
203 }
204
205 return $this->error( 'nothing_changed', __( 'There are no changes to be saved.', 'betterdocs' ), 200 );
206 }
207
208 public function test_reporting( $request ){
209 return $this->container->get(ReportEmail::class)->test_email_report( $request );
210 }
211
212 public function do_wizard_tracking() {
213 $insights = betterdocs()->admin->plugin_insights( true );
214 // Get our data
215 $insights->schedule_tracking();
216 $insights->set_is_tracking_allowed( true, 'betterdocs' );
217 if ( $insights->do_tracking( true ) ) {
218 $insights->update_block_notice( 'betterdocs' );
219 }
220
221 return true;
222 }
223
224 public function plugin_insights( $request ){
225 if ( $this->do_wizard_tracking() ) {
226 wp_send_json_success('done');
227 }
228 wp_send_json_error('Something went wrong.');
229 }
230 }
231