component-bases
1 year ago
filter-hooks
3 years ago
class-fwp-debug.php
1 year ago
class-fwp-html-element.php
3 years ago
class-fwp-html-fields.php
3 years ago
class-fwp-debug.php
107 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class FWP_Debug_1x0x0 |
| 4 | * |
| 5 | * @package FWP |
| 6 | * @category WordPress Library |
| 7 | * @version 1.0.0 |
| 8 | |
| 9 | * @link https://www.webfactoryltd.com/ |
| 10 | */ |
| 11 | class FWP_Debug_1x0x0 extends WPRun_Base_1x0x0 |
| 12 | { |
| 13 | |
| 14 | /** |
| 15 | * @var array |
| 16 | */ |
| 17 | private $settings = array( |
| 18 | 'debug_func_name' => 'debug', |
| 19 | 'log_hooks' => false, |
| 20 | ); |
| 21 | |
| 22 | /** |
| 23 | * @var array |
| 24 | */ |
| 25 | private static $benchmarks = array(); |
| 26 | |
| 27 | /** |
| 28 | * Initialize |
| 29 | * @param array $settings Optional |
| 30 | */ |
| 31 | protected function init( array $settings = array() ) |
| 32 | { |
| 33 | $this->settings = wp_parse_args( $settings, $this->settings ); |
| 34 | |
| 35 | $this->create_func(); |
| 36 | |
| 37 | if ( $this->settings[ 'log_hooks' ] ) { |
| 38 | register_shutdown_function( $this->get_callback( 'log_hooks' ) ); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Create logbal debug function |
| 44 | * @return void |
| 45 | */ |
| 46 | private function create_func() |
| 47 | { |
| 48 | $func = $this->settings[ 'debug_func_name' ]; |
| 49 | |
| 50 | if ( function_exists( $func ) || !is_callable( $func, true ) ) { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @param mixed $entry |
| 58 | */ |
| 59 | public static function log( $entry, $title = '' ) |
| 60 | { |
| 61 | $content = ''; |
| 62 | |
| 63 | if ( !empty($title) ) { |
| 64 | $content = $title . ': '; |
| 65 | } |
| 66 | |
| 67 | $content .= var_export( $entry, true ); // phpcs:ignore |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Log all hooks being applied |
| 72 | * @global array $wp_filter |
| 73 | */ |
| 74 | protected function log_hooks() |
| 75 | { |
| 76 | global $wp_filter; |
| 77 | |
| 78 | $hooks = array_keys( $wp_filter ); |
| 79 | self::log( $hooks, 'WP Hooks' ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * |
| 84 | */ |
| 85 | public static function start_benchmark( $label = 'benchmark' ) |
| 86 | { |
| 87 | self::$benchmarks[ $label ][ 'start' ] = microtime( true ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * |
| 92 | */ |
| 93 | public static function end_benchmark( $label = 'benchmark' ) |
| 94 | { |
| 95 | $end_time = microtime( true ); |
| 96 | self::$benchmarks[ $label ][ 'end' ] = $end_time; |
| 97 | $start_time = self::$benchmarks[ $label ][ 'start' ]; |
| 98 | |
| 99 | $total_time = $end_time - $start_time; |
| 100 | |
| 101 | self::log( $total_time, $label ); |
| 102 | |
| 103 | return $total_time; |
| 104 | } |
| 105 | |
| 106 | } |
| 107 |