Skip to content

Commit 99c2b74

Browse files
authored
Merge pull request #3 from web-lifter/codex/implement-github-action-for-packaging-and-release
Add GitHub release workflow and update updater
2 parents 2300325 + 3e202a2 commit 99c2b74

File tree

3 files changed

+57
-29
lines changed

3 files changed

+57
-29
lines changed

.github/workflows/release.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Build Release
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v*'
7+
8+
jobs:
9+
package:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
- name: Create zip
14+
run: |
15+
mkdir -p build
16+
zip -r build/woocommerce-utils.zip . -x '*.git*' '*.github*' 'build*'
17+
- name: Create GitHub release
18+
uses: softprops/action-gh-release@v1
19+
with:
20+
files: build/woocommerce-utils.zip
21+
env:
22+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

includes/class-wc-utils-updater.php

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,11 @@
99
class WC_Utils_Updater {
1010

1111
/**
12-
* GitHub repository raw URL.
12+
* GitHub repository slug (e.g. "owner/repo").
1313
*
1414
* @var string
1515
*/
16-
private $raw_url;
17-
18-
/**
19-
* GitHub repository zip URL.
20-
*
21-
* @var string
22-
*/
23-
private $zip_url;
16+
private $repo;
2417

2518
/**
2619
* Plugin file basename.
@@ -39,11 +32,10 @@ class WC_Utils_Updater {
3932
/**
4033
* Constructor.
4134
*/
42-
public function __construct( $repo_raw_url, $repo_zip_url, $plugin_basename, $version ) {
43-
$this->raw_url = trailingslashit( $repo_raw_url );
44-
$this->zip_url = $repo_zip_url;
35+
public function __construct( $repo_slug, $plugin_basename, $version ) {
36+
$this->repo = $repo_slug;
4537
$this->plugin_basename = $plugin_basename;
46-
$this->version = $version;
38+
$this->version = $version;
4739

4840
add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_for_update' ) );
4941
add_filter( 'plugins_api', array( $this, 'plugins_api' ), 10, 3 );
@@ -60,7 +52,8 @@ public function check_for_update( $transient ) {
6052
return $transient;
6153
}
6254

63-
$remote = wp_remote_get( $this->raw_url . 'woocommerce-utils.php' );
55+
$api_url = sprintf( 'https://api.github.com/repos/%s/releases/latest', $this->repo );
56+
$remote = wp_remote_get( $api_url, array( 'headers' => array( 'Accept' => 'application/vnd.github.v3+json' ) ) );
6457

6558
if ( is_wp_error( $remote ) ) {
6659
return $transient;
@@ -70,13 +63,13 @@ public function check_for_update( $transient ) {
7063
return $transient;
7164
}
7265

73-
$body = wp_remote_retrieve_body( $remote );
74-
75-
if ( ! preg_match( '/^\s*\*\s*Version:\s*(.*)$/mi', $body, $matches ) ) {
66+
$release = json_decode( wp_remote_retrieve_body( $remote ), true );
67+
if ( empty( $release['tag_name'] ) || empty( $release['zipball_url'] ) ) {
7668
return $transient;
7769
}
7870

79-
$remote_version = trim( $matches[1] );
71+
$remote_tag = ltrim( $release['tag_name'], 'v' );
72+
$remote_version = $remote_tag;
8073

8174
if ( version_compare( $this->version, $remote_version, '>=' ) ) {
8275
return $transient;
@@ -85,8 +78,8 @@ public function check_for_update( $transient ) {
8578
$plugin = new stdClass();
8679
$plugin->slug = dirname( $this->plugin_basename );
8780
$plugin->new_version = $remote_version;
88-
$plugin->url = $this->raw_url;
89-
$plugin->package = $this->zip_url;
81+
$plugin->url = $release['html_url'];
82+
$plugin->package = $release['zipball_url'];
9083

9184
$transient->response[ $this->plugin_basename ] = $plugin;
9285

@@ -110,7 +103,22 @@ public function plugins_api( $result, $action, $args ) {
110103
return $result;
111104
}
112105

113-
$remote = wp_remote_get( $this->raw_url . 'readme.txt' );
106+
$api_url = sprintf( 'https://api.github.com/repos/%s/releases/latest', $this->repo );
107+
$release = wp_remote_get( $api_url, array( 'headers' => array( 'Accept' => 'application/vnd.github.v3+json' ) ) );
108+
109+
if ( is_wp_error( $release ) || 200 !== wp_remote_retrieve_response_code( $release ) ) {
110+
return $result;
111+
}
112+
113+
$release_data = json_decode( wp_remote_retrieve_body( $release ), true );
114+
if ( empty( $release_data['tag_name'] ) || empty( $release_data['zipball_url'] ) ) {
115+
return $result;
116+
}
117+
118+
$tag = $release_data['tag_name'];
119+
$zip = $release_data['zipball_url'];
120+
121+
$remote = wp_remote_get( sprintf( 'https://raw.githubusercontent.com/%s/%s/readme.txt', $this->repo, $tag ) );
114122

115123
if ( is_wp_error( $remote ) || 200 !== wp_remote_retrieve_response_code( $remote ) ) {
116124
return $result;
@@ -121,11 +129,11 @@ public function plugins_api( $result, $action, $args ) {
121129
$info = new stdClass();
122130
$info->name = 'WooCommerce Utils';
123131
$info->slug = dirname( $this->plugin_basename );
124-
$info->version = $this->version;
132+
$info->version = ltrim( $tag, 'v' );
125133
$info->sections = array(
126134
'description' => $readme,
127135
);
128-
$info->download_link = $this->zip_url;
136+
$info->download_link = $zip;
129137

130138
return $info;
131139
}

woocommerce-utils.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@
2323
if ( ! defined( 'WC_UTILS_URL' ) ) {
2424
define( 'WC_UTILS_URL', plugin_dir_url( __FILE__ ) );
2525
}
26-
if ( ! defined( 'WC_UTILS_REPO_RAW' ) ) {
27-
define( 'WC_UTILS_REPO_RAW', 'https://raw.githubusercontent.com/web-lifter/woocommerce-utils/main/' );
28-
}
29-
if ( ! defined( 'WC_UTILS_REPO_ZIP' ) ) {
30-
define( 'WC_UTILS_REPO_ZIP', 'https://github.com/web-lifter/woocommerce-utils/archive/refs/heads/main.zip' );
26+
// GitHub repository slug used for updates.
27+
if ( ! defined( 'WC_UTILS_GITHUB_REPO' ) ) {
28+
define( 'WC_UTILS_GITHUB_REPO', 'web-lifter/woocommerce-utils' );
3129
}
3230

3331
/**
@@ -48,7 +46,7 @@ function wc_utils_init() {
4846
// Initialize classes.
4947
new WC_Utils_Admin();
5048
new WC_Utils_Features();
51-
new WC_Utils_Updater( WC_UTILS_REPO_RAW, WC_UTILS_REPO_ZIP, plugin_basename( __FILE__ ), WC_UTILS_VERSION );
49+
new WC_Utils_Updater( WC_UTILS_GITHUB_REPO, plugin_basename( __FILE__ ), WC_UTILS_VERSION );
5250
}
5351
add_action( 'plugins_loaded', 'wc_utils_init' );
5452

0 commit comments

Comments
 (0)