Type "exit" to close session.
"""
- Scenario: $_ special variable
- Given a WP install
- And a session file:
- """
- WP_ADMIN
- $_
- get_current_user_id()
- $_
- """
-
- When I run `wp shell --quiet < session`
- Then it should run without errors
- And STDOUT should be:
- """
- true
- true
- 0
- 0
- """
-
Scenario: Persistent environment
Given a WP install
And a session file:
`wp shell` allows you to evaluate PHP statements and expressions interactively, from within a WordPress environment. This means that you have access to all the functions, classes and globals that you would have access to from inside a WordPress plugin, for example.
-`$_` is a special variable that contains the result from the last evaluated statement.
-
You can split a statement over multiple lines by adding a `\` (backslash) before hitting Return.
If you type `history` and hit Return, WP-CLI will print the list of previously evaluated statements, which you can copy+paste into a PHP file.
if ( self::starts_with( self::non_expressions(), $line ) ) {
eval( $line );
} else {
- if ( self::starts_with( 'return', $line ) )
- $line = substr( $line, strlen( 'return' ) );
+ if ( !self::starts_with( 'return', $line ) )
+ $line = 'return ' . $line;
- $line = '$_ = ' . $line;
-
- eval( $line );
-
- \WP_CLI::print_value( var_export( $_, false ) );
+ \WP_CLI::print_value( var_export( eval( $line ), false ) );
}
}
}