| @@ -1,230 +1,366 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace WPDeveloper\BetterDocs\REST; |
| 4 | 4 | |
| 5 | -use WPDeveloper\BetterDocs\Admin\ReportEmail; | |
| 5 | +use WP_Query; | |
| 6 | 6 | use WP_REST_Request; |
| 7 | -use WPDeveloper\BetterDocs\Core\BaseAPI; | |
| 8 | -use WPDeveloper\BetterDocs\Admin\WPExporter; | |
| 9 | 7 | use WPDeveloper\BetterDocs\Admin\CSVExporter; |
| 8 | +use WPDeveloper\BetterDocs\Admin\Importer\Parsers\CSV_Parser; | |
| 10 | 9 | use WPDeveloper\BetterDocs\Admin\Importer\WPImport; |
| 11 | -use WPDeveloper\BetterDocs\Admin\Importer\Parsers\CSV_Parser; | |
| 12 | -use WP_Query; | |
| 10 | +use WPDeveloper\BetterDocs\Admin\ReportEmail; | |
| 11 | +use WPDeveloper\BetterDocs\Admin\XMLExporter; | |
| 12 | +use WPDeveloper\BetterDocs\Core\BaseAPI; | |
| 13 | +use WPDeveloper\BetterDocs\Dependencies\DI\DependencyException; | |
| 14 | +use WPDeveloper\BetterDocs\Dependencies\DI\NotFoundException; | |
| 15 | +use WPDeveloper\BetterDocsChatbot\Core\AIChatbot; | |
| 16 | +use WPDeveloper\BetterDocs\Core\Settings as CoreSettings; | |
| 17 | +use WPDeveloper\BetterDocs\Utils\Helper; | |
| 13 | 18 | |
| 14 | 19 | class Settings extends BaseAPI { |
| 15 | 20 | |
| 16 | - public function permission_check() { | |
| 17 | - return current_user_can( 'edit_docs_settings' ); | |
| 21 | + public function permission_check(): bool { | |
| 22 | + return current_user_can( 'edit_docs_settings' ); | |
| 23 | + } | |
| 24 | + | |
| 25 | + public function register() { | |
| 26 | + $this->get( 'settings', [ $this, 'get_settings' ] ); | |
| 27 | + $this->post( 'dark-mode', [ $this, 'dark_mode' ] ); | |
| 28 | + $this->post( 'settings', [ $this, 'save_settings' ] ); | |
| 29 | + $this->post( 'plugin_insights', [ $this, 'plugin_insights' ] ); | |
| 30 | + $this->post( 'create-sample-docs', [$this, 'sample_docs'] ); | |
| 31 | + $this->post( 'reporting-test', [ $this, 'test_reporting' ] ); | |
| 32 | + $this->post( 'export-docs', [ $this, 'export_docs' ] ); | |
| 33 | + $this->post( 'export-settings', [ $this, 'export_settings' ] ); | |
| 34 | + $this->post( 'import-docs', [ $this, 'import_docs' ] ); | |
| 35 | + $this->post( 'import-settings', [ $this, 'import_settings' ] ); | |
| 36 | + $this->post( 'parse-xml', [ $this, 'parse_xml' ] ); | |
| 37 | + $this->post( 'parse-csv', [ $this, 'parse_csv' ] ); | |
| 38 | + $this->post( 'migrate', [ $this, 'migrate_plugins' ] ); | |
| 39 | + $this->post( 'helpscout-migration', [$this, 'helpscout_migration'] ); | |
| 40 | + $this->post( 'dashboard-mode', [$this, 'dashboard_mode'] ); | |
| 18 | 41 | } |
| 19 | 42 | |
| 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'] ); | |
| 43 | + public function dashboard_mode( WP_REST_Request $request ) { | |
| 44 | + $mode = $request->get_param( 'dashboard_mode' ); | |
| 45 | + return update_option( 'dashboard_mode', $mode ); | |
| 46 | + } | |
| 24 | 47 | |
| 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 | - } | |
| 48 | + public function dark_mode( WP_REST_Request $request ): bool { | |
| 49 | + $dark_mode = rest_sanitize_boolean( $request->get_param( 'mode' ) ); | |
| 34 | 50 | |
| 51 | + if ( betterdocs()->settings->save( 'dark_mode', $dark_mode ) ) { | |
| 52 | + return true; | |
| 53 | + } | |
| 54 | + | |
| 55 | + return false; | |
| 56 | + } | |
| 57 | + | |
| 58 | + public function sample_docs( WP_REST_Request $request ) { | |
| 59 | + $action = $request->get_param( 'action' ); | |
| 60 | + | |
| 61 | + if ( $action == 'create-dummy-data' ) { | |
| 62 | + $file = BETTERDOCS_ABSPATH . 'assets/static/admin/images/BetterDocs-sample-data.csv'; | |
| 63 | + $args = [ | |
| 64 | + 'fetch_attachments' => true, | |
| 65 | + 'action' => '', | |
| 66 | + 'existing_slug' => '', | |
| 67 | + 'file_type' => 'text/csv' | |
| 68 | + ]; | |
| 69 | + $wp_import_object = new WPImport( $file, $args ); | |
| 70 | + return $wp_import_object->run(); | |
| 71 | + } | |
| 72 | + } | |
| 73 | + | |
| 35 | 74 | 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 | - ]; | |
| 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 | + ]; | |
| 41 | 83 | |
| 42 | - $data = $request->get_params(); | |
| 84 | + if ( $data['export_type'] == 'docs' && $data['export_docs'][0] != 'all' ) { | |
| 85 | + $args['post__in'] = $data['export_docs']; | |
| 86 | + } | |
| 43 | 87 | |
| 44 | - if ( $data['export_type'] == 'docs' && $data['export_docs'][0] != 'all' ) { | |
| 45 | - $args['post__in'] = $data['export_docs']; | |
| 46 | - } | |
| 88 | + if ( $data['export_type'] == 'doc_category' || $data['export_type'] == 'knowledge_base' ) { | |
| 89 | + $args['content'] = 'docs'; | |
| 90 | + } | |
| 47 | 91 | |
| 48 | - if ( $data['export_type'] == 'doc_category' && $data['export_categories'][0] != 'all' ) { | |
| 49 | - $args['category_terms'] = $data['export_categories']; | |
| 50 | - } | |
| 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' ) { | |
| 95 | + $args['kb_terms'] = $data['export_kbs']; | |
| 96 | + } | |
| 51 | 97 | |
| 52 | - if ( $data['export_type'] == 'knowledge_base' && $data['export_kbs'][0] != 'all' ) { | |
| 53 | - $args['kb_terms'] = $data['export_kbs']; | |
| 54 | - } | |
| 98 | + if ( $data['export_type'] == 'glossaries' && $data['export_glossaries'][0] != 'all' ) { | |
| 99 | + $args['glossary_terms'] = $data['export_glossaries']; | |
| 100 | + unset( $args['selected_docs'] ); | |
| 101 | + } | |
| 55 | 102 | |
| 56 | - if ($data['file_type'] == 'xml') { | |
| 57 | - $exporter = new WPExporter( $args ); | |
| 58 | - } else if ($data['file_type'] == 'csv') { | |
| 103 | + if ( $data['file_type'] == 'xml' ) { | |
| 104 | + $exporter = new XMLExporter( $args ); | |
| 105 | + } else if ( $data['file_type'] == 'csv' ) { | |
| 59 | 106 | $exporter = new CSVExporter( $args ); |
| 60 | 107 | } |
| 61 | 108 | |
| 62 | - return $exporter->run(); | |
| 63 | - } | |
| 109 | + /** | |
| 110 | + * @var XMLExporter|CSVExporter $exporter | |
| 111 | + */ | |
| 64 | 112 | |
| 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 | - } | |
| 113 | + return $exporter->run(); | |
| 114 | + } | |
| 78 | 115 | |
| 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 ); | |
| 116 | + public function export_settings( WP_REST_Request $request ): array { | |
| 117 | + $betterdocs_settings = get_option( 'betterdocs_settings' ); | |
| 118 | + $json_str = json_encode( $betterdocs_settings, JSON_PRETTY_PRINT ); | |
| 119 | + $file_name = 'betterdocs-settings.json'; | |
| 84 | 120 | |
| 85 | - if ( $save == true ) { | |
| 86 | - return [ | |
| 87 | - 'status' => 'success' | |
| 88 | - ]; | |
| 89 | - } else { | |
| 90 | - return [ | |
| 91 | - 'status' => 'failed' | |
| 92 | - ]; | |
| 93 | - } | |
| 94 | - } | |
| 121 | + return [ | |
| 122 | + 'success' => true, | |
| 123 | + 'data' => [ | |
| 124 | + 'filename' => $file_name, | |
| 125 | + 'filetype' => 'text/json', | |
| 126 | + 'download' => $json_str, | |
| 127 | + ] | |
| 128 | + ]; | |
| 129 | + } | |
| 95 | 130 | |
| 96 | - public function import_docs( WP_REST_Request $request ) { | |
| 97 | - $existing_slug = $request->get_param('existing_slug'); | |
| 98 | - $action = $request->get_param('action'); | |
| 131 | + public function import_settings( WP_REST_Request $request ): array { | |
| 132 | + $settings = $request->get_param( 'settings' ); | |
| 133 | + // Decode the JSON data into a PHP array | |
| 134 | + $settings = json_decode( $settings, true ); | |
| 135 | + $saved = update_option( 'betterdocs_settings', $settings ); | |
| 99 | 136 | |
| 100 | - $files = $request->get_file_params(); | |
| 137 | + if ( ! $saved ) { | |
| 138 | + return [ | |
| 139 | + 'status' => 'failed' | |
| 140 | + ]; | |
| 141 | + } | |
| 101 | 142 | |
| 143 | + return [ | |
| 144 | + 'status' => 'success' | |
| 145 | + ]; | |
| 146 | + } | |
| 147 | + | |
| 148 | + public function import_docs( WP_REST_Request $request ): array { | |
| 149 | + $existing_slug = $request->get_param( 'existing_slug' ); | |
| 150 | + $action = $request->get_param( 'action' ); | |
| 151 | + | |
| 152 | + $files = $request->get_file_params(); | |
| 153 | + | |
| 102 | 154 | $file = $files['file']['tmp_name']; |
| 103 | 155 | |
| 104 | - $args = [ | |
| 156 | + $args = [ | |
| 105 | 157 | 'fetch_attachments' => true, |
| 106 | - 'existing_slug' => $existing_slug, | |
| 107 | - 'action' => $action, | |
| 108 | - 'file_type' => $files['file']['type'] | |
| 158 | + 'existing_slug' => $existing_slug, | |
| 159 | + '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'] : '' | |
| 109 | 165 | ]; |
| 110 | 166 | |
| 111 | - $wp_importer = new WPImport( $file, $args ); | |
| 167 | + $wp_importer = new WPImport( $file, $args ); | |
| 112 | 168 | |
| 113 | 169 | return $wp_importer->run(); |
| 114 | - } | |
| 170 | + } | |
| 115 | 171 | |
| 116 | - public function parse_xml( WP_REST_Request $request ) { | |
| 117 | - $data = $request->get_params(); | |
| 118 | - $existing_post_slugs = $this->get_existing_slugs($data['posts']); | |
| 172 | + public function parse_xml( WP_REST_Request $request ): array { | |
| 173 | + $data = $request->get_params(); | |
| 174 | + $existing_post_slugs = $this->get_existing_slugs( $data['posts'] ); | |
| 119 | 175 | |
| 120 | - // Output the array of existing post slugs | |
| 121 | - return [ | |
| 122 | - 'status' => 'success', | |
| 123 | - 'data' => $existing_post_slugs, | |
| 124 | - ]; | |
| 125 | - } | |
| 176 | + // Output the array of existing post slugs | |
| 177 | + return [ | |
| 178 | + 'status' => 'success', | |
| 179 | + 'data' => $existing_post_slugs, | |
| 180 | + ]; | |
| 181 | + } | |
| 126 | 182 | |
| 127 | - public function parse_csv( WP_REST_Request $request ) { | |
| 128 | - $files = $request->get_file_params(); | |
| 183 | + public function parse_csv( WP_REST_Request $request ): array { | |
| 184 | + $files = $request->get_file_params(); | |
| 129 | 185 | |
| 130 | 186 | $file = $files['file']['tmp_name']; |
| 131 | 187 | |
| 132 | - $parser = new CSV_Parser(); | |
| 188 | + $parser = new CSV_Parser(); | |
| 133 | 189 | $parsed = $parser->parse( $file ); |
| 134 | 190 | |
| 135 | - // if ( $parsed['type'] == 'sample/csv' ) { | |
| 136 | - // return [ | |
| 137 | - // 'status' => 'success' | |
| 138 | - // ]; | |
| 139 | - // } | |
| 191 | + $existing_post_slugs = []; | |
| 140 | 192 | |
| 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 | - } | |
| 193 | + if ( isset( $parsed['posts'] ) && is_array( $parsed['posts'] ) ) { | |
| 194 | + $post_names = array_map( | |
| 195 | + function ( $post ) { | |
| 196 | + return $post['post_name']; | |
| 197 | + }, | |
| 198 | + $parsed['posts'] | |
| 199 | + ); | |
| 146 | 200 | |
| 147 | - $existing_post_slugs = $this->get_existing_slugs( $post_names) ; | |
| 201 | + $existing_post_slugs = $this->get_existing_slugs( $post_names ); | |
| 202 | + } | |
| 148 | 203 | |
| 149 | - return [ | |
| 150 | - 'status' => 'success', | |
| 151 | - 'data' => $existing_post_slugs, | |
| 152 | - ]; | |
| 153 | - } | |
| 204 | + return [ | |
| 205 | + 'status' => 'success', | |
| 206 | + 'data' => $existing_post_slugs, | |
| 207 | + ]; | |
| 208 | + } | |
| 154 | 209 | |
| 155 | - public function migrate_plugins( WP_REST_Request $request ) { | |
| 156 | - betterdocs()->kbmigration->migrate(); | |
| 210 | + public function migrate_plugins( WP_REST_Request $request ): array { | |
| 211 | + betterdocs()->kbmigration->migrate(); | |
| 157 | 212 | |
| 158 | - return [ | |
| 159 | - 'status' => 'success' | |
| 160 | - ]; | |
| 161 | - } | |
| 213 | + return [ | |
| 214 | + 'status' => 'success' | |
| 215 | + ]; | |
| 216 | + } | |
| 162 | 217 | |
| 163 | - public function get_existing_slugs( $slugs ) { | |
| 164 | - // Initialize the array for existing post slugs | |
| 165 | - $existing_post_slugs = []; | |
| 218 | + public function helpscout_migration( WP_REST_Request $request ) { | |
| 219 | + $api_key = $request->get_param( 'helpscout_api_key' ); | |
| 220 | + $collection_id = $request->get_param( 'helpscout_collection_id' ); | |
| 166 | 221 | |
| 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 | - ]; | |
| 222 | + if ( ! $api_key || ! $collection_id ) { | |
| 223 | + return [ | |
| 224 | + 'status' => 'error', | |
| 225 | + 'message' => esc_html__( 'Please provide both API Key and Collection ID', 'betterdocs' ) | |
| 226 | + ]; | |
| 227 | + } | |
| 174 | 228 | |
| 175 | - // Create a new instance of WP_Query | |
| 176 | - $query = new WP_Query($args); | |
| 229 | + $api_endpoint = 'https://docsapi.helpscout.net/v1/collections/' . $collection_id . '/articles?pageSize=1'; | |
| 177 | 230 | |
| 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); | |
| 231 | + $headers = array( | |
| 232 | + 'Authorization' => 'Basic ' . base64_encode( $api_key . ':X' ), | |
| 233 | + 'Content-Type' => 'application/json', | |
| 234 | + ); | |
| 182 | 235 | |
| 183 | - // Restore original post data | |
| 184 | - wp_reset_postdata(); | |
| 236 | + $initial_response = wp_remote_get( $api_endpoint, array( 'headers' => $headers ) ); | |
| 185 | 237 | |
| 186 | - // Find the intersection of the two arrays to get existing post slugs | |
| 187 | - $existing_post_slugs = array_intersect($slugs, $all_post_slugs); | |
| 238 | + if ( $initial_response['response']['code'] == '404' || $initial_response['response']['code'] == 401 ) { | |
| 239 | + return [ | |
| 240 | + 'status' => 'error', | |
| 241 | + 'message' => esc_html__( 'Unauthorized API Key or Collection ID', 'betterdocs' ) | |
| 242 | + ]; | |
| 243 | + } | |
| 188 | 244 | |
| 189 | - return $existing_post_slugs; | |
| 245 | + // Enqueue migration task | |
| 246 | + betterdocs()->backgroundProccessor->push_to_queue( | |
| 247 | + array( | |
| 248 | + 'headers' => $headers, | |
| 249 | + 'initial_response' => $initial_response, | |
| 250 | + 'api_key' => $api_key, | |
| 251 | + 'collection_id' => $collection_id | |
| 252 | + ) | |
| 253 | + )->save(); | |
| 190 | 254 | |
| 191 | - } | |
| 192 | - public function insights(){ | |
| 193 | - return true; | |
| 194 | - } | |
| 255 | + // Start processing the queue | |
| 256 | + betterdocs()->backgroundProccessor->dispatch(); | |
| 195 | 257 | |
| 196 | - public function get_settings() { | |
| 197 | - return betterdocs()->settings->get_all( true ); | |
| 198 | - } | |
| 258 | + return [ | |
| 259 | + 'status' => 'success', | |
| 260 | + 'message' => esc_html__( 'Migration process has started in the background.', 'betterdocs' ) | |
| 261 | + ]; | |
| 262 | + } | |
| 199 | 263 | |
| 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 | - } | |
| 264 | + public function get_existing_slugs( $slugs ) { | |
| 265 | + // Initialize the array for existing post slugs | |
| 266 | + $existing_post_slugs = []; | |
| 204 | 267 | |
| 205 | - return $this->error( 'nothing_changed', __( 'There are no changes to be saved.', 'betterdocs' ), 200 ); | |
| 206 | - } | |
| 268 | + // Initialize the query arguments | |
| 269 | + $args = [ | |
| 270 | + 'post_type' => [ 'docs', 'betterdocs_faq' ], // Change 'post' to your custom post type if applicable | |
| 271 | + 'post_status' => 'publish', | |
| 272 | + 'posts_per_page' => -1, // Retrieve all posts | |
| 273 | + 'fields' => 'ids', // Retrieve only post IDs to reduce memory usage | |
| 274 | + ]; | |
| 207 | 275 | |
| 208 | - public function test_reporting( $request ){ | |
| 209 | - return $this->container->get(ReportEmail::class)->test_email_report( $request ); | |
| 210 | - } | |
| 276 | + // Create a new instance of WP_Query | |
| 277 | + $query = new WP_Query( $args ); | |
| 211 | 278 | |
| 212 | - public function do_wizard_tracking() { | |
| 279 | + // Get an array of post slugs from the query | |
| 280 | + $all_post_slugs = array_map( | |
| 281 | + function ( $post_id ) { | |
| 282 | + return get_post_field( 'post_name', $post_id ); | |
| 283 | + }, | |
| 284 | + $query->posts | |
| 285 | + ); | |
| 286 | + | |
| 287 | + // Restore original post data | |
| 288 | + wp_reset_postdata(); | |
| 289 | + | |
| 290 | + // Find the intersection of the two arrays to get existing post slugs | |
| 291 | + return array_intersect( $slugs, $all_post_slugs ); | |
| 292 | + } | |
| 293 | + | |
| 294 | + public function insights(): bool { | |
| 295 | + return true; | |
| 296 | + } | |
| 297 | + | |
| 298 | + 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; | |
| 324 | + } | |
| 325 | + | |
| 326 | + 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 ) { | |
| 332 | + return $this->success( __( 'Settings Saved!', 'betterdocs' ) ); | |
| 333 | + } | |
| 334 | + | |
| 335 | + return $this->error( 'nothing_changed', __( 'There are no changes to be saved.', 'betterdocs' ), 200 ); | |
| 336 | + } | |
| 337 | + | |
| 338 | + /** | |
| 339 | + * @throws NotFoundException | |
| 340 | + * @throws DependencyException | |
| 341 | + */ | |
| 342 | + public function test_reporting( $request ) { | |
| 343 | + return $this->container->get( ReportEmail::class )->test_email_report( $request ); | |
| 344 | + } | |
| 345 | + | |
| 346 | + public function do_wizard_tracking(): bool { | |
| 213 | 347 | $insights = betterdocs()->admin->plugin_insights( true ); |
| 214 | 348 | // Get our data |
| 215 | 349 | $insights->schedule_tracking(); |
| 216 | - $insights->set_is_tracking_allowed( true, 'betterdocs' ); | |
| 217 | - if ( $insights->do_tracking( true ) ) { | |
| 218 | - $insights->update_block_notice( 'betterdocs' ); | |
| 219 | - } | |
| 350 | + $insights->set_is_tracking_allowed( true, 'betterdocs' ); | |
| 351 | + if ( $insights->do_tracking( true ) ) { | |
| 352 | + $insights->update_block_notice( 'betterdocs' ); | |
| 353 | + } | |
| 354 | + if ( ! isset( $_COOKIE['betterdocs_insights_allowed'] ) ) { | |
| 355 | + setcookie( 'betterdocs_insights_allowed', true, time() + ( 30 * 24 * 60 * 60 ), '/' ); | |
| 356 | + } | |
| 357 | + return true; | |
| 358 | + } | |
| 220 | 359 | |
| 221 | - return true; | |
| 360 | + public function plugin_insights( $request ) { | |
| 361 | + if ( $this->do_wizard_tracking() ) { | |
| 362 | + wp_send_json_success( 'done' ); | |
| 363 | + } | |
| 364 | + wp_send_json_error( 'Something went wrong.' ); | |
| 222 | 365 | } |
| 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 | 366 | } |