helper
3 years ago
importers
3 years ago
list-tables
3 years ago
marketplace-suggestions
3 years ago
meta-boxes
3 years ago
notes
3 years ago
plugin-updates
5 years ago
reports
3 years ago
settings
3 years ago
views
3 years ago
class-wc-admin-addons.php
3 years ago
class-wc-admin-api-keys-table-list.php
6 years ago
class-wc-admin-api-keys.php
6 years ago
class-wc-admin-assets.php
3 years ago
class-wc-admin-attributes.php
3 years ago
class-wc-admin-customize.php
5 years ago
class-wc-admin-dashboard-setup.php
3 years ago
class-wc-admin-dashboard.php
3 years ago
class-wc-admin-duplicate-product.php
5 years ago
class-wc-admin-exporters.php
3 years ago
class-wc-admin-help.php
4 years ago
class-wc-admin-importers.php
5 years ago
class-wc-admin-log-table-list.php
5 years ago
class-wc-admin-menus.php
3 years ago
class-wc-admin-meta-boxes.php
3 years ago
class-wc-admin-notices.php
3 years ago
class-wc-admin-permalink-settings.php
5 years ago
class-wc-admin-pointers.php
3 years ago
class-wc-admin-post-types.php
3 years ago
class-wc-admin-profile.php
4 years ago
class-wc-admin-reports.php
5 years ago
class-wc-admin-settings.php
3 years ago
class-wc-admin-setup-wizard.php
4 years ago
class-wc-admin-status.php
3 years ago
class-wc-admin-taxonomies.php
3 years ago
class-wc-admin-webhooks-table-list.php
4 years ago
class-wc-admin-webhooks.php
3 years ago
class-wc-admin.php
4 years ago
wc-admin-functions.php
3 years ago
wc-meta-box-functions.php
3 years ago
class-wc-admin-status.php
440 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Debug/Status page |
| 4 | * |
| 5 | * @package WooCommerce\Admin\System Status |
| 6 | * @version 2.2.0 |
| 7 | */ |
| 8 | |
| 9 | use Automattic\Jetpack\Constants; |
| 10 | use Automattic\WooCommerce\Utilities\ArrayUtil; |
| 11 | |
| 12 | defined( 'ABSPATH' ) || exit; |
| 13 | |
| 14 | /** |
| 15 | * WC_Admin_Status Class. |
| 16 | */ |
| 17 | class WC_Admin_Status { |
| 18 | |
| 19 | /** |
| 20 | * Handles output of the reports page in admin. |
| 21 | */ |
| 22 | public static function output() { |
| 23 | include_once __DIR__ . '/views/html-admin-page-status.php'; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Handles output of report. |
| 28 | */ |
| 29 | public static function status_report() { |
| 30 | include_once __DIR__ . '/views/html-admin-page-status-report.php'; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Handles output of tools. |
| 35 | */ |
| 36 | public static function status_tools() { |
| 37 | if ( ! class_exists( 'WC_REST_System_Status_Tools_Controller' ) ) { |
| 38 | wp_die( 'Cannot load the REST API to access WC_REST_System_Status_Tools_Controller.' ); |
| 39 | } |
| 40 | |
| 41 | $tools = self::get_tools(); |
| 42 | $tool_requires_refresh = false; |
| 43 | |
| 44 | if ( ! empty( $_GET['action'] ) && ! empty( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( wp_unslash( $_REQUEST['_wpnonce'] ), 'debug_action' ) ) { // WPCS: input var ok, sanitization ok. |
| 45 | $tools_controller = new WC_REST_System_Status_Tools_Controller(); |
| 46 | $action = wc_clean( wp_unslash( $_GET['action'] ) ); // WPCS: input var ok. |
| 47 | |
| 48 | if ( array_key_exists( $action, $tools ) ) { |
| 49 | $response = $tools_controller->execute_tool( $action ); |
| 50 | |
| 51 | $tool = $tools[ $action ]; |
| 52 | $tool_requires_refresh = ArrayUtil::get_value_or_default( $tool, 'requires_refresh', false ); |
| 53 | $tool = array( |
| 54 | 'id' => $action, |
| 55 | 'name' => $tool['name'], |
| 56 | 'action' => $tool['button'], |
| 57 | 'description' => $tool['desc'], |
| 58 | 'disabled' => ArrayUtil::get_value_or_default( $tool, 'disabled', false ), |
| 59 | ); |
| 60 | $tool = array_merge( $tool, $response ); |
| 61 | |
| 62 | /** |
| 63 | * Fires after a WooCommerce system status tool has been executed. |
| 64 | * |
| 65 | * @param array $tool Details about the tool that has been executed. |
| 66 | */ |
| 67 | do_action( 'woocommerce_system_status_tool_executed', $tool ); |
| 68 | } else { |
| 69 | $response = array( |
| 70 | 'success' => false, |
| 71 | 'message' => __( 'Tool does not exist.', 'woocommerce' ), |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | if ( $response['success'] ) { |
| 76 | echo '<div class="updated inline"><p>' . esc_html( $response['message'] ) . '</p></div>'; |
| 77 | } else { |
| 78 | echo '<div class="error inline"><p>' . esc_html( $response['message'] ) . '</p></div>'; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // Display message if settings settings have been saved. |
| 83 | if ( isset( $_REQUEST['settings-updated'] ) ) { // WPCS: input var ok. |
| 84 | echo '<div class="updated inline"><p>' . esc_html__( 'Your changes have been saved.', 'woocommerce' ) . '</p></div>'; |
| 85 | } |
| 86 | |
| 87 | if ( $tool_requires_refresh ) { |
| 88 | $tools = self::get_tools(); |
| 89 | } |
| 90 | |
| 91 | include_once __DIR__ . '/views/html-admin-page-status-tools.php'; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Get tools. |
| 96 | * |
| 97 | * @return array of tools |
| 98 | */ |
| 99 | public static function get_tools() { |
| 100 | $tools_controller = new WC_REST_System_Status_Tools_Controller(); |
| 101 | return $tools_controller->get_tools(); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Show the logs page. |
| 106 | */ |
| 107 | public static function status_logs() { |
| 108 | $log_handler = Constants::get_constant( 'WC_LOG_HANDLER' ); |
| 109 | |
| 110 | if ( 'WC_Log_Handler_DB' === $log_handler ) { |
| 111 | self::status_logs_db(); |
| 112 | } else { |
| 113 | self::status_logs_file(); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Show the log page contents for file log handler. |
| 119 | */ |
| 120 | public static function status_logs_file() { |
| 121 | $logs = self::scan_log_files(); |
| 122 | |
| 123 | if ( ! empty( $_REQUEST['log_file'] ) && isset( $logs[ sanitize_title( wp_unslash( $_REQUEST['log_file'] ) ) ] ) ) { // WPCS: input var ok, CSRF ok. |
| 124 | $viewed_log = $logs[ sanitize_title( wp_unslash( $_REQUEST['log_file'] ) ) ]; // WPCS: input var ok, CSRF ok. |
| 125 | } elseif ( ! empty( $logs ) ) { |
| 126 | $viewed_log = current( $logs ); |
| 127 | } |
| 128 | |
| 129 | $handle = ! empty( $viewed_log ) ? self::get_log_file_handle( $viewed_log ) : ''; |
| 130 | |
| 131 | if ( ! empty( $_REQUEST['handle'] ) ) { // WPCS: input var ok, CSRF ok. |
| 132 | self::remove_log(); |
| 133 | } |
| 134 | |
| 135 | include_once __DIR__ . '/views/html-admin-page-status-logs.php'; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Show the log page contents for db log handler. |
| 140 | */ |
| 141 | public static function status_logs_db() { |
| 142 | if ( ! empty( $_REQUEST['flush-logs'] ) ) { // WPCS: input var ok, CSRF ok. |
| 143 | self::flush_db_logs(); |
| 144 | } |
| 145 | |
| 146 | if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['log'] ) ) { // WPCS: input var ok, CSRF ok. |
| 147 | self::log_table_bulk_actions(); |
| 148 | } |
| 149 | |
| 150 | $log_table_list = new WC_Admin_Log_Table_List(); |
| 151 | $log_table_list->prepare_items(); |
| 152 | |
| 153 | include_once __DIR__ . '/views/html-admin-page-status-logs-db.php'; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Retrieve metadata from a file. Based on WP Core's get_file_data function. |
| 158 | * |
| 159 | * @since 2.1.1 |
| 160 | * @param string $file Path to the file. |
| 161 | * @return string |
| 162 | */ |
| 163 | public static function get_file_version( $file ) { |
| 164 | |
| 165 | // Avoid notices if file does not exist. |
| 166 | if ( ! file_exists( $file ) ) { |
| 167 | return ''; |
| 168 | } |
| 169 | |
| 170 | // We don't need to write to the file, so just open for reading. |
| 171 | $fp = fopen( $file, 'r' ); // @codingStandardsIgnoreLine. |
| 172 | |
| 173 | // Pull only the first 8kiB of the file in. |
| 174 | $file_data = fread( $fp, 8192 ); // @codingStandardsIgnoreLine. |
| 175 | |
| 176 | // PHP will close file handle, but we are good citizens. |
| 177 | fclose( $fp ); // @codingStandardsIgnoreLine. |
| 178 | |
| 179 | // Make sure we catch CR-only line endings. |
| 180 | $file_data = str_replace( "\r", "\n", $file_data ); |
| 181 | $version = ''; |
| 182 | |
| 183 | if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( '@version', '/' ) . '(.*)$/mi', $file_data, $match ) && $match[1] ) { |
| 184 | $version = _cleanup_header_comment( $match[1] ); |
| 185 | } |
| 186 | |
| 187 | return $version; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Return the log file handle. |
| 192 | * |
| 193 | * @param string $filename Filename to get the handle for. |
| 194 | * @return string |
| 195 | */ |
| 196 | public static function get_log_file_handle( $filename ) { |
| 197 | return substr( $filename, 0, strlen( $filename ) > 48 ? strlen( $filename ) - 48 : strlen( $filename ) - 4 ); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Scan the template files. |
| 202 | * |
| 203 | * @param string $template_path Path to the template directory. |
| 204 | * @return array |
| 205 | */ |
| 206 | public static function scan_template_files( $template_path ) { |
| 207 | $files = @scandir( $template_path ); // @codingStandardsIgnoreLine. |
| 208 | $result = array(); |
| 209 | |
| 210 | if ( ! empty( $files ) ) { |
| 211 | |
| 212 | foreach ( $files as $key => $value ) { |
| 213 | |
| 214 | if ( ! in_array( $value, array( '.', '..' ), true ) ) { |
| 215 | |
| 216 | if ( is_dir( $template_path . DIRECTORY_SEPARATOR . $value ) ) { |
| 217 | $sub_files = self::scan_template_files( $template_path . DIRECTORY_SEPARATOR . $value ); |
| 218 | foreach ( $sub_files as $sub_file ) { |
| 219 | $result[] = $value . DIRECTORY_SEPARATOR . $sub_file; |
| 220 | } |
| 221 | } else { |
| 222 | $result[] = $value; |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | return $result; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Scan the log files. |
| 232 | * |
| 233 | * @return array |
| 234 | */ |
| 235 | public static function scan_log_files() { |
| 236 | return WC_Log_Handler_File::get_log_files(); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Get latest version of a theme by slug. |
| 241 | * |
| 242 | * @param object $theme WP_Theme object. |
| 243 | * @return string Version number if found. |
| 244 | */ |
| 245 | public static function get_latest_theme_version( $theme ) { |
| 246 | include_once ABSPATH . 'wp-admin/includes/theme.php'; |
| 247 | |
| 248 | $api = themes_api( |
| 249 | 'theme_information', |
| 250 | array( |
| 251 | 'slug' => $theme->get_stylesheet(), |
| 252 | 'fields' => array( |
| 253 | 'sections' => false, |
| 254 | 'tags' => false, |
| 255 | ), |
| 256 | ) |
| 257 | ); |
| 258 | |
| 259 | $update_theme_version = 0; |
| 260 | |
| 261 | // Check .org for updates. |
| 262 | if ( is_object( $api ) && ! is_wp_error( $api ) && isset( $api->version ) ) { |
| 263 | $update_theme_version = $api->version; |
| 264 | } elseif ( strstr( $theme->{'Author URI'}, 'woothemes' ) ) { // Check WooThemes Theme Version. |
| 265 | $theme_dir = substr( strtolower( str_replace( ' ', '', $theme->Name ) ), 0, 45 ); // @codingStandardsIgnoreLine. |
| 266 | $theme_version_data = get_transient( $theme_dir . '_version_data' ); |
| 267 | |
| 268 | if ( false === $theme_version_data ) { |
| 269 | $theme_changelog = wp_safe_remote_get( 'http://dzv365zjfbd8v.cloudfront.net/changelogs/' . $theme_dir . '/changelog.txt' ); |
| 270 | $cl_lines = explode( "\n", wp_remote_retrieve_body( $theme_changelog ) ); |
| 271 | if ( ! empty( $cl_lines ) ) { |
| 272 | foreach ( $cl_lines as $line_num => $cl_line ) { |
| 273 | if ( preg_match( '/^[0-9]/', $cl_line ) ) { |
| 274 | $theme_date = str_replace( '.', '-', trim( substr( $cl_line, 0, strpos( $cl_line, '-' ) ) ) ); |
| 275 | $theme_version = preg_replace( '~[^0-9,.]~', '', stristr( $cl_line, 'version' ) ); |
| 276 | $theme_update = trim( str_replace( '*', '', $cl_lines[ $line_num + 1 ] ) ); |
| 277 | $theme_version_data = array( |
| 278 | 'date' => $theme_date, |
| 279 | 'version' => $theme_version, |
| 280 | 'update' => $theme_update, |
| 281 | 'changelog' => $theme_changelog, |
| 282 | ); |
| 283 | set_transient( $theme_dir . '_version_data', $theme_version_data, DAY_IN_SECONDS ); |
| 284 | break; |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | if ( ! empty( $theme_version_data['version'] ) ) { |
| 291 | $update_theme_version = $theme_version_data['version']; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | return $update_theme_version; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Remove/delete the chosen file. |
| 300 | */ |
| 301 | public static function remove_log() { |
| 302 | if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( wp_unslash( $_REQUEST['_wpnonce'] ), 'remove_log' ) ) { // WPCS: input var ok, sanitization ok. |
| 303 | wp_die( esc_html__( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) ); |
| 304 | } |
| 305 | |
| 306 | if ( ! empty( $_REQUEST['handle'] ) ) { // WPCS: input var ok. |
| 307 | $log_handler = new WC_Log_Handler_File(); |
| 308 | $log_handler->remove( wp_unslash( $_REQUEST['handle'] ) ); // WPCS: input var ok, sanitization ok. |
| 309 | } |
| 310 | |
| 311 | wp_safe_redirect( esc_url_raw( admin_url( 'admin.php?page=wc-status&tab=logs' ) ) ); |
| 312 | exit(); |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Clear DB log table. |
| 317 | * |
| 318 | * @since 3.0.0 |
| 319 | */ |
| 320 | private static function flush_db_logs() { |
| 321 | if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'woocommerce-status-logs' ) ) { // WPCS: input var ok, sanitization ok. |
| 322 | wp_die( esc_html__( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) ); |
| 323 | } |
| 324 | |
| 325 | WC_Log_Handler_DB::flush(); |
| 326 | |
| 327 | wp_safe_redirect( esc_url_raw( admin_url( 'admin.php?page=wc-status&tab=logs' ) ) ); |
| 328 | exit(); |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Bulk DB log table actions. |
| 333 | * |
| 334 | * @since 3.0.0 |
| 335 | */ |
| 336 | private static function log_table_bulk_actions() { |
| 337 | if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'woocommerce-status-logs' ) ) { // WPCS: input var ok, sanitization ok. |
| 338 | wp_die( esc_html__( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) ); |
| 339 | } |
| 340 | |
| 341 | $log_ids = array_map( 'absint', (array) isset( $_REQUEST['log'] ) ? wp_unslash( $_REQUEST['log'] ) : array() ); // WPCS: input var ok, sanitization ok. |
| 342 | |
| 343 | if ( ( isset( $_REQUEST['action'] ) && 'delete' === $_REQUEST['action'] ) || ( isset( $_REQUEST['action2'] ) && 'delete' === $_REQUEST['action2'] ) ) { // WPCS: input var ok, sanitization ok. |
| 344 | WC_Log_Handler_DB::delete( $log_ids ); |
| 345 | wp_safe_redirect( esc_url_raw( admin_url( 'admin.php?page=wc-status&tab=logs' ) ) ); |
| 346 | exit(); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Prints table info if a base table is not present. |
| 352 | */ |
| 353 | private static function output_tables_info() { |
| 354 | $missing_tables = WC_Install::verify_base_tables( false ); |
| 355 | if ( 0 === count( $missing_tables ) ) { |
| 356 | return; |
| 357 | } |
| 358 | ?> |
| 359 | |
| 360 | <br> |
| 361 | <strong style="color:#a00;"> |
| 362 | <span class="dashicons dashicons-warning"></span> |
| 363 | <?php |
| 364 | echo esc_html( |
| 365 | sprintf( |
| 366 | // translators: Comma seperated list of missing tables. |
| 367 | __( 'Missing base tables: %s. Some WooCommerce functionality may not work as expected.', 'woocommerce' ), |
| 368 | implode( ', ', $missing_tables ) |
| 369 | ) |
| 370 | ); |
| 371 | ?> |
| 372 | </strong> |
| 373 | |
| 374 | <?php |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Prints the information about plugins for the system status report. |
| 379 | * Used for both active and inactive plugins sections. |
| 380 | * |
| 381 | * @param array $plugins List of plugins to display. |
| 382 | * @param array $untested_plugins List of plugins that haven't been tested with the current WooCommerce version. |
| 383 | * @return void |
| 384 | */ |
| 385 | private static function output_plugins_info( $plugins, $untested_plugins ) { |
| 386 | $wc_version = Constants::get_constant( 'WC_VERSION' ); |
| 387 | |
| 388 | if ( 'major' === Constants::get_constant( 'WC_SSR_PLUGIN_UPDATE_RELEASE_VERSION_TYPE' ) ) { |
| 389 | // Since we're only testing against major, we don't need to show minor and patch version. |
| 390 | $wc_version = $wc_version[0] . '.0'; |
| 391 | } |
| 392 | |
| 393 | foreach ( $plugins as $plugin ) { |
| 394 | if ( ! empty( $plugin['name'] ) ) { |
| 395 | // Link the plugin name to the plugin url if available. |
| 396 | $plugin_name = esc_html( $plugin['name'] ); |
| 397 | if ( ! empty( $plugin['url'] ) ) { |
| 398 | $plugin_name = '<a href="' . esc_url( $plugin['url'] ) . '" aria-label="' . esc_attr__( 'Visit plugin homepage', 'woocommerce' ) . '" target="_blank">' . $plugin_name . '</a>'; |
| 399 | } |
| 400 | |
| 401 | $has_newer_version = false; |
| 402 | $version_string = $plugin['version']; |
| 403 | $network_string = ''; |
| 404 | if ( strstr( $plugin['url'], 'woothemes.com' ) || strstr( $plugin['url'], 'woocommerce.com' ) ) { |
| 405 | if ( ! empty( $plugin['version_latest'] ) && version_compare( $plugin['version_latest'], $plugin['version'], '>' ) ) { |
| 406 | /* translators: 1: current version. 2: latest version */ |
| 407 | $version_string = sprintf( __( '%1$s (update to version %2$s is available)', 'woocommerce' ), $plugin['version'], $plugin['version_latest'] ); |
| 408 | } |
| 409 | |
| 410 | if ( false !== $plugin['network_activated'] ) { |
| 411 | $network_string = ' – <strong style="color: black;">' . esc_html__( 'Network enabled', 'woocommerce' ) . '</strong>'; |
| 412 | } |
| 413 | } |
| 414 | $untested_string = ''; |
| 415 | if ( array_key_exists( $plugin['plugin'], $untested_plugins ) ) { |
| 416 | $untested_string = ' – <strong style="color: #a00;">'; |
| 417 | |
| 418 | /* translators: %s: version */ |
| 419 | $untested_string .= esc_html( sprintf( __( 'Installed version not tested with active version of WooCommerce %s', 'woocommerce' ), $wc_version ) ); |
| 420 | |
| 421 | $untested_string .= '</strong>'; |
| 422 | } |
| 423 | ?> |
| 424 | <tr> |
| 425 | <td><?php echo wp_kses_post( $plugin_name ); ?></td> |
| 426 | <td class="help"> </td> |
| 427 | <td> |
| 428 | <?php |
| 429 | /* translators: %s: plugin author */ |
| 430 | printf( esc_html__( 'by %s', 'woocommerce' ), esc_html( $plugin['author_name'] ) ); |
| 431 | echo ' – ' . esc_html( $version_string ) . $untested_string . $network_string; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 432 | ?> |
| 433 | </td> |
| 434 | </tr> |
| 435 | <?php |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 |