FileV2
1 year ago
LogHandlerFileV2.php
1 year ago
PageController.php
1 year ago
Settings.php
1 year ago
Settings.php
534 lines
| 1 | <?php |
| 2 | declare( strict_types=1 ); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\Internal\Admin\Logging; |
| 5 | |
| 6 | use Automattic\Jetpack\Constants; |
| 7 | use Automattic\WooCommerce\Internal\Admin\Logging\FileV2\File; |
| 8 | use Automattic\WooCommerce\Internal\Admin\Logging\LogHandlerFileV2; |
| 9 | use Automattic\WooCommerce\Internal\Admin\Logging\FileV2\FileController; |
| 10 | use Automattic\WooCommerce\Internal\Utilities\FilesystemUtil; |
| 11 | use Automattic\WooCommerce\Proxies\LegacyProxy; |
| 12 | use Exception; |
| 13 | use WC_Admin_Settings; |
| 14 | use WC_Log_Handler_DB, WC_Log_Handler_File, WC_Log_Levels; |
| 15 | use WP_Filesystem_Direct; |
| 16 | |
| 17 | /** |
| 18 | * Settings class. |
| 19 | */ |
| 20 | class Settings { |
| 21 | |
| 22 | /** |
| 23 | * Default values for logging settings. |
| 24 | * |
| 25 | * @const array |
| 26 | */ |
| 27 | private const DEFAULTS = array( |
| 28 | 'logging_enabled' => true, |
| 29 | 'default_handler' => LogHandlerFileV2::class, |
| 30 | 'retention_period_days' => 30, |
| 31 | 'level_threshold' => 'none', |
| 32 | ); |
| 33 | |
| 34 | /** |
| 35 | * The prefix for settings keys used in the options table. |
| 36 | * |
| 37 | * @const string |
| 38 | */ |
| 39 | private const PREFIX = 'woocommerce_logs_'; |
| 40 | |
| 41 | /** |
| 42 | * Class Settings. |
| 43 | */ |
| 44 | public function __construct() { |
| 45 | add_action( 'wc_logs_load_tab', array( $this, 'save_settings' ) ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Get the directory for storing log files. |
| 50 | * |
| 51 | * The `wp_upload_dir` function takes into account the possibility of multisite, and handles changing |
| 52 | * the directory if the context is switched to a different site in the network mid-request. |
| 53 | * |
| 54 | * @param bool $create_dir Optional. True to attempt to create the log directory if it doesn't exist. Default true. |
| 55 | * |
| 56 | * @return string The full directory path, with trailing slash. |
| 57 | */ |
| 58 | public static function get_log_directory( bool $create_dir = true ): string { |
| 59 | if ( true === Constants::get_constant( 'WC_LOG_DIR_CUSTOM' ) ) { |
| 60 | $dir = Constants::get_constant( 'WC_LOG_DIR' ); |
| 61 | } else { |
| 62 | $upload_dir = wc_get_container()->get( LegacyProxy::class )->call_function( 'wp_upload_dir', null, $create_dir ); |
| 63 | |
| 64 | /** |
| 65 | * Filter to change the directory for storing WooCommerce's log files. |
| 66 | * |
| 67 | * @param string $dir The full directory path, with trailing slash. |
| 68 | * |
| 69 | * @since 8.8.0 |
| 70 | */ |
| 71 | $dir = apply_filters( 'woocommerce_log_directory', $upload_dir['basedir'] . '/wc-logs/' ); |
| 72 | } |
| 73 | |
| 74 | $dir = trailingslashit( $dir ); |
| 75 | |
| 76 | if ( true === $create_dir ) { |
| 77 | $realpath = realpath( $dir ); |
| 78 | if ( false === $realpath ) { |
| 79 | $result = wp_mkdir_p( $dir ); |
| 80 | |
| 81 | if ( true === $result ) { |
| 82 | // Create infrastructure to prevent listing contents of the logs directory. |
| 83 | try { |
| 84 | $filesystem = FilesystemUtil::get_wp_filesystem(); |
| 85 | $filesystem->put_contents( $dir . '.htaccess', 'deny from all' ); |
| 86 | $filesystem->put_contents( $dir . 'index.html', '' ); |
| 87 | } catch ( Exception $exception ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch |
| 88 | // Creation failed. |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return $dir; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * The definitions used by WC_Admin_Settings to render and save settings controls. |
| 99 | * |
| 100 | * @return array |
| 101 | */ |
| 102 | private function get_settings_definitions(): array { |
| 103 | $settings = array( |
| 104 | 'start' => array( |
| 105 | 'title' => __( 'Logs settings', 'woocommerce' ), |
| 106 | 'id' => self::PREFIX . 'settings', |
| 107 | 'type' => 'title', |
| 108 | ), |
| 109 | 'logging_enabled' => array( |
| 110 | 'title' => __( 'Logger', 'woocommerce' ), |
| 111 | 'desc' => __( 'Enable logging', 'woocommerce' ), |
| 112 | 'id' => self::PREFIX . 'logging_enabled', |
| 113 | 'type' => 'checkbox', |
| 114 | 'value' => $this->logging_is_enabled() ? 'yes' : 'no', |
| 115 | 'default' => self::DEFAULTS['logging_enabled'] ? 'yes' : 'no', |
| 116 | 'autoload' => false, |
| 117 | ), |
| 118 | 'default_handler' => array(), |
| 119 | 'retention_period_days' => array(), |
| 120 | 'level_threshold' => array(), |
| 121 | 'end' => array( |
| 122 | 'id' => self::PREFIX . 'settings', |
| 123 | 'type' => 'sectionend', |
| 124 | ), |
| 125 | ); |
| 126 | |
| 127 | if ( true === $this->logging_is_enabled() ) { |
| 128 | $settings['default_handler'] = $this->get_default_handler_setting_definition(); |
| 129 | $settings['retention_period_days'] = $this->get_retention_period_days_setting_definition(); |
| 130 | $settings['level_threshold'] = $this->get_level_threshold_setting_definition(); |
| 131 | |
| 132 | $default_handler = $this->get_default_handler(); |
| 133 | if ( in_array( $default_handler, array( LogHandlerFileV2::class, WC_Log_Handler_File::class ), true ) ) { |
| 134 | $settings += $this->get_filesystem_settings_definitions(); |
| 135 | } elseif ( WC_Log_Handler_DB::class === $default_handler ) { |
| 136 | $settings += $this->get_database_settings_definitions(); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return $settings; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * The definition for the default_handler setting. |
| 145 | * |
| 146 | * @return array |
| 147 | */ |
| 148 | private function get_default_handler_setting_definition(): array { |
| 149 | $handler_options = array( |
| 150 | LogHandlerFileV2::class => __( 'File system (default)', 'woocommerce' ), |
| 151 | WC_Log_Handler_DB::class => __( 'Database (not recommended on live sites)', 'woocommerce' ), |
| 152 | ); |
| 153 | |
| 154 | /** |
| 155 | * Filter the list of logging handlers that can be set as the default handler. |
| 156 | * |
| 157 | * @param array $handler_options An associative array of class_name => description. |
| 158 | * |
| 159 | * @since 8.6.0 |
| 160 | */ |
| 161 | $handler_options = apply_filters( 'woocommerce_logger_handler_options', $handler_options ); |
| 162 | |
| 163 | $current_value = $this->get_default_handler(); |
| 164 | if ( ! array_key_exists( $current_value, $handler_options ) ) { |
| 165 | $handler_options[ $current_value ] = $current_value; |
| 166 | } |
| 167 | |
| 168 | $desc = array(); |
| 169 | |
| 170 | $desc[] = __( 'Note that if this setting is changed, any log entries that have already been recorded will remain stored in their current location, but will not migrate.', 'woocommerce' ); |
| 171 | |
| 172 | $hardcoded = ! is_null( Constants::get_constant( 'WC_LOG_HANDLER' ) ); |
| 173 | if ( $hardcoded ) { |
| 174 | $desc[] = sprintf( |
| 175 | // translators: %s is the name of a code variable. |
| 176 | __( 'This setting cannot be changed here because it is defined in the %s constant.', 'woocommerce' ), |
| 177 | '<code>WC_LOG_HANDLER</code>' |
| 178 | ); |
| 179 | } |
| 180 | |
| 181 | return array( |
| 182 | 'title' => __( 'Log storage', 'woocommerce' ), |
| 183 | 'desc_tip' => __( 'This determines where log entries are saved.', 'woocommerce' ), |
| 184 | 'id' => self::PREFIX . 'default_handler', |
| 185 | 'type' => 'radio', |
| 186 | 'value' => $current_value, |
| 187 | 'default' => self::DEFAULTS['default_handler'], |
| 188 | 'autoload' => false, |
| 189 | 'options' => $handler_options, |
| 190 | 'disabled' => $hardcoded ? array_keys( $handler_options ) : array(), |
| 191 | 'desc' => implode( '<br><br>', $desc ), |
| 192 | 'desc_at_end' => true, |
| 193 | ); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * The definition for the retention_period_days setting. |
| 198 | * |
| 199 | * @return array |
| 200 | */ |
| 201 | private function get_retention_period_days_setting_definition(): array { |
| 202 | $custom_attributes = array( |
| 203 | 'min' => 1, |
| 204 | 'step' => 1, |
| 205 | ); |
| 206 | |
| 207 | $desc = array(); |
| 208 | |
| 209 | $hardcoded = has_filter( 'woocommerce_logger_days_to_retain_logs' ); |
| 210 | if ( $hardcoded ) { |
| 211 | $custom_attributes['disabled'] = 'true'; |
| 212 | |
| 213 | $desc[] = sprintf( |
| 214 | // translators: %s is the name of a filter hook. |
| 215 | __( 'This setting cannot be changed here because it is being set by a filter on the %s hook.', 'woocommerce' ), |
| 216 | '<code>woocommerce_logger_days_to_retain_logs</code>' |
| 217 | ); |
| 218 | } |
| 219 | |
| 220 | $file_delete_has_filter = LogHandlerFileV2::class === $this->get_default_handler() && has_filter( 'woocommerce_logger_delete_expired_file' ); |
| 221 | if ( $file_delete_has_filter ) { |
| 222 | $desc[] = sprintf( |
| 223 | // translators: %s is the name of a filter hook. |
| 224 | __( 'The %s hook has a filter set, so some log files may have different retention settings.', 'woocommerce' ), |
| 225 | '<code>woocommerce_logger_delete_expired_file</code>' |
| 226 | ); |
| 227 | } |
| 228 | |
| 229 | return array( |
| 230 | 'title' => __( 'Retention period', 'woocommerce' ), |
| 231 | 'desc_tip' => __( 'This sets how many days log entries will be kept before being auto-deleted.', 'woocommerce' ), |
| 232 | 'id' => self::PREFIX . 'retention_period_days', |
| 233 | 'type' => 'number', |
| 234 | 'value' => $this->get_retention_period(), |
| 235 | 'default' => self::DEFAULTS['retention_period_days'], |
| 236 | 'autoload' => false, |
| 237 | 'custom_attributes' => $custom_attributes, |
| 238 | 'css' => 'width:70px;', |
| 239 | 'row_class' => 'logs-retention-period-days', |
| 240 | 'suffix' => sprintf( |
| 241 | ' %s', |
| 242 | __( 'days', 'woocommerce' ), |
| 243 | ), |
| 244 | 'desc' => implode( '<br><br>', $desc ), |
| 245 | ); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * The definition for the level_threshold setting. |
| 250 | * |
| 251 | * @return array |
| 252 | */ |
| 253 | private function get_level_threshold_setting_definition(): array { |
| 254 | $hardcoded = ! is_null( Constants::get_constant( 'WC_LOG_THRESHOLD' ) ); |
| 255 | $desc = ''; |
| 256 | if ( $hardcoded ) { |
| 257 | $desc = sprintf( |
| 258 | // translators: %1$s is the name of a code variable. %2$s is the name of a file. |
| 259 | __( 'This setting cannot be changed here because it is defined in the %1$s constant, probably in your %2$s file.', 'woocommerce' ), |
| 260 | '<code>WC_LOG_THRESHOLD</code>', |
| 261 | '<b>wp-config.php</b>' |
| 262 | ); |
| 263 | } |
| 264 | |
| 265 | $labels = WC_Log_Levels::get_all_level_labels(); |
| 266 | $labels['none'] = __( 'None', 'woocommerce' ); |
| 267 | |
| 268 | $custom_attributes = array(); |
| 269 | if ( $hardcoded ) { |
| 270 | $custom_attributes['disabled'] = 'true'; |
| 271 | } |
| 272 | |
| 273 | return array( |
| 274 | 'title' => __( 'Level threshold', 'woocommerce' ), |
| 275 | 'desc_tip' => __( 'This sets the minimum severity level of logs that will be stored. Lower severity levels will be ignored. "None" means all logs will be stored.', 'woocommerce' ), |
| 276 | 'id' => self::PREFIX . 'level_threshold', |
| 277 | 'type' => 'select', |
| 278 | 'value' => $this->get_level_threshold(), |
| 279 | 'default' => self::DEFAULTS['level_threshold'], |
| 280 | 'autoload' => false, |
| 281 | 'options' => $labels, |
| 282 | 'custom_attributes' => $custom_attributes, |
| 283 | 'css' => 'width:auto;', |
| 284 | 'desc' => $desc, |
| 285 | ); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * The definitions used by WC_Admin_Settings to render settings related to filesystem log handlers. |
| 290 | * |
| 291 | * @return array |
| 292 | */ |
| 293 | private function get_filesystem_settings_definitions(): array { |
| 294 | $location_info = array(); |
| 295 | $directory = self::get_log_directory(); |
| 296 | |
| 297 | $status_info = array(); |
| 298 | try { |
| 299 | $filesystem = FilesystemUtil::get_wp_filesystem(); |
| 300 | if ( $filesystem instanceof WP_Filesystem_Direct ) { |
| 301 | $status_info[] = __( '� |
| 302 | Ready', 'woocommerce' ); |
| 303 | } else { |
| 304 | $status_info[] = __( '⚠️ The file system is not configured for direct writes. This could cause problems for the logger.', 'woocommerce' ); |
| 305 | $status_info[] = __( 'You may want to switch to the database for log storage.', 'woocommerce' ); |
| 306 | } |
| 307 | } catch ( Exception $exception ) { |
| 308 | $status_info[] = __( '⚠️ The file system connection could not be initialized.', 'woocommerce' ); |
| 309 | $status_info[] = __( 'You may want to switch to the database for log storage.', 'woocommerce' ); |
| 310 | } |
| 311 | |
| 312 | $location_info[] = sprintf( |
| 313 | // translators: %s is a location in the filesystem. |
| 314 | __( 'Log files are stored in this directory: %s', 'woocommerce' ), |
| 315 | sprintf( |
| 316 | '<code>%s</code>', |
| 317 | esc_html( $directory ) |
| 318 | ) |
| 319 | ); |
| 320 | |
| 321 | if ( ! wp_is_writable( $directory ) ) { |
| 322 | $location_info[] = __( '⚠️ This directory does not appear to be writable.', 'woocommerce' ); |
| 323 | } |
| 324 | |
| 325 | $location_info[] = sprintf( |
| 326 | // translators: %s is an amount of computer disk space, e.g. 5 KB. |
| 327 | __( 'Directory size: %s', 'woocommerce' ), |
| 328 | size_format( wc_get_container()->get( FileController::class )->get_log_directory_size() ) |
| 329 | ); |
| 330 | |
| 331 | return array( |
| 332 | 'file_start' => array( |
| 333 | 'title' => __( 'File system settings', 'woocommerce' ), |
| 334 | 'id' => self::PREFIX . 'settings', |
| 335 | 'type' => 'title', |
| 336 | ), |
| 337 | 'file_status' => array( |
| 338 | 'title' => __( 'Status', 'woocommerce' ), |
| 339 | 'type' => 'info', |
| 340 | 'text' => implode( "\n\n", $status_info ), |
| 341 | ), |
| 342 | 'log_directory' => array( |
| 343 | 'title' => __( 'Location', 'woocommerce' ), |
| 344 | 'type' => 'info', |
| 345 | 'text' => implode( "\n\n", $location_info ), |
| 346 | ), |
| 347 | 'entry_format' => array(), |
| 348 | 'file_end' => array( |
| 349 | 'id' => self::PREFIX . 'settings', |
| 350 | 'type' => 'sectionend', |
| 351 | ), |
| 352 | ); |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * The definitions used by WC_Admin_Settings to render settings related to database log handlers. |
| 357 | * |
| 358 | * @return array |
| 359 | */ |
| 360 | private function get_database_settings_definitions(): array { |
| 361 | global $wpdb; |
| 362 | $table = "{$wpdb->prefix}woocommerce_log"; |
| 363 | |
| 364 | $location_info = sprintf( |
| 365 | // translators: %s is the name of a table in the database. |
| 366 | __( 'Log entries are stored in this database table: %s', 'woocommerce' ), |
| 367 | "<code>$table</code>" |
| 368 | ); |
| 369 | |
| 370 | return array( |
| 371 | 'file_start' => array( |
| 372 | 'title' => __( 'Database settings', 'woocommerce' ), |
| 373 | 'id' => self::PREFIX . 'settings', |
| 374 | 'type' => 'title', |
| 375 | ), |
| 376 | 'database_table' => array( |
| 377 | 'title' => __( 'Location', 'woocommerce' ), |
| 378 | 'type' => 'info', |
| 379 | 'text' => $location_info, |
| 380 | ), |
| 381 | 'file_end' => array( |
| 382 | 'id' => self::PREFIX . 'settings', |
| 383 | 'type' => 'sectionend', |
| 384 | ), |
| 385 | ); |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * Handle the submission of the settings form and update the settings values. |
| 390 | * |
| 391 | * @param string $view The current view within the Logs tab. |
| 392 | * |
| 393 | * @return void |
| 394 | * |
| 395 | * @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed. |
| 396 | */ |
| 397 | public function save_settings( string $view ): void { |
| 398 | $is_saving = 'settings' === $view && isset( $_POST['save_settings'] ); |
| 399 | |
| 400 | if ( $is_saving ) { |
| 401 | check_admin_referer( self::PREFIX . 'settings' ); |
| 402 | |
| 403 | if ( ! current_user_can( 'manage_woocommerce' ) ) { |
| 404 | wp_die( esc_html__( 'You do not have permission to manage logging settings.', 'woocommerce' ) ); |
| 405 | } |
| 406 | |
| 407 | $settings = $this->get_settings_definitions(); |
| 408 | |
| 409 | WC_Admin_Settings::save_fields( $settings ); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Render the settings page. |
| 415 | * |
| 416 | * @return void |
| 417 | */ |
| 418 | public function render_form(): void { |
| 419 | $settings = $this->get_settings_definitions(); |
| 420 | |
| 421 | ?> |
| 422 | <form id="mainform" class="wc-logs-settings" method="post"> |
| 423 | <?php WC_Admin_Settings::output_fields( $settings ); ?> |
| 424 | <?php |
| 425 | /** |
| 426 | * Action fires after the built-in logging settings controls have been rendered. |
| 427 | * |
| 428 | * This is intended as a way to allow other logging settings controls to be added by extensions. |
| 429 | * |
| 430 | * @param bool $enabled True if logging is currently enabled. |
| 431 | * |
| 432 | * @since 8.6.0 |
| 433 | */ |
| 434 | do_action( 'wc_logs_settings_form_fields', $this->logging_is_enabled() ); |
| 435 | ?> |
| 436 | <?php wp_nonce_field( self::PREFIX . 'settings' ); ?> |
| 437 | <?php submit_button( __( 'Save changes', 'woocommerce' ), 'primary', 'save_settings' ); ?> |
| 438 | </form> |
| 439 | <?php |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * Determine the current value of the logging_enabled setting. |
| 444 | * |
| 445 | * @return bool |
| 446 | */ |
| 447 | public function logging_is_enabled(): bool { |
| 448 | $key = self::PREFIX . 'logging_enabled'; |
| 449 | |
| 450 | $enabled = WC_Admin_Settings::get_option( $key, self::DEFAULTS['logging_enabled'] ); |
| 451 | $enabled = filter_var( $enabled, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE ); |
| 452 | |
| 453 | if ( is_null( $enabled ) ) { |
| 454 | $enabled = self::DEFAULTS['logging_enabled']; |
| 455 | } |
| 456 | |
| 457 | return $enabled; |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * Determine the current value of the default_handler setting. |
| 462 | * |
| 463 | * @return string |
| 464 | */ |
| 465 | public function get_default_handler(): string { |
| 466 | $key = self::PREFIX . 'default_handler'; |
| 467 | |
| 468 | $handler = Constants::get_constant( 'WC_LOG_HANDLER' ); |
| 469 | |
| 470 | if ( is_null( $handler ) ) { |
| 471 | $handler = WC_Admin_Settings::get_option( $key ); |
| 472 | } |
| 473 | |
| 474 | if ( ! class_exists( $handler ) || ! is_a( $handler, 'WC_Log_Handler_Interface', true ) ) { |
| 475 | $handler = self::DEFAULTS['default_handler']; |
| 476 | } |
| 477 | |
| 478 | return $handler; |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * Determine the current value of the retention_period_days setting. |
| 483 | * |
| 484 | * @return int |
| 485 | */ |
| 486 | public function get_retention_period(): int { |
| 487 | $key = self::PREFIX . 'retention_period_days'; |
| 488 | |
| 489 | $retention_period = self::DEFAULTS['retention_period_days']; |
| 490 | |
| 491 | if ( has_filter( 'woocommerce_logger_days_to_retain_logs' ) ) { |
| 492 | /** |
| 493 | * Filter the retention period of log entries. |
| 494 | * |
| 495 | * @param int $days The number of days to retain log entries. |
| 496 | * |
| 497 | * @since 3.4.0 |
| 498 | */ |
| 499 | $retention_period = apply_filters( 'woocommerce_logger_days_to_retain_logs', $retention_period ); |
| 500 | } else { |
| 501 | $retention_period = WC_Admin_Settings::get_option( $key ); |
| 502 | } |
| 503 | |
| 504 | $retention_period = absint( $retention_period ); |
| 505 | |
| 506 | if ( $retention_period < 1 ) { |
| 507 | $retention_period = self::DEFAULTS['retention_period_days']; |
| 508 | } |
| 509 | |
| 510 | return $retention_period; |
| 511 | } |
| 512 | |
| 513 | /** |
| 514 | * Determine the current value of the level_threshold setting. |
| 515 | * |
| 516 | * @return string |
| 517 | */ |
| 518 | public function get_level_threshold(): string { |
| 519 | $key = self::PREFIX . 'level_threshold'; |
| 520 | |
| 521 | $threshold = Constants::get_constant( 'WC_LOG_THRESHOLD' ); |
| 522 | |
| 523 | if ( is_null( $threshold ) ) { |
| 524 | $threshold = WC_Admin_Settings::get_option( $key ); |
| 525 | } |
| 526 | |
| 527 | if ( ! WC_Log_Levels::is_valid_level( $threshold ) ) { |
| 528 | $threshold = self::DEFAULTS['level_threshold']; |
| 529 | } |
| 530 | |
| 531 | return $threshold; |
| 532 | } |
| 533 | } |
| 534 |