| @@ -28,22 +28,22 @@ | ||
| 28 | 28 | global $wp_version; |
| 29 | 29 | if (! defined('CODE_PROFILER_UA') ) { // UA signatures can be user-defined in the wp-config.php |
| 30 | 30 | define ('CODE_PROFILER_UA', [ |
| 31 | 31 | esc_html__('Desktop', 'code-profiler') => [ |
| 32 | - 'Firefox' => 'Mozilla/5.0 (Linux x86_64; rv:104.0) Gecko/20100101 Firefox/104.0', | |
| 32 | + 'Firefox' => 'Mozilla/5.0 (Linux x86_64; rv:110.0) Gecko/20100101 Firefox/110.0', | |
| 33 | 33 | 'Chrome' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)'. |
| 34 | - ' Chrome/105.0.0.0 Safari/537.36', | |
| 34 | + ' Chrome/111.0.0.0 Safari/537.36', | |
| 35 | 35 | 'Edge' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,'. |
| 36 | - ' like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.27' | |
| 36 | + ' like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/110.0.1587.63' | |
| 37 | 37 | ], |
| 38 | 38 | esc_html__('Mobile', 'code-profiler') => [ |
| 39 | - 'Android Phone' => 'Mozilla/5.0 (Android 12; Mobile; rv:68.0) Gecko/68.0 Firefox/104.0', | |
| 40 | - 'Android Tablet' => 'Mozilla/5.0 (Linux; Android 12.0; SAMSUNG-SM-T377A Build/NMF26X)'. | |
| 41 | - ' AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Mobile Safari/537.36', | |
| 42 | - 'iPhone' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 15_5 like Mac OS X) AppleWebKit/605.1.15'. | |
| 43 | - ' (KHTML, like Gecko) Version/15.4 Mobile/15E148 Safari/604.1', | |
| 44 | - 'iPad' => 'Mozilla/5.0 (iPad; CPU OS 15_5 like Mac OS X) AppleWebKit/605.1.15'. | |
| 45 | - ' (KHTML, like Gecko) GSA/213.0.449417121 Mobile/15E148 Safari/604.1' | |
| 39 | + 'Android Phone' => 'Mozilla/5.0 (Android 13; Mobile; rv:68.0) Gecko/68.0 Firefox/110.0', | |
| 40 | + 'Android Tablet' => 'Mozilla/5.0 (Linux; Android 13.0; SAMSUNG-SM-T377A Build/NMF26X)'. | |
| 41 | + ' AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Mobile Safari/537.36', | |
| 42 | + 'iPhone' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 16_3_1 like Mac OS X) AppleWebKit/605.1.15'. | |
| 43 | + ' (KHTML, like Gecko) Version/16.3 Mobile/15E148 Safari/604.1', | |
| 44 | + 'iPad' => 'Mozilla/5.0 (iPad; CPU OS 16_3_1 like Mac OS X) AppleWebKit/605.1.15'. | |
| 45 | + ' (KHTML, like Gecko) GSA/213.0.449417121 Mobile/15E148 Safari/605.1.15' | |
| 46 | 46 | ], |
| 47 | 47 | esc_html__('Bot', 'code-profiler') => [ |
| 48 | 48 | 'Google Bot' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)', |
| 49 | 49 | 'WordPress' => 'Mozilla/5.0 (compatible; CodeProfiler for WordPress/'. $wp_version . |
| @@ -128,8 +128,17 @@ | ||
| 128 | 128 | |
| 129 | 129 | |
| 130 | 130 | if (! file_exists( WPMU_PLUGIN_DIR .'/'. CODE_PROFILER_MUPLUGIN ) ) { |
| 131 | 131 | |
| 132 | + if (! is_writable( WPMU_PLUGIN_DIR ) ) { | |
| 133 | + wp_die( | |
| 134 | + sprintf( | |
| 135 | + __('Error: The MU folder is read only, please make it writable: %s', 'code-profiler'), | |
| 136 | + esc_html( WPMU_PLUGIN_DIR ) .'/' | |
| 137 | + ) | |
| 138 | + ); | |
| 139 | + } | |
| 140 | + | |
| 132 | 141 | if (! file_exists( __DIR__ .'/mu.plugin') ) { |
| 133 | 142 | code_profiler_log_error( sprintf( |
| 134 | 143 | esc_html__('Cannot find the MU plugin: %s', 'code-profiler'), |
| 135 | 144 | __DIR__ .'/mu.plugin' |
| @@ -245,18 +254,39 @@ | ||
| 245 | 254 | } |
| 246 | 255 | |
| 247 | 256 | // ===================================================================== |
| 248 | 257 | // Write message to the log. |
| 249 | -// Log level can be a combination of INFO (1), WARN (2) and ERROR (4). | |
| 258 | +// Log level can be a combination of INFO (1), WARN (2), ERROR (4) | |
| 259 | +// and DEBUG (8). | |
| 250 | 260 | |
| 251 | -function code_profiler_write2log( $message, $level = 1 ) { | |
| 261 | +function code_profiler_write2log( $message, $level, $create ) { | |
| 252 | 262 | |
| 253 | - file_put_contents( CODE_PROFILER_LOG, time() ."~~$level~~$message\n", FILE_APPEND ); | |
| 263 | + if ( empty( $create ) ) { | |
| 264 | + file_put_contents( | |
| 265 | + CODE_PROFILER_LOG, | |
| 266 | + time() ."~~$level~~$message\n", | |
| 267 | + FILE_APPEND | |
| 268 | + ); | |
| 269 | + } else { | |
| 270 | + file_put_contents( | |
| 271 | + CODE_PROFILER_LOG, | |
| 272 | + time() ."~~$level~~$message\n" | |
| 273 | + ); | |
| 274 | + } | |
| 254 | 275 | } |
| 255 | 276 | |
| 256 | -function code_profiler_log_info( $string ) { code_profiler_write2log( $string, 1 ); } | |
| 257 | -function code_profiler_log_warn( $string ) { code_profiler_write2log( $string, 2 ); } | |
| 258 | -function code_profiler_log_error( $string ) { code_profiler_write2log( $string, 4 ); } | |
| 277 | +function code_profiler_log_info( $string, $create = 0 ) { | |
| 278 | + code_profiler_write2log( $string, 1, $create ); | |
| 279 | +} | |
| 280 | +function code_profiler_log_warn( $string, $create = 0 ) { | |
| 281 | + code_profiler_write2log( $string, 2, $create ); | |
| 282 | +} | |
| 283 | +function code_profiler_log_error( $string, $create = 0 ) { | |
| 284 | + code_profiler_write2log( $string, 4, $create ); | |
| 285 | +} | |
| 286 | +function code_profiler_log_debug( $string, $create = 0 ) { | |
| 287 | + code_profiler_write2log( $string, 8, $create ); | |
| 288 | +} | |
| 259 | 289 | |
| 260 | 290 | // ===================================================================== |
| 261 | 291 | // Verify if a profile exists and return its full path. |
| 262 | 292 | |
| @@ -354,54 +384,63 @@ | ||
| 354 | 384 | |
| 355 | 385 | $string = $open; |
| 356 | 386 | |
| 357 | 387 | if ( empty( $decode->items ) ) { |
| 358 | - return false; | |
| 388 | + $tmp = 'N/A'; | |
| 389 | + } else { | |
| 390 | + $tmp = (int) --$decode->items; | |
| 359 | 391 | } |
| 360 | - $decode->items--; | |
| 361 | 392 | $string .= sprintf( |
| 362 | 393 | __('%s plugins and 1 theme', 'code-profiler'), |
| 363 | - (int) $decode->items | |
| 394 | + $tmp | |
| 364 | 395 | ); |
| 365 | 396 | |
| 366 | 397 | $string .= $div; |
| 367 | 398 | |
| 368 | 399 | if ( empty( $decode->time ) ) { |
| 369 | - return false; | |
| 400 | + $tmp = 'N/A'; | |
| 401 | + } else { | |
| 402 | + $tmp = number_format( $decode->time, 4 ); | |
| 370 | 403 | } |
| 371 | 404 | $string .= sprintf( |
| 372 | 405 | __('Execution time: %ss', 'code-profiler'), |
| 373 | - number_format( $decode->time, 4 ) | |
| 406 | + $tmp | |
| 374 | 407 | ); |
| 375 | 408 | |
| 376 | 409 | $string .= $div; |
| 377 | 410 | |
| 378 | 411 | if ( empty( $decode->memory ) ) { |
| 379 | - return false; | |
| 412 | + $tmp = 'N/A'; | |
| 413 | + } else { | |
| 414 | + $tmp = number_format( $decode->memory, 2); | |
| 380 | 415 | } |
| 381 | 416 | $string .= sprintf( |
| 382 | 417 | __('Peak memory: %s MB', 'code-profiler'), |
| 383 | - number_format( $decode->memory, 2) | |
| 418 | + $tmp | |
| 384 | 419 | ); |
| 385 | 420 | |
| 386 | 421 | $string .= $div; |
| 387 | 422 | |
| 388 | 423 | if ( empty( $decode->io ) ) { |
| 389 | - return false; | |
| 424 | + $tmp = 'N/A'; | |
| 425 | + } else { | |
| 426 | + $tmp = number_format( $decode->io ); | |
| 390 | 427 | } |
| 391 | 428 | $string .= sprintf( |
| 392 | 429 | __('File I/O operations: %s', 'code-profiler'), |
| 393 | - number_format( $decode->io ) | |
| 430 | + $tmp | |
| 394 | 431 | ); |
| 395 | 432 | |
| 396 | 433 | $string .= $div; |
| 397 | 434 | |
| 398 | 435 | if ( empty( $decode->queries ) ) { |
| 399 | - return false; | |
| 436 | + $tmp = 'N/A'; | |
| 437 | + } else { | |
| 438 | + $tmp = number_format( $decode->queries ); | |
| 400 | 439 | } |
| 401 | 440 | $string .= sprintf( |
| 402 | 441 | __('SQL queries: %s', 'code-profiler'), |
| 403 | - number_format( $decode->queries ) | |
| 442 | + $tmp | |
| 404 | 443 | ); |
| 405 | 444 | |
| 406 | 445 | $string .= $close; |
| 407 | 446 | |
| @@ -425,8 +464,18 @@ | ||
| 425 | 464 | __('Deleting %s temporary files found in the profiles folder', 'code-profiler'), |
| 426 | 465 | (int) $count |
| 427 | 466 | ) ); |
| 428 | 467 | } |
| 468 | + } | |
| 469 | +} | |
| 470 | + | |
| 471 | +// ===================================================================== | |
| 472 | +// Clear the log if it's bigger than 100KB. | |
| 473 | + | |
| 474 | +function code_profiler_clearlog() { | |
| 475 | + | |
| 476 | + if ( file_exists( CODE_PROFILER_LOG ) && filesize( CODE_PROFILER_LOG ) > 100000 ) { | |
| 477 | + file_put_contents( CODE_PROFILER_LOG, "<?php exit; ?>\n" ); | |
| 429 | 478 | } |
| 430 | 479 | } |
| 431 | 480 | |
| 432 | 481 | // ===================================================================== |