| @@ -45,11 +45,59 @@ | ||
| 45 | 45 | 'description' |
| 46 | 46 | ), |
| 47 | 47 | 'quickwebp_settings_cleanup' => '1', |
| 48 | 48 | 'quickwebp_settings_paste_image' => '0', |
| 49 | + 'quickwebp_settings_debug_mode' => '0', | |
| 49 | 50 | ); |
| 50 | 51 | |
| 51 | 52 | return isset($settings_arr[$id]) ? $settings_arr[$id] : ''; |
| 53 | +} | |
| 54 | + | |
| 55 | +/** | |
| 56 | + * Get the QuickWebP debug log path. | |
| 57 | + * | |
| 58 | + * @return string | |
| 59 | + */ | |
| 60 | +function quickwebp_get_debug_log_path() { | |
| 61 | + $upload_dir = wp_upload_dir(); | |
| 62 | + $base_dir = trailingslashit( $upload_dir['basedir'] ) . 'quickwebp'; | |
| 63 | + | |
| 64 | + if ( ! is_dir( $base_dir ) ) { | |
| 65 | + wp_mkdir_p( $base_dir ); | |
| 66 | + } | |
| 67 | + | |
| 68 | + return trailingslashit( $base_dir ) . 'quickwebp-debug.log'; | |
| 69 | +} | |
| 70 | + | |
| 71 | +/** | |
| 72 | + * Check whether QuickWebP debug mode is enabled. | |
| 73 | + * | |
| 74 | + * @return bool | |
| 75 | + */ | |
| 76 | +function quickwebp_is_debug_mode_enabled() { | |
| 77 | + return '1' === (string) get_option( 'quickwebp_settings_debug_mode', quickwebp_settings_default( 'quickwebp_settings_debug_mode' ) ); | |
| 78 | +} | |
| 79 | + | |
| 80 | +/** | |
| 81 | + * Write a debug message to the QuickWebP custom log file. | |
| 82 | + * | |
| 83 | + * @param string $message Debug message. | |
| 84 | + * @param array $context Optional contextual data. | |
| 85 | + * | |
| 86 | + * @return void | |
| 87 | + */ | |
| 88 | +function quickwebp_debug_log( $message, $context = array() ) { | |
| 89 | + if ( ! quickwebp_is_debug_mode_enabled() ) { | |
| 90 | + return; | |
| 91 | + } | |
| 92 | + | |
| 93 | + $formatted_message = '[' . gmdate( 'Y-m-d H:i:s' ) . '] ' . sanitize_text_field( $message ); | |
| 94 | + | |
| 95 | + if ( ! empty( $context ) ) { | |
| 96 | + $formatted_message .= ' ' . wp_json_encode( $context ); | |
| 97 | + } | |
| 98 | + | |
| 99 | + error_log( $formatted_message . PHP_EOL, 3, quickwebp_get_debug_log_path() ); | |
| 52 | 100 | } |
| 53 | 101 | |
| 54 | 102 | /** |
| 55 | 103 | * Allowed tags for SVG files |