Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions features/eval.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
<?php
echo "Hook: " . current_action() . "\n";
echo "Is admin: " . (is_admin() ? 'yes' : 'no') . "\n";
"""

When I run `wp eval-file hook-script.php --hook=init`
Then STDOUT should contain:
"""
Hook: init
"""
And STDOUT should contain:
"""
Is admin:
"""

When I run `wp eval-file hook-script.php --hook=wp_loaded`
Then STDOUT should contain:
"""
Hook: wp_loaded
"""

Scenario: Eval with --hook and --skip-wordpress should error
Given a WP install

When I try `wp eval 'echo "test";' --hook=init --skip-wordpress`
Then STDERR should contain:
"""
Error: The --hook parameter cannot be used with --skip-wordpress.
"""
And the return code should be 1

Scenario: Eval-file with --hook and --skip-wordpress should error
Given an empty directory
And a script.php file:
"""
<?php
echo "test";
"""

When I try `wp eval-file script.php --hook=init --skip-wordpress`
Then STDERR should contain:
"""
Error: The --hook parameter cannot be used with --skip-wordpress.
"""
And the return code should be 1

Scenario: Eval-file with --hook and positional arguments
Given a WP install
And a args-script.php file:
"""
<?php
echo "Hook: " . current_action() . "\n";
echo "Args: " . implode(' ', $args) . "\n";
"""

When I run `wp eval-file args-script.php arg1 arg2 --hook=init`
Then STDOUT should contain:
"""
Hook: init
"""
And STDOUT should contain:
"""
Args: arg1 arg2
"""

28 changes: 26 additions & 2 deletions src/EvalFile_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@ class EvalFile_Command extends WP_CLI_Command {
* [--use-include]
* : Process the provided file via include instead of evaluating its contents.
*
* [--hook=<hook>]
* : Execute file after a specific WordPress hook has fired.
*
* @when before_wp_load
*
* ## EXAMPLES
*
* # 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 );
Expand All @@ -52,11 +58,29 @@ 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' ) ) {
$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,
function () use ( $file, $args, $use_include ) {
self::execute_eval( $file, $args, $use_include );
}
);
}

if ( null === $skip_wordpress ) {
WP_CLI::get_runner()->load_wordpress();
}

self::execute_eval( $file, $args, $use_include );
if ( ! $hook ) {
self::execute_eval( $file, $args, $use_include );
}
}

/**
Expand Down
30 changes: 28 additions & 2 deletions src/Eval_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class Eval_Command extends WP_CLI_Command {
* [--skip-wordpress]
* : Execute code without loading WordPress.
*
* [--hook=<hook>]
* : Execute code after a specific WordPress hook has fired.
*
* ## EXAMPLES
*
* # Display WordPress content directory.
Expand All @@ -28,14 +31,37 @@ 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' ) ) {
$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 ) {
$code = $args[0];
WP_CLI::add_wp_hook(
$hook,
function () use ( $code ) {
eval( $code );
}
);
}

if ( null === $skip_wordpress ) {
WP_CLI::get_runner()->load_wordpress();
}

eval( $args[0] );
if ( ! $hook ) {
eval( $args[0] );
}
}
}