| 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_clear_log(); |
| 34 |
$this->maybe_download_log(); |
| 35 |
$this->maybe_download_system_info(); |
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Clears the Log. |
| 41 |
* |
| 42 |
* @since 1.9.6 |
| 43 |
*/ |
| 44 |
private function maybe_clear_log() { |
| 45 |
|
| 46 |
// Bail if nonce is invalid. |
| 47 |
if ( ! $this->verify_nonce() ) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
// Bail if the submit button for clearing the debug log was not clicked. |
| 52 |
if ( ! array_key_exists( 'convertkit-clear-debug-log', $_REQUEST ) ) { // phpcs:ignore |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
// Clear Log. |
| 57 |
$log = new ConvertKit_Log(); |
| 58 |
$log->clear(); |
| 59 |
|
| 60 |
// Redirect to Tools screen. |
| 61 |
wp_safe_redirect( 'options-general.php?page=_wp_convertkit_settings&tab=tools' ); |
| 62 |
exit(); |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Prompts a browser download for the log file, if the user clicked |
| 68 |
* the Download Log button. |
| 69 |
* |
| 70 |
* @since 1.9.6 |
| 71 |
*/ |
| 72 |
private function maybe_download_log() { |
| 73 |
|
| 74 |
global $wp_filesystem; |
| 75 |
|
| 76 |
// Bail if nonce is invalid. |
| 77 |
if ( ! $this->verify_nonce() ) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
// Bail if the submit button for downloading the debug log was not clicked. |
| 82 |
if ( ! array_key_exists( 'convertkit-download-debug-log', $_REQUEST ) ) { // phpcs:ignore |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
// Get Log and download. |
| 87 |
$log = new ConvertKit_Log(); |
| 88 |
|
| 89 |
// Download. |
| 90 |
header( 'Content-type: application/octet-stream' ); |
| 91 |
header( 'Content-Disposition: attachment; filename=' . $log->get_filename() . '.txt' ); |
| 92 |
header( 'Pragma: no-cache' ); |
| 93 |
header( 'Expires: 0' ); |
| 94 |
echo $wp_filesystem->get_contents( $log->get_filename() ); // phpcs:ignore |
| 95 |
exit(); |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Prompts a browser download for the system information, if the user clicked |
| 101 |
* the Download System Info button. |
| 102 |
* |
| 103 |
* @since 1.9.6 |
| 104 |
*/ |
| 105 |
private function maybe_download_system_info() { |
| 106 |
|
| 107 |
global $wp_filesystem; |
| 108 |
|
| 109 |
// Bail if nonce is invalid. |
| 110 |
if ( ! $this->verify_nonce() ) { |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
// Bail if the submit button for downloading the system info was not clicked. |
| 115 |
if ( ! array_key_exists( 'convertkit-download-system-info', $_REQUEST ) ) { // phpcs:ignore |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
// Get System Info. |
| 120 |
$system_info = new ConvertKit_System_Info(); |
| 121 |
|
| 122 |
// Write contents to temporary file. |
| 123 |
$tmpfile = tmpfile(); |
| 124 |
$filename = stream_get_meta_data( $tmpfile )['uri']; |
| 125 |
$wp_filesystem->put_contents( |
| 126 |
$filename, |
| 127 |
$system_info->get() |
| 128 |
); |
| 129 |
|
| 130 |
// Download. |
| 131 |
header( 'Content-type: application/octet-stream' ); |
| 132 |
header( 'Content-Disposition: attachment; filename=system-info.txt' ); |
| 133 |
header( 'Pragma: no-cache' ); |
| 134 |
header( 'Expires: 0' ); |
| 135 |
echo $wp_filesystem->get_contents( $filename ); // phpcs:ignore |
| 136 |
$wp_filesystem->delete( $filename ); |
| 137 |
exit(); |
| 138 |
|
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Verifies if the _convertkit_settings_tools_nonce nonce was included in the request, |
| 143 |
* and if so whether the nonce action is valid. |
| 144 |
* |
| 145 |
* @since 1.9.6 |
| 146 |
* |
| 147 |
* @return bool |
| 148 |
*/ |
| 149 |
private function verify_nonce() { |
| 150 |
|
| 151 |
// Bail if nonce verification fails. |
| 152 |
if ( ! isset( $_REQUEST['_convertkit_settings_tools_nonce'] ) ) { |
| 153 |
return false; |
| 154 |
} |
| 155 |
|
| 156 |
return wp_verify_nonce( $_REQUEST['_convertkit_settings_tools_nonce'], 'convertkit-settings-tools' ); |
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Register fields for this section |
| 162 |
*/ |
| 163 |
public function register_fields() { |
| 164 |
|
| 165 |
// No fields are registered for the Debug Log. |
| 166 |
// This function is deliberately blank. |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Outputs the Debug Log and System Info view. |
| 171 |
* |
| 172 |
* @since 1.9.6 |
| 173 |
*/ |
| 174 |
public function render() { |
| 175 |
|
| 176 |
// Get Log and System Info. |
| 177 |
$log = new ConvertKit_Log(); |
| 178 |
$system_info = new ConvertKit_System_Info(); |
| 179 |
|
| 180 |
// Output view. |
| 181 |
require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/settings/tools.php'; |
| 182 |
|
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Prints help info for this section |
| 187 |
*/ |
| 188 |
public function print_section_info() { |
| 189 |
?> |
| 190 |
<p><?php esc_html_e( 'Tools to help you manage ConvertKit on your site.', 'convertkit' ); ?></p> |
| 191 |
<?php |
| 192 |
} |
| 193 |
|
| 194 |
} |
| 195 |
|