-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass-github-theme-updater.php
184 lines (160 loc) · 4.6 KB
/
class-github-theme-updater.php
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* The GitHub_Theme_Updater Class File
*
* This file registers the GitHub_Theme_Updater class.
*
* @link https://site.tld
* @since 1.0.0 Introduced on 2023-10-14 15:30
* @package Themes
* @author Your Name <[email protected]>
*/
declare( strict_types = 1 );
namespace MyTheme;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* The GitHub_Theme_Updater class
*
* Hooks into the update themes transient to update the update details.
* Hoooks into theme page admin screen to force check if update is available.
*
* @since 1.0.0 Introduced on 2023-10-14 14:14
* @package Themes
* @author Your Name <[email protected]>
*/
class GitHub_Theme_Updater {
/**
* The Theme slug
*
* @since 1.0.0 Introduced on 2023-10-14 18:02
* @author Beda Schmid <[email protected]>
* @access private
* @var string $theme_slug The Theme name (FOLDER slug, for example my-theme).
*/
private $theme_slug;
/**
* The Current Version
*
* @since 1.0.0 Introduced on 2023-10-14 18:02
* @author Beda Schmid <[email protected]>
* @access private
* @var string $current_version The Theme version
*/
private $current_version;
/**
* The GitHub Repo URL
*
* @since 1.0.0 Introduced on 2023-10-14 18:02
* @author Beda Schmid <[email protected]>
* @access private
* @var string $github_repo_url The Theme's GitHub Repo URL
*/
private $github_repo_url;
/**
* Initialise the class
*
* @since 1.0.0 Introduced on 2023-10-14 18:06
* @author Beda Schmid <[email protected]>
* @param string $theme_slug The Theme slug.
* @param string $current_version The Current Version.
* @param string $github_repo_url The GitHub Repo URL.
*/
public function __construct( $theme_slug, $current_version, $github_repo_url ) {
$this->theme_slug = $theme_slug;
$this->current_version = $current_version;
$this->github_repo_url = $github_repo_url;
}
/**
* Add all hooks
*
* @since 1.0.0 Introduced on 2023-10-14 18:05
* @author Beda Schmid <[email protected]>
* @return void
*/
public function init() {
add_filter( 'pre_set_site_transient_update_themes', array( $this, 'check_for_updates' ) );
add_action( 'admin_init', array( $this, 'force_github_update_check' ) );
}
/**
* Force Update Check
*
* If the URL parameter `force-github-update` is set, we force an Update check.
* This plugin uses the URL parameter only on the Plugin List > Check for Updates.
*
* @since 1.0.0 Introduced on 2023-10-14 18:07
* @author Beda Schmid <[email protected]>
* @return void
*/
public function force_github_update_check() {
if ( $this->check_caps() ) {
delete_site_transient( 'update_themes' );
}
}
/**
* Check for updates
*
* @since 1.0.0 Introduced on 2023-10-14 18:07
* @author Beda Schmid <[email protected]>
* @param array $links The existing plugin action links.
* @return array The plugin action links.
*/
public function check_for_updates( $transient ) {
if ( empty( $transient->checked ) ) {
return $transient;
}
$release = $this->get_github_release_info();
if ( isset( $release['assets'][0]['browser_download_url'] ) ) {
$package = $release['assets'][0]['browser_download_url'];
} else {
return $transient;
}
if ( is_array( $release )
&& isset( $release['tag_name'] )
&& version_compare( $release['tag_name'], $this->current_version, '>' )
) {
$transient->response[ $this->theme_slug ] = array(
'theme' => $this->theme_slug,
'new_version' => $release['tag_name'],
'package' => $package,
'url' => $this->github_repo_url,
);
}
return $transient;
}
/**
* Get update info
*
* @since 1.0.0 Introduced on 2023-10-14 18:07
* @author Beda Schmid <[email protected]>
* @return array|bool The update information array on success or false on failure.
*/
private function get_github_release_info() {
$url = $this->github_repo_url . '/releases/latest';
$response = wp_remote_get( $url );
if ( is_wp_error( $response ) ) {
return false;
}
$body = wp_remote_retrieve_body( $response );
return json_decode( $body, true );
}
/**
* Check permission and location
*
* Make sure the force-check only is available to allowed users and locations.
*
* @since 1.0.0 Introduced on 2023-10-14 18:12
* @author Beda Schmid <[email protected]>
* @return bool true if the user/location is allowed. False otherwise.
*/
private function check_caps() {
global $pagenow;
if ( 'themes.php' === $pagenow
&& current_user_can( 'update_themes' )
) {
return true;
}
return false;
}
}