← All changes
|
admin/section/class-convertkit-admin-section-tools.php
+51
-113
3.4.2
→
2.7.4
View file →
| @@ -18,8 +18,12 @@ | ||
| 18 | 18 | * Constructor |
| 19 | 19 | */ |
| 20 | 20 | public function __construct() { |
| 21 | 21 | |
| 22 | + // Initialize WP_Filesystem. | |
| 23 | + require_once ABSPATH . 'wp-admin/includes/file.php'; | |
| 24 | + WP_Filesystem(); | |
| 25 | + | |
| 22 | 26 | $this->settings_key = '_wp_convertkit_tools'; // Required for ConvertKit_Settings_Base, but we don't save settings on the Tools screen. |
| 23 | 27 | $this->name = 'tools'; |
| 24 | 28 | $this->title = __( 'Tools', 'convertkit' ); |
| 25 | 29 | $this->tab_text = __( 'Tools', 'convertkit' ); |
| @@ -27,8 +31,9 @@ | ||
| 27 | 31 | // Register and maybe output notices for this settings screen, and the Intercom messenger. |
| 28 | 32 | if ( $this->on_settings_screen( $this->name ) ) { |
| 29 | 33 | add_filter( 'convertkit_settings_base_register_notices', array( $this, 'register_notices' ) ); |
| 30 | 34 | add_action( 'convertkit_settings_base_render_before', array( $this, 'maybe_output_notices' ) ); |
| 35 | + add_action( 'admin_footer', array( $this, 'output_intercom' ) ); | |
| 31 | 36 | } |
| 32 | 37 | |
| 33 | 38 | parent::__construct(); |
| 34 | 39 | |
| @@ -48,19 +53,12 @@ | ||
| 48 | 53 | |
| 49 | 54 | return array_merge( |
| 50 | 55 | $notices, |
| 51 | 56 | array( |
| 52 | - 'import_configuration_upload_error' => __( 'An error occured uploading the configuration file.', 'convertkit' ), | |
| 53 | - 'import_configuration_invalid_file_type' => __( 'The uploaded configuration file isn\'t valid.', 'convertkit' ), | |
| 54 | - 'import_configuration_empty' => __( 'The uploaded configuration file contains no settings.', 'convertkit' ), | |
| 55 | - 'import_configuration_success' => __( 'Configuration imported successfully.', 'convertkit' ), | |
| 56 | - 'migrate_activecampaign_configuration_success' => __( 'ActiveCampaign forms migrated successfully.', 'convertkit' ), | |
| 57 | - 'migrate_aweber_configuration_success' => __( 'AWeber forms migrated successfully.', 'convertkit' ), | |
| 58 | - 'migrate_campaignmonitor_configuration_success' => __( 'Campaign Monitor forms migrated successfully.', 'convertkit' ), | |
| 59 | - 'migrate_convertkit_legacy_forms_configuration_success' => __( 'Kit Legacy Forms migrated successfully.', 'convertkit' ), | |
| 60 | - 'migrate_mc4wp_configuration_success' => __( 'MC4WP forms migrated successfully.', 'convertkit' ), | |
| 61 | - 'migrate_mailpoet_configuration_success' => __( 'MailPoet forms migrated successfully.', 'convertkit' ), | |
| 62 | - 'migrate_newsletter_configuration_success' => __( 'Newsletter forms migrated successfully.', 'convertkit' ), | |
| 57 | + 'import_configuration_upload_error' => __( 'An error occured uploading the configuration file.', 'convertkit' ), | |
| 58 | + 'import_configuration_invalid_file_type' => __( 'The uploaded configuration file isn\'t valid.', 'convertkit' ), | |
| 59 | + 'import_configuration_empty' => __( 'The uploaded configuration file contains no settings.', 'convertkit' ), | |
| 60 | + 'import_configuration_success' => __( 'Configuration imported successfully.', 'convertkit' ), | |
| 63 | 61 | ) |
| 64 | 62 | ); |
| 65 | 63 | |
| 66 | 64 | } |
| @@ -72,14 +70,18 @@ | ||
| 72 | 70 | * @since 1.9.7.4 |
| 73 | 71 | */ |
| 74 | 72 | private function maybe_perform_actions() { |
| 75 | 73 | |
| 74 | + // Bail if nonce is invalid. | |
| 75 | + if ( ! $this->verify_nonce() ) { | |
| 76 | + return; | |
| 77 | + } | |
| 78 | + | |
| 76 | 79 | $this->maybe_clear_log(); |
| 77 | 80 | $this->maybe_download_log(); |
| 78 | 81 | $this->maybe_download_system_info(); |
| 79 | 82 | $this->maybe_export_configuration(); |
| 80 | 83 | $this->maybe_import_configuration(); |
| 81 | - $this->maybe_migrate_forms(); | |
| 82 | 84 | |
| 83 | 85 | } |
| 84 | 86 | |
| 85 | 87 | /** |
| @@ -88,20 +90,11 @@ | ||
| 88 | 90 | * @since 1.9.6 |
| 89 | 91 | */ |
| 90 | 92 | private function maybe_clear_log() { |
| 91 | 93 | |
| 92 | - // Bail if nonce verification fails. | |
| 93 | - if ( ! isset( $_REQUEST['_convertkit_settings_tools_nonce'] ) ) { | |
| 94 | - return; | |
| 95 | - } | |
| 96 | - | |
| 97 | - if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_convertkit_settings_tools_nonce'] ), 'convertkit-settings-tools' ) ) { | |
| 98 | - return; | |
| 99 | - } | |
| 100 | - | |
| 101 | 94 | // Bail if the submit button for clearing the debug log was not clicked. |
| 102 | 95 | // Nonce verification already performed in maybe_perform_actions() which calls this function. |
| 103 | - if ( ! array_key_exists( 'convertkit-clear-debug-log', $_REQUEST ) ) { | |
| 96 | + if ( ! array_key_exists( 'convertkit-clear-debug-log', $_REQUEST ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 104 | 97 | return; |
| 105 | 98 | } |
| 106 | 99 | |
| 107 | 100 | // Clear Log. |
| @@ -120,19 +113,13 @@ | ||
| 120 | 113 | * @since 1.9.6 |
| 121 | 114 | */ |
| 122 | 115 | private function maybe_download_log() { |
| 123 | 116 | |
| 124 | - // Bail if nonce verification fails. | |
| 125 | - if ( ! isset( $_REQUEST['_convertkit_settings_tools_nonce'] ) ) { | |
| 126 | - return; | |
| 127 | - } | |
| 117 | + global $wp_filesystem; | |
| 128 | 118 | |
| 129 | - if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_convertkit_settings_tools_nonce'] ), 'convertkit-settings-tools' ) ) { | |
| 130 | - return; | |
| 131 | - } | |
| 132 | - | |
| 133 | 119 | // Bail if the submit button for downloading the debug log was not clicked. |
| 134 | - if ( ! array_key_exists( 'convertkit-download-debug-log', $_REQUEST ) ) { | |
| 120 | + // Nonce verification already performed in maybe_perform_actions() which calls this function. | |
| 121 | + if ( ! array_key_exists( 'convertkit-download-debug-log', $_REQUEST ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 135 | 122 | return; |
| 136 | 123 | } |
| 137 | 124 | |
| 138 | 125 | // Get Log and download. |
| @@ -142,10 +129,9 @@ | ||
| 142 | 129 | header( 'Content-type: application/octet-stream' ); |
| 143 | 130 | header( 'Content-Disposition: attachment; filename=convertkit-log.txt' ); |
| 144 | 131 | header( 'Pragma: no-cache' ); |
| 145 | 132 | header( 'Expires: 0' ); |
| 146 | - // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents | |
| 147 | - echo esc_html( (string) file_get_contents( $log->get_filename() ) ); | |
| 133 | + echo esc_html( $wp_filesystem->get_contents( $log->get_filename() ) ); | |
| 148 | 134 | exit(); |
| 149 | 135 | |
| 150 | 136 | } |
| 151 | 137 | |
| @@ -156,19 +142,13 @@ | ||
| 156 | 142 | * @since 1.9.6 |
| 157 | 143 | */ |
| 158 | 144 | private function maybe_download_system_info() { |
| 159 | 145 | |
| 160 | - // Bail if nonce verification fails. | |
| 161 | - if ( ! isset( $_REQUEST['_convertkit_settings_tools_nonce'] ) ) { | |
| 162 | - return; | |
| 163 | - } | |
| 146 | + global $wp_filesystem; | |
| 164 | 147 | |
| 165 | - if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_convertkit_settings_tools_nonce'] ), 'convertkit-settings-tools' ) ) { | |
| 166 | - return; | |
| 167 | - } | |
| 168 | - | |
| 169 | 148 | // Bail if the submit button for downloading the system info was not clicked. |
| 170 | - if ( ! array_key_exists( 'convertkit-download-system-info', $_REQUEST ) ) { | |
| 149 | + // Nonce verification already performed in maybe_perform_actions() which calls this function. | |
| 150 | + if ( ! array_key_exists( 'convertkit-download-system-info', $_REQUEST ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 171 | 151 | return; |
| 172 | 152 | } |
| 173 | 153 | |
| 174 | 154 | // Get System Info. |
| @@ -173,14 +153,23 @@ | ||
| 173 | 153 | |
| 174 | 154 | // Get System Info. |
| 175 | 155 | $system_info = $this->get_system_info(); |
| 176 | 156 | |
| 157 | + // Write contents to temporary file. | |
| 158 | + $tmpfile = tmpfile(); | |
| 159 | + $filename = stream_get_meta_data( $tmpfile )['uri']; | |
| 160 | + $wp_filesystem->put_contents( | |
| 161 | + $filename, | |
| 162 | + esc_attr( $system_info ) | |
| 163 | + ); | |
| 164 | + | |
| 177 | 165 | // Download. |
| 178 | 166 | header( 'Content-type: application/octet-stream' ); |
| 179 | 167 | header( 'Content-Disposition: attachment; filename=convertkit-system-info.txt' ); |
| 180 | 168 | header( 'Pragma: no-cache' ); |
| 181 | 169 | header( 'Expires: 0' ); |
| 182 | - echo esc_html( $system_info ); | |
| 170 | + echo esc_html( $wp_filesystem->get_contents( $filename ) ); | |
| 171 | + $wp_filesystem->delete( $filename ); | |
| 183 | 172 | exit(); |
| 184 | 173 | |
| 185 | 174 | } |
| 186 | 175 | |
| @@ -191,19 +180,11 @@ | ||
| 191 | 180 | * @since 1.9.7.4 |
| 192 | 181 | */ |
| 193 | 182 | private function maybe_export_configuration() { |
| 194 | 183 | |
| 195 | - // Bail if nonce verification fails. | |
| 196 | - if ( ! isset( $_REQUEST['_convertkit_settings_tools_nonce'] ) ) { | |
| 197 | - return; | |
| 198 | - } | |
| 199 | - | |
| 200 | - if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_convertkit_settings_tools_nonce'] ), 'convertkit-settings-tools' ) ) { | |
| 201 | - return; | |
| 202 | - } | |
| 203 | - | |
| 204 | 184 | // Bail if the submit button for exporting the configuration was not clicked. |
| 205 | - if ( ! array_key_exists( 'convertkit-export', $_REQUEST ) ) { | |
| 185 | + // Nonce verification already performed in maybe_perform_actions() which calls this function. | |
| 186 | + if ( ! array_key_exists( 'convertkit-export', $_REQUEST ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 206 | 187 | return; |
| 207 | 188 | } |
| 208 | 189 | |
| 209 | 190 | // Initialize classes that hold settings. |
| @@ -237,35 +218,26 @@ | ||
| 237 | 218 | * @since 1.9.7.4 |
| 238 | 219 | */ |
| 239 | 220 | private function maybe_import_configuration() { |
| 240 | 221 | |
| 241 | - // Bail if nonce verification fails. | |
| 242 | - if ( ! isset( $_REQUEST['_convertkit_settings_tools_nonce'] ) ) { | |
| 243 | - return; | |
| 244 | - } | |
| 222 | + // Allow us to easily interact with the filesystem. | |
| 223 | + require_once ABSPATH . 'wp-admin/includes/file.php'; | |
| 224 | + WP_Filesystem(); | |
| 225 | + global $wp_filesystem; | |
| 245 | 226 | |
| 246 | - if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_convertkit_settings_tools_nonce'] ), 'convertkit-settings-tools' ) ) { | |
| 247 | - return; | |
| 248 | - } | |
| 249 | - | |
| 250 | 227 | // Bail if the submit button for importing the configuration was not clicked. |
| 251 | - if ( ! array_key_exists( 'convertkit-import', $_REQUEST ) ) { | |
| 228 | + // Nonce verification already performed in maybe_perform_actions() which calls this function. | |
| 229 | + if ( ! array_key_exists( 'convertkit-import', $_REQUEST ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 252 | 230 | return; |
| 253 | 231 | } |
| 254 | 232 | |
| 255 | 233 | // Bail if no configuration file was supplied. |
| 256 | - if ( isset( $_FILES['import']['error'] ) && $_FILES['import']['error'] !== 0 ) { | |
| 234 | + if ( $_FILES['import']['error'] !== 0 ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 257 | 235 | $this->redirect_with_error_notice( 'import_configuration_upload_error' ); |
| 258 | 236 | } |
| 259 | 237 | |
| 260 | - // Bail if the file cannot be read. | |
| 261 | - if ( ! isset( $_FILES['import']['tmp_name'] ) ) { | |
| 262 | - $this->redirect_with_error_notice( 'import_configuration_upload_error' ); | |
| 263 | - } | |
| 264 | - | |
| 265 | 238 | // Read file. |
| 266 | - // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents | |
| 267 | - $json = file_get_contents( sanitize_text_field( wp_unslash( $_FILES['import']['tmp_name'] ) ) ); | |
| 239 | + $json = $wp_filesystem->get_contents( $_FILES['import']['tmp_name'] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 268 | 240 | |
| 269 | 241 | // Decode. |
| 270 | 242 | $import = json_decode( $json, true ); |
| 271 | 243 | |
| @@ -302,49 +274,24 @@ | ||
| 302 | 274 | |
| 303 | 275 | } |
| 304 | 276 | |
| 305 | 277 | /** |
| 306 | - * Replaces ActiveCampaign Form Shortcodes and Blocks with Kit Form Shortcodes and Blocks, if the user submitted the | |
| 307 | - * ActiveCampaign Migrate Configuration section. | |
| 278 | + * Verifies if the _convertkit_settings_tools_nonce nonce was included in the request, | |
| 279 | + * and if so whether the nonce action is valid. | |
| 308 | 280 | * |
| 309 | - * @since 3.1.7 | |
| 281 | + * @since 1.9.6 | |
| 282 | + * | |
| 283 | + * @return bool | |
| 310 | 284 | */ |
| 311 | - private function maybe_migrate_forms() { | |
| 285 | + private function verify_nonce() { | |
| 312 | 286 | |
| 313 | 287 | // Bail if nonce verification fails. |
| 314 | 288 | if ( ! isset( $_REQUEST['_convertkit_settings_tools_nonce'] ) ) { |
| 315 | - return; | |
| 289 | + return false; | |
| 316 | 290 | } |
| 317 | 291 | |
| 318 | - if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_convertkit_settings_tools_nonce'] ), 'convertkit-settings-tools' ) ) { | |
| 319 | - return; | |
| 320 | - } | |
| 292 | + return wp_verify_nonce( sanitize_key( $_REQUEST['_convertkit_settings_tools_nonce'] ), 'convertkit-settings-tools' ); | |
| 321 | 293 | |
| 322 | - // Get importers. | |
| 323 | - $importers = convertkit_get_form_importers(); | |
| 324 | - | |
| 325 | - // Find the importer that was used for the form submission. | |
| 326 | - foreach ( $importers as $importer ) { | |
| 327 | - // Skip if this importer was not used. | |
| 328 | - if ( ! isset( $_REQUEST[ 'convertkit-import-' . $importer['name'] ] ) ) { | |
| 329 | - continue; | |
| 330 | - } | |
| 331 | - | |
| 332 | - // Skip if no mappings were submitted for the importer. | |
| 333 | - if ( ! isset( $_REQUEST[ '_wp_convertkit_integration_' . $importer['name'] . '_settings' ] ) ) { | |
| 334 | - continue; | |
| 335 | - } | |
| 336 | - | |
| 337 | - // Sanitize mappings. | |
| 338 | - $mappings = array_map( 'sanitize_text_field', wp_unslash( $_REQUEST[ '_wp_convertkit_integration_' . $importer['name'] . '_settings' ] ) ); | |
| 339 | - | |
| 340 | - // Replace third party form shortcodes and blocks with Kit form shortcodes and blocks. | |
| 341 | - WP_ConvertKit()->get_class( 'admin_importer_' . $importer['name'] )->import( $mappings ); | |
| 342 | - | |
| 343 | - // Redirect to Tools screen. | |
| 344 | - $this->redirect_with_success_notice( 'migrate_' . $importer['name'] . '_configuration_success' ); | |
| 345 | - } | |
| 346 | - | |
| 347 | 294 | } |
| 348 | 295 | |
| 349 | 296 | /** |
| 350 | 297 | * Outputs the Debug Log and System Info view. |
| @@ -363,17 +310,8 @@ | ||
| 363 | 310 | // Get Log and System Info. |
| 364 | 311 | $log = new ConvertKit_Log( CONVERTKIT_PLUGIN_PATH ); |
| 365 | 312 | $system_info = $this->get_system_info(); |
| 366 | 313 | |
| 367 | - // Get Forms. | |
| 368 | - $forms = new ConvertKit_Resource_Forms(); | |
| 369 | - | |
| 370 | - // Get Importers, if Kit Forms exist. | |
| 371 | - $importers = array(); | |
| 372 | - if ( $forms->exist() ) { | |
| 373 | - $importers = convertkit_get_form_importers(); | |
| 374 | - } | |
| 375 | - | |
| 376 | 314 | // Output view. |
| 377 | 315 | require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/settings/tools.php'; |
| 378 | 316 | |
| 379 | 317 | /** |
| @@ -404,9 +342,9 @@ | ||
| 404 | 342 | * @return string Documentation URL. |
| 405 | 343 | */ |
| 406 | 344 | public function documentation_url() { |
| 407 | 345 | |
| 408 | - return 'https://help.kit.com/en/articles/2502591-how-to-set-up-the-kit-plugin-on-your-wordpress-website'; | |
| 346 | + return 'https://help.kit.com/en/articles/2502591-the-convertkit-wordpress-plugin'; | |
| 409 | 347 | |
| 410 | 348 | } |
| 411 | 349 | |
| 412 | 350 | /** |