PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.0.8
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.0.8
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-settings-tools.php

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

393 lines 11.0 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 > ConvertKit > Tools.
11 *
12 * @package ConvertKit
13 * @author ConvertKit
14 */
15 class ConvertKit_Settings_Tools extends ConvertKit_Settings_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 // Output notices.
32 add_action( 'convertkit_settings_base_render_before', array( $this, 'maybe_output_notices' ) );
33
34 parent::__construct();
35
36 $this->maybe_perform_actions();
37 }
38
39 /**
40 * Possibly perform some actions, such as clearing the log, downloading the log,
41 * downloading system information or any third party actions now.
42 *
43 * @since 1.9.7.4
44 */
45 private function maybe_perform_actions() {
46
47 // Bail if nonce is invalid.
48 if ( ! $this->verify_nonce() ) {
49 return;
50 }
51
52 $this->maybe_clear_log();
53 $this->maybe_download_log();
54 $this->maybe_download_system_info();
55 $this->maybe_export_configuration();
56 $this->maybe_import_configuration();
57
58 }
59
60 /**
61 * Clears the Log.
62 *
63 * @since 1.9.6
64 */
65 private function maybe_clear_log() {
66
67 // Bail if the submit button for clearing the debug log was not clicked.
68 // Nonce verification already performed in maybe_perform_actions() which calls this function.
69 if ( ! array_key_exists( 'convertkit-clear-debug-log', $_REQUEST ) ) { // phpcs:ignore WordPress.Security.NonceVerification
70 return;
71 }
72
73 // Clear Log.
74 $log = new ConvertKit_Log( CONVERTKIT_PLUGIN_PATH );
75 $log->clear();
76
77 // Redirect to Tools screen.
78 $this->redirect_to_tools_screen();
79
80 }
81
82 /**
83 * Prompts a browser download for the log file, if the user clicked
84 * the Download Log button.
85 *
86 * @since 1.9.6
87 */
88 private function maybe_download_log() {
89
90 global $wp_filesystem;
91
92 // Bail if the submit button for downloading the debug log was not clicked.
93 // Nonce verification already performed in maybe_perform_actions() which calls this function.
94 if ( ! array_key_exists( 'convertkit-download-debug-log', $_REQUEST ) ) { // phpcs:ignore WordPress.Security.NonceVerification
95 return;
96 }
97
98 // Get Log and download.
99 $log = new ConvertKit_Log( CONVERTKIT_PLUGIN_PATH );
100
101 // Download.
102 header( 'Content-type: application/octet-stream' );
103 header( 'Content-Disposition: attachment; filename=convertkit-log.txt' );
104 header( 'Pragma: no-cache' );
105 header( 'Expires: 0' );
106 echo esc_html( $wp_filesystem->get_contents( $log->get_filename() ) );
107 exit();
108
109 }
110
111 /**
112 * Prompts a browser download for the system information, if the user clicked
113 * the Download System Info button.
114 *
115 * @since 1.9.6
116 */
117 private function maybe_download_system_info() {
118
119 global $wp_filesystem;
120
121 // Bail if the submit button for downloading the system info was not clicked.
122 // Nonce verification already performed in maybe_perform_actions() which calls this function.
123 if ( ! array_key_exists( 'convertkit-download-system-info', $_REQUEST ) ) { // phpcs:ignore WordPress.Security.NonceVerification
124 return;
125 }
126
127 // Get System Info.
128 $system_info = $this->get_system_info();
129
130 // Write contents to temporary file.
131 $tmpfile = tmpfile();
132 $filename = stream_get_meta_data( $tmpfile )['uri'];
133 $wp_filesystem->put_contents(
134 $filename,
135 esc_attr( $system_info )
136 );
137
138 // Download.
139 header( 'Content-type: application/octet-stream' );
140 header( 'Content-Disposition: attachment; filename=convertkit-system-info.txt' );
141 header( 'Pragma: no-cache' );
142 header( 'Expires: 0' );
143 echo esc_html( $wp_filesystem->get_contents( $filename ) );
144 $wp_filesystem->delete( $filename );
145 exit();
146
147 }
148
149 /**
150 * Prompts a browser download for the configuration file, if the user clicked
151 * the Export button.
152 *
153 * @since 1.9.7.4
154 */
155 private function maybe_export_configuration() {
156
157 // Bail if the submit button for exporting the configuration was not clicked.
158 // Nonce verification already performed in maybe_perform_actions() which calls this function.
159 if ( ! array_key_exists( 'convertkit-export', $_REQUEST ) ) { // phpcs:ignore WordPress.Security.NonceVerification
160 return;
161 }
162
163 // Define configuration data to include in the export file.
164 $settings = new ConvertKit_Settings();
165 $json = wp_json_encode(
166 array(
167 'settings' => $settings->get(),
168 )
169 );
170
171 // Download.
172 header( 'Content-type: application/x-msdownload' );
173 header( 'Content-Disposition: attachment; filename=convertkit-export.json' );
174 header( 'Pragma: no-cache' );
175 header( 'Expires: 0' );
176 echo $json; /* phpcs:ignore */
177 exit();
178
179 }
180
181 /**
182 * Imports the configuration file, if it's included in the form request
183 * and has the expected structure.
184 *
185 * @since 1.9.7.4
186 */
187 private function maybe_import_configuration() {
188
189 // Allow us to easily interact with the filesystem.
190 require_once ABSPATH . 'wp-admin/includes/file.php';
191 WP_Filesystem();
192 global $wp_filesystem;
193
194 // Bail if the submit button for importing the configuration was not clicked.
195 // Nonce verification already performed in maybe_perform_actions() which calls this function.
196 if ( ! array_key_exists( 'convertkit-import', $_REQUEST ) ) { // phpcs:ignore WordPress.Security.NonceVerification
197 return;
198 }
199
200 // Bail if no configuration file was supplied.
201 if ( ! is_array( $_FILES ) ) {
202 $this->redirect_to_tools_screen();
203 }
204 if ( $_FILES['import']['error'] !== 0 ) {
205 $this->redirect_to_tools_screen( 'import_configuration_upload_error' );
206 }
207
208 // Read file.
209 $json = $wp_filesystem->get_contents( $_FILES['import']['tmp_name'] );
210
211 // Decode.
212 $import = json_decode( $json, true );
213
214 // Bail if the data isn't JSON.
215 if ( is_null( $import ) ) {
216 $this->redirect_to_tools_screen( 'import_configuration_invalid_file_type' );
217 }
218
219 // Bail if no settings exist.
220 if ( ! array_key_exists( 'settings', $import ) ) {
221 $this->redirect_to_tools_screen( 'import_configuration_empty' );
222 }
223
224 // Import: Settings.
225 $settings = new ConvertKit_Settings();
226 update_option( $settings::SETTINGS_NAME, $import['settings'] );
227
228 // Redirect to Tools screen.
229 $this->redirect_to_tools_screen( false, 'import_configuration_success' );
230
231 }
232
233 /**
234 * Verifies if the _convertkit_settings_tools_nonce nonce was included in the request,
235 * and if so whether the nonce action is valid.
236 *
237 * @since 1.9.6
238 *
239 * @return bool
240 */
241 private function verify_nonce() {
242
243 // Bail if nonce verification fails.
244 if ( ! isset( $_REQUEST['_convertkit_settings_tools_nonce'] ) ) {
245 return false;
246 }
247
248 return wp_verify_nonce( sanitize_key( $_REQUEST['_convertkit_settings_tools_nonce'] ), 'convertkit-settings-tools' );
249
250 }
251
252 /**
253 * Redirects to the ConvertKit > Tools screen.
254 *
255 * @since 1.9.7.4
256 *
257 * @param false|string $error The error message key.
258 * @param false|string $success The success message key.
259 */
260 private function redirect_to_tools_screen( $error = false, $success = false ) {
261
262 // Build URL to redirect to, depending on whether a message is included.
263 $args = array(
264 'page' => '_wp_convertkit_settings',
265 'tab' => 'tools',
266 );
267 if ( $error !== false ) {
268 $args['error'] = $error;
269 }
270 if ( $success !== false ) {
271 $args['success'] = $success;
272 }
273
274 // Redirect to ConvertKit > Tools screen.
275 wp_safe_redirect( add_query_arg( $args, 'options-general.php' ) );
276 exit();
277
278 }
279
280 /**
281 * Register fields for this section
282 */
283 public function register_fields() {
284
285 // No fields are registered for the Debug Log.
286 // This function is deliberately blank.
287 }
288
289 /**
290 * Outputs success and/or error notices if required.
291 *
292 * @since 2.0.0
293 */
294 public function maybe_output_notices() {
295
296 // Define messages that might be displayed as a notification.
297 $messages = array(
298 'import_configuration_upload_error' => __( 'An error occured uploading the configuration file.', 'convertkit' ),
299 'import_configuration_invalid_file_type' => __( 'The uploaded configuration file isn\'t valid.', 'convertkit' ),
300 'import_configuration_empty' => __( 'The uploaded configuration file contains no settings.', 'convertkit' ),
301 'import_configuration_success' => __( 'Configuration imported successfully.', 'convertkit' ),
302 );
303
304 // Output error notification if defined.
305 if ( isset( $_REQUEST['error'] ) && array_key_exists( sanitize_text_field( $_REQUEST['error'] ), $messages ) ) { // phpcs:ignore WordPress.Security.NonceVerification
306 $this->output_error( $messages[ sanitize_text_field( $_REQUEST['error'] ) ] ); // phpcs:ignore WordPress.Security.NonceVerification
307 }
308
309 // Output success notification if defined.
310 if ( isset( $_REQUEST['success'] ) && array_key_exists( sanitize_text_field( $_REQUEST['success'] ), $messages ) ) { // phpcs:ignore WordPress.Security.NonceVerification
311 $this->output_success( $messages[ sanitize_text_field( $_REQUEST['success'] ) ] ); // phpcs:ignore WordPress.Security.NonceVerification
312 }
313
314 }
315
316 /**
317 * Outputs the Debug Log and System Info view.
318 *
319 * @since 1.9.6
320 */
321 public function render() {
322
323 /**
324 * Performs actions prior to rendering the settings form.
325 *
326 * @since 2.0.0
327 */
328 do_action( 'convertkit_settings_base_render_before' );
329
330 // Get Log and System Info.
331 $log = new ConvertKit_Log( CONVERTKIT_PLUGIN_PATH );
332 $system_info = $this->get_system_info();
333
334 // Output view.
335 require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/settings/tools.php';
336
337 /**
338 * Performs actions after rendering of the settings form.
339 *
340 * @since 2.0.0
341 */
342 do_action( 'convertkit_settings_base_render_after' );
343
344 }
345
346 /**
347 * Prints help info for this section
348 */
349 public function print_section_info() {
350
351 ?>
352 <p><?php esc_html_e( 'Tools to help you manage ConvertKit on your site.', 'convertkit' ); ?></p>
353 <?php
354
355 }
356
357 /**
358 * Returns the URL for the ConvertKit documentation for this setting section.
359 *
360 * @since 2.0.8
361 *
362 * @return string Documentation URL.
363 */
364 public function documentation_url() {
365
366 return 'https://help.convertkit.com/en/articles/2502591-the-convertkit-wordpress-plugin';
367
368 }
369
370 /**
371 * Returns a string comprising of the WordPress system information, with Plugin information
372 * prepended.
373 *
374 * @since 1.9.8.3
375 */
376 private function get_system_info() {
377
378 // If we're using WordPress < 5.2, there's no WP_Debug_Data class to fetch system information from.
379 if ( version_compare( get_bloginfo( 'version' ), '5.2', '<' ) ) {
380 return __( 'WordPress 5.2 or higher is required for system information report.', 'convertkit' );
381 }
382
383 // Use WordPress' debug_data() function to get system info, matching how Tools > Site Health > Info works.
384 if ( ! class_exists( 'WP_Debug_Data' ) ) {
385 require_once ABSPATH . 'wp-admin/includes/class-wp-debug-data.php';
386 }
387
388 return str_replace( '`', '', WP_Debug_Data::format( WP_Debug_Data::debug_data(), 'debug' ) );
389
390 }
391
392 }
393