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