Skip to content

Commit efd06cb

Browse files
authored
Merge pull request #16 from soderlind/genereic/pluginupdater
Add GitHub updater integration
2 parents 97e8003 + 576bcf0 commit efd06cb

5 files changed

Lines changed: 170 additions & 20 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to the Multisite Exporter plugin will be documented in this
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [1.2.1] - 2025-05-14
8+
### Added
9+
- Use generic [WordPress Plugin GitHub Updater](https://github.com/soderlind/wordpress-plugin-gitHub-updater?tab=readme-ov-file#wordpress-plugin-github-updater)
10+
711
## [1.2.0] - 2025-05-14
812
### Added
913
- WP CLI command for exporting content from multisite installations
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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+
* @link https://github.com/soderlind/wordpress-plugin-gitHub-updater
14+
* @version 1.0.0
15+
* @author Per Soderlind
16+
* @license GPL-2.0+
17+
*/
18+
class GitHub_Plugin_Updater {
19+
/**
20+
* @var string GitHub repository URL
21+
*/
22+
private $github_url;
23+
24+
/**
25+
* @var string Branch to check for updates
26+
*/
27+
private $branch;
28+
29+
/**
30+
* @var string Regex pattern to match the plugin zip file name
31+
*/
32+
private $name_regex;
33+
34+
/**
35+
* @var string The plugin slug
36+
*/
37+
private $plugin_slug;
38+
39+
/**
40+
* @var string The main plugin file path
41+
*/
42+
private $plugin_file;
43+
44+
/**
45+
* @var bool Whether to enable release assets
46+
*/
47+
private $enable_release_assets;
48+
49+
/**
50+
* Constructor
51+
*
52+
* @param array $config Configuration array with the following keys:
53+
* - github_url: GitHub repository URL (required)
54+
* - plugin_file: Main plugin file path (required)
55+
* - plugin_slug: Plugin slug for updates (required)
56+
* - branch: Branch to check for updates (default: 'main')
57+
* - name_regex: Regex pattern for zip file name (optional)
58+
* - enable_release_assets: Whether to enable release assets (default: true if name_regex provided)
59+
*/
60+
public function __construct( $config = array() ) {
61+
// Validate required parameters
62+
$required = array( 'github_url', 'plugin_file', 'plugin_slug' );
63+
foreach ( $required as $key ) {
64+
if ( empty( $config[ $key ] ) ) {
65+
throw new \InvalidArgumentException( "Required parameter '{$key}' is missing or empty." );
66+
}
67+
}
68+
69+
$this->github_url = $config[ 'github_url' ];
70+
$this->plugin_file = $config[ 'plugin_file' ];
71+
$this->plugin_slug = $config[ 'plugin_slug' ];
72+
$this->branch = isset( $config[ 'branch' ] ) ? $config[ 'branch' ] : 'main';
73+
$this->name_regex = isset( $config[ 'name_regex' ] ) ? $config[ 'name_regex' ] : '';
74+
$this->enable_release_assets = isset( $config[ 'enable_release_assets' ] )
75+
? $config[ 'enable_release_assets' ]
76+
: ! empty( $this->name_regex );
77+
78+
// Initialize the updater
79+
add_action( 'init', array( $this, 'setup_updater' ) );
80+
}
81+
82+
/**
83+
* Set up the update checker using GitHub integration
84+
*/
85+
public function setup_updater() {
86+
try {
87+
$update_checker = PucFactory::buildUpdateChecker(
88+
$this->github_url,
89+
$this->plugin_file,
90+
$this->plugin_slug
91+
);
92+
93+
$update_checker->setBranch( $this->branch );
94+
95+
// Enable release assets if configured
96+
if ( $this->enable_release_assets && ! empty( $this->name_regex ) ) {
97+
$update_checker->getVcsApi()->enableReleaseAssets( $this->name_regex );
98+
}
99+
100+
} catch (\Exception $e) {
101+
// Log error if WordPress debug is enabled
102+
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
103+
error_log( 'GitHub Plugin Updater Error: ' . $e->getMessage() );
104+
}
105+
}
106+
}
107+
108+
/**
109+
* Create updater instance with minimal configuration
110+
*
111+
* @param string $github_url GitHub repository URL
112+
* @param string $plugin_file Main plugin file path
113+
* @param string $plugin_slug Plugin slug
114+
* @param string $branch Branch name (default: 'main')
115+
*
116+
* @return GitHub_Plugin_Updater
117+
*/
118+
public static function create( $github_url, $plugin_file, $plugin_slug, $branch = 'main' ) {
119+
return new self( array(
120+
'github_url' => $github_url,
121+
'plugin_file' => $plugin_file,
122+
'plugin_slug' => $plugin_slug,
123+
'branch' => $branch,
124+
) );
125+
}
126+
127+
/**
128+
* Create updater instance for plugins with release assets
129+
*
130+
* @param string $github_url GitHub repository URL
131+
* @param string $plugin_file Main plugin file path
132+
* @param string $plugin_slug Plugin slug
133+
* @param string $name_regex Regex pattern for release assets
134+
* @param string $branch Branch name (default: 'main')
135+
*
136+
* @return GitHub_Plugin_Updater
137+
*/
138+
public static function create_with_assets( $github_url, $plugin_file, $plugin_slug, $name_regex, $branch = 'main' ) {
139+
return new self( array(
140+
'github_url' => $github_url,
141+
'plugin_file' => $plugin_file,
142+
'plugin_slug' => $plugin_slug,
143+
'branch' => $branch,
144+
'name_regex' => $name_regex,
145+
) );
146+
}
147+
}

