Skip to content
This repository was archived by the owner on Sep 6, 2024. It is now read-only.

Commit 3adb6d9

Browse files
authored
Merge pull request #274 from Automattic/dev/integrate-woocommerce-beta-tester
Add WooCommerce Beta Tester Live Branches Integration
2 parents 17c7a05 + 0bb9942 commit 3adb6d9

File tree

4 files changed

+158
-10
lines changed

4 files changed

+158
-10
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"php": "~7.1 || 8.*"
66
},
77
"require-dev": {
8-
"automattic/jetpack-codesniffer": "^2.0",
8+
"automattic/jetpack-codesniffer": "2.6.0",
99
"brain/monkey": "^2.6",
1010
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.0",
1111
"phpunit/phpunit": "7.* || 8.*",

features/woocommerce-beta-tester.php

+97-8
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,27 @@
77

88
namespace jn;
99

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+
);
1124

1225
add_action(
1326
'jurassic_ninja_init',
1427
function () {
1528
$defaults = array(
1629
'woocommerce-beta-tester' => false,
30+
'woocommerce-beta-tester-live-branch' => false,
1731
);
1832

1933
add_action(
@@ -23,6 +37,13 @@ function ( &$app = null, $features, $domain ) use ( $defaults ) {
2337
if ( $features['woocommerce-beta-tester'] ) {
2438
debug( '%s: Adding WooCommerce Beta Tester Plugin', $domain );
2539
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+
}
2647
}
2748
},
2849
10,
@@ -46,11 +67,12 @@ function ( $defaults ) {
4667
function ( $features, $json_params ) {
4768
if ( isset( $json_params['woocommerce-beta-tester'] ) ) {
4869
$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-
}
5370
}
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+
5476
return $features;
5577
},
5678
10,
@@ -83,11 +105,78 @@ function ( $fields ) {
83105
);
84106

85107
/**
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.
87155
*/
88156
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+
91180
add_filter(
92181
'jurassic_ninja_feature_command',
93182
function ( $s ) use ( $cmd ) {

jurassic.ninja.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Plugin Name: Jurassic Ninja
44
* Description: Launch ephemeral instances of WordPress + Jetpack using ServerPilot and an Ubuntu Box.
5-
* Version: 5.20
5+
* Version: 5.21
66
* Author: Automattic
77
*
88
* @package jurassic-ninja

jurassicninja.js

+59
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ function init() {
1919

2020
if ( isSpecialOpsPage() ) {
2121
hookJetpackBranches();
22+
hookWooCommerceBetaBranches();
2223
hookAvailableLanguages();
2324
jQuery( '[data-is-create-button]' ).click( function () {
2425
const $this = jQuery( this );
@@ -319,6 +320,18 @@ function favicon_update_colour( colour ) {
319320
}
320321
}
321322

323+
function getAvailableWooCommerceBetaBranches() {
324+
return fetch( '/wp-json/jurassic.ninja/woocommerce-beta-tester/branches' )
325+
.then( response => response.json() )
326+
.then( body => {
327+
if (body.data && body.data.pr) {
328+
return Object.values(body.data.pr).sort((a, b) => a.branch.localeCompare(b.branch));
329+
}
330+
331+
return [];
332+
} );
333+
}
334+
322335
function getAvailableJetpackBetaPlugins() {
323336
return fetch( '/wp-json/jurassic.ninja/jetpack-beta/plugins')
324337
.then( response => response.json() )
@@ -330,6 +343,7 @@ function getAvailableJetpackBetaPlugins() {
330343
return plugins;
331344
} );
332345
}
346+
333347
function getAvailableJetpackBetaBranches( plugin_slug ) {
334348
return fetch( '/wp-json/jurassic.ninja/jetpack-beta/plugins/' + ( plugin_slug || 'jetpack' ) + '/branches' )
335349
.then( response => response.json() )
@@ -463,3 +477,48 @@ function hookJetpackBranches() {
463477
} );
464478
} );
465479
}
480+
481+
function hookWooCommerceBetaBranches() {
482+
getAvailableWooCommerceBetaBranches().then( branches => {
483+
const wooBetaCheckBox = document.querySelector('[data-feature=woocommerce-beta-tester]');
484+
485+
if (branches.length && wooBetaCheckBox) {
486+
const checkboxContainer = wooBetaCheckBox.closest('.checkbox');
487+
const wooBetaSelect = document.createElement('input');
488+
489+
wooBetaSelect.id = 'woocommerce_beta_branch';
490+
wooBetaSelect.name = 'woocommerce_beta_branch';
491+
wooBetaSelect.className = 'form-control';
492+
wooBetaSelect.setAttribute('role' , 'search') ;
493+
wooBetaSelect.setAttribute('list', 'woocommerce_branches');
494+
wooBetaSelect.setAttribute('type', 'text');
495+
wooBetaSelect.setAttribute('Placeholder', 'Select a branch to enable');
496+
wooBetaSelect.setAttribute('data-feature', 'woocommerce-beta-tester-live-branch');
497+
wooBetaSelect.style.display = "none";
498+
499+
const datalist = document.createElement('datalist');
500+
datalist.id = 'woocommerce_branches';
501+
502+
branches.forEach( branch => {
503+
const option = document.createElement('option');
504+
option.innerHTML = branch.branch;
505+
option.value = branch.branch;
506+
datalist.appendChild(option);
507+
} );
508+
509+
checkboxContainer.appendChild(wooBetaSelect);
510+
checkboxContainer.appendChild(datalist);
511+
512+
// Toggle display of the select list
513+
wooBetaCheckBox.addEventListener('change', function() {
514+
if (this.checked) {
515+
wooBetaSelect.style.display = "block";
516+
} else {
517+
wooBetaSelect.style.display = "none";
518+
}
519+
});
520+
}
521+
});
522+
}
523+
524+

0 commit comments

Comments
 (0)