0.0, 'queries' => 0 ); } self::$data[ $owner ]['ms'] += $ms; self::$data[ $owner ]['queries'] += $queries; } public static function count_query( $sql ) { if ( self::$stack ) { $top = end( self::$stack ); self::add( $top['owner'], 0, 1 ); } return $sql; } private static function owner_from_file( $file ) { $file = wp_normalize_path( (string) $file ); if ( 0 === strpos( $file, self::$plugin_dir ) ) { $rest = substr( $file, strlen( self::$plugin_dir ) ); $slug = strtok( $rest, '/' ); return 'plugin:' . preg_replace( '/\.php$/', '', $slug ); } if ( 0 === strpos( $file, self::$mu_dir ) ) { return 'mu-plugin:' . basename( $file, '.php' ); } if ( null === self::$theme_dirs && did_action( 'setup_theme' ) ) { self::$theme_dirs = array_unique( array( wp_normalize_path( get_stylesheet_directory() ) . '/', wp_normalize_path( get_template_directory() ) . '/' ) ); } foreach ( (array) self::$theme_dirs as $dir ) { if ( 0 === strpos( $file, $dir ) ) { return 'theme:' . basename( rtrim( $dir, '/' ) ); } } return 'core'; } private static function callback_owner( $callback, $key ) { if ( isset( self::$owners[ $key ] ) ) { return self::$owners[ $key ]; } $owner = 'core'; try { if ( is_string( $callback ) && false !== strpos( $callback, '::' ) ) { $callback = explode( '::', $callback ); } if ( is_array( $callback ) && 2 === count( $callback ) ) { $ref = new ReflectionMethod( $callback[0], $callback[1] ); } elseif ( $callback instanceof Closure || is_string( $callback ) ) { $ref = new ReflectionFunction( $callback ); } elseif ( is_object( $callback ) && method_exists( $callback, '__invoke' ) ) { $ref = new ReflectionMethod( $callback, '__invoke' ); } if ( isset( $ref ) && $ref->getFileName() ) { $owner = self::owner_from_file( $ref->getFileName() ); } } catch ( Exception $e ) { $owner = 'core'; } self::$owners[ $key ] = $owner; return $owner; } /** * Replace each callback of the hook about to run with a timing wrapper. * Array keys are left unchanged, so remove_action() keeps working. */ public static function wrap_hook( $hook ) { global $wp_filter; if ( 'all' === $hook || 'query' === $hook || empty( $wp_filter[ $hook ] ) || ! ( $wp_filter[ $hook ] instanceof WP_Hook ) ) { return; } foreach ( $wp_filter[ $hook ]->callbacks as $priority => $callbacks ) { foreach ( $callbacks as $key => $cb ) { $id = $hook . '|' . $priority . '|' . $key; if ( isset( self::$wrapped[ $id ] ) ) { continue; } $owner = self::callback_owner( $cb['function'], $key ); if ( 'core' === $owner ) { self::$wrapped[ $id ] = true; continue; } $original = $cb['function']; $wp_filter[ $hook ]->callbacks[ $priority ][ $key ]['function'] = static function () use ( $original, $owner ) { return Fixwise_Profiler::timed( $owner, $original, func_get_args() ); }; self::$wrapped[ $id ] = true; } } } public static function timed( $owner, $callback, $args ) { self::$stack[] = array( 'owner' => $owner, 'start' => microtime( true ), 'child' => 0.0, ); try { return call_user_func_array( $callback, $args ); } finally { $frame = array_pop( self::$stack ); $elapsed = ( microtime( true ) - $frame['start'] ) * 1000; self::add( $owner, $elapsed - $frame['child'], 0 ); // self time only. if ( self::$stack ) { self::$stack[ count( self::$stack ) - 1 ]['child'] += $elapsed; } } } public static function finish() { $total = ( microtime( true ) - ( isset( $_SERVER['REQUEST_TIME_FLOAT'] ) ? (float) $_SERVER['REQUEST_TIME_FLOAT'] : microtime( true ) ) ) * 1000; set_transient( 'fixwise_profile_result', array( 'total_ms' => round( $total, 1 ), 'queries' => function_exists( 'get_num_queries' ) ? get_num_queries() : 0, 'memory_mb' => round( memory_get_peak_usage() / 1048576, 1 ), 'components' => self::$data, 'at' => time(), ), 10 * MINUTE_IN_SECONDS ); } } Fixwise_Profiler::start();