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2.0-rc.1 3.0.9 6.2.0-rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Internal / Admin / Logging / Settings.php
woocommerce / src / Internal / Admin / Logging Last commit date
FileV2 3 days ago LogHandlerFileV2.php 3 days ago PageController.php 3 days ago Settings.php 3 days ago
Settings.php
524 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
16 /**
17 * Settings class.
18 */
19 class Settings {
20
21 /**
22 * Default values for logging settings.
23 *
24 * @const array
25 */
26 private const DEFAULTS = array(
27 'logging_enabled' => true,
28 'default_handler' => LogHandlerFileV2::class,
29 'retention_period_days' => 30,
30 'level_threshold' => 'none',
31 );
32
33 /**
34 * The prefix for settings keys used in the options table.
35 *
36 * @const string
37 */
38 private const PREFIX = 'woocommerce_logs_';
39
40 /**
41 * Class Settings.
42 */
43 public function __construct() {
44 add_action( 'wc_logs_load_tab', array( $this, 'save_settings' ) );
45 }
46
47 /**
48 * Get the directory for storing log files.
49 *
50 * The `wp_upload_dir` function takes into account the possibility of multisite, and handles changing
51 * the directory if the context is switched to a different site in the network mid-request.
52 *
53 * @param bool $create_dir Optional. True to attempt to create the log directory if it doesn't exist. Default true.
54 *
55 * @return string The full directory path, with trailing slash.
56 */
57 public static function get_log_directory( bool $create_dir = true ): string {
58 if ( true === Constants::get_constant( 'WC_LOG_DIR_CUSTOM' ) ) {
59 $dir = Constants::get_constant( 'WC_LOG_DIR' );
60 } else {
61 $upload_dir = wc_get_container()->get( LegacyProxy::class )->call_function( 'wp_upload_dir', null, $create_dir );
62
63 /**
64 * Filter to change the directory for storing WooCommerce's log files.
65 *
66 * @param string $dir The full directory path, with trailing slash.
67 *
68 * @since 8.8.0
69 */
70 $dir = apply_filters( 'woocommerce_log_directory', $upload_dir['basedir'] . '/wc-logs/' );
71 }
72
73 $dir = trailingslashit( $dir );
74
75 if ( true === $create_dir ) {
76 $realpath = realpath( $dir );
77 if ( false === $realpath ) {
78 $result = wp_mkdir_p( $dir );
79
80 if ( true === $result ) {
81 // Create infrastructure to prevent listing contents of the logs directory.
82 try {
83 $filesystem = FilesystemUtil::get_wp_filesystem_direct();
84 $filesystem->put_contents( $dir . '.htaccess', 'deny from all' );
85 $filesystem->put_contents( $dir . 'index.html', '' );
86 } catch ( Exception $exception ) {
87 // Best-effort: the directory exists and stays usable without the no-listing files, but log a trace.
88 error_log( 'WooCommerce: could not write log directory protection files: ' . $exception->getMessage() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
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 if ( wp_is_writable( $directory ) ) {
299 $status_info[] = __( '�
300 Ready', 'woocommerce' );
301 } else {
302 $status_info[] = __( '⚠️ The log directory is not writable.', 'woocommerce' );
303 $status_info[] = __( 'You may want to switch to the database for log storage.', 'woocommerce' );
304 }
305
306 $location_info[] = sprintf(
307 // translators: %s is a location in the filesystem.
308 __( 'Log files are stored in this directory: %s', 'woocommerce' ),
309 sprintf(
310 '<code>%s</code>',
311 esc_html( $directory )
312 )
313 );
314
315 $location_info[] = sprintf(
316 // translators: %s is an amount of computer disk space, e.g. 5 KB.
317 __( 'Directory size: %s', 'woocommerce' ),
318 size_format( wc_get_container()->get( FileController::class )->get_log_directory_size() )
319 );
320
321 return array(
322 'file_start' => array(
323 'title' => __( 'File system settings', 'woocommerce' ),
324 'id' => self::PREFIX . 'settings',
325 'type' => 'title',
326 ),
327 'file_status' => array(
328 'title' => __( 'Status', 'woocommerce' ),
329 'type' => 'info',
330 'text' => implode( "\n\n", $status_info ),
331 ),
332 'log_directory' => array(
333 'title' => __( 'Location', 'woocommerce' ),
334 'type' => 'info',
335 'text' => implode( "\n\n", $location_info ),
336 ),
337 'entry_format' => array(),
338 'file_end' => array(
339 'id' => self::PREFIX . 'settings',
340 'type' => 'sectionend',
341 ),
342 );
343 }
344
345 /**
346 * The definitions used by WC_Admin_Settings to render settings related to database log handlers.
347 *
348 * @return array
349 */
350 private function get_database_settings_definitions(): array {
351 global $wpdb;
352 $table = "{$wpdb->prefix}woocommerce_log";
353
354 $location_info = sprintf(
355 // translators: %s is the name of a table in the database.
356 __( 'Log entries are stored in this database table: %s', 'woocommerce' ),
357 "<code>$table</code>"
358 );
359
360 return array(
361 'file_start' => array(
362 'title' => __( 'Database settings', 'woocommerce' ),
363 'id' => self::PREFIX . 'settings',
364 'type' => 'title',
365 ),
366 'database_table' => array(
367 'title' => __( 'Location', 'woocommerce' ),
368 'type' => 'info',
369 'text' => $location_info,
370 ),
371 'file_end' => array(
372 'id' => self::PREFIX . 'settings',
373 'type' => 'sectionend',
374 ),
375 );
376 }
377
378 /**
379 * Handle the submission of the settings form and update the settings values.
380 *
381 * @param string $view The current view within the Logs tab.
382 *
383 * @return void
384 *
385 * @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed.
386 */
387 public function save_settings( string $view ): void {
388 $is_saving = 'settings' === $view && isset( $_POST['save_settings'] );
389
390 if ( $is_saving ) {
391 check_admin_referer( self::PREFIX . 'settings' );
392
393 if ( ! current_user_can( 'manage_woocommerce' ) ) {
394 wp_die( esc_html__( 'You do not have permission to manage logging settings.', 'woocommerce' ) );
395 }
396
397 $settings = $this->get_settings_definitions();
398
399 WC_Admin_Settings::save_fields( $settings );
400 }
401 }
402
403 /**
404 * Render the settings page.
405 *
406 * @return void
407 */
408 public function render_form(): void {
409 $settings = $this->get_settings_definitions();
410
411 ?>
412 <form id="mainform" class="wc-logs-settings" method="post">
413 <?php WC_Admin_Settings::output_fields( $settings ); ?>
414 <?php
415 /**
416 * Action fires after the built-in logging settings controls have been rendered.
417 *
418 * This is intended as a way to allow other logging settings controls to be added by extensions.
419 *
420 * @param bool $enabled True if logging is currently enabled.
421 *
422 * @since 8.6.0
423 */
424 do_action( 'wc_logs_settings_form_fields', $this->logging_is_enabled() );
425 ?>
426 <?php wp_nonce_field( self::PREFIX . 'settings' ); ?>
427 <?php submit_button( __( 'Save changes', 'woocommerce' ), 'primary', 'save_settings' ); ?>
428 </form>
429 <?php
430 }
431
432 /**
433 * Determine the current value of the logging_enabled setting.
434 *
435 * @return bool
436 */
437 public function logging_is_enabled(): bool {
438 $key = self::PREFIX . 'logging_enabled';
439
440 $enabled = WC_Admin_Settings::get_option( $key, self::DEFAULTS['logging_enabled'] );
441 $enabled = filter_var( $enabled, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE );
442
443 if ( is_null( $enabled ) ) {
444 $enabled = self::DEFAULTS['logging_enabled'];
445 }
446
447 return $enabled;
448 }
449
450 /**
451 * Determine the current value of the default_handler setting.
452 *
453 * @return string
454 */
455 public function get_default_handler(): string {
456 $key = self::PREFIX . 'default_handler';
457
458 $handler = Constants::get_constant( 'WC_LOG_HANDLER' );
459
460 if ( is_null( $handler ) ) {
461 $handler = WC_Admin_Settings::get_option( $key );
462 }
463
464 if ( ! class_exists( $handler ) || ! is_a( $handler, 'WC_Log_Handler_Interface', true ) ) {
465 $handler = self::DEFAULTS['default_handler'];
466 }
467
468 return $handler;
469 }
470
471 /**
472 * Determine the current value of the retention_period_days setting.
473 *
474 * @return int
475 */
476 public function get_retention_period(): int {
477 $key = self::PREFIX . 'retention_period_days';
478
479 $retention_period = self::DEFAULTS['retention_period_days'];
480
481 if ( has_filter( 'woocommerce_logger_days_to_retain_logs' ) ) {
482 /**
483 * Filter the retention period of log entries.
484 *
485 * @param int $days The number of days to retain log entries.
486 *
487 * @since 3.4.0
488 */
489 $retention_period = apply_filters( 'woocommerce_logger_days_to_retain_logs', $retention_period );
490 } else {
491 $retention_period = WC_Admin_Settings::get_option( $key );
492 }
493
494 $retention_period = absint( $retention_period );
495
496 if ( $retention_period < 1 ) {
497 $retention_period = self::DEFAULTS['retention_period_days'];
498 }
499
500 return $retention_period;
501 }
502
503 /**
504 * Determine the current value of the level_threshold setting.
505 *
506 * @return string
507 */
508 public function get_level_threshold(): string {
509 $key = self::PREFIX . 'level_threshold';
510
511 $threshold = Constants::get_constant( 'WC_LOG_THRESHOLD' );
512
513 if ( is_null( $threshold ) ) {
514 $threshold = WC_Admin_Settings::get_option( $key );
515 }
516
517 if ( ! WC_Log_Levels::is_valid_level( $threshold ) ) {
518 $threshold = self::DEFAULTS['level_threshold'];
519 }
520
521 return $threshold;
522 }
523 }
524