7
7
8
8
namespace jn ;
9
9
10
- define ( 'WOOCOMMERCE_BETA_TESTER_PLUGIN_URL ' , 'https://github.com/woocommerce/woocommerce-beta-tester/archive/refs/heads/trunk.zip ' );
10
+ add_action (
11
+ 'jurassic_ninja_added_rest_api_endpoints ' ,
12
+ function () {
13
+ add_get_endpoint (
14
+ 'woocommerce-beta-tester/branches ' ,
15
+ function () {
16
+ $ manifest_url = 'https://betadownload.jetpack.me/woocommerce-branches.json ' ;
17
+ $ manifest = json_decode ( wp_remote_retrieve_body ( wp_remote_get ( $ manifest_url ) ) );
18
+
19
+ return $ manifest ;
20
+ }
21
+ );
22
+ }
23
+ );
11
24
12
25
add_action (
13
26
'jurassic_ninja_init ' ,
14
27
function () {
15
28
$ defaults = array (
16
29
'woocommerce-beta-tester ' => false ,
30
+ 'woocommerce-beta-tester-live-branch ' => false ,
17
31
);
18
32
19
33
add_action (
@@ -23,6 +37,13 @@ function ( &$app = null, $features, $domain ) use ( $defaults ) {
23
37
if ( $ features ['woocommerce-beta-tester ' ] ) {
24
38
debug ( '%s: Adding WooCommerce Beta Tester Plugin ' , $ domain );
25
39
add_woocommerce_beta_tester_plugin ();
40
+
41
+ if ( $ features ['woocommerce-beta-tester-live-branch ' ] ) {
42
+ $ branch = $ features ['woocommerce-beta-tester-live-branch ' ];
43
+
44
+ debug ( '%s: Adding WooCommerce Live Branch: %s ' , $ domain , $ branch );
45
+ add_woocommerce_live_branch ( $ branch );
46
+ }
26
47
}
27
48
},
28
49
10 ,
@@ -46,11 +67,12 @@ function ( $defaults ) {
46
67
function ( $ features , $ json_params ) {
47
68
if ( isset ( $ json_params ['woocommerce-beta-tester ' ] ) ) {
48
69
$ features ['woocommerce-beta-tester ' ] = $ json_params ['woocommerce-beta-tester ' ];
49
- // The WooCommerce Beta Tester Plugin works only when woocommerce is installed and active too.
50
- if ( $ features ['woocommerce-beta-tester ' ] ) {
51
- $ features ['woocommerce ' ] = true ;
52
- }
53
70
}
71
+
72
+ if ( isset ( $ json_params ['woocommerce-beta-tester-live-branch ' ] ) ) {
73
+ $ features ['woocommerce-beta-tester-live-branch ' ] = $ json_params ['woocommerce-beta-tester-live-branch ' ];
74
+ }
75
+
54
76
return $ features ;
55
77
},
56
78
10 ,
@@ -83,11 +105,78 @@ function ( $fields ) {
83
105
);
84
106
85
107
/**
86
- * Installs and activates WooCommerce Beta Tester plugin on the site.
108
+ * Get the WooCommerce Beta Tester Plugin Zip URL from Github.
109
+ */
110
+ function get_woocommerce_beta_tester_zip_url () {
111
+ $ url = 'https://api.github.com/repos/woocommerce/woocommerce/releases ' ;
112
+ $ response = wp_remote_get ( $ url );
113
+
114
+ if ( is_wp_error ( $ response ) ) {
115
+ return false ;
116
+ } else {
117
+ $ releases = json_decode ( wp_remote_retrieve_body ( $ response ), true );
118
+
119
+ $ filtered_releases = array_filter (
120
+ $ releases ,
121
+ function ( $ release ) {
122
+ return strpos ( $ release ['tag_name ' ], 'wc-beta-tester ' ) !== false ;
123
+ }
124
+ );
125
+
126
+ usort (
127
+ $ filtered_releases ,
128
+ function ( $ a , $ b ) {
129
+ return strtotime ( $ b ['created_at ' ] ) - strtotime ( $ a ['created_at ' ] );
130
+ }
131
+ );
132
+
133
+ $ latest_release = $ filtered_releases [0 ];
134
+
135
+ $ assets = $ latest_release ['assets ' ];
136
+ $ zip = array_filter (
137
+ $ assets ,
138
+ function ( $ asset ) {
139
+ return strpos ( $ asset ['name ' ], 'zip ' ) !== false ;
140
+ }
141
+ );
142
+
143
+ if ( count ( $ zip ) > 0 ) {
144
+ return $ zip [0 ]['browser_download_url ' ];
145
+ } else {
146
+ return false ;
147
+ }
148
+ }
149
+ }
150
+
151
+ /**
152
+ * Retrieve and install the WooCommerce Beta Tester Plugin.
153
+ *
154
+ * @throws Exception If the plugin zip file cannot be found.
87
155
*/
88
156
function add_woocommerce_beta_tester_plugin () {
89
- $ woocommerce_beta_tester_plugin_url = WOOCOMMERCE_BETA_TESTER_PLUGIN_URL ;
90
- $ cmd = "wp plugin install $ woocommerce_beta_tester_plugin_url --activate " ;
157
+ $ zip_url = get_woocommerce_beta_tester_zip_url ();
158
+
159
+ if ( $ zip_url ) {
160
+ $ cmd = "wp plugin install $ zip_url --activate " ;
161
+ add_filter (
162
+ 'jurassic_ninja_feature_command ' ,
163
+ function ( $ s ) use ( $ cmd ) {
164
+ return "$ s && $ cmd " ;
165
+ }
166
+ );
167
+ } else {
168
+ throw new Exception ( 'Could not find WooCommerce Beta Tester plugin zip file. ' );
169
+ }
170
+ }
171
+
172
+ /**
173
+ * Installs and activates a live branch of WooCommerce on the site.
174
+ *
175
+ * @param string $branch_name The name of the branch to install.
176
+ */
177
+ function add_woocommerce_live_branch ( $ branch_name ) {
178
+ $ cmd = "wp wc-beta-tester deactivate_woocommerce && wp wc-beta-tester install $ branch_name && wp wc-beta-tester activate $ branch_name " ;
179
+
91
180
add_filter (
92
181
'jurassic_ninja_feature_command ' ,
93
182
function ( $ s ) use ( $ cmd ) {
0 commit comments