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:", "mu:", "theme:", "dropin:", "core", "self" or "other". * * @param string $file Absolute path. * @return string */ function pirkoo_profile_file_component( $file ) { if ( isset( Pirkoo_Profiler::$file_component[ $file ] ) ) { return Pirkoo_Profiler::$file_component[ $file ]; } $path = wp_normalize_path( $file ); $component = 'other'; if ( in_array( trailingslashit( dirname( $path ) ), pirkoo_profile_dir_variants( ABSPATH ), true ) && preg_match( '/^(wp-[\w-]+|index)\.php$/', basename( $path ) ) ) { $component = 'core'; // index.php, wp-blog-header.php, wp-load.php and the like. } foreach ( 'core' === $component ? array() : array( 'self', 'mu', 'plugin', 'theme', 'core', 'dropin' ) as $type ) { foreach ( Pirkoo_Profiler::$dirs[ $type ] as $dir ) { if ( 0 !== strpos( $path, $dir ) ) { continue; } $first = explode( '/', substr( $path, strlen( $dir ) ), 2 )[0]; switch ( $type ) { case 'self': case 'core': $component = $type; break; case 'dropin': $component = false === strpos( substr( $path, strlen( $dir ) ), '/' ) ? 'dropin:' . $first : 'other'; break; default: $component = $type . ':' . $first; } break 2; } } Pirkoo_Profiler::$file_component[ $file ] = $component; return $component; } /** * The component that caused the current call: the first plugin, theme or other * non-core file on the call stack, else core. For queries, the stack is read * from the outermost $wpdb call, so a database drop-in (db.php) isn't blamed. * * @return string */ function pirkoo_profile_caller() { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace -- only on the profiled request. $frames = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 40 ); for ( $i = count( $frames ) - 1; $i > 0; $i-- ) { if ( isset( $frames[ $i ]['class'] ) && is_a( $frames[ $i ]['class'], 'wpdb', true ) ) { $frames = array_slice( $frames, $i ); // Its 'file' is the code that called $wpdb. break; } } foreach ( $frames as $frame ) { if ( empty( $frame['file'] ) ) { continue; } $component = pirkoo_profile_file_component( $frame['file'] ); if ( 'core' !== $component && 'self' !== $component ) { return $component; } } return 'core'; } /** * Tags each logged query with the component that ran it (filter log_query_custom_data). * * @param mixed $data Custom query data (an array, unless another filter broke it). * @return array */ function pirkoo_profile_tag_query( $data ) { $data = is_array( $data ) ? $data : array(); $data['pirkoo'] = pirkoo_profile_caller(); return $data; } /** * Notes the start of an outgoing HTTP call (filter pre_http_request, passes $pre through). * * @param mixed $pre Short-circuit value. * @param array $args Request arguments. * @param string $url URL. * @return mixed */ function pirkoo_profile_http_start( $pre, $args, $url ) { Pirkoo_Profiler::$http_open[] = array( hrtime( true ), (string) $url, pirkoo_profile_caller() ); return $pre; } /** * Records a finished outgoing HTTP call: host, time and status only (action http_api_debug). * * @param array|WP_Error $response Response. * @param string $context "response". * @param string $transport Transport class. * @param array $args Request arguments. * @param string $url URL. * @return void */ function pirkoo_profile_http_end( $response, $context, $transport, $args, $url ) { for ( $i = count( Pirkoo_Profiler::$http_open ) - 1; $i >= 0; $i-- ) { if ( Pirkoo_Profiler::$http_open[ $i ][1] !== (string) $url ) { continue; } list( $start, , $component ) = Pirkoo_Profiler::$http_open[ $i ]; array_splice( Pirkoo_Profiler::$http_open, $i, 1 ); $host = wp_parse_url( (string) $url, PHP_URL_HOST ); $host = is_string( $host ) ? strtolower( $host ) : 'unknown'; $own = array( 'localhost', '127.0.0.1', '::1', '[::1]', strtolower( (string) wp_parse_url( home_url(), PHP_URL_HOST ) ), strtolower( (string) wp_parse_url( site_url(), PHP_URL_HOST ) ) ); if ( count( Pirkoo_Profiler::$http ) < 25 ) { Pirkoo_Profiler::$http[] = array( 'host' => pirkoo_identifier( $host ), 'own_site' => in_array( $host, $own, true ), // E.g. WordPress starting WP-Cron. 'component' => $component, 'ms' => round( ( hrtime( true ) - $start ) / 1e6, 1 ), 'status' => is_wp_error( $response ) ? null : (int) wp_remote_retrieve_response_code( $response ), ); } return; } } /** * Puts the chosen product in a cart that exists only for this request, so the * real checkout renders. No add-to-cart actions run, no cookie is set and the * session isn't saved. * * @return void */ function pirkoo_profile_fill_cart() { if ( ! function_exists( 'WC' ) || ! WC()->cart || ! WC()->cart->is_empty() ) { return; } add_filter( 'woocommerce_set_cookie_enabled', '__return_false' ); if ( WC()->session ) { remove_action( 'shutdown', array( WC()->session, 'save_data' ), 20 ); } $product = Pirkoo_Profiler::$product_id ? wc_get_product( Pirkoo_Profiler::$product_id ) : null; if ( ! $product || ! $product->is_purchasable() || ! $product->is_in_stock() ) { Pirkoo_Profiler::$product_id = 0; add_filter( 'woocommerce_checkout_redirect_empty_cart', '__return_false' ); // Profile the empty checkout instead. return; } $key = WC()->cart->generate_cart_id( $product->get_id() ); WC()->cart->set_cart_contents( array( $key => array( 'key' => $key, 'product_id' => $product->get_id(), 'variation_id' => 0, 'variation' => array(), 'quantity' => 1, 'data' => $product, 'data_hash' => wc_get_cart_item_data_hash( $product ), ), ) ); }