diff --git a/features/profile-hook.feature b/features/profile-hook.feature index f900872d..bb30ccbc 100644 --- a/features/profile-hook.feature +++ b/features/profile-hook.feature @@ -1,6 +1,5 @@ Feature: Profile a specific hook - @require-wp-4.0 Scenario: Profile all hooks when a specific hook isn't specified Given a WP install @@ -20,21 +19,6 @@ Feature: Profile a specific hook | callback | cache_hits | cache_misses | | sanitize_comment_cookies() | 0 | 0 | - @less-than-php-7 @require-wp-4.0 - Scenario: Profile an intermediate stage hook - Given a WP install - - When I run `wp profile hook wp_head:before --fields=callback,cache_hits,cache_misses` - Then STDOUT should be a table containing rows: - | callback | cache_hits | cache_misses | - | parse_blocks() | 0 | 0 | - | _get_wptexturize_split_regex() | 0 | 0 | - And STDOUT should not contain: - """ - WP_CLI\Profile\Profiler->wp_tick_profile_begin() - """ - - @require-wp-4.0 Scenario: Profile a hook before the template is loaded Given a WP install @@ -43,7 +27,6 @@ Feature: Profile a specific hook | callback | And STDERR should be empty - @require-wp-4.0 Scenario: Profile a hook without any callbacks Given a WP install @@ -65,7 +48,7 @@ Feature: Profile a specific hook [--hook[=]] [--fields=] [--format=] [--order=] [--orderby=] or: wp profile eval-file [--hook[=]] [--fields=] [--format=] [--order=] [--orderby=] - or: wp profile hook [] [--all] [--spotlight] [--url=] [--fields=] [--format=] [--order=] [--orderby=] + or: wp profile hook [] [--all] [--spotlight] [--url=] [--fields=] [--format=] [--order=] [--orderby=] [--search=] or: wp profile stage [] [--all] [--spotlight] [--url=] [--fields=] [--format=] [--order=] [--orderby=] See 'wp help profile ' for more information on a specific command. diff --git a/src/Command.php b/src/Command.php index b3d6936d..b9f19e61 100644 --- a/src/Command.php +++ b/src/Command.php @@ -233,6 +233,9 @@ public function stage( $args, $assoc_args ) { * [--orderby=] * : Set orderby which field. * + * [--search=] + * : Filter callbacks to those matching the given search pattern (case-insensitive). + * * ## EXAMPLES * * # Profile a hook. @@ -291,6 +294,13 @@ public function hook( $args, $assoc_args ) { if ( Utils\get_flag_value( $assoc_args, 'spotlight' ) ) { $loggers = self::shine_spotlight( $loggers, $metrics ); } + $search = Utils\get_flag_value( $assoc_args, 'search', false ); + if ( false !== $search && '' !== $search ) { + if ( ! $focus ) { + WP_CLI::error( '--search requires --all or a specific hook.' ); + } + $loggers = self::filter_by_callback( $loggers, $search ); + } $formatter->display_items( $loggers, true, $order, $orderby ); } @@ -534,4 +544,20 @@ private static function shine_spotlight( $loggers, $metrics ) { return $loggers; } + + /** + * Filter loggers to only those whose callback name matches a pattern. + * + * @param array $loggers + * @param string $pattern + * @return array + */ + private static function filter_by_callback( $loggers, $pattern ) { + return array_filter( + $loggers, + function ( $logger ) use ( $pattern ) { + return isset( $logger->callback ) && false !== stripos( $logger->callback, $pattern ); + } + ); + } }