diff --git a/features/eval.feature b/features/eval.feature index 82169214..ba4f2f29 100644 --- a/features/eval.feature +++ b/features/eval.feature @@ -248,3 +248,88 @@ Feature: Evaluating PHP code and files. """ eval()'d code """ + + Scenario: Eval with --hook flag + Given a WP install + + When I run `wp eval 'echo "Hook: " . current_action();' --hook=init` + Then STDOUT should contain: + """ + Hook: init + """ + + When I run `wp eval 'echo "Hook: " . current_action();' --hook=wp_loaded` + Then STDOUT should contain: + """ + Hook: wp_loaded + """ + + Scenario: Eval-file with --hook flag + Given a WP install + And a hook-script.php file: + """ + ] + * : Execute file after a specific WordPress hook has fired. + * * @when before_wp_load * * ## EXAMPLES @@ -38,6 +41,9 @@ class EvalFile_Command extends WP_CLI_Command { * # Execute file my-code.php and pass value1 and value2 arguments. * # Access arguments in $args array ($args[0] = value1, $args[1] = value2). * $ wp eval-file my-code.php value1 value2 + * + * # Execute file after the 'init' hook. + * $ wp eval-file my-code.php --hook=init */ public function __invoke( $args, $assoc_args ) { $file = array_shift( $args ); @@ -52,11 +58,28 @@ public function __invoke( $args, $assoc_args ) { WP_CLI::error( '"-" and "--use-include" parameters cannot be used at the same time' ); } - if ( null === Utils\get_flag_value( $assoc_args, 'skip-wordpress' ) ) { + $execute_closure = function () use ( $file, $args, $use_include ) { + self::execute_eval( $file, $args, $use_include ); + }; + + $hook = Utils\get_flag_value( $assoc_args, 'hook' ); + $skip_wordpress = Utils\get_flag_value( $assoc_args, 'skip-wordpress' ); + + if ( $hook && null !== $skip_wordpress ) { + WP_CLI::error( 'The --hook parameter cannot be used with --skip-wordpress.' ); + } + + if ( $hook ) { + WP_CLI::add_wp_hook( $hook, $execute_closure ); + } + + if ( null === $skip_wordpress ) { WP_CLI::get_runner()->load_wordpress(); } - self::execute_eval( $file, $args, $use_include ); + if ( ! $hook ) { + $execute_closure(); + } } /** diff --git a/src/Eval_Command.php b/src/Eval_Command.php index 2029680e..1c94a853 100644 --- a/src/Eval_Command.php +++ b/src/Eval_Command.php @@ -18,6 +18,9 @@ class Eval_Command extends WP_CLI_Command { * [--skip-wordpress] * : Execute code without loading WordPress. * + * [--hook=] + * : Execute code after a specific WordPress hook has fired. + * * ## EXAMPLES * * # Display WordPress content directory. @@ -28,14 +31,35 @@ class Eval_Command extends WP_CLI_Command { * $ wp eval 'echo rand();' --skip-wordpress * 479620423 * + * # Execute code after WordPress is fully loaded. + * $ wp eval 'echo "Current user: " . wp_get_current_user()->user_login;' --hook=wp_loaded + * Current user: admin + * * @when before_wp_load */ public function __invoke( $args, $assoc_args ) { - if ( null === Utils\get_flag_value( $assoc_args, 'skip-wordpress' ) ) { + $execute_closure = function () use ( $args ) { + eval( $args[0] ); + }; + + $hook = Utils\get_flag_value( $assoc_args, 'hook' ); + $skip_wordpress = Utils\get_flag_value( $assoc_args, 'skip-wordpress' ); + + if ( $hook && null !== $skip_wordpress ) { + WP_CLI::error( 'The --hook parameter cannot be used with --skip-wordpress.' ); + } + + if ( $hook ) { + WP_CLI::add_wp_hook( $hook, $execute_closure ); + } + + if ( null === $skip_wordpress ) { WP_CLI::get_runner()->load_wordpress(); } - eval( $args[0] ); + if ( ! $hook ) { + $execute_closure(); + } } }