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

435 lines 13.1 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 $this->settings_key = '_wp_convertkit_tools'; // Required for ConvertKit_Settings_Base, but we don't save settings on the Tools screen.
23 $this->name = 'tools';
24 $this->title = __( 'Tools', 'convertkit' );
25 $this->tab_text = __( 'Tools', 'convertkit' );
26
27 // Register and maybe output notices for this settings screen, and the Intercom messenger.
28 if ( $this->on_settings_screen( $this->name ) ) {
29 add_filter( 'convertkit_settings_base_register_notices', array( $this, 'register_notices' ) );
30 add_action( 'convertkit_settings_base_render_before', array( $this, 'maybe_output_notices' ) );
31 }
32
33 parent::__construct();
34
35 $this->maybe_perform_actions();
36 }
37
38 /**
39 * Registers success and error notices for the Tools screen, to be displayed
40 * depending on the action.
41 *
42 * @since 2.5.1
43 *
44 * @param array $notices Regsitered success and error notices.
45 * @return array
46 */
47 public function register_notices( $notices ) {
48
49 return array_merge(
50 $notices,
51 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' ),
63 )
64 );
65
66 }
67
68 /**
69 * Possibly perform some actions, such as clearing the log, downloading the log,
70 * downloading system information or any third party actions now.
71 *
72 * @since 1.9.7.4
73 */
74 private function maybe_perform_actions() {
75
76 $this->maybe_clear_log();
77 $this->maybe_download_log();
78 $this->maybe_download_system_info();
79 $this->maybe_export_configuration();
80 $this->maybe_import_configuration();
81 $this->maybe_migrate_forms();
82
83 }
84
85 /**
86 * Clears the Log.
87 *
88 * @since 1.9.6
89 */
90 private function maybe_clear_log() {
91
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 // Bail if the submit button for clearing the debug log was not clicked.
102 // Nonce verification already performed in maybe_perform_actions() which calls this function.
103 if ( ! array_key_exists( 'convertkit-clear-debug-log', $_REQUEST ) ) {
104 return;
105 }
106
107 // Clear Log.
108 $log = new ConvertKit_Log( CONVERTKIT_PLUGIN_PATH );
109 $log->clear();
110
111 // Redirect to Tools screen.
112 $this->redirect();
113
114 }
115
116 /**
117 * Prompts a browser download for the log file, if the user clicked
118 * the Download Log button.
119 *
120 * @since 1.9.6
121 */
122 private function maybe_download_log() {
123
124 // Bail if nonce verification fails.
125 if ( ! isset( $_REQUEST['_convertkit_settings_tools_nonce'] ) ) {
126 return;
127 }
128
129 if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_convertkit_settings_tools_nonce'] ), 'convertkit-settings-tools' ) ) {
130 return;
131 }
132
133 // Bail if the submit button for downloading the debug log was not clicked.
134 if ( ! array_key_exists( 'convertkit-download-debug-log', $_REQUEST ) ) {
135 return;
136 }
137
138 // Get Log and download.
139 $log = new ConvertKit_Log( CONVERTKIT_PLUGIN_PATH );
140
141 // Download.
142 header( 'Content-type: application/octet-stream' );
143 header( 'Content-Disposition: attachment; filename=convertkit-log.txt' );
144 header( 'Pragma: no-cache' );
145 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() ) );
148 exit();
149
150 }
151
152 /**
153 * Prompts a browser download for the system information, if the user clicked
154 * the Download System Info button.
155 *
156 * @since 1.9.6
157 */
158 private function maybe_download_system_info() {
159
160 // Bail if nonce verification fails.
161 if ( ! isset( $_REQUEST['_convertkit_settings_tools_nonce'] ) ) {
162 return;
163 }
164
165 if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_convertkit_settings_tools_nonce'] ), 'convertkit-settings-tools' ) ) {
166 return;
167 }
168
169 // Bail if the submit button for downloading the system info was not clicked.
170 if ( ! array_key_exists( 'convertkit-download-system-info', $_REQUEST ) ) {
171 return;
172 }
173
174 // Get System Info.
175 $system_info = $this->get_system_info();
176
177 // Download.
178 header( 'Content-type: application/octet-stream' );
179 header( 'Content-Disposition: attachment; filename=convertkit-system-info.txt' );
180 header( 'Pragma: no-cache' );
181 header( 'Expires: 0' );
182 echo esc_html( $system_info );
183 exit();
184
185 }
186
187 /**
188 * Prompts a browser download for the configuration file, if the user clicked
189 * the Export button.
190 *
191 * @since 1.9.7.4
192 */
193 private function maybe_export_configuration() {
194
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 // Bail if the submit button for exporting the configuration was not clicked.
205 if ( ! array_key_exists( 'convertkit-export', $_REQUEST ) ) {
206 return;
207 }
208
209 // Initialize classes that hold settings.
210 $settings = new ConvertKit_Settings();
211 $restrict_content_settings = new ConvertKit_Settings_Restrict_Content();
212 $broadcasts_settings = new ConvertKit_Settings_Broadcasts();
213
214 // Define configuration data to include in the export file.
215 $json = wp_json_encode(
216 array(
217 'settings' => $settings->get(),
218 'restrict_content' => $restrict_content_settings->get(),
219 'broadcasts' => $broadcasts_settings->get(),
220 )
221 );
222
223 // Download.
224 header( 'Content-type: application/x-msdownload' );
225 header( 'Content-Disposition: attachment; filename=convertkit-export.json' );
226 header( 'Pragma: no-cache' );
227 header( 'Expires: 0' );
228 echo $json; // phpcs:ignore WordPress.Security.EscapeOutput
229 exit();
230
231 }
232
233 /**
234 * Imports the configuration file, if it's included in the form request
235 * and has the expected structure.
236 *
237 * @since 1.9.7.4
238 */
239 private function maybe_import_configuration() {
240
241 // Bail if nonce verification fails.
242 if ( ! isset( $_REQUEST['_convertkit_settings_tools_nonce'] ) ) {
243 return;
244 }
245
246 if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_convertkit_settings_tools_nonce'] ), 'convertkit-settings-tools' ) ) {
247 return;
248 }
249
250 // Bail if the submit button for importing the configuration was not clicked.
251 if ( ! array_key_exists( 'convertkit-import', $_REQUEST ) ) {
252 return;
253 }
254
255 // Bail if no configuration file was supplied.
256 if ( isset( $_FILES['import']['error'] ) && $_FILES['import']['error'] !== 0 ) {
257 $this->redirect_with_error_notice( 'import_configuration_upload_error' );
258 }
259
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 // 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'] ) ) );
268
269 // Decode.
270 $import = json_decode( $json, true );
271
272 // Bail if the data isn't JSON.
273 if ( is_null( $import ) ) {
274 $this->redirect_with_error_notice( 'import_configuration_invalid_file_type' );
275 }
276
277 // Bail if no settings exist.
278 if ( ! array_key_exists( 'settings', $import ) ) {
279 $this->redirect_with_error_notice( 'import_configuration_empty' );
280 }
281
282 // Import: Settings.
283 if ( array_key_exists( 'settings', $import ) ) {
284 $settings = new ConvertKit_Settings();
285 update_option( $settings::SETTINGS_NAME, $import['settings'] );
286 }
287
288 // Import: Restrict Content Settings.
289 if ( array_key_exists( 'restrict_content', $import ) ) {
290 $restrict_content_settings = new ConvertKit_Settings_Restrict_Content();
291 update_option( $restrict_content_settings::SETTINGS_NAME, $import['restrict_content'] );
292 }
293
294 // Import: Broadcasts Settings.
295 if ( array_key_exists( 'broadcasts', $import ) ) {
296 $broadcasts_settings = new ConvertKit_Settings_Broadcasts();
297 update_option( $broadcasts_settings::SETTINGS_NAME, $import['broadcasts'] );
298 }
299
300 // Redirect to Tools screen.
301 $this->redirect_with_success_notice( 'import_configuration_success' );
302
303 }
304
305 /**
306 * Replaces ActiveCampaign Form Shortcodes and Blocks with Kit Form Shortcodes and Blocks, if the user submitted the
307 * ActiveCampaign Migrate Configuration section.
308 *
309 * @since 3.1.7
310 */
311 private function maybe_migrate_forms() {
312
313 // Bail if nonce verification fails.
314 if ( ! isset( $_REQUEST['_convertkit_settings_tools_nonce'] ) ) {
315 return;
316 }
317
318 if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_convertkit_settings_tools_nonce'] ), 'convertkit-settings-tools' ) ) {
319 return;
320 }
321
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 }
348
349 /**
350 * Outputs the Debug Log and System Info view.
351 *
352 * @since 1.9.6
353 */
354 public function render() {
355
356 /**
357 * Performs actions prior to rendering the settings form.
358 *
359 * @since 2.0.0
360 */
361 do_action( 'convertkit_settings_base_render_before' );
362
363 // Get Log and System Info.
364 $log = new ConvertKit_Log( CONVERTKIT_PLUGIN_PATH );
365 $system_info = $this->get_system_info();
366
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 // Output view.
377 require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/settings/tools.php';
378
379 /**
380 * Performs actions after rendering of the settings form.
381 *
382 * @since 2.0.0
383 */
384 do_action( 'convertkit_settings_base_render_after' );
385
386 }
387
388 /**
389 * Prints help info for this section
390 */
391 public function print_section_info() {
392
393 ?>
394 <p><?php esc_html_e( 'Tools to help you manage Kit on your site.', 'convertkit' ); ?></p>
395 <?php
396
397 }
398
399 /**
400 * Returns the URL for the ConvertKit documentation for this setting section.
401 *
402 * @since 2.0.8
403 *
404 * @return string Documentation URL.
405 */
406 public function documentation_url() {
407
408 return 'https://help.kit.com/en/articles/2502591-how-to-set-up-the-kit-plugin-on-your-wordpress-website';
409
410 }
411
412 /**
413 * Returns a string comprising of the WordPress system information, with Plugin information
414 * prepended.
415 *
416 * @since 1.9.8.3
417 */
418 private function get_system_info() {
419
420 // If we're using WordPress < 5.2, there's no WP_Debug_Data class to fetch system information from.
421 if ( version_compare( get_bloginfo( 'version' ), '5.2', '<' ) ) {
422 return __( 'WordPress 5.2 or higher is required for system information report.', 'convertkit' );
423 }
424
425 // Use WordPress' debug_data() function to get system info, matching how Tools > Site Health > Info works.
426 if ( ! class_exists( 'WP_Debug_Data' ) ) {
427 require_once ABSPATH . 'wp-admin/includes/class-wp-debug-data.php';
428 }
429
430 return str_replace( '`', '', WP_Debug_Data::format( WP_Debug_Data::debug_data(), 'debug' ) );
431
432 }
433
434 }
435