PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 3.8.9
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v3.8.9
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 +96 -24 4.4.13.8.9 View file →
@@ -7,9 +7,9 @@
7 7 use WPDeveloper\BetterDocs\Admin\CSVExporter;
8 8 use WPDeveloper\BetterDocs\Admin\Importer\Parsers\CSV_Parser;
9 9 use WPDeveloper\BetterDocs\Admin\Importer\WPImport;
10 10 use WPDeveloper\BetterDocs\Admin\ReportEmail;
11 -use WPDeveloper\BetterDocs\Admin\XMLExporter;
11 +use WPDeveloper\BetterDocs\Admin\WPExporter;
12 12 use WPDeveloper\BetterDocs\Core\BaseAPI;
13 13 use WPDeveloper\BetterDocs\Dependencies\DI\DependencyException;
14 14 use WPDeveloper\BetterDocs\Dependencies\DI\NotFoundException;
15 15 use WPDeveloper\BetterDocsChatbot\Core\AIChatbot;
@@ -27,8 +27,14 @@
27 27 $this->post( 'plugin_insights', [ $this, 'plugin_insights' ] );
28 28 $this->post( 'create-sample-docs', [$this, 'sample_docs'] );
29 29 $this->post( 'reporting-test', [ $this, 'test_reporting' ] );
30 30 $this->post( 'export-docs', [ $this, 'export_docs' ] );
31 + if(
32 + get_option('betterdocs_chatbot_software__license_status') === 'valid' &&
33 + get_option('betterdocs_pro_software__license_status') === 'valid'
34 + ){
35 + $this->post( 'save-aichatbot', [ $this, 'save_aichatbot' ] );
36 + }
31 37 $this->post( 'export-settings', [ $this, 'export_settings' ] );
32 38 $this->post( 'import-docs', [ $this, 'import_docs' ] );
33 39 $this->post( 'import-settings', [ $this, 'import_settings' ] );
34 40 $this->post( 'parse-xml', [ $this, 'parse_xml' ] );
@@ -68,29 +74,27 @@
68 74 return $wp_import_object->run();
69 75 }
70 76 }
71 77
72 - public function export_docs( WP_REST_Request $request ) {
73 - $data = $request->get_params();
74 - $args = [
75 - 'include_post_featured_image_as_attachment' => true,
76 - 'status' => 'publish',
77 - 'content' => $data['export_type'],
78 - 'selected_docs' => $data['export_docs'],
79 - 'include_faq' => $data['enable_export_faq'],
80 - ];
78 + public function export_docs( WP_REST_Request $request ) {
79 + $data = $request->get_params();
80 + $args = [
81 + 'include_post_featured_image_as_attachment' => true,
82 + 'status' => 'publish',
83 + 'content' => $data['export_type'] == 'glossaries' ? 'glossaries' : 'docs',
84 + 'selected_docs' => $data['export_docs'],
85 + 'include_faq' => $data['enable_export_faq'],
86 + ];
81 87
82 88 if ( $data['export_type'] == 'docs' && $data['export_docs'][0] != 'all' ) {
83 89 $args['post__in'] = $data['export_docs'];
84 90 }
85 91
86 - if ( $data['export_type'] == 'doc_category' || $data['export_type'] == 'knowledge_base' ) {
87 - $args['content'] = 'docs';
92 + if ( $data['export_type'] == 'doc_category' && $data['export_categories'][0] != 'all' ) {
93 + $args['category_terms'] = $data['export_categories'];
88 94 }
89 95
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' ) {
96 + if ( $data['export_type'] == 'knowledge_base' && $data['export_kbs'][0] != 'all' ) {
93 97 $args['kb_terms'] = $data['export_kbs'];
94 98 }
95 99
96 100 if ( $data['export_type'] == 'glossaries' && $data['export_glossaries'][0] != 'all' ) {
@@ -97,21 +101,89 @@
97 101 $args['glossary_terms'] = $data['export_glossaries'];
98 102 unset( $args['selected_docs'] );
99 103 }
100 104
101 - if ( $data['file_type'] == 'xml' ) {
102 - $exporter = new XMLExporter( $args );
103 - } else if ( $data['file_type'] == 'csv' ) {
104 - $exporter = new CSVExporter( $args );
105 - }
105 + if ( $data['file_type'] == 'xml' ) {
106 + $exporter = new WPExporter( $args );
107 + } elseif ( $data['file_type'] == 'csv' ) {
108 + $exporter = new CSVExporter( $args );
109 + }
106 110
107 - /**
108 - * @var XMLExporter|CSVExporter $exporter
109 - */
111 + /**
112 + * @var WPExporter|CSVExporter $exporter
113 + */
110 114
111 115 return $exporter->run();
112 116 }
113 117
118 + public function save_aichatbot( WP_REST_Request $request ) {
119 + $data = $request->get_params();
120 +
121 + betterdocs()->settings->save( 'enable_ai_chatbot', $data['enable_ai_chatbot'] );
122 +
123 + $api_status = AIChatbot::check_openai_quota($data['ai_chatbot_api_key']);
124 +
125 + $api_status = json_decode($api_status, true); // Decode the JSON response
126 +
127 + // Check if required data is empty
128 + if ( empty( $data['ai_chatbot_api_key'] ) ||
129 + empty( $data['ai_chatbot_embed_model'] ) ||
130 + empty( $data['ai_chatbot_chat_model'] ) ) {
131 + return [
132 + 'status' => 'error',
133 + 'message' => __('Required fields cannot be empty.', 'betterdocs'),
134 + ];
135 + }
136 +
137 + if (isset($api_status['error'])) { // Check if the 'error' key exists in the response
138 + $error_message = $api_status['error']['message'] ?? 'Unknown error';
139 + return [
140 + 'status' => 'error',
141 + 'message' => __($error_message, 'betterdocs'),
142 + ];
143 + }
144 +
145 +
146 + // Get previous values
147 + $prev_api_key = betterdocs()->settings->get( 'ai_chatbot_api_key', '' );
148 + $prev_embed_model = betterdocs()->settings->get( 'ai_chatbot_embed_model', '' );
149 +
150 + // Save settings if all required fields are provided
151 + betterdocs()->settings->save( 'ai_chatbot_api_key', $data['ai_chatbot_api_key'] );
152 + betterdocs()->settings->save( 'ai_chatbot_embed_model', $data['ai_chatbot_embed_model'] );
153 + betterdocs()->settings->save( 'ai_chatbot_chat_model', $data['ai_chatbot_chat_model'] );
154 +
155 + // Execute if ai_chatbot_embed_model has changed
156 + if ( $prev_embed_model !== $data['ai_chatbot_embed_model'] ) {
157 + update_option( 'is_ai_chatbot_bg_complete', 'running' );
158 + update_option( 'background_process_status', 1 );
159 + AIChatbot::send_data();
160 + }
161 +
162 + $post_type = 'docs'; //betterdocs()->settings->get('ai_chatbot_allow_post_types', 'docs');
163 + $docs_count = AIChatbot::get_docs_count( $post_type ); // Calculate the number of iterations needed
164 + update_option( 'docs_count', $docs_count );
165 +
166 +
167 + if($docs_count < 1){
168 + update_option( 'background_process_status', 0 );
169 + update_option( 'is_ai_chatbot_bg_complete', 'complete' );
170 + update_option( 'show_ai_chatbot_process_notices', 0 );
171 + }
172 +
173 + // Execute if ai_chatbot_api_key has changed
174 + if ( $prev_api_key !== $data['ai_chatbot_api_key'] ) {
175 + $openai_api_key = $data['ai_chatbot_api_key'];
176 + AIChatbot::encrypted_api_key( $openai_api_key, '0kXsYZmsHgvIB85miXWlq3nigYYD4PktOSVvOs3vlbA=' );
177 + }
178 +
179 + return [
180 + 'status' => 'success',
181 + 'message' => __( 'Settings saved successfully.', 'betterdocs' ),
182 + ];
183 + }
184 +
185 +
114 186 public function export_settings( WP_REST_Request $request ): array {
115 187 $betterdocs_settings = get_option( 'betterdocs_settings' );
116 188 $json_str = json_encode( $betterdocs_settings, JSON_PRETTY_PRINT );
117 189 $file_name = 'betterdocs-settings.json';
@@ -228,9 +300,9 @@
228 300 );
229 301
230 302 $initial_response = wp_remote_get( $api_endpoint, array( 'headers' => $headers ) );
231 303
232 - if ( $initial_response['response']['code'] == '404' || $initial_response['response']['code'] == 401 ) {
304 + if ( $initial_response['response']['code'] == '404' ) {
233 305 return [
234 306 'status' => 'error',
235 307 'message' => esc_html__( 'Unauthorized API Key or Collection ID', 'betterdocs' )
236 308 ];