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