PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.6.2
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.6.2
3.4.2 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 All 195 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.6.2, at admin/section/class-convertkit-settings-tools.php

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