10
10
add_action (
11
11
'jurassic_ninja_added_rest_api_endpoints ' ,
12
12
function () {
13
+ // Old endpoint, deprecated.
13
14
add_get_endpoint (
14
15
'available-jetpack-built-branches ' ,
15
16
function () {
@@ -19,6 +20,33 @@ function () {
19
20
return $ output ;
20
21
}
21
22
);
23
+
24
+ // New endpoints.
25
+ add_get_endpoint (
26
+ 'jetpack-beta/plugins ' ,
27
+ function () {
28
+ $ manifest_url = 'https://betadownload.jetpack.me/plugins.json ' ;
29
+ $ manifest = json_decode ( wp_remote_retrieve_body ( wp_remote_get ( $ manifest_url ) ) );
30
+ $ output = $ manifest ;
31
+ return $ output ;
32
+ }
33
+ );
34
+
35
+ add_get_endpoint (
36
+ 'jetpack-beta/plugins/(?P<plugin>[a-zA-Z0-9-]+)/branches ' ,
37
+ function ( $ data ) {
38
+ $ plugin = $ data ['plugin ' ];
39
+ $ manifest_url = 'https://betadownload.jetpack.me/plugins.json ' ;
40
+ $ manifest = json_decode ( wp_remote_retrieve_body ( wp_remote_get ( $ manifest_url ) ) );
41
+ if ( ! isset ( $ manifest ->{$ plugin }->manifest_url ) ) {
42
+ return new \WP_Error ( 'unknown_plugin ' , 'Plugin not known. ' , array ( 'status ' => 404 ) );
43
+ }
44
+ $ manifest_url = $ manifest ->{$ plugin }->manifest_url ;
45
+ $ manifest = json_decode ( wp_remote_retrieve_body ( wp_remote_get ( $ manifest_url ) ) );
46
+ $ output = $ manifest ;
47
+ return $ output ;
48
+ }
49
+ );
22
50
}
23
51
);
24
52
@@ -28,6 +56,7 @@ function () {
28
56
$ defaults = array (
29
57
'jetpack-beta ' => false ,
30
58
'branch ' => false ,
59
+ 'branches ' => array (),
31
60
);
32
61
33
62
add_action (
@@ -39,9 +68,18 @@ function ( &$app = null, $features, $domain ) use ( $defaults ) {
39
68
add_jetpack_beta_plugin ();
40
69
}
41
70
42
- if ( $ features ['branch ' ] ) {
43
- debug ( '%s: Activating Jetpack %s branch in Beta plugin ' , $ domain , $ features ['branch ' ] );
44
- activate_jetpack_branch ( $ features ['branch ' ] );
71
+ // Deprecated parameter.
72
+ if ( $ features ['branch ' ] && ! $ features ['branches ' ] ) {
73
+ $ features ['branches ' ] = array ( 'jetpack ' => $ features ['branch ' ] );
74
+ }
75
+
76
+ if ( $ features ['branches ' ] ) {
77
+ foreach ( $ features ['branches ' ] as $ plugin_name => $ branch_name ) {
78
+ if ( $ branch_name ) {
79
+ debug ( '%s: Activating %s plugin %s branch in Beta plugin ' , $ domain , $ plugin_name , $ branch_name );
80
+ activate_jetpack_branch ( $ plugin_name , $ branch_name );
81
+ }
82
+ }
45
83
}
46
84
},
47
85
10 ,
@@ -63,23 +101,41 @@ function ( $defaults ) {
63
101
add_filter (
64
102
'jurassic_ninja_rest_create_request_features ' ,
65
103
function ( $ features , $ json_params ) {
66
- $ branch = isset ( $ json_params ['branch ' ] ) && $ json_params ['branch ' ] ? $ json_params ['branch ' ] : 'master ' ;
67
104
if ( isset ( $ json_params ['jetpack-beta ' ] ) && $ json_params ['jetpack-beta ' ] ) {
68
- $ url = get_jetpack_beta_url ( $ branch );
105
+
106
+ // Deprecated parameter.
107
+ if ( isset ( $ json_params ['branch ' ] ) && empty ( $ json_params ['branches ' ] ) ) {
108
+ $ json_params ['branches ' ] = array ( 'jetpack ' => $ json_params ['branch ' ] );
109
+ }
110
+
111
+ // Default.
112
+ if ( ! isset ( $ json_params ['branches ' ] ) ) {
113
+ $ json_params ['branches ' ] = array ( 'jetpack ' => 'master ' );
114
+ }
115
+
69
116
70
117
$ error = null ;
71
- if ( null === $ url ) {
72
- $ error = new \WP_Error (
73
- 'failed_to_launch_site_with_branch ' ,
74
- /* translators: is a GitHub branch name */
75
- sprintf ( esc_html__ ( 'Invalid branch name or not ready yet: %s ' , 'jurassic-ninja ' ), $ branch ),
76
- array (
77
- 'status ' => 400 ,
78
- )
79
- );
118
+ foreach ( $ json_params ['branches ' ] as $ plugin_name => $ branch_name ) {
119
+ if ( ! $ branch_name ) {
120
+ continue ;
121
+ }
122
+
123
+ $ url = get_jetpack_beta_url ( $ plugin_name , $ branch_name );
124
+ if ( null === $ url ) {
125
+ $ error = new \WP_Error (
126
+ 'failed_to_launch_site_with_branch ' ,
127
+ /* translators: %1$s: Plugin slug. %2$s: GitHub branch name */
128
+ sprintf ( esc_html__ ( 'Invalid branch name for %1$s or not ready yet: %2$s ' , 'jurassic-ninja ' ), $ plugin_name , $ branch_name ),
129
+ array (
130
+ 'status ' => 400 ,
131
+ )
132
+ );
133
+ break ;
134
+ }
135
+ $ features ['branches ' ][ $ plugin_name ] = $ branch_name ;
80
136
}
81
137
$ features ['jetpack-beta ' ] = null === $ error ? $ json_params ['jetpack-beta ' ] : $ error ;
82
- $ features ['branch ' ] = $ branch ;
138
+ $ features ['branch ' ] = isset ( $ features [ ' branches ' ][ ' jetpack ' ] ) ? $ features [ ' branches ' ][ ' jetpack ' ] : null ; // Deprecated.
83
139
}
84
140
85
141
return $ features ;
@@ -129,10 +185,11 @@ function ( $s ) use ( $cmd ) {
129
185
/**
130
186
* Activates jetpack branch in Beta plugin
131
187
*
188
+ * @param string $plugin_name Plugin name.
132
189
* @param string $branch_name Branch name.
133
190
*/
134
- function activate_jetpack_branch ( $ branch_name ) {
135
- $ cmd = "wp jetpack-beta branch activate $ branch_name " ;
191
+ function activate_jetpack_branch ( $ plugin_name , $ branch_name ) {
192
+ $ cmd = "wp jetpack-beta activate $ plugin_name $ branch_name " ;
136
193
add_filter (
137
194
'jurassic_ninja_feature_command ' ,
138
195
function ( $ s ) use ( $ cmd ) {
@@ -144,13 +201,20 @@ function ( $s ) use ( $cmd ) {
144
201
/**
145
202
* Get URL for beta download.
146
203
*
204
+ * @param string $plugin_name Plugin name.
147
205
* @param string $branch_name Branch name.
148
206
*
149
- * @return string Download URL.
207
+ * @return string|null Download URL.
150
208
*/
151
- function get_jetpack_beta_url ( $ branch_name ) {
209
+ function get_jetpack_beta_url ( $ plugin_name , $ branch_name ) {
210
+ $ manifest_url = 'https://betadownload.jetpack.me/plugins.json ' ;
211
+ $ manifest = json_decode ( wp_remote_retrieve_body ( wp_remote_get ( $ manifest_url ) ) );
212
+ if ( ! isset ( $ manifest ->{$ plugin_name }->manifest_url ) ) {
213
+ return null ;
214
+ }
215
+
152
216
$ branch_name = str_replace ( '/ ' , '_ ' , $ branch_name );
153
- $ manifest_url = ' https://betadownload.jetpack.me/jetpack-branches.json ' ;
217
+ $ manifest_url = $ manifest ->{ $ plugin_name }-> manifest_url ;
154
218
$ manifest = json_decode ( wp_remote_retrieve_body ( wp_remote_get ( $ manifest_url ) ) );
155
219
156
220
if ( ( 'rc ' === $ branch_name || 'master ' === $ branch_name ) && isset ( $ manifest ->{$ branch_name }->download_url ) ) {
@@ -160,4 +224,6 @@ function get_jetpack_beta_url( $branch_name ) {
160
224
if ( isset ( $ manifest ->pr ->{$ branch_name }->download_url ) ) {
161
225
return $ manifest ->pr ->{$ branch_name }->download_url ;
162
226
}
227
+
228
+ return null ;
163
229
}
0 commit comments