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

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