@@ -127,6 +127,7 @@ class Command {
127127 * | total (7) | 1.0335s | 77.42% |
128128 * +--------------------------+---------+-------------+
129129 *
130+ * @skipglobalargcheck
130131 * @when before_wp_load
131132 */
132133 public function stage ( $ args , $ assoc_args ) {
@@ -233,6 +234,9 @@ public function stage( $args, $assoc_args ) {
233234 * [--orderby=<fields>]
234235 * : Set orderby which field.
235236 *
237+ * [--search=<pattern>]
238+ * : Filter callbacks to those matching the given search pattern (case-insensitive).
239+ *
236240 * ## EXAMPLES
237241 *
238242 * # Profile a hook.
@@ -251,6 +255,7 @@ public function stage( $args, $assoc_args ) {
251255 * | total (7) | 8 | 0 |
252256 * +--------------------------------+------------+--------------+
253257 *
258+ * @skipglobalargcheck
254259 * @when before_wp_load
255260 */
256261 public function hook ( $ args , $ assoc_args ) {
@@ -291,6 +296,13 @@ public function hook( $args, $assoc_args ) {
291296 if ( Utils \get_flag_value ( $ assoc_args , 'spotlight ' ) ) {
292297 $ loggers = self ::shine_spotlight ( $ loggers , $ metrics );
293298 }
299+ $ search = Utils \get_flag_value ( $ assoc_args , 'search ' , false );
300+ if ( false !== $ search && '' !== $ search ) {
301+ if ( ! $ focus ) {
302+ WP_CLI ::error ( '--search requires --all or a specific hook. ' );
303+ }
304+ $ loggers = self ::filter_by_callback ( $ loggers , $ search );
305+ }
294306 $ formatter ->display_items ( $ loggers , true , $ order , $ orderby );
295307 }
296308
@@ -700,4 +712,20 @@ private static function shine_spotlight( $loggers, $metrics ) {
700712
701713 return $ loggers ;
702714 }
715+
716+ /**
717+ * Filter loggers to only those whose callback name matches a pattern.
718+ *
719+ * @param array $loggers
720+ * @param string $pattern
721+ * @return array
722+ */
723+ private static function filter_by_callback ( $ loggers , $ pattern ) {
724+ return array_filter (
725+ $ loggers ,
726+ function ( $ logger ) use ( $ pattern ) {
727+ return isset ( $ logger ->callback ) && false !== stripos ( $ logger ->callback , $ pattern );
728+ }
729+ );
730+ }
703731}
0 commit comments