Skip to content

Commit 55f7a18

Browse files
committed
build: add GitHub updater + workflows and modify main plugin file
1 parent 643d5aa commit 55f7a18

File tree

5 files changed

+245
-0
lines changed

5 files changed

+245
-0
lines changed

.github/FUNDING.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# These are supported funding model platforms
2+
3+
github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
4+
patreon: # Replace with a single Patreon username
5+
open_collective: # Replace with a single Open Collective username
6+
ko_fi: # Replace with a single Ko-fi username
7+
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8+
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9+
liberapay: # Replace with a single Liberapay username
10+
issuehunt: # Replace with a single IssueHunt username
11+
otechie: # Replace with a single Otechie username
12+
custom: https://paypal.me/PerSoderlind # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Manually Build release zip
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
tag:
7+
description: 'Tag to deploy'
8+
required: true
9+
type: string
10+
default: ''
11+
12+
jobs:
13+
build:
14+
name: Build release zip
15+
runs-on: ubuntu-latest
16+
permissions:
17+
contents: write
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@v4
21+
22+
- name: Build plugin # Remove or modify this step as needed
23+
run: |
24+
composer install --no-dev
25+
26+
- name: Archive Release
27+
uses: thedoctor0/zip-release@b57d897cb5d60cb78b51a507f63fa184cfe35554 #0.7.6
28+
with:
29+
type: 'zip'
30+
filename: 'redis-queue-demo.zip'
31+
exclusions: '*.git* .editorconfig composer* *.md package.json package-lock.json'
32+
33+
- name: Release
34+
uses: softprops/action-gh-release@c95fe1489396fe8a9eb87c0abf8aa5b2ef267fda #v2
35+
env:
36+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
37+
with:
38+
files: redis-queue-demo.zip
39+
tag_name: ${{ github.event.inputs.tag }}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: On Release, Build release zip
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
jobs:
8+
build:
9+
name: Build release zip
10+
runs-on: ubuntu-latest
11+
permissions:
12+
contents: write
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v4
16+
17+
- name: Build plugin # Remove or modify this step as needed
18+
run: |
19+
composer install --no-dev
20+
21+
- name: Archive Release
22+
uses: thedoctor0/zip-release@b57d897cb5d60cb78b51a507f63fa184cfe35554 #0.7.6
23+
with:
24+
type: 'zip'
25+
filename: 'redis-queue-demo.zip'
26+
exclusions: '*.git* .editorconfig composer* *.md package.json package-lock.json'
27+
28+
- name: Release
29+
uses: softprops/action-gh-release@c95fe1489396fe8a9eb87c0abf8aa5b2ef267fda #v2
30+
env:
31+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
32+
with:
33+
files: redis-queue-demo.zip
34+
tag_name: ${{ github.event.release.tag_name }}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
namespace Soderlind\WordPress;
3+
4+
use YahnisElsts\PluginUpdateChecker\v5\PucFactory;
5+
6+
/**
7+
* Generic WordPress Plugin GitHub Updater
8+
*
9+
* A reusable class for handling WordPress plugin updates from GitHub repositories
10+
* using the plugin-update-checker library.
11+
*
12+
* @package Soderlind\WordPress
13+
* @version 1.0.0
14+
* @author Per Soderlind
15+
* @license GPL-2.0+
16+
*/
17+
class GitHub_Plugin_Updater {
18+
/**
19+
* @var string GitHub repository URL
20+
*/
21+
private $github_url;
22+
23+
/**
24+
* @var string Branch to check for updates
25+
*/
26+
private $branch;
27+
28+
/**
29+
* @var string Regex pattern to match the plugin zip file name
30+
*/
31+
private $name_regex;
32+
33+
/**
34+
* @var string The plugin slug
35+
*/
36+
private $plugin_slug;
37+
38+
/**
39+
* @var string The main plugin file path
40+
*/
41+
private $plugin_file;
42+
43+
/**
44+
* @var bool Whether to enable release assets
45+
*/
46+
private $enable_release_assets;
47+
48+
/**
49+
* Constructor
50+
*
51+
* @param array $config Configuration array with the following keys:
52+
* - github_url: GitHub repository URL (required)
53+
* - plugin_file: Main plugin file path (required)
54+
* - plugin_slug: Plugin slug for updates (required)
55+
* - branch: Branch to check for updates (default: 'main')
56+
* - name_regex: Regex pattern for zip file name (optional)
57+
* - enable_release_assets: Whether to enable release assets (default: true if name_regex provided)
58+
*/
59+
public function __construct( $config = array() ) {
60+
// Validate required parameters
61+
$required = array( 'github_url', 'plugin_file', 'plugin_slug' );
62+
foreach ( $required as $key ) {
63+
if ( empty( $config[ $key ] ) ) {
64+
throw new \InvalidArgumentException( "Required parameter '{$key}' is missing or empty." );
65+
}
66+
}
67+
68+
$this->github_url = $config[ 'github_url' ];
69+
$this->plugin_file = $config[ 'plugin_file' ];
70+
$this->plugin_slug = $config[ 'plugin_slug' ];
71+
$this->branch = isset( $config[ 'branch' ] ) ? $config[ 'branch' ] : 'main';
72+
$this->name_regex = isset( $config[ 'name_regex' ] ) ? $config[ 'name_regex' ] : '';
73+
$this->enable_release_assets = isset( $config[ 'enable_release_assets' ] )
74+
? $config[ 'enable_release_assets' ]
75+
: ! empty( $this->name_regex );
76+
77+
// Initialize the updater
78+
add_action( 'init', array( $this, 'setup_updater' ) );
79+
}
80+
81+
/**
82+
* Set up the update checker using GitHub integration
83+
*/
84+
public function setup_updater() {
85+
try {
86+
$update_checker = PucFactory::buildUpdateChecker(
87+
$this->github_url,
88+
$this->plugin_file,
89+
$this->plugin_slug
90+
);
91+
92+
$update_checker->setBranch( $this->branch );
93+
94+
// Enable release assets if configured
95+
if ( $this->enable_release_assets && ! empty( $this->name_regex ) ) {
96+
$update_checker->getVcsApi()->enableReleaseAssets( $this->name_regex );
97+
}
98+
99+
} catch (\Exception $e) {
100+
// Log error if WordPress debug is enabled
101+
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
102+
error_log( 'GitHub Plugin Updater Error: ' . $e->getMessage() );
103+
}
104+
}
105+
}
106+
107+
/**
108+
* Create updater instance with minimal configuration
109+
*
110+
* @param string $github_url GitHub repository URL
111+
* @param string $plugin_file Main plugin file path
112+
* @param string $plugin_slug Plugin slug
113+
* @param string $branch Branch name (default: 'main')
114+
*
115+
* @return GitHub_Plugin_Updater
116+
*/
117+
public static function create( $github_url, $plugin_file, $plugin_slug, $branch = 'main' ) {
118+
return new self( array(
119+
'github_url' => $github_url,
120+
'plugin_file' => $plugin_file,
121+
'plugin_slug' => $plugin_slug,
122+
'branch' => $branch,
123+
) );
124+
}
125+
126+
/**
127+
* Create updater instance for plugins with release assets
128+
*
129+
* @param string $github_url GitHub repository URL
130+
* @param string $plugin_file Main plugin file path
131+
* @param string $plugin_slug Plugin slug
132+
* @param string $name_regex Regex pattern for release assets
133+
* @param string $branch Branch name (default: 'main')
134+
*
135+
* @return GitHub_Plugin_Updater
136+
*/
137+
public static function create_with_assets( $github_url, $plugin_file, $plugin_slug, $name_regex, $branch = 'main' ) {
138+
return new self( array(
139+
'github_url' => $github_url,
140+
'plugin_file' => $plugin_file,
141+
'plugin_slug' => $plugin_slug,
142+
'branch' => $branch,
143+
'name_regex' => $name_regex,
144+
) );
145+
}
146+
}

