1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Jetpack Beta Tester CLI controls |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-beta |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Automattic\JetpackBeta; |
9
|
|
|
|
10
|
|
|
use WP_CLI; |
11
|
|
|
use WP_CLI_Command; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Control your local Jetpack Beta Tester plugin. |
15
|
|
|
*/ |
16
|
|
|
class CliCommand extends WP_CLI_Command { |
17
|
|
|
/** |
18
|
|
|
* Activate a branch version |
19
|
|
|
* |
20
|
|
|
* ## OPTIONS |
21
|
|
|
* |
22
|
|
|
* activate master: Get a version of the master branch built every 15 minutes |
23
|
|
|
* activate stable: Get the latest stable version of Jetpack |
24
|
|
|
* activate branch_name: Get a version of PR. PR must be built and unit-tested before it become availabe |
25
|
|
|
* list: Get list of available jetpack branches to install |
26
|
|
|
* |
27
|
|
|
* ## EXAMPLES |
28
|
|
|
* |
29
|
|
|
* wp jetpack-beta branch activate master |
30
|
|
|
* wp jetpack-beta branch activate stable |
31
|
|
|
* wp jetpack-beta branch activate branch_name |
32
|
|
|
* wp jetpack-beta branch list |
33
|
|
|
* |
34
|
|
|
* @param array $args arguments passed to CLI, per the examples above. |
35
|
|
|
*/ |
36
|
|
|
public function branch( $args ) { |
37
|
|
|
|
38
|
|
|
$this->validation_checks( $args ); |
39
|
|
|
|
40
|
|
|
if ( 'list' === $args[0] ) { |
41
|
|
|
return $this->branches_list(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$branches = array( 'master', 'stable', 'rc' ); |
45
|
|
|
|
46
|
|
|
if ( in_array( $args[1], $branches, true ) ) { |
47
|
|
|
return $this->install_jetpack( $args[1], $args[1] ); |
48
|
|
|
} else { |
49
|
|
|
$branch_name = str_replace( '/', '_', $args[1] ); |
50
|
|
|
$url = Utils::get_install_url( $branch_name, 'pr' ); |
51
|
|
|
if ( null === $url ) { |
52
|
|
|
return WP_CLI::error( __( 'Invalid branch name. Try `wp jetpack-beta branch list` for list of available branches', 'jetpack-beta' ) ); |
53
|
|
|
} |
54
|
|
|
return $this->install_jetpack( $branch_name, 'pr' ); |
55
|
|
|
} |
56
|
|
|
return WP_CLI::error( __( 'Unrecognized branch version. ', 'jetpack-beta' ) ); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Validate that we can switch branches. |
61
|
|
|
* |
62
|
|
|
* @param array $args arguments passed to CLI. |
63
|
|
|
*/ |
64
|
|
|
private function validation_checks( $args ) { |
65
|
|
|
|
66
|
|
|
if ( is_multisite() && ! is_main_site() ) { |
67
|
|
|
return WP_CLI::error( __( 'Secondary sites in multisite instalations are not supported', 'jetpack-beta' ) ); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ( empty( $args ) ) { |
71
|
|
|
return WP_CLI::error( __( 'Specify subcommand. "activate" and "list" subcommands are supported', 'jetpack-beta' ) ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
View Code Duplication |
if ( 'activate' !== $args[0] && 'list' !== $args[0] ) { |
75
|
|
|
return WP_CLI::error( __( 'Only "activate" and "list" subcommands are supported', 'jetpack-beta' ) ); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
View Code Duplication |
if ( 'activate' === $args[0] && empty( $args[1] ) ) { |
79
|
|
|
return WP_CLI::error( __( 'Specify branch name. Try `wp jetpack-beta branch list` for list of available branches', 'jetpack-beta' ) ); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Install Jetpack using selected branch. |
85
|
|
|
* |
86
|
|
|
* @param array $branch is the selected branch. |
87
|
|
|
* @param array $section what we're specifically installing (PR, master, stable, etc). |
88
|
|
|
*/ |
89
|
|
|
private function install_jetpack( $branch, $section ) { |
90
|
|
|
|
91
|
|
|
WP_CLI::line( 'Activating ' . $branch . ' branch...' ); |
92
|
|
|
|
93
|
|
|
$result = Utils::install_and_activate( $branch, $section ); |
|
|
|
|
94
|
|
|
if ( is_wp_error( $result ) ) { |
95
|
|
|
return WP_CLI::error( __( 'Error', 'jetpack-beta' ) . $result->get_error_message() ); |
96
|
|
|
} |
97
|
|
|
// translators: $branch is what branch we've switched to. |
98
|
|
|
return WP_CLI::success( printf( esc_html__( 'Jetpack is currently on %s branch', 'jetpack-beta' ), esc_html( $branch ) ) ); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Display list of branches. |
103
|
|
|
*/ |
104
|
|
|
private function branches_list() { |
105
|
|
|
$manifest = Utils::get_beta_manifest(); |
106
|
|
|
$jetpack_beta_active = get_option( 'jetpack_beta_active' ); |
107
|
|
|
$current_branch = str_replace( '_', '/', $jetpack_beta_active[0] ); |
108
|
|
|
$branches = array( 'stable', 'master', 'rc' ); |
109
|
|
|
foreach ( get_object_vars( $manifest->pr ) as $key ) { |
110
|
|
|
$branches[] = $key->branch; |
111
|
|
|
} |
112
|
|
|
sort( $branches ); |
113
|
|
|
WP_CLI::line( 'Available branches: ' ); |
114
|
|
|
foreach ( $branches as $branch ) { |
115
|
|
|
WP_CLI::line( $current_branch === $branch ? '* ' . $branch : ' ' . $branch ); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
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
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.