PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 1.9.7.4
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v1.9.7.4
3.4.4 3.4.3 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 All 197 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 1.9.7.4, at admin/section/class-convertkit-settings-tools.php

324 lines 8.5 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 parent::__construct();
32
33 $this->maybe_perform_actions();
34 }
35
36 /**
37 * Possibly perform some actions, such as clearing the log, downloading the log,
38 * downloading system information or any third party actions now.
39 *
40 * @since 1.9.7.4
41 */
42 private function maybe_perform_actions() {
43
44 // Bail if nonce is invalid.
45 if ( ! $this->verify_nonce() ) {
46 return;
47 }
48
49 $this->maybe_clear_log();
50 $this->maybe_download_log();
51 $this->maybe_download_system_info();
52 $this->maybe_export_configuration();
53 $this->maybe_import_configuration();
54
55 }
56
57 /**
58 * Clears the Log.
59 *
60 * @since 1.9.6
61 */
62 private function maybe_clear_log() {
63
64 // Bail if the submit button for clearing the debug log was not clicked.
65 if ( ! array_key_exists( 'convertkit-clear-debug-log', $_REQUEST ) ) { // phpcs:ignore
66 return;
67 }
68
69 // Clear Log.
70 $log = new ConvertKit_Log();
71 $log->clear();
72
73 // Redirect to Tools screen.
74 $this->redirect_to_tools_screen();
75
76 }
77
78 /**
79 * Prompts a browser download for the log file, if the user clicked
80 * the Download Log button.
81 *
82 * @since 1.9.6
83 */
84 private function maybe_download_log() {
85
86 global $wp_filesystem;
87
88 // Bail if the submit button for downloading the debug log was not clicked.
89 if ( ! array_key_exists( 'convertkit-download-debug-log', $_REQUEST ) ) { // phpcs:ignore
90 return;
91 }
92
93 // Get Log and download.
94 $log = new ConvertKit_Log();
95
96 // Download.
97 header( 'Content-type: application/octet-stream' );
98 header( 'Content-Disposition: attachment; filename=' . $log->get_filename() . '.txt' );
99 header( 'Pragma: no-cache' );
100 header( 'Expires: 0' );
101 echo $wp_filesystem->get_contents( $log->get_filename() ); // phpcs:ignore
102 exit();
103
104 }
105
106 /**
107 * Prompts a browser download for the system information, if the user clicked
108 * the Download System Info button.
109 *
110 * @since 1.9.6
111 */
112 private function maybe_download_system_info() {
113
114 global $wp_filesystem;
115
116 // Bail if the submit button for downloading the system info was not clicked.
117 if ( ! array_key_exists( 'convertkit-download-system-info', $_REQUEST ) ) { // phpcs:ignore
118 return;
119 }
120
121 // Get System Info.
122 $system_info = new ConvertKit_System_Info();
123
124 // Write contents to temporary file.
125 $tmpfile = tmpfile();
126 $filename = stream_get_meta_data( $tmpfile )['uri'];
127 $wp_filesystem->put_contents(
128 $filename,
129 $system_info->get()
130 );
131
132 // Download.
133 header( 'Content-type: application/octet-stream' );
134 header( 'Content-Disposition: attachment; filename=system-info.txt' );
135 header( 'Pragma: no-cache' );
136 header( 'Expires: 0' );
137 echo $wp_filesystem->get_contents( $filename ); // phpcs:ignore
138 $wp_filesystem->delete( $filename );
139 exit();
140
141 }
142
143 /**
144 * Prompts a browser download for the configuration file, if the user clicked
145 * the Export button.
146 *
147 * @since 1.9.7.4
148 */
149 private function maybe_export_configuration() {
150
151 // Bail if the submit button for exporting the configuration was not clicked.
152 if ( ! array_key_exists( 'convertkit-export', $_REQUEST ) ) { // phpcs:ignore
153 return;
154 }
155
156 // Define configuration data to include in the export file.
157 $settings = new ConvertKit_Settings();
158 $json = wp_json_encode(
159 array(
160 'settings' => $settings->get(),
161 )
162 );
163
164 // Download.
165 header( 'Content-type: application/x-msdownload' );
166 header( 'Content-Disposition: attachment; filename=convertkit-export.json' );
167 header( 'Pragma: no-cache' );
168 header( 'Expires: 0' );
169 echo $json; /* phpcs:ignore */
170 exit();
171
172 }
173
174 /**
175 * Imports the configuration file, if it's included in the form request
176 * and has the expected structure.
177 *
178 * @since 1.9.7.4
179 */
180 private function maybe_import_configuration() {
181
182 // Allow us to easily interact with the filesystem.
183 require_once ABSPATH . 'wp-admin/includes/file.php';
184 WP_Filesystem();
185 global $wp_filesystem;
186
187 // Bail if the submit button for importing the configuration was not clicked.
188 if ( ! array_key_exists( 'convertkit-import', $_REQUEST ) ) { // phpcs:ignore
189 return;
190 }
191
192 // Bail if no configuration file was supplied.
193 if ( ! is_array( $_FILES ) ) {
194 $this->redirect_to_tools_screen();
195 }
196 if ( $_FILES['import']['error'] !== 0 ) {
197 $this->redirect_to_tools_screen( 'import_configuration_upload_error' );
198 }
199
200 // Read file.
201 $json = $wp_filesystem->get_contents( $_FILES['import']['tmp_name'] );
202
203 // Decode.
204 $import = json_decode( $json, true );
205
206 // Bail if the data isn't JSON.
207 if ( is_null( $import ) ) {
208 $this->redirect_to_tools_screen( 'import_configuration_invalid_file_type' );
209 }
210
211 // Bail if no settings exist.
212 if ( ! array_key_exists( 'settings', $import ) ) {
213 $this->redirect_to_tools_screen( 'import_configuration_empty' );
214 }
215
216 // Import: Settings.
217 $settings = new ConvertKit_Settings();
218 update_option( $settings::SETTINGS_NAME, $import['settings'] );
219
220 // Redirect to Tools screen.
221 $this->redirect_to_tools_screen( false, 'import_configuration_success' );
222
223 }
224
225 /**
226 * Verifies if the _convertkit_settings_tools_nonce nonce was included in the request,
227 * and if so whether the nonce action is valid.
228 *
229 * @since 1.9.6
230 *
231 * @return bool
232 */
233 private function verify_nonce() {
234
235 // Bail if nonce verification fails.
236 if ( ! isset( $_REQUEST['_convertkit_settings_tools_nonce'] ) ) {
237 return false;
238 }
239
240 return wp_verify_nonce( $_REQUEST['_convertkit_settings_tools_nonce'], 'convertkit-settings-tools' );
241
242 }
243
244 /**
245 * Redirects to the ConvertKit > Tools screen.
246 *
247 * @since 1.9.7.4
248 *
249 * @param false|string $error The error message key.
250 * @param false|string $success The success message key.
251 */
252 private function redirect_to_tools_screen( $error = false, $success = false ) {
253
254 // Build URL to redirect to, depending on whether a message is included.
255 $args = array(
256 'page' => '_wp_convertkit_settings',
257 'tab' => 'tools',
258 );
259 if ( $error !== false ) {
260 $args['error'] = $error;
261 }
262 if ( $success !== false ) {
263 $args['success'] = $success;
264 }
265
266 // Redirect to ConvertKit > Tools screen.
267 wp_safe_redirect( add_query_arg( $args, 'options-general.php' ) );
268 exit();
269
270 }
271
272 /**
273 * Register fields for this section
274 */
275 public function register_fields() {
276
277 // No fields are registered for the Debug Log.
278 // This function is deliberately blank.
279 }
280
281 /**
282 * Outputs the Debug Log and System Info view.
283 *
284 * @since 1.9.6
285 */
286 public function render() {
287
288 // Get Log and System Info.
289 $log = new ConvertKit_Log();
290 $system_info = new ConvertKit_System_Info();
291
292 // Define messages that might be displayed as a notification.
293 $messages = array(
294 'import_configuration_upload_error' => __( 'An error occured uploading the configuration file.', 'convertkit' ),
295 'import_configuration_invalid_file_type' => __( 'The uploaded configuration file isn\'t valid.', 'convertkit' ),
296 'import_configuration_empty' => __( 'The uploaded configuration file contains no settings.', 'convertkit' ),
297 'import_configuration_success' => __( 'Configuration imported successfully.', 'convertkit' ),
298 );
299 $error = false;
300 if ( isset( $_REQUEST['error'] ) && array_key_exists( sanitize_text_field( $_REQUEST['error'] ), $messages ) ) { // phpcs:ignore
301 $error = $messages[ sanitize_text_field( $_REQUEST['error'] ) ]; // phpcs:ignore
302 }
303
304 $success = false;
305 if ( isset( $_REQUEST['success'] ) && array_key_exists( sanitize_text_field( $_REQUEST['success'] ), $messages ) ) { // phpcs:ignore
306 $success = $messages[ sanitize_text_field( $_REQUEST['success'] ) ]; // phpcs:ignore
307 }
308
309 // Output view.
310 require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/settings/tools.php';
311
312 }
313
314 /**
315 * Prints help info for this section
316 */
317 public function print_section_info() {
318 ?>
319 <p><?php esc_html_e( 'Tools to help you manage ConvertKit on your site.', 'convertkit' ); ?></p>
320 <?php
321 }
322
323 }
324