redis-queue-demo.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,18 @@ public static function get_instance() {
9696
* @since 1.0.0
9797
*/
9898
private function __construct() {
99+
//Add plugin updater
100+
if ( ! class_exists( 'Soderlind\WordPress\GitHub_Plugin_Updater' ) ) {
101+
require_once REDIS_QUEUE_DEMO_PLUGIN_DIR . 'includes/class-plugin-updater.php';
102+
}
103+
104+
\Soderlind\WordPress\GitHub_Plugin_Updater::create_with_assets(
105+
'https://github.com/soderlind/redis-queue-demo',
106+
REDIS_QUEUE_DEMO_PLUGIN_FILE,
107+
'redis-queue-demo',
108+
'/redis-queue-demo\.zip/',
109+
'main'
110+
);
99111
$this->init_hooks();
100112
}
101113

@@ -140,6 +152,8 @@ public function init() {
140152
* @since 1.0.0
141153
*/
142154
private function load_dependencies() {
155+
156+
143157
// Load interfaces.
144158
require_once REDIS_QUEUE_DEMO_PLUGIN_DIR . 'includes/interfaces/interface-queue-job.php';
145159
require_once REDIS_QUEUE_DEMO_PLUGIN_DIR . 'includes/interfaces/interface-job-result.php';

0 commit comments

Comments
 (0)