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

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