PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.2.4
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.2.4
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.2.4, at admin/section/class-convertkit-admin-section-tools.php

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