-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathclass-convertkit-admin-section-tools.php
More file actions
515 lines (410 loc) · 15.4 KB
/
class-convertkit-admin-section-tools.php
File metadata and controls
515 lines (410 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
<?php
/**
* ConvertKit Settings Tools class.
*
* @package ConvertKit
* @author ConvertKit
*/
/**
* Registers Tools for debugging and system information that can be accessed at Settings > Kit > Tools.
*
* @package ConvertKit
* @author ConvertKit
*/
class ConvertKit_Admin_Section_Tools extends ConvertKit_Admin_Section_Base {
/**
* Constructor
*/
public function __construct() {
// Initialize WP_Filesystem.
require_once ABSPATH . 'wp-admin/includes/file.php';
WP_Filesystem();
$this->settings_key = '_wp_convertkit_tools'; // Required for ConvertKit_Settings_Base, but we don't save settings on the Tools screen.
$this->name = 'tools';
$this->title = __( 'Tools', 'convertkit' );
$this->tab_text = __( 'Tools', 'convertkit' );
// Register and maybe output notices for this settings screen, and the Intercom messenger.
if ( $this->on_settings_screen( $this->name ) ) {
add_filter( 'convertkit_settings_base_register_notices', array( $this, 'register_notices' ) );
add_action( 'convertkit_settings_base_render_before', array( $this, 'maybe_output_notices' ) );
}
parent::__construct();
$this->maybe_perform_actions();
}
/**
* Registers success and error notices for the Tools screen, to be displayed
* depending on the action.
*
* @since 2.5.1
*
* @param array $notices Regsitered success and error notices.
* @return array
*/
public function register_notices( $notices ) {
return array_merge(
$notices,
array(
'import_configuration_upload_error' => __( 'An error occured uploading the configuration file.', 'convertkit' ),
'import_configuration_invalid_file_type' => __( 'The uploaded configuration file isn\'t valid.', 'convertkit' ),
'import_configuration_empty' => __( 'The uploaded configuration file contains no settings.', 'convertkit' ),
'import_configuration_success' => __( 'Configuration imported successfully.', 'convertkit' ),
'migrate_aweber_configuration_success' => __( 'AWeber forms migrated successfully.', 'convertkit' ),
'migrate_mc4wp_configuration_success' => __( 'MC4WP forms migrated successfully.', 'convertkit' ),
'migrate_mailpoet_configuration_success' => __( 'MailPoet forms migrated successfully.', 'convertkit' ),
)
);
}
/**
* Possibly perform some actions, such as clearing the log, downloading the log,
* downloading system information or any third party actions now.
*
* @since 1.9.7.4
*/
private function maybe_perform_actions() {
$this->maybe_clear_log();
$this->maybe_download_log();
$this->maybe_download_system_info();
$this->maybe_export_configuration();
$this->maybe_import_configuration();
$this->maybe_migrate_aweber_configuration();
$this->maybe_migrate_mc4wp_configuration();
$this->maybe_migrate_mailpoet_configuration();
}
/**
* Clears the Log.
*
* @since 1.9.6
*/
private function maybe_clear_log() {
// Bail if nonce verification fails.
if ( ! isset( $_REQUEST['_convertkit_settings_tools_nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_convertkit_settings_tools_nonce'] ), 'convertkit-settings-tools' ) ) {
return;
}
// Bail if the submit button for clearing the debug log was not clicked.
// Nonce verification already performed in maybe_perform_actions() which calls this function.
if ( ! array_key_exists( 'convertkit-clear-debug-log', $_REQUEST ) ) {
return;
}
// Clear Log.
$log = new ConvertKit_Log( CONVERTKIT_PLUGIN_PATH );
$log->clear();
// Redirect to Tools screen.
$this->redirect();
}
/**
* Prompts a browser download for the log file, if the user clicked
* the Download Log button.
*
* @since 1.9.6
*/
private function maybe_download_log() {
global $wp_filesystem;
// Bail if nonce verification fails.
if ( ! isset( $_REQUEST['_convertkit_settings_tools_nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_convertkit_settings_tools_nonce'] ), 'convertkit-settings-tools' ) ) {
return;
}
// Bail if the submit button for downloading the debug log was not clicked.
if ( ! array_key_exists( 'convertkit-download-debug-log', $_REQUEST ) ) {
return;
}
// Get Log and download.
$log = new ConvertKit_Log( CONVERTKIT_PLUGIN_PATH );
// Download.
header( 'Content-type: application/octet-stream' );
header( 'Content-Disposition: attachment; filename=convertkit-log.txt' );
header( 'Pragma: no-cache' );
header( 'Expires: 0' );
echo esc_html( $wp_filesystem->get_contents( $log->get_filename() ) );
exit();
}
/**
* Prompts a browser download for the system information, if the user clicked
* the Download System Info button.
*
* @since 1.9.6
*/
private function maybe_download_system_info() {
global $wp_filesystem;
// Bail if nonce verification fails.
if ( ! isset( $_REQUEST['_convertkit_settings_tools_nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_convertkit_settings_tools_nonce'] ), 'convertkit-settings-tools' ) ) {
return;
}
// Bail if the submit button for downloading the system info was not clicked.
if ( ! array_key_exists( 'convertkit-download-system-info', $_REQUEST ) ) {
return;
}
// Get System Info.
$system_info = $this->get_system_info();
// Write contents to temporary file.
$tmpfile = tmpfile();
$filename = stream_get_meta_data( $tmpfile )['uri'];
$wp_filesystem->put_contents(
$filename,
esc_attr( $system_info )
);
// Download.
header( 'Content-type: application/octet-stream' );
header( 'Content-Disposition: attachment; filename=convertkit-system-info.txt' );
header( 'Pragma: no-cache' );
header( 'Expires: 0' );
echo esc_html( $wp_filesystem->get_contents( $filename ) );
$wp_filesystem->delete( $filename );
exit();
}
/**
* Prompts a browser download for the configuration file, if the user clicked
* the Export button.
*
* @since 1.9.7.4
*/
private function maybe_export_configuration() {
// Bail if nonce verification fails.
if ( ! isset( $_REQUEST['_convertkit_settings_tools_nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_convertkit_settings_tools_nonce'] ), 'convertkit-settings-tools' ) ) {
return;
}
// Bail if the submit button for exporting the configuration was not clicked.
if ( ! array_key_exists( 'convertkit-export', $_REQUEST ) ) {
return;
}
// Initialize classes that hold settings.
$settings = new ConvertKit_Settings();
$restrict_content_settings = new ConvertKit_Settings_Restrict_Content();
$broadcasts_settings = new ConvertKit_Settings_Broadcasts();
// Define configuration data to include in the export file.
$json = wp_json_encode(
array(
'settings' => $settings->get(),
'restrict_content' => $restrict_content_settings->get(),
'broadcasts' => $broadcasts_settings->get(),
)
);
// Download.
header( 'Content-type: application/x-msdownload' );
header( 'Content-Disposition: attachment; filename=convertkit-export.json' );
header( 'Pragma: no-cache' );
header( 'Expires: 0' );
echo $json; // phpcs:ignore WordPress.Security.EscapeOutput
exit();
}
/**
* Imports the configuration file, if it's included in the form request
* and has the expected structure.
*
* @since 1.9.7.4
*/
private function maybe_import_configuration() {
// Bail if nonce verification fails.
if ( ! isset( $_REQUEST['_convertkit_settings_tools_nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_convertkit_settings_tools_nonce'] ), 'convertkit-settings-tools' ) ) {
return;
}
// Allow us to easily interact with the filesystem.
require_once ABSPATH . 'wp-admin/includes/file.php';
WP_Filesystem();
global $wp_filesystem;
// Bail if the submit button for importing the configuration was not clicked.
if ( ! array_key_exists( 'convertkit-import', $_REQUEST ) ) {
return;
}
// Bail if no configuration file was supplied.
if ( isset( $_FILES['import']['error'] ) && $_FILES['import']['error'] !== 0 ) {
$this->redirect_with_error_notice( 'import_configuration_upload_error' );
}
// Bail if the file cannot be read.
if ( ! isset( $_FILES['import']['tmp_name'] ) ) {
$this->redirect_with_error_notice( 'import_configuration_upload_error' );
}
// Read file.
$json = $wp_filesystem->get_contents( sanitize_text_field( wp_unslash( $_FILES['import']['tmp_name'] ) ) );
// Decode.
$import = json_decode( $json, true );
// Bail if the data isn't JSON.
if ( is_null( $import ) ) {
$this->redirect_with_error_notice( 'import_configuration_invalid_file_type' );
}
// Bail if no settings exist.
if ( ! array_key_exists( 'settings', $import ) ) {
$this->redirect_with_error_notice( 'import_configuration_empty' );
}
// Import: Settings.
if ( array_key_exists( 'settings', $import ) ) {
$settings = new ConvertKit_Settings();
update_option( $settings::SETTINGS_NAME, $import['settings'] );
}
// Import: Restrict Content Settings.
if ( array_key_exists( 'restrict_content', $import ) ) {
$restrict_content_settings = new ConvertKit_Settings_Restrict_Content();
update_option( $restrict_content_settings::SETTINGS_NAME, $import['restrict_content'] );
}
// Import: Broadcasts Settings.
if ( array_key_exists( 'broadcasts', $import ) ) {
$broadcasts_settings = new ConvertKit_Settings_Broadcasts();
update_option( $broadcasts_settings::SETTINGS_NAME, $import['broadcasts'] );
}
// Redirect to Tools screen.
$this->redirect_with_success_notice( 'import_configuration_success' );
}
/**
* Replaces AWeber Form Shortcodes with Kit Form Shortcodes, if the user submitted the
* AWeber Migrate Configuration section.
*
* @since 3.1.5
*/
private function maybe_migrate_aweber_configuration() {
// Bail if nonce verification fails.
if ( ! isset( $_REQUEST['_convertkit_settings_tools_nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_convertkit_settings_tools_nonce'] ), 'convertkit-settings-tools' ) ) {
return;
}
// Bail if no AWeber Form IDs were submitted.
if ( ! isset( $_REQUEST['_wp_convertkit_integration_aweber_settings'] ) ) {
return;
}
// Initialise the importer.
$aweber = new ConvertKit_Admin_Importer_AWeber();
// Iterate through the AWeber Form IDs and replace the shortcodes with the Kit Form Shortcodes.
foreach ( array_map( 'sanitize_text_field', wp_unslash( $_REQUEST['_wp_convertkit_integration_aweber_settings'] ) ) as $aweber_form_id => $kit_form_id ) {
$aweber->replace_blocks_in_posts( (int) $aweber_form_id, (int) $kit_form_id );
$aweber->replace_shortcodes_in_posts( (int) $aweber_form_id, (int) $kit_form_id );
}
// Redirect to Tools screen.
$this->redirect_with_success_notice( 'migrate_aweber_configuration_success' );
}
/**
* Replaces MC4WP Form Shortcodes with Kit Form Shortcodes, if the user submitted the
* MC4WP Migrate Configuration section.
*
* @since 3.1.0
*/
private function maybe_migrate_mc4wp_configuration() {
// Bail if nonce verification fails.
if ( ! isset( $_REQUEST['_convertkit_settings_tools_nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_convertkit_settings_tools_nonce'] ), 'convertkit-settings-tools' ) ) {
return;
}
// Bail if no MC4WP Form IDs were submitted.
if ( ! isset( $_REQUEST['_wp_convertkit_integration_mc4wp_settings'] ) ) {
return;
}
// Initialise the importer.
$mc4wp = new ConvertKit_Admin_Importer_MC4WP();
// Iterate through the MC4WP Form IDs and replace the shortcodes with the Kit Form Shortcodes.
foreach ( array_map( 'sanitize_text_field', wp_unslash( $_REQUEST['_wp_convertkit_integration_mc4wp_settings'] ) ) as $mc4wp_form_id => $kit_form_id ) {
$mc4wp->replace_blocks_in_posts( (int) $mc4wp_form_id, (int) $kit_form_id );
$mc4wp->replace_shortcodes_in_posts( (int) $mc4wp_form_id, (int) $kit_form_id );
}
// Redirect to Tools screen.
$this->redirect_with_success_notice( 'migrate_mc4wp_configuration_success' );
}
/**
* Replaces Mailpoet Form Shortcodes and Blocks with Kit Form Shortcodes and Blocks, if the user submitted the
* Mailpoet Migrate Configuration section.
*
* @since 3.1.6
*/
private function maybe_migrate_mailpoet_configuration() {
// Bail if nonce verification fails.
if ( ! isset( $_REQUEST['_convertkit_settings_tools_nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_convertkit_settings_tools_nonce'] ), 'convertkit-settings-tools' ) ) {
return;
}
// Bail if no Mailpoet Form IDs were submitted.
if ( ! isset( $_REQUEST['_wp_convertkit_integration_mailpoet_settings'] ) ) {
return;
}
// Initialise the importer.
$mailpoet = new ConvertKit_Admin_Importer_Mailpoet();
// Iterate through the Mailpoet Form IDs and replace the shortcodes with the Kit Form Shortcodes.
foreach ( array_map( 'sanitize_text_field', wp_unslash( $_REQUEST['_wp_convertkit_integration_mailpoet_settings'] ) ) as $mailpoet_form_id => $kit_form_id ) {
$mailpoet->replace_blocks_in_posts( (int) $mailpoet_form_id, (int) $kit_form_id );
$mailpoet->replace_shortcodes_in_posts( (int) $mailpoet_form_id, (int) $kit_form_id );
}
// Redirect to Tools screen.
$this->redirect_with_success_notice( 'migrate_mailpoet_configuration_success' );
}
/**
* Outputs the Debug Log and System Info view.
*
* @since 1.9.6
*/
public function render() {
/**
* Performs actions prior to rendering the settings form.
*
* @since 2.0.0
*/
do_action( 'convertkit_settings_base_render_before' );
// Get Log and System Info.
$log = new ConvertKit_Log( CONVERTKIT_PLUGIN_PATH );
$system_info = $this->get_system_info();
// Get Forms.
$forms = new ConvertKit_Resource_Forms();
// Get Importers.
$aweber = new ConvertKit_Admin_Importer_AWeber();
$mc4wp = new ConvertKit_Admin_Importer_MC4WP();
$mailpoet = new ConvertKit_Admin_Importer_Mailpoet();
// Output view.
require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/settings/tools.php';
/**
* Performs actions after rendering of the settings form.
*
* @since 2.0.0
*/
do_action( 'convertkit_settings_base_render_after' );
}
/**
* Prints help info for this section
*/
public function print_section_info() {
?>
<p><?php esc_html_e( 'Tools to help you manage Kit on your site.', 'convertkit' ); ?></p>
<?php
}
/**
* Returns the URL for the ConvertKit documentation for this setting section.
*
* @since 2.0.8
*
* @return string Documentation URL.
*/
public function documentation_url() {
return 'https://help.kit.com/en/articles/2502591-how-to-set-up-the-kit-plugin-on-your-wordpress-website';
}
/**
* Returns a string comprising of the WordPress system information, with Plugin information
* prepended.
*
* @since 1.9.8.3
*/
private function get_system_info() {
// If we're using WordPress < 5.2, there's no WP_Debug_Data class to fetch system information from.
if ( version_compare( get_bloginfo( 'version' ), '5.2', '<' ) ) {
return __( 'WordPress 5.2 or higher is required for system information report.', 'convertkit' );
}
// Use WordPress' debug_data() function to get system info, matching how Tools > Site Health > Info works.
if ( ! class_exists( 'WP_Debug_Data' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-debug-data.php';
}
return str_replace( '`', '', WP_Debug_Data::format( WP_Debug_Data::debug_data(), 'debug' ) );
}
}