array_unique( array( $self, false !== $real ? wp_normalize_path( $real ) . '/' : $self ) ), 'plugin' => pirkoo_profile_dir_variants( WP_PLUGIN_DIR ), 'mu' => pirkoo_profile_dir_variants( WPMU_PLUGIN_DIR ), 'theme' => pirkoo_profile_dir_variants( get_theme_root() ), 'core' => array_merge( pirkoo_profile_dir_variants( ABSPATH . WPINC ), pirkoo_profile_dir_variants( ABSPATH . 'wp-admin' ) ), 'dropin' => pirkoo_profile_dir_variants( WP_CONTENT_DIR ), ); pirkoo_profile_calibrate(); // Callbacks added after a pass are picked up by the next one. pirkoo_profile_wrap(); add_action( 'plugins_loaded', 'pirkoo_profile_wrap', PHP_INT_MAX ); add_action( 'after_setup_theme', 'pirkoo_profile_wrap', PHP_INT_MIN ); add_action( 'init', 'pirkoo_profile_wrap', PHP_INT_MAX ); add_action( 'wp_loaded', 'pirkoo_profile_wrap', PHP_INT_MAX ); add_action( 'template_redirect', 'pirkoo_profile_wrap', PHP_INT_MIN ); add_action( 'wp_head', 'pirkoo_profile_wrap', PHP_INT_MIN ); foreach ( array( 'plugins_loaded', 'wp_loaded', 'template_redirect' ) as $phase ) { add_action( $phase, function () use ( $phase ) { Pirkoo_Profiler::$marks[ $phase ] = pirkoo_profile_elapsed(); }, PHP_INT_MIN ); } add_filter( 'log_query_custom_data', 'pirkoo_profile_tag_query', 10, 1 ); add_filter( 'pre_http_request', 'pirkoo_profile_http_start', PHP_INT_MAX, 3 ); add_action( 'http_api_debug', 'pirkoo_profile_http_end', 10, 5 ); add_action( 'wp_loaded', 'pirkoo_profile_fill_cart', 30 ); add_action( 'shutdown', 'pirkoo_profile_finish', PHP_INT_MAX ); } /** * A directory as normalized paths with a trailing slash, as given and resolved (symlinks). * * @param string $dir Directory. * @return string[] */ function pirkoo_profile_dir_variants( $dir ) { $out = array( trailingslashit( wp_normalize_path( $dir ) ) ); $real = realpath( $dir ); if ( false !== $real ) { $out[] = trailingslashit( wp_normalize_path( $real ) ); } return array_values( array_unique( $out ) ); } /** * Seconds since WordPress started loading. * * @return float */ function pirkoo_profile_elapsed() { global $timestart; return microtime( true ) - (float) $timestart; } /** * Measures what one wrapped call costs, to report the profiler's own overhead. * * @return void */ function pirkoo_profile_calibrate() { $wrapper = pirkoo_profile_wrapper( '__return_null', 'self', 'calibrate' ); $runs = 200; $start = hrtime( true ); for ( $i = 0; $i < $runs; $i++ ) { $wrapper(); } Pirkoo_Profiler::$call_cost_ns = ( hrtime( true ) - $start ) / $runs; Pirkoo_Profiler::$components = array(); Pirkoo_Profiler::$hooks = array(); } /** * A callable that runs $callback and records its own time for $component. * * @param callable $callback Original callback. * @param string $component Component key. * @param string $hook Hook name. * @return Closure */ function pirkoo_profile_wrapper( $callback, $component, $hook ) { return static function ( ...$args ) use ( $callback, $component, $hook ) { Pirkoo_Profiler::$stack[] = 0; $start = hrtime( true ); try { return call_user_func_array( $callback, $args ); } finally { $elapsed = hrtime( true ) - $start; $children = (int) array_pop( Pirkoo_Profiler::$stack ); $depth = count( Pirkoo_Profiler::$stack ); if ( $depth > 0 ) { Pirkoo_Profiler::$stack[ $depth - 1 ] += $elapsed; } Pirkoo_Profiler::record( $component, $hook, $elapsed - $children ); } }; } /** * Wraps every hook callback not wrapped yet. The callback keeps its key in the * hook, so remove_action() and has_filter() by other plugins still find it. * Callbacks taking arguments by reference are left alone (a wrapper would break them). * * @return void */ function pirkoo_profile_wrap() { global $wp_filter; $start = hrtime( true ); foreach ( $wp_filter as $tag => $hook ) { if ( ! $hook instanceof WP_Hook || 'all' === $tag ) { continue; } $name = pirkoo_profile_hook_name( (string) $tag ); foreach ( $hook->callbacks as $priority => $callbacks ) { foreach ( $callbacks as $id => $callback ) { $key = $tag . "\0" . $priority . "\0" . $id; if ( isset( Pirkoo_Profiler::$wrapped[ $key ] ) ) { continue; } Pirkoo_Profiler::$wrapped[ $key ] = true; if ( ! isset( Pirkoo_Profiler::$callback_component[ $id ] ) ) { Pirkoo_Profiler::$callback_component[ $id ] = pirkoo_profile_callback_component( $callback['function'] ); } $component = Pirkoo_Profiler::$callback_component[ $id ]; if ( false === $component || 'self' === $component ) { continue; } $hook->callbacks[ $priority ][ $id ]['function'] = pirkoo_profile_wrapper( $callback['function'], $component, $name ); } } } Pirkoo_Profiler::$wrap_ns += hrtime( true ) - $start; } /** * Hook name as sent: printable ASCII, at most 120 characters. * * @param string $tag Hook name. * @return string */ function pirkoo_profile_hook_name( $tag ) { return substr( (string) preg_replace( '/[^\x21-\x7e]/', '?', $tag ), 0, 120 ); } /** * Component key of a callback from the file that defines it, or false to leave * it unwrapped (unknown callable or arguments by reference). * * @param mixed $callback Callback. * @return string|false */ function pirkoo_profile_callback_component( $callback ) { try { if ( is_string( $callback ) && false !== strpos( $callback, '::' ) ) { $callback = explode( '::', $callback, 2 ); } 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' ); } else { return false; } } catch ( ReflectionException $e ) { return false; // E.g. a method handled by __call(). } foreach ( $ref->getParameters() as $param ) { if ( $param->isPassedByReference() ) { return false; } } $file = $ref->getFileName(); return false === $file ? 'core' : pirkoo_profile_file_component( $file ); // Internal PHP functions count as core. } /** * Component key of a file: "plugin: