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

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

377 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();
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 // Initialize classes that hold settings.
164 $settings = new ConvertKit_Settings();
165 $restrict_content_settings = new ConvertKit_Settings_Restrict_Content();
166
167 // Define configuration data to include in the export file.
168 $json = wp_json_encode(
169 array(
170 'settings' => $settings->get(),
171 'restrict_content' => $restrict_content_settings->get(),
172 )
173 );
174
175 // Download.
176 header( 'Content-type: application/x-msdownload' );
177 header( 'Content-Disposition: attachment; filename=convertkit-export.json' );
178 header( 'Pragma: no-cache' );
179 header( 'Expires: 0' );
180 echo $json; // phpcs:ignore WordPress.Security.EscapeOutput
181 exit();
182
183 }
184
185 /**
186 * Imports the configuration file, if it's included in the form request
187 * and has the expected structure.
188 *
189 * @since 1.9.7.4
190 */
191 private function maybe_import_configuration() {
192
193 // Allow us to easily interact with the filesystem.
194 require_once ABSPATH . 'wp-admin/includes/file.php';
195 WP_Filesystem();
196 global $wp_filesystem;
197
198 // Bail if the submit button for importing the configuration was not clicked.
199 // Nonce verification already performed in maybe_perform_actions() which calls this function.
200 if ( ! array_key_exists( 'convertkit-import', $_REQUEST ) ) { // phpcs:ignore WordPress.Security.NonceVerification
201 return;
202 }
203
204 // Bail if no configuration file was supplied.
205 if ( ! is_array( $_FILES ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
206 $this->redirect();
207 }
208 if ( $_FILES['import']['error'] !== 0 ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
209 $this->redirect( 'import_configuration_upload_error' );
210 }
211
212 // Read file.
213 $json = $wp_filesystem->get_contents( $_FILES['import']['tmp_name'] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
214
215 // Decode.
216 $import = json_decode( $json, true );
217
218 // Bail if the data isn't JSON.
219 if ( is_null( $import ) ) {
220 $this->redirect( 'import_configuration_invalid_file_type' );
221 }
222
223 // Bail if no settings exist.
224 if ( ! array_key_exists( 'settings', $import ) ) {
225 $this->redirect( 'import_configuration_empty' );
226 }
227
228 // Import: Settings.
229 if ( array_key_exists( 'settings', $import ) ) {
230 $settings = new ConvertKit_Settings();
231 update_option( $settings::SETTINGS_NAME, $import['settings'] );
232 }
233
234 // Import: Restrict Content Settings.
235 if ( array_key_exists( 'restrict_content', $import ) ) {
236 $restrict_content_settings = new ConvertKit_Settings_Restrict_Content();
237 update_option( $restrict_content_settings::SETTINGS_NAME, $import['restrict_content'] );
238 }
239
240 // Redirect to Tools screen.
241 $this->redirect( false, 'import_configuration_success' );
242
243 }
244
245 /**
246 * Verifies if the _convertkit_settings_tools_nonce nonce was included in the request,
247 * and if so whether the nonce action is valid.
248 *
249 * @since 1.9.6
250 *
251 * @return bool
252 */
253 private function verify_nonce() {
254
255 // Bail if nonce verification fails.
256 if ( ! isset( $_REQUEST['_convertkit_settings_tools_nonce'] ) ) {
257 return false;
258 }
259
260 return wp_verify_nonce( sanitize_key( $_REQUEST['_convertkit_settings_tools_nonce'] ), 'convertkit-settings-tools' );
261
262 }
263
264 /**
265 * Register fields for this section
266 */
267 public function register_fields() {
268
269 // No fields are registered for the Debug Log.
270 // This function is deliberately blank.
271 }
272
273 /**
274 * Outputs success and/or error notices if required.
275 *
276 * @since 2.0.0
277 */
278 public function maybe_output_notices() {
279
280 // Define messages that might be displayed as a notification.
281 $messages = array(
282 'import_configuration_upload_error' => __( 'An error occured uploading the configuration file.', 'convertkit' ),
283 'import_configuration_invalid_file_type' => __( 'The uploaded configuration file isn\'t valid.', 'convertkit' ),
284 'import_configuration_empty' => __( 'The uploaded configuration file contains no settings.', 'convertkit' ),
285 'import_configuration_success' => __( 'Configuration imported successfully.', 'convertkit' ),
286 );
287
288 // Output error notification if defined.
289 if ( isset( $_REQUEST['error'] ) && array_key_exists( sanitize_text_field( $_REQUEST['error'] ), $messages ) ) { // phpcs:ignore WordPress.Security.NonceVerification
290 $this->output_error( $messages[ sanitize_text_field( $_REQUEST['error'] ) ] ); // phpcs:ignore WordPress.Security.NonceVerification
291 }
292
293 // Output success notification if defined.
294 if ( isset( $_REQUEST['success'] ) && array_key_exists( sanitize_text_field( $_REQUEST['success'] ), $messages ) ) { // phpcs:ignore WordPress.Security.NonceVerification
295 $this->output_success( $messages[ sanitize_text_field( $_REQUEST['success'] ) ] ); // phpcs:ignore WordPress.Security.NonceVerification
296 }
297
298 }
299
300 /**
301 * Outputs the Debug Log and System Info view.
302 *
303 * @since 1.9.6
304 */
305 public function render() {
306
307 /**
308 * Performs actions prior to rendering the settings form.
309 *
310 * @since 2.0.0
311 */
312 do_action( 'convertkit_settings_base_render_before' );
313
314 // Get Log and System Info.
315 $log = new ConvertKit_Log( CONVERTKIT_PLUGIN_PATH );
316 $system_info = $this->get_system_info();
317
318 // Output view.
319 require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/settings/tools.php';
320
321 /**
322 * Performs actions after rendering of the settings form.
323 *
324 * @since 2.0.0
325 */
326 do_action( 'convertkit_settings_base_render_after' );
327
328 }
329
330 /**
331 * Prints help info for this section
332 */
333 public function print_section_info() {
334
335 ?>
336 <p><?php esc_html_e( 'Tools to help you manage ConvertKit on your site.', 'convertkit' ); ?></p>
337 <?php
338
339 }
340
341 /**
342 * Returns the URL for the ConvertKit documentation for this setting section.
343 *
344 * @since 2.0.8
345 *
346 * @return string Documentation URL.
347 */
348 public function documentation_url() {
349
350 return 'https://help.convertkit.com/en/articles/2502591-the-convertkit-wordpress-plugin';
351
352 }
353
354 /**
355 * Returns a string comprising of the WordPress system information, with Plugin information
356 * prepended.
357 *
358 * @since 1.9.8.3
359 */
360 private function get_system_info() {
361
362 // If we're using WordPress < 5.2, there's no WP_Debug_Data class to fetch system information from.
363 if ( version_compare( get_bloginfo( 'version' ), '5.2', '<' ) ) {
364 return __( 'WordPress 5.2 or higher is required for system information report.', 'convertkit' );
365 }
366
367 // Use WordPress' debug_data() function to get system info, matching how Tools > Site Health > Info works.
368 if ( ! class_exists( 'WP_Debug_Data' ) ) {
369 require_once ABSPATH . 'wp-admin/includes/class-wp-debug-data.php';
370 }
371
372 return str_replace( '`', '', WP_Debug_Data::format( WP_Debug_Data::debug_data(), 'debug' ) );
373
374 }
375
376 }
377