-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathEval_Command.php
More file actions
66 lines (57 loc) · 1.56 KB
/
Eval_Command.php
File metadata and controls
66 lines (57 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
use WP_CLI\Utils;
class Eval_Command extends WP_CLI_Command {
/**
* Executes arbitrary PHP code.
*
* Note: because code is executed within a method, global variables need
* to be explicitly globalized.
*
* ## OPTIONS
*
* <php-code>
* : The code to execute, as a string.
*
* [--skip-wordpress]
* : Execute code without loading WordPress.
*
* [--hook=<hook>]
* : Execute code after a specific WordPress hook has fired.
*
* ## EXAMPLES
*
* # Display WordPress content directory.
* $ wp eval 'echo WP_CONTENT_DIR;'
* /var/www/wordpress/wp-content
*
* # Generate a random number.
* $ 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 ) {
// @phpstan-ignore closure.unusedUse
$execute_closure = function () use ( $args, $assoc_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();
}
if ( ! $hook ) {
$execute_closure();
}
}
}