WebPageCache
7 months ago
external
2 months ago
ExcludeJsFromDelay.php
2 years ago
OptimizerBanner.php
11 months ago
OptimizerBase.php
2 years ago
OptimizerCSSMin.php
2 years ago
OptimizerCache.php
1 year ago
OptimizerCacheStructure.php
2 years ago
OptimizerCriticalCss.php
1 year ago
OptimizerElementor.php
2 years ago
OptimizerFonts.php
2 years ago
OptimizerImages.php
1 year ago
OptimizerLogger.php
1 year ago
OptimizerMain.php
2 months ago
OptimizerOnInit.php
11 months ago
OptimizerScripts.php
2 years ago
OptimizerSettings.php
2 months ago
OptimizerStyles.php
2 years ago
OptimizerUrl.php
1 year ago
OptimizerUtils.php
2 months ago
OptimizerWhiteLabel.php
2 years ago
OptimizerLogger.php
240 lines
| 1 | <?php |
| 2 | |
| 3 | namespace TenWebOptimizer; |
| 4 | |
| 5 | class OptimizerLogger |
| 6 | { |
| 7 | protected static $instance = null; |
| 8 | |
| 9 | protected static $log_options = [ |
| 10 | 'performance_requests' => 'two_performance_requests_logs', |
| 11 | 'clear_cache' => 'two_clear_cache_logs', |
| 12 | 'critical_css' => 'two_critical_css_logs', |
| 13 | 'not_optimized_pages' => 'two_serve_not_optimized_page_logs' |
| 14 | ]; |
| 15 | |
| 16 | private function __construct() |
| 17 | { |
| 18 | add_action('http_api_debug', [$this, 'log_performance_requests_api_debug'], 10, 5); |
| 19 | |
| 20 | if (!wp_next_scheduled('two_daily_cron_hook')) { |
| 21 | wp_schedule_event(time(), 'daily', 'two_daily_cron_hook'); |
| 22 | } |
| 23 | add_action('two_daily_cron_hook', [$this, 'two_daily_cron_hook_function']); |
| 24 | } |
| 25 | |
| 26 | private static function add_performance_requests_log($response, $url, $parsed_args) |
| 27 | { |
| 28 | if (strpos($url, TENWEB_SO_CRITICAL_URL) !== 0) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | if (isset($parsed_args['blocking']) && $parsed_args['blocking'] === false) { |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | $code = wp_remote_retrieve_response_code($response); |
| 37 | $msg = wp_remote_retrieve_response_message($response); |
| 38 | |
| 39 | if ($code === 200) { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | $log_data = [ |
| 44 | 'code' => $code, |
| 45 | 'msg' => $msg, |
| 46 | 'url' => $url |
| 47 | ]; |
| 48 | |
| 49 | self::add_log('performance_requests', $log_data, false); |
| 50 | } |
| 51 | |
| 52 | public static function add_clear_cache_log($is_json, $excludeCriticalRegeneration, $delete_tenweb_manager_cache, $delete_cloudflare_cache, $critical_regeneration_mode, $clear_critical) |
| 53 | { |
| 54 | $log_data = [ |
| 55 | 'is_json' => $is_json, |
| 56 | 'excludeCriticalRegeneration' => $excludeCriticalRegeneration, |
| 57 | 'delete_tenweb_manager_cache' => $delete_tenweb_manager_cache, |
| 58 | 'delete_cloudflare_cache' => $delete_cloudflare_cache, |
| 59 | 'critical_regeneration_mode' => $critical_regeneration_mode, |
| 60 | 'clear_critical' => boolval($clear_critical), |
| 61 | ]; |
| 62 | |
| 63 | self::add_log('clear_cache', $log_data); |
| 64 | } |
| 65 | |
| 66 | public static function add_critical_css_log($request_data, $newly_connected_website, $flow_id, $status_code, $res_body = '') |
| 67 | { |
| 68 | $log_data = [ |
| 69 | 'domain_id' => $request_data['tenweb_domain_id'], |
| 70 | 'newly_connected_website' => $newly_connected_website, |
| 71 | 'flow_id' => $flow_id, |
| 72 | 'page_id' => $request_data['page_id'], |
| 73 | 'status_code' => $status_code, |
| 74 | 'res_body' => $res_body |
| 75 | ]; |
| 76 | |
| 77 | if (isset($request_data['notification_id'])) { |
| 78 | $log_data['notification_id'] = $request_data['notification_id']; |
| 79 | } |
| 80 | $force_write_log = false; |
| 81 | |
| 82 | if ($newly_connected_website) { |
| 83 | $force_write_log = true; |
| 84 | } |
| 85 | self::add_log('critical_css', $log_data, true, $force_write_log); |
| 86 | } |
| 87 | |
| 88 | public static function add_not_optimized_page_log($reason) |
| 89 | { |
| 90 | $log_data = [ |
| 91 | 'request_uri' => (isset($_SERVER['REQUEST_URI'])) ? sanitize_text_field($_SERVER['REQUEST_URI']) : 'No REQUEST_URI', |
| 92 | 'reason' => $reason |
| 93 | ]; |
| 94 | |
| 95 | self::add_log('not_optimized_pages', $log_data, false); |
| 96 | } |
| 97 | |
| 98 | public static function add_log($log_type, $log_data, $include_stack_trace = true, $force_write_log = false) |
| 99 | { |
| 100 | if (!$force_write_log && !TENWEB_SO_DEBUG_MODE) { |
| 101 | return; |
| 102 | } |
| 103 | $option_name = self::get_log_option_name($log_type); |
| 104 | |
| 105 | foreach ($log_data as $key => $value) { |
| 106 | if (!is_bool($value)) { |
| 107 | $log_data[$key] = sanitize_text_field($value); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | if ($include_stack_trace) { |
| 112 | $log_data['stack_trace'] = self::get_stack_trace(); |
| 113 | } |
| 114 | $log_data['date'] = time(); |
| 115 | |
| 116 | $clear_cache_log = get_option($option_name, []); |
| 117 | $clear_cache_log = array_slice($clear_cache_log, -TENWEB_SO_LOGS_MAX_LINES_LIMIT); |
| 118 | $clear_cache_log[] = $log_data; |
| 119 | update_option($option_name, $clear_cache_log, false); |
| 120 | } |
| 121 | |
| 122 | public static function get_logs($log_type) |
| 123 | { |
| 124 | $logs = get_option(self::get_log_option_name($log_type), []); |
| 125 | self::sort_logs($logs); |
| 126 | |
| 127 | return $logs; |
| 128 | } |
| 129 | |
| 130 | public static function delete_logs($log_type) |
| 131 | { |
| 132 | $option_name = self::get_log_option_name($log_type); |
| 133 | |
| 134 | if ($option_name) { |
| 135 | delete_option($option_name); |
| 136 | |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | public static function get_all_logs() |
| 144 | { |
| 145 | $all_logs = []; |
| 146 | |
| 147 | foreach (self::$log_options as $key => $val) { |
| 148 | $logs = self::get_logs($key); |
| 149 | |
| 150 | foreach ($logs as $index => $log) { |
| 151 | $logs[$index]['log_type'] = $key; |
| 152 | } |
| 153 | $all_logs = array_merge($all_logs, $logs); |
| 154 | } |
| 155 | |
| 156 | self::sort_logs($all_logs); |
| 157 | |
| 158 | return $all_logs; |
| 159 | } |
| 160 | |
| 161 | public static function get_log_option_name($log_type) |
| 162 | { |
| 163 | return (isset(self::$log_options[$log_type])) ? self::$log_options[$log_type] : null; |
| 164 | } |
| 165 | |
| 166 | public function log_performance_requests_api_debug($response, $context, $class, $parsed_args, $url) |
| 167 | { |
| 168 | self::add_performance_requests_log($response, $url, $parsed_args); |
| 169 | } |
| 170 | |
| 171 | public function log_performance_requests_http_response($response, $parsed_args, $url) |
| 172 | { |
| 173 | self::add_performance_requests_log($response, $url, $parsed_args); |
| 174 | |
| 175 | return $response; |
| 176 | } |
| 177 | |
| 178 | public function two_daily_cron_hook_function() |
| 179 | { |
| 180 | foreach (array_values(self::$log_options) as $option_name) { |
| 181 | $logs = get_option($option_name, []); |
| 182 | $filtered_logs = []; |
| 183 | $three_days_in_seconds = 3 * 24 * 60 * 60; |
| 184 | |
| 185 | foreach ($logs as $log) { |
| 186 | if ($log['date'] + $three_days_in_seconds > time()) { |
| 187 | $filtered_logs[] = $log; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | update_option($option_name, $filtered_logs, false); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | public static function sort_logs(&$logs) |
| 196 | { |
| 197 | usort($logs, function ($item1, $item2) { |
| 198 | return $item2['date'] <=> $item1['date']; |
| 199 | }); |
| 200 | } |
| 201 | |
| 202 | protected static function get_stack_trace($limit = 10, $remove_first_n = 2) |
| 203 | { |
| 204 | $stack = []; |
| 205 | |
| 206 | foreach (debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, $limit + $remove_first_n) as $num => $frame) { // phpcs:ignore |
| 207 | |
| 208 | // remove first n frames. This will remove get_stack_trace and add_log functions |
| 209 | if ($num < $remove_first_n) { |
| 210 | continue; |
| 211 | } |
| 212 | |
| 213 | $file = !empty($frame['file']) ? str_replace(ABSPATH, '', $frame['file']) : ''; |
| 214 | $line = !empty($frame['line']) ? $frame['line'] : ''; |
| 215 | $func = $frame['function']; |
| 216 | $class = (isset($frame['class'])) ? $frame['class'] : null; |
| 217 | $type = (isset($frame['type'])) ? $frame['type'] : null; |
| 218 | |
| 219 | $stack_msg = '#' . $num . ' ' . $file . '(' . $line . '): '; |
| 220 | |
| 221 | if ($class) { |
| 222 | $stack_msg .= $class . $type; |
| 223 | } |
| 224 | |
| 225 | $stack[] = $stack_msg . $func . '()'; |
| 226 | } |
| 227 | |
| 228 | return $stack; |
| 229 | } |
| 230 | |
| 231 | public static function get_instance() |
| 232 | { |
| 233 | if (null == self::$instance) { |
| 234 | self::$instance = new self(); |
| 235 | } |
| 236 | |
| 237 | return self::$instance; |
| 238 | } |
| 239 | } |
| 240 |