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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 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