@@ -85,6 +85,7 @@ class Cron_Event_Command extends WP_CLI_Command {
8585 * * schedule
8686 * * interval
8787 * * next_run
88+ * * actions
8889 *
8990 * ## EXAMPLES
9091 *
@@ -113,6 +114,14 @@ public function list_( $args, $assoc_args ) {
113114 $ events = array ();
114115 }
115116
117+ // Populate actions field only if requested
118+ $ requested_fields = $ formatter ->fields ;
119+ if ( ! empty ( $ requested_fields ) && in_array ( 'actions ' , $ requested_fields , true ) ) {
120+ foreach ( $ events as $ event ) {
121+ $ event ->actions = self ::get_hook_actions ( $ event ->hook );
122+ }
123+ }
124+
116125 foreach ( $ events as $ key => $ event ) {
117126 foreach ( $ this ->fields as $ field ) {
118127 if ( ! empty ( $ assoc_args [ $ field ] ) && $ event ->{$ field } !== $ assoc_args [ $ field ] ) {
@@ -399,6 +408,7 @@ protected static function format_event( stdClass $event ) {
399408 $ event ->next_run = get_date_from_gmt ( date ( 'Y-m-d H:i:s ' , $ event ->time ), self ::$ time_format ); //phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
400409 $ event ->next_run_gmt = date ( self ::$ time_format , $ event ->time ); //phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
401410 $ event ->next_run_relative = self ::interval ( $ event ->time - time () );
411+ $ event ->actions = '' ;
402412
403413 return $ event ;
404414 }
@@ -607,4 +617,92 @@ private static function interval( $since ) {
607617 private function get_formatter ( &$ assoc_args ) {
608618 return new \WP_CLI \Formatter ( $ assoc_args , $ this ->fields , 'event ' );
609619 }
620+
621+ /**
622+ * Gets the actions (callbacks) registered for a specific hook.
623+ *
624+ * @param string $hook_name The name of the hook.
625+ * @return string A comma-separated list of action callbacks, or 'None' if no actions are registered.
626+ */
627+ protected static function get_hook_actions ( $ hook_name ) {
628+ static $ cache = array ();
629+
630+ if ( isset ( $ cache [ $ hook_name ] ) ) {
631+ return $ cache [ $ hook_name ];
632+ }
633+
634+ global $ wp_filter ;
635+
636+ if ( ! isset ( $ wp_filter [ $ hook_name ] ) ) {
637+ $ cache [ $ hook_name ] = 'None ' ;
638+ return $ cache [ $ hook_name ];
639+ }
640+
641+ $ hook = $ wp_filter [ $ hook_name ];
642+
643+ // Get callbacks from the WP_Hook object (WordPress 4.7+)
644+ if ( $ hook instanceof \WP_Hook ) {
645+ $ callbacks = $ hook ->callbacks ;
646+ } else {
647+ // Fallback for older WordPress versions
648+ $ callbacks = $ hook ;
649+ }
650+
651+ if ( empty ( $ callbacks ) ) {
652+ $ cache [ $ hook_name ] = 'None ' ;
653+ return $ cache [ $ hook_name ];
654+ }
655+
656+ ksort ( $ callbacks );
657+
658+ $ actions = array ();
659+
660+ // Iterate through all priorities
661+ foreach ( $ callbacks as $ priority => $ priority_callbacks ) {
662+ foreach ( $ priority_callbacks as $ callback_info ) {
663+ if ( ! isset ( $ callback_info ['function ' ] ) ) {
664+ continue ;
665+ }
666+ $ callback = $ callback_info ['function ' ];
667+ $ actions [] = self ::format_callback ( $ callback );
668+ }
669+ }
670+
671+ $ result = empty ( $ actions ) ? 'None ' : implode ( ', ' , $ actions );
672+ $ cache [ $ hook_name ] = $ result ;
673+ return $ cache [ $ hook_name ];
674+ }
675+
676+ /**
677+ * Formats a callback into a readable string.
678+ *
679+ * @param callable $callback The callback to format.
680+ * @return string A formatted string representing the callback.
681+ */
682+ protected static function format_callback ( $ callback ) {
683+ if ( is_string ( $ callback ) ) {
684+ return $ callback ;
685+ } elseif ( is_array ( $ callback ) && count ( $ callback ) === 2 ) {
686+ /**
687+ * @var array{0: string, 1: string} $callback
688+ */
689+ $ class = $ callback [0 ];
690+ $ method = $ callback [1 ];
691+
692+ if ( is_object ( $ class ) ) {
693+ $ class_name = get_class ( $ class );
694+ } else {
695+ $ class_name = $ class ;
696+ }
697+
698+ return $ class_name . ':: ' . $ method ;
699+ } elseif ( $ callback instanceof \Closure ) {
700+ return 'Closure ' ;
701+ } elseif ( is_object ( $ callback ) ) {
702+ return get_class ( $ callback ) . '::__invoke ' ;
703+ }
704+
705+ // Fallback for unknown callback types
706+ return 'Unknown ' ;
707+ }
610708}
0 commit comments