|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
if ( defined( 'WP_CLI' ) && WP_CLI ) { |
|
4
|
|
|
/** |
|
5
|
|
|
* Control your local Jetpack Beta Tester plugin. |
|
6
|
|
|
*/ |
|
7
|
|
|
class JetpackBetaCliCommand extends WP_CLI_Command { |
|
8
|
|
|
/** |
|
9
|
|
|
* Activate a branch version |
|
10
|
|
|
* |
|
11
|
|
|
* ## OPTIONS |
|
12
|
|
|
* |
|
13
|
|
|
* activate master: Get a version of the master branch built every 15 minutes |
|
14
|
|
|
* activate stable: Get the latest stable version of Jetpack |
|
15
|
|
|
* activate branch_name: Get a version of PR. PR must be built and unit-tested before it become availabe |
|
16
|
|
|
* list: Get list of available jetpack branches to install |
|
17
|
|
|
* |
|
18
|
|
|
* ## EXAMPLES |
|
19
|
|
|
* |
|
20
|
|
|
* wp jetpack-beta branch activate master |
|
21
|
|
|
* wp jetpack-beta branch activate stable |
|
22
|
|
|
* wp jetpack-beta branch activate branch_name |
|
23
|
|
|
* wp jetpack-beta branch list |
|
24
|
|
|
* |
|
25
|
|
|
*/ |
|
26
|
|
|
public function branch( $args ) { |
|
27
|
|
|
|
|
28
|
|
|
$this->validation_checks( $args ); |
|
29
|
|
|
|
|
30
|
|
|
if ( 'list' === $args[0] ) { |
|
31
|
|
|
return $this->branches_list(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$branches = array( 'master', 'stable', 'rc' ); |
|
35
|
|
|
|
|
36
|
|
|
if ( in_array( $args[1], $branches)) { |
|
37
|
|
|
return $this->install_jetpack( $args[1], $args[1] ); |
|
38
|
|
|
} else { |
|
39
|
|
|
$branch_name = str_replace( '/', '_', $args[1] ); |
|
40
|
|
|
$url = Jetpack_Beta::get_install_url( $branch_name, 'pr' ); |
|
41
|
|
|
if ( $url === null ) { |
|
42
|
|
|
return WP_CLI::error( __( 'Invalid branch name. Try `wp jetpack-beta branch list` for list of available branches', 'jetpack' ) ); |
|
43
|
|
|
} |
|
44
|
|
|
return $this->install_jetpack( $branch_name, 'pr' ); |
|
45
|
|
|
} |
|
46
|
|
|
return WP_CLI::error( __( 'Unrecognized branch version. ', 'jetpack' ) ); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
private function validation_checks( $args ) { |
|
50
|
|
|
if ( is_multisite() && ! is_main_site() ) { |
|
51
|
|
|
return WP_CLI::error( __( 'Secondary sites in multisite instalations are not supported', 'jetpack' ) ); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if ( empty( $args ) ) { |
|
55
|
|
|
return WP_CLI::error( __( 'Specify subcommand. "activate" and "list" subcommands are supported', 'jetpack' ) ); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
View Code Duplication |
if ( 'activate' !== $args[0] && 'list' !== $args[0] ) { |
|
59
|
|
|
return WP_CLI::error( __( 'Only "activate" and "list" subcommands are supported', 'jetpack' ) ); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
View Code Duplication |
if ( 'activate' === $args[0] && empty( $args[1] ) ) { |
|
63
|
|
|
return WP_CLI::error( __( 'Specify branch name. Try `wp jetpack-beta branch list` for list of available branches', 'jetpack' ) ); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
private function install_jetpack( $branch, $section ) { |
|
68
|
|
|
WP_CLI::line( 'Activating ' . $branch . ' branch...' ); |
|
69
|
|
|
|
|
70
|
|
|
$result = Jetpack_Beta::install_and_activate( $branch, $section ); |
|
71
|
|
|
if ( is_wp_error( $result ) ) { |
|
72
|
|
|
return WP_CLI::error( __( 'Error', 'jetpack' ) . $result->get_error_message() ); |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
return WP_CLI::success( __( 'Jetpack is currently on ' . $branch . ' branch', 'jetpack-beta' ) ); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
private function branches_list() { |
|
78
|
|
|
$manifest = Jetpack_Beta::get_beta_manifest(); |
|
79
|
|
|
$jetpack_beta_active = get_option( 'jetpack_beta_active' ); |
|
80
|
|
|
$current_branch = str_replace( '_', '/', $jetpack_beta_active[0] ); |
|
81
|
|
|
$branches = array( 'stable', 'master', 'rc' ); |
|
82
|
|
|
foreach( get_object_vars( $manifest->pr ) as $key ) { |
|
83
|
|
|
$branches[] = $key->branch; |
|
84
|
|
|
} |
|
85
|
|
|
sort($branches); |
|
86
|
|
|
WP_CLI::line( 'Available branches: '); |
|
87
|
|
|
foreach( $branches as $branch ) { |
|
88
|
|
|
WP_CLI::line( $current_branch == $branch ? '* ' . $branch : ' ' . $branch ); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
WP_CLI::add_command( 'jetpack-beta', 'JetpackBetaCliCommand' ); |
|
94
|
|
|
} |
|
95
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return,dieorexitstatements that have been added for debug purposes.In the above example, the last
return falsewill never be executed, because a return statement has already been met in every possible execution path.