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