PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.3.9
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.3.9
3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 All 194 releases
convertkit / admin / section / class-convertkit-admin-section-tools.php

class-convertkit-admin-section-tools.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 3.3.9, at admin/section/class-convertkit-admin-section-tools.php

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