includes/class-multisite-exporter.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,13 @@ public static function instance() {
5454
*/
5555
public function __construct() {
5656
$this->includes();
57-
$this->setup_updater();
57+
\Soderlind\WordPress\GitHub_Plugin_Updater::create_with_assets(
58+
$this->github_url,
59+
MULTISITE_EXPORTER_PLUGIN_FILE,
60+
'multisite-exporter',
61+
'/multisite-exporter\.zip/',
62+
'main'
63+
);
5864

5965
// Check if we're running in a multisite environment
6066
if ( ! is_multisite() ) {
@@ -96,20 +102,6 @@ private function includes() {
96102
}
97103
}
98104

99-
/**
100-
* Set up the update checker using GitHub integration
101-
*/
102-
public function setup_updater() {
103-
$update_checker = PucFactory::buildUpdateChecker(
104-
$this->github_url,
105-
MULTISITE_EXPORTER_PLUGIN_FILE,
106-
'multisite-exporter'
107-
);
108-
109-
$update_checker->setBranch( 'main' );
110-
$update_checker->getVcsApi()->enableReleaseAssets( '/multisite-exporter\.zip/' );
111-
}
112-
113105
/**
114106
* Initialize the plugin
115107
*

multisite-exporter.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/*
33
Plugin Name: Multisite Exporter
44
Description: Runs WordPress Exporter on each subsite in a multisite, in the background.
5-
Version: 1.2.0
5+
Version: 1.2.1
66
Author: Per Søderlind
77
Author URI: https://soderlind.no
88
License: GPL2
@@ -18,7 +18,7 @@
1818
}
1919

2020
// Define plugin constants
21-
define( 'MULTISITE_EXPORTER_VERSION', '1.2.0' );
21+
define( 'MULTISITE_EXPORTER_VERSION', '1.2.1' );
2222
define( 'MULTISITE_EXPORTER_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
2323
define( 'MULTISITE_EXPORTER_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
2424
define( 'MULTISITE_EXPORTER_PLUGIN_FILE', __FILE__ );
@@ -31,9 +31,12 @@
3131

3232
require_once MULTISITE_EXPORTER_PLUGIN_DIR . 'vendor/autoload.php';
3333

34+
// Include plugin updater
35+
if ( ! class_exists( 'Soderlind\WordPress\GitHub_Plugin_Updater' ) ) {
36+
require_once MULTISITE_EXPORTER_PLUGIN_DIR . 'includes/class-github-plugin-updater.php';
37+
}
3438
// Include the main plugin class
3539
require_once MULTISITE_EXPORTER_PLUGIN_DIR . 'includes/class-multisite-exporter.php';
36-
3740
/**
3841
* Returns the main instance of Multisite_Exporter.
3942
*

readme.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
=== Multisite Exporter ===
22
Contributors: persoderlind
33
Tags: multisite, export, background processing, action scheduler, wp-cli
4-
Requires at least: 6.3
4+
Requires at least: 6.5
55
Tested up to: 6.8
66
Requires PHP: 8.2
7-
Stable tag: 1.2.0
7+
Stable tag: 1.2.1
88
License: GPLv2 or later
99
License URI: https://www.gnu.org/licenses/gpl-2.0.html
1010

@@ -130,6 +130,10 @@ Yes, you can use the `multisite_exporter_directory` filter to specify a custom d
130130

131131
== Changelog ==
132132

133+
= 1.2.1 =
134+
* Use generic [WordPress Plugin GitHub Updater](https://github.com/soderlind/wordpress-plugin-gitHub-updater?tab=readme-ov-file#wordpress-plugin-github-updater)
135+
136+
133137
= 1.2.0 =
134138
* Added: WP CLI command for exporting content from multisite installations
135139
* Added: Command line progress bar during export

0 commit comments

Comments
 (0)