-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-github-plugin-updater.php
More file actions
147 lines (131 loc) · 4.34 KB
/
class-github-plugin-updater.php
File metadata and controls
147 lines (131 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
namespace Soderlind\WordPress;
use YahnisElsts\PluginUpdateChecker\v5\PucFactory;
/**
* Generic WordPress Plugin GitHub Updater
*
* A reusable class for handling WordPress plugin updates from GitHub repositories
* using the plugin-update-checker library.
*
* @package Soderlind\WordPress
* @link https://github.com/soderlind/wordpress-plugin-github-updater
* @version 1.0.0
* @author Per Soderlind
* @license GPL-2.0+
*/
class GitHub_Plugin_Updater {
/**
* @var string GitHub repository URL
*/
private $github_url;
/**
* @var string Branch to check for updates
*/
private $branch;
/**
* @var string Regex pattern to match the plugin zip file name
*/
private $name_regex;
/**
* @var string The plugin slug
*/
private $plugin_slug;
/**
* @var string The main plugin file path
*/
private $plugin_file;
/**
* @var bool Whether to enable release assets
*/
private $enable_release_assets;
/**
* Constructor
*
* @param array $config Configuration array with the following keys:
* - github_url: GitHub repository URL (required)
* - plugin_file: Main plugin file path (required)
* - plugin_slug: Plugin slug for updates (required)
* - branch: Branch to check for updates (default: 'main')
* - name_regex: Regex pattern for zip file name (optional)
* - enable_release_assets: Whether to enable release assets (default: true if name_regex provided)
*/
public function __construct( $config = array() ) {
// Validate required parameters
$required = array( 'github_url', 'plugin_file', 'plugin_slug' );
foreach ( $required as $key ) {
if ( empty( $config[ $key ] ) ) {
throw new \InvalidArgumentException( "Required parameter '{$key}' is missing or empty." );
}
}
$this->github_url = $config[ 'github_url' ];
$this->plugin_file = $config[ 'plugin_file' ];
$this->plugin_slug = $config[ 'plugin_slug' ];
$this->branch = isset( $config[ 'branch' ] ) ? $config[ 'branch' ] : 'main';
$this->name_regex = isset( $config[ 'name_regex' ] ) ? $config[ 'name_regex' ] : '';
$this->enable_release_assets = isset( $config[ 'enable_release_assets' ] )
? $config[ 'enable_release_assets' ]
: ! empty( $this->name_regex );
// Initialize the updater
add_action( 'init', array( $this, 'setup_updater' ) );
}
/**
* Set up the update checker using GitHub integration
*/
public function setup_updater() {
try {
$update_checker = PucFactory::buildUpdateChecker(
$this->github_url,
$this->plugin_file,
$this->plugin_slug
);
$update_checker->setBranch( $this->branch );
// Enable release assets if configured
if ( $this->enable_release_assets && ! empty( $this->name_regex ) ) {
$update_checker->getVcsApi()->enableReleaseAssets( $this->name_regex );
}
} catch (\Exception $e) {
// Log error if WordPress debug is enabled
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( 'GitHub Plugin Updater Error: ' . $e->getMessage() );
}
}
}
/**
* Create updater instance with minimal configuration
*
* @param string $github_url GitHub repository URL
* @param string $plugin_file Main plugin file path
* @param string $plugin_slug Plugin slug
* @param string $branch Branch name (default: 'main')
*
* @return GitHub_Plugin_Updater
*/
public static function create( $github_url, $plugin_file, $plugin_slug, $branch = 'main' ) {
return new self( array(
'github_url' => $github_url,
'plugin_file' => $plugin_file,
'plugin_slug' => $plugin_slug,
'branch' => $branch,
) );
}
/**
* Create updater instance for plugins with release assets
*
* @param string $github_url GitHub repository URL
* @param string $plugin_file Main plugin file path
* @param string $plugin_slug Plugin slug
* @param string $name_regex Regex pattern for release assets
* @param string $branch Branch name (default: 'main')
*
* @return GitHub_Plugin_Updater
*/
public static function create_with_assets( $github_url, $plugin_file, $plugin_slug, $name_regex, $branch = 'main' ) {
return new self( array(
'github_url' => $github_url,
'plugin_file' => $plugin_file,
'plugin_slug' => $plugin_slug,
'branch' => $branch,
'name_regex' => $name_regex,
) );
}
}