Skip to content

Commit dafa9b4

Browse files
committed
Use yoda condition
1 parent fb9f5ba commit dafa9b4

File tree

5 files changed

+493
-1
lines changed

5 files changed

+493
-1
lines changed

features/back-compat.feature

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
Feature: Back-compat command translations
2+
3+
Scenario: Top level aliases
4+
When I run `wp sql`
5+
Then STDOUT should contain:
6+
"""
7+
wp db
8+
"""
9+
10+
Scenario: Meta commands
11+
When I run `wp post-meta`
12+
Then STDOUT should contain:
13+
"""
14+
wp post meta
15+
"""
16+
17+
Scenario: CLI aliases
18+
When I run `wp cli aliases`
19+
Then STDOUT should contain:
20+
"""
21+
wp cli alias list
22+
"""
23+
24+
Scenario: Core install admin name
25+
When I run `wp core install --admin_name=admin`
26+
Then STDOUT should contain:
27+
"""
28+
--admin_user=admin
29+
"""
30+
31+
Scenario: Core config
32+
When I run `wp core config`
33+
Then STDOUT should contain:
34+
"""
35+
wp config create
36+
"""
37+
38+
Scenario: Core language
39+
When I run `wp core language`
40+
Then STDOUT should contain:
41+
"""
42+
wp language core
43+
"""
44+
45+
Scenario: Checksum core
46+
When I run `wp checksum core`
47+
Then STDOUT should contain:
48+
"""
49+
wp core verify-checksums
50+
"""
51+
52+
Scenario: Checksum plugin
53+
When I run `wp checksum plugin`
54+
Then STDOUT should contain:
55+
"""
56+
wp plugin verify-checksums
57+
"""
58+
59+
Scenario: Site create with site_id
60+
When I run `wp site create --site_id=1`
61+
Then STDOUT should contain:
62+
"""
63+
--network_id=1
64+
"""
65+
66+
Scenario: Plugin update-all
67+
When I run `wp plugin update-all`
68+
Then STDOUT should contain:
69+
"""
70+
wp plugin update --all
71+
"""
72+
73+
Scenario: Transient delete-expired
74+
When I run `wp transient delete-expired`
75+
Then STDOUT should contain:
76+
"""
77+
wp transient delete --expired
78+
"""
79+
80+
Scenario: Transient delete-all
81+
When I run `wp transient delete-all`
82+
Then STDOUT should contain:
83+
"""
84+
wp transient delete --all
85+
"""
86+
87+
Scenario: Plugin scaffold
88+
When I run `wp plugin scaffold`
89+
Then STDOUT should contain:
90+
"""
91+
wp scaffold plugin
92+
"""
93+
94+
Scenario: Help flag
95+
When I run `wp some-command --help`
96+
Then STDOUT should contain:
97+
"""
98+
wp help some-command
99+
"""
100+
101+
Scenario: Post list ids
102+
When I run `wp post list --ids`
103+
Then STDOUT should contain:
104+
"""
105+
wp post list --format=ids
106+
"""
107+
108+
Scenario: JSON flag
109+
When I run `wp some-command --json`
110+
Then STDOUT should contain:
111+
"""
112+
wp some-command --format=json
113+
"""
114+
115+
Scenario: Version flag
116+
When I run `wp --version`
117+
Then STDOUT should contain:
118+
"""
119+
wp cli version
120+
"""
121+
122+
Scenario: Post URL
123+
When I run `wp post url 1 2 3`
124+
Then STDOUT should contain:
125+
"""
126+
wp post list --post__in=1,2,3 --post_type=any --orderby=post__in --field=url
127+
"""
128+
129+
Scenario: Config get
130+
When I run `wp config get --global=WP_DEBUG`
131+
Then STDOUT should contain:
132+
"""
133+
wp config get WP_DEBUG --type=variable
134+
"""
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class FeatureContext implements Context {
2+
// ...
3+
4+
/**
5+
* @BeforeScenario
6+
*/
7+
public function setUpScenario() {
8+
putenv( 'WP_CLI_TEST_BACK_COMPAT=1' );
9+
}
10+
11+
// ...
12+
}

