| @@ -7,15 +7,13 @@ | ||
| 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; |
| 16 | -use WPDeveloper\BetterDocs\Core\Settings as CoreSettings; | |
| 17 | -use WPDeveloper\BetterDocs\Utils\Helper; | |
| 18 | 16 | |
| 19 | 17 | class Settings extends BaseAPI { |
| 20 | 18 | |
| 21 | 19 | public function permission_check(): bool { |
| @@ -29,8 +27,14 @@ | ||
| 29 | 27 | $this->post( 'plugin_insights', [ $this, 'plugin_insights' ] ); |
| 30 | 28 | $this->post( 'create-sample-docs', [$this, 'sample_docs'] ); |
| 31 | 29 | $this->post( 'reporting-test', [ $this, 'test_reporting' ] ); |
| 32 | 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 | + } | |
| 33 | 37 | $this->post( 'export-settings', [ $this, 'export_settings' ] ); |
| 34 | 38 | $this->post( 'import-docs', [ $this, 'import_docs' ] ); |
| 35 | 39 | $this->post( 'import-settings', [ $this, 'import_settings' ] ); |
| 36 | 40 | $this->post( 'parse-xml', [ $this, 'parse_xml' ] ); |
| @@ -58,9 +62,9 @@ | ||
| 58 | 62 | public function sample_docs( WP_REST_Request $request ) { |
| 59 | 63 | $action = $request->get_param( 'action' ); |
| 60 | 64 | |
| 61 | 65 | if ( $action == 'create-dummy-data' ) { |
| 62 | - $file = BETTERDOCS_ABSPATH . 'assets/static/admin/images/BetterDocs-sample-data.csv'; | |
| 66 | + $file = BETTERDOCS_ABSPATH . 'assets/admin/images/BetterDocs-sample-data.csv'; | |
| 63 | 67 | $args = [ |
| 64 | 68 | 'fetch_attachments' => true, |
| 65 | 69 | 'action' => '', |
| 66 | 70 | 'existing_slug' => '', |
| @@ -70,29 +74,27 @@ | ||
| 70 | 74 | return $wp_import_object->run(); |
| 71 | 75 | } |
| 72 | 76 | } |
| 73 | 77 | |
| 74 | - public function export_docs( WP_REST_Request $request ) { | |
| 75 | - $data = $request->get_params(); | |
| 76 | - $args = [ | |
| 77 | - 'include_post_featured_image_as_attachment' => true, | |
| 78 | - 'status' => 'publish', | |
| 79 | - 'content' => $data['export_type'], | |
| 80 | - 'selected_docs' => $data['export_docs'], | |
| 81 | - 'include_faq' => $data['enable_export_faq'], | |
| 82 | - ]; | |
| 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 | + ]; | |
| 83 | 87 | |
| 84 | 88 | if ( $data['export_type'] == 'docs' && $data['export_docs'][0] != 'all' ) { |
| 85 | 89 | $args['post__in'] = $data['export_docs']; |
| 86 | 90 | } |
| 87 | 91 | |
| 88 | - if ( $data['export_type'] == 'doc_category' || $data['export_type'] == 'knowledge_base' ) { | |
| 89 | - $args['content'] = 'docs'; | |
| 92 | + if ( $data['export_type'] == 'doc_category' && $data['export_categories'][0] != 'all' ) { | |
| 93 | + $args['category_terms'] = $data['export_categories']; | |
| 90 | 94 | } |
| 91 | 95 | |
| 92 | - if ( $data['export_type'] == 'doc_category' && $data['export_categories'][0] != 'all' ) { | |
| 93 | - $args['category_terms'] = $data['export_categories']; | |
| 94 | - } else if ( $data['export_type'] == 'knowledge_base' && $data['export_kbs'][0] != 'all' ) { | |
| 96 | + if ( $data['export_type'] == 'knowledge_base' && $data['export_kbs'][0] != 'all' ) { | |
| 95 | 97 | $args['kb_terms'] = $data['export_kbs']; |
| 96 | 98 | } |
| 97 | 99 | |
| 98 | 100 | if ( $data['export_type'] == 'glossaries' && $data['export_glossaries'][0] != 'all' ) { |
| @@ -99,21 +101,89 @@ | ||
| 99 | 101 | $args['glossary_terms'] = $data['export_glossaries']; |
| 100 | 102 | unset( $args['selected_docs'] ); |
| 101 | 103 | } |
| 102 | 104 | |
| 103 | - if ( $data['file_type'] == 'xml' ) { | |
| 104 | - $exporter = new XMLExporter( $args ); | |
| 105 | - } else if ( $data['file_type'] == 'csv' ) { | |
| 106 | - $exporter = new CSVExporter( $args ); | |
| 107 | - } | |
| 105 | + if ( $data['file_type'] == 'xml' ) { | |
| 106 | + $exporter = new WPExporter( $args ); | |
| 107 | + } elseif ( $data['file_type'] == 'csv' ) { | |
| 108 | + $exporter = new CSVExporter( $args ); | |
| 109 | + } | |
| 108 | 110 | |
| 109 | - /** | |
| 110 | - * @var XMLExporter|CSVExporter $exporter | |
| 111 | - */ | |
| 111 | + /** | |
| 112 | + * @var WPExporter|CSVExporter $exporter | |
| 113 | + */ | |
| 112 | 114 | |
| 113 | 115 | return $exporter->run(); |
| 114 | 116 | } |
| 115 | 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 | + | |
| 116 | 186 | public function export_settings( WP_REST_Request $request ): array { |
| 117 | 187 | $betterdocs_settings = get_option( 'betterdocs_settings' ); |
| 118 | 188 | $json_str = json_encode( $betterdocs_settings, JSON_PRETTY_PRINT ); |
| 119 | 189 | $file_name = 'betterdocs-settings.json'; |
| @@ -156,13 +226,9 @@ | ||
| 156 | 226 | $args = [ |
| 157 | 227 | 'fetch_attachments' => true, |
| 158 | 228 | 'existing_slug' => $existing_slug, |
| 159 | 229 | 'action' => $action, |
| 160 | - 'file_type' => $files['file']['type'], | |
| 161 | - // The original upload name (with its extension) is the reliable | |
| 162 | - // signal for choosing the parser; the browser-reported MIME on | |
| 163 | - // $files['file']['type'] is not. | |
| 164 | - 'file_name' => isset( $files['file']['name'] ) ? $files['file']['name'] : '' | |
| 230 | + 'file_type' => $files['file']['type'] | |
| 165 | 231 | ]; |
| 166 | 232 | |
| 167 | 233 | $wp_importer = new WPImport( $file, $args ); |
| 168 | 234 | |
| @@ -234,9 +300,9 @@ | ||
| 234 | 300 | ); |
| 235 | 301 | |
| 236 | 302 | $initial_response = wp_remote_get( $api_endpoint, array( 'headers' => $headers ) ); |
| 237 | 303 | |
| 238 | - if ( $initial_response['response']['code'] == '404' || $initial_response['response']['code'] == 401 ) { | |
| 304 | + if ( $initial_response['response']['code'] == '404' ) { | |
| 239 | 305 | return [ |
| 240 | 306 | 'status' => 'error', |
| 241 | 307 | 'message' => esc_html__( 'Unauthorized API Key or Collection ID', 'betterdocs' ) |
| 242 | 308 | ]; |
| @@ -295,41 +361,13 @@ | ||
| 295 | 361 | return true; |
| 296 | 362 | } |
| 297 | 363 | |
| 298 | 364 | public function get_settings(): array { |
| 299 | - $settings = betterdocs()->settings->get_all( true ); | |
| 300 | - | |
| 301 | - // Never hand raw API keys back over REST. The route is already gated to | |
| 302 | - // `edit_docs_settings` (see permission_check() above), but get_all( true ) returned | |
| 303 | - // UNMASKED values, so an authorized admin still received every key in full — the admin | |
| 304 | - // UI only ever needs the masked form. Mask for managers, and strip entirely for anyone | |
| 305 | - // else as defense-in-depth should that capability gate ever loosen. Mirrors the | |
| 306 | - // admin-page localizer (Core\Settings::enqueue()) so the masked-key save round-trip | |
| 307 | - // stays consistent. | |
| 308 | - $sensitive_api_keys = CoreSettings::sensitive_api_key_fields(); | |
| 309 | - $can_manage = current_user_can( 'edit_docs_settings' ); | |
| 310 | - foreach ( $sensitive_api_keys as $api_key_field ) { | |
| 311 | - if ( ! isset( $settings[ $api_key_field ] ) ) { | |
| 312 | - continue; | |
| 313 | - } | |
| 314 | - if ( ! $can_manage ) { | |
| 315 | - unset( $settings[ $api_key_field ] ); | |
| 316 | - continue; | |
| 317 | - } | |
| 318 | - if ( ! empty( $settings[ $api_key_field ] ) ) { | |
| 319 | - $settings[ $api_key_field ] = Helper::mask_api_key( $settings[ $api_key_field ] ); | |
| 320 | - } | |
| 321 | - } | |
| 322 | - | |
| 323 | - return $settings; | |
| 365 | + return betterdocs()->settings->get_all( true ); | |
| 324 | 366 | } |
| 325 | 367 | |
| 326 | 368 | public function save_settings( WP_REST_Request $request ) { |
| 327 | - $result = betterdocs()->settings->save_settings( $request->get_params() ); | |
| 328 | - if ( is_wp_error( $result ) ) { | |
| 329 | - return $this->error( $result->get_error_code(), $result->get_error_message(), 400 ); | |
| 330 | - } | |
| 331 | - if ( $result ) { | |
| 369 | + if ( betterdocs()->settings->save_settings( $request->get_params() ) ) { | |
| 332 | 370 | return $this->success( __( 'Settings Saved!', 'betterdocs' ) ); |
| 333 | 371 | } |
| 334 | 372 | |
| 335 | 373 | return $this->error( 'nothing_changed', __( 'There are no changes to be saved.', 'betterdocs' ), 200 ); |