php/WP_CLI/BackCompatParser.php

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
<?php
2+
3+
namespace WP_CLI;
4+
5+
class BackCompatParser {
6+
/**
7+
* Transparently convert deprecated syntaxes
8+
*
9+
* @param array $args
10+
* @param array $assoc_args
11+
* @return array
12+
*/
13+
public static function parse($args, $assoc_args) {
14+
$top_level_aliases = [
15+
'sql' => 'db',
16+
'blog' => 'site',
17+
];
18+
if ( count( $args ) > 0 ) {
19+
foreach ( $top_level_aliases as $old => $new ) {
20+
if ( $old === $args[0] ) {
21+
$args[0] = $new;
22+
break;
23+
}
24+
}
25+
}
26+
27+
// *-meta -> * meta
28+
if ( ! empty( $args ) && preg_match( '/(post|comment|user|network)-meta/', $args[0], $matches ) ) {
29+
array_shift( $args );
30+
array_unshift( $args, 'meta' );
31+
array_unshift( $args, $matches[1] );
32+
}
33+
34+
// cli aliases -> cli alias list
35+
if ( [ 'cli', 'aliases' ] === array_slice( $args, 0, 2 ) ) {
36+
list( $args[0], $args[1], $args[2] ) = [ 'cli', 'alias', 'list' ];
37+
}
38+
39+
// core (multsite-)install --admin_name= -> --admin_user=
40+
if ( count( $args ) > 0 && 'core' === $args[0] && isset( $assoc_args['admin_name'] ) ) {
41+
$assoc_args['admin_user'] = $assoc_args['admin_name'];
42+
unset( $assoc_args['admin_name'] );
43+
}
44+
45+
// core config -> config create
46+
if ( [ 'core', 'config' ] === array_slice( $args, 0, 2 ) ) {
47+
list( $args[0], $args[1] ) = [ 'config', 'create' ];
48+
}
49+
// core language -> language core
50+
if ( [ 'core', 'language' ] === array_slice( $args, 0, 2 ) ) {
51+
list( $args[0], $args[1] ) = [ 'language', 'core' ];
52+
}
53+
54+
// checksum core -> core verify-checksums
55+
if ( [ 'checksum', 'core' ] === array_slice( $args, 0, 2 ) ) {
56+
list( $args[0], $args[1] ) = [ 'core', 'verify-checksums' ];
57+
}
58+
59+
// checksum plugin -> plugin verify-checksums
60+
if ( [ 'checksum', 'plugin' ] === array_slice( $args, 0, 2 ) ) {
61+
list( $args[0], $args[1] ) = [ 'plugin', 'verify-checksums' ];
62+
}
63+
64+
// site create --site_id= -> site create --network_id=
65+
if ( count( $args ) >= 2 && 'site' === $args[0] && 'create' === $args[1] && isset( $assoc_args['site_id'] ) ) {
66+
$assoc_args['network_id'] = $assoc_args['site_id'];
67+
unset( $assoc_args['site_id'] );
68+
}
69+
70+
// {plugin|theme} update-all -> {plugin|theme} update --all
71+
if ( count( $args ) > 1 && in_array( $args[0], [ 'plugin', 'theme' ], true )
72+
&& 'update-all' === $args[1]
73+
) {
74+
$args[1] = 'update';
75+
$assoc_args['all'] = true;
76+
}
77+
78+
// transient delete-expired -> transient delete --expired
79+
if ( count( $args ) > 1 && 'transient' === $args[0] && 'delete-expired' === $args[1] ) {
80+
$args[1] = 'delete';
81+
$assoc_args['expired'] = true;
82+
}
83+
84+
// transient delete-all -> transient delete --all
85+
if ( count( $args ) > 1 && 'transient' === $args[0] && 'delete-all' === $args[1] ) {
86+
$args[1] = 'delete';
87+
$assoc_args['all'] = true;
88+
}
89+
90+
// plugin scaffold -> scaffold plugin
91+
if ( [ 'plugin', 'scaffold' ] === array_slice( $args, 0, 2 ) ) {
92+
list( $args[0], $args[1] ) = [ $args[1], $args[0] ];
93+
}
94+
95+
// foo --help -> help foo
96+
if ( isset( $assoc_args['help'] ) ) {
97+
array_unshift( $args, 'help' );
98+
unset( $assoc_args['help'] );
99+
}
100+
101+
// {post|user} list --ids -> {post|user} list --format=ids
102+
if ( count( $args ) > 1 && in_array( $args[0], [ 'post', 'user' ], true )
103+
&& 'list' === $args[1]
104+
&& isset( $assoc_args['ids'] )
105+
) {
106+
$assoc_args['format'] = 'ids';
107+
unset( $assoc_args['ids'] );
108+
}
109+
110+
// --json -> --format=json
111+
if ( isset( $assoc_args['json'] ) ) {
112+
$assoc_args['format'] = 'json';
113+
unset( $assoc_args['json'] );
114+
}
115+
116+
// --{version|info} -> cli {version|info}
117+
if ( empty( $args ) ) {
118+
$special_flags = [ 'version', 'info' ];
119+
foreach ( $special_flags as $key ) {
120+
if ( isset( $assoc_args[ $key ] ) ) {
121+
$args = [ 'cli', $key ];
122+
unset( $assoc_args[ $key ] );
123+
break;
124+
}
125+
}
126+
}
127+
128+
// (post|comment|site|term) url --> (post|comment|site|term) list --*__in --field=url
129+
if ( count( $args ) >= 2 && in_array( $args[0], [ 'post', 'comment', 'site', 'term' ], true ) && 'url' === $args[1] ) {
130+
switch ( $args[0] ) {
131+
case 'post':
132+
$post_ids = array_slice( $args, 2 );
133+
$args = [ 'post', 'list' ];
134+
$assoc_args['post__in'] = implode( ',', $post_ids );
135+
$assoc_args['post_type'] = 'any';
136+
$assoc_args['orderby'] = 'post__in';
137+
$assoc_args['field'] = 'url';
138+
break;
139+
case 'comment':
140+
$comment_ids = array_slice( $args, 2 );
141+
$args = [ 'comment', 'list' ];
142+
$assoc_args['comment__in'] = implode( ',', $comment_ids );
143+
$assoc_args['orderby'] = 'comment__in';
144+
$assoc_args['field'] = 'url';
145+
break;
146+
case 'site':
147+
$site_ids = array_slice( $args, 2 );
148+
$args = [ 'site', 'list' ];
149+
$assoc_args['site__in'] = implode( ',', $site_ids );
150+
$assoc_args['field'] = 'url';
151+
break;
152+
case 'term':
153+
$taxonomy = '';
154+
if ( isset( $args[2] ) ) {
155+
$taxonomy = $args[2];
156+
}
157+
$term_ids = array_slice( $args, 3 );
158+
$args = [ 'term', 'list', $taxonomy ];
159+
$assoc_args['include'] = implode( ',', $term_ids );
160+
$assoc_args['orderby'] = 'include';
161+
$assoc_args['field'] = 'url';
162+
break;
163+
}
164+
}
165+
166+
// config get --[global|constant]=<global|constant> --> config get <name> --type=constant|variable
167+
// config get --> config list
168+
if ( count( $args ) === 2
169+
&& 'config' === $args[0]
170+
&& 'get' === $args[1] ) {
171+
if ( isset( $assoc_args['global'] ) ) {
172+
$name = $assoc_args['global'];
173+
$type = 'variable';
174+
unset( $assoc_args['global'] );
175+
} elseif ( isset( $assoc_args['constant'] ) ) {
176+
$name = $assoc_args['constant'];
177+
$type = 'constant';
178+
unset( $assoc_args['constant'] );
179+
}
180+
if ( ! empty( $name ) && ! empty( $type ) ) {
181+
$args[] = $name;
182+
$assoc_args['type'] = $type;
183+
} else {
184+
// We had a 'config get' without a '<name>', so assume 'list' was wanted.
185+
$args[1] = 'list';
186+
}
187+
}
188+
189+
return [ $args, $assoc_args ];
190+
}
191+
}

php/WP_CLI/Bootstrap/CheckRoot.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class CheckRoot implements BootstrapStep {
2323
*/
2424
public function process( BootstrapState $state ) {
2525
$config = $state->getValue( 'config', [] );
26-
if ( array_key_exists( 'allow-root', $config ) && $config['allow-root'] === true ) {
26+
if ( array_key_exists( 'allow-root', $config ) && true === $config['allow-root'] ) {
2727
// They're aware of the risks and set a flag to allow root.
2828
return $state;
2929
}

0 commit comments

Comments
 (0)