-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstorefront-beta-tester.php
368 lines (320 loc) · 11.3 KB
/
storefront-beta-tester.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?php
/**
* Plugin Name: Storefront Beta Tester
* Plugin URI: https://github.com/seb86/Storefront-Beta-Tester
* Description: Run bleeding edge versions of Storefront from Github.
* Version: 1.0.3
* Author: Sébastien Dumont
* Author URI: http://sebastiendumont.com
* GitHub Plugin URI: https://github.com/seb86/Storefront-Beta-Tester
*
* Text Domain: storefront-beta-tester
* Domain Path: /languages/
*
* Requires at least: 4.2
* Tested up to: 4.4.2
*
* Based on WP_GitHub_Updater by Joachim Kudish.
* Forked from WooCommerce Beta Tester by Mike Jolly and Claudio Sanches.
*/
if ( ! defined('ABSPATH') ) {
exit;
}
/**
* Confirm Storefront is installed before doing anything.
* Curiously, there is not a constant for getting the theme directory so we have
* to use get_theme_root() function instead, so this is what we have to do.
*/
if ( ! file_exists( trailingslashit( get_theme_root() ) . 'storefront/style.css' ) ) {
add_action( 'admin_notices', 'storefront_not_installed' );
}
elseif ( ! class_exists( 'Storefront_Beta_Tester' ) ) {
/**
* Storefront_Beta_Tester Main Class
*/
class Storefront_Beta_Tester {
/**
* Config
*
* @access private
*/
private $config = array();
/**
* Github Data
*
* @access protected
* @static
*/
protected static $_instance = null;
/**
* Main Instance
*
* @access public
* @static
*/
public static function instance() {
return self::$_instance = is_null( self::$_instance ) ? new self() : self::$_instance;
}
/**
* Run on activation to flush update cache.
*
* @access public
* @static
*/
public static function activate() {
delete_site_transient( 'update_themes' );
delete_site_transient( 'storefront_latest_tag' );
}
/**
* Constructor
*
* @access public
*/
public function __construct() {
$this->config = array(
'theme_file' => 'storefront/style.css',
'slug' => 'storefront',
'proper_folder_name' => 'storefront',
'api_url' => 'https://api.github.com/repos/woocommerce/storefront',
'github_url' => 'https://github.com/woocommerce/storefront',
'requires' => '4.2',
'tested' => '4.4.2'
);
add_filter( 'pre_set_site_transient_update_themes', array( $this, 'api_check' ) );
add_filter( 'themes_api', array( $this, 'get_theme_info' ), 10, 3 );
add_action( 'init', array( $this, 'load_textdomain' ) );
add_filter( 'plugin_row_meta', array( $this, 'plugin_meta_links' ), 10, 4 );
}
/**
* Update args
*
* @access public
* @return array
*/
public function set_update_args() {
$theme_data = $this->get_theme_data();
$this->config['theme_name'] = $theme_data['Name'];
$this->config['version'] = $theme_data['Version'];
$this->config['author'] = $theme_data['Author'];
$this->config['homepage'] = esc_url_raw( $theme_data['ThemeURI'] );
$this->config['new_version'] = str_replace( 'version/', '', $this->get_latest_tag() );
$this->config['new_release'] = $this->get_latest_tag();
$this->config['last_updated'] = $this->get_date();
$this->config['description'] = $this->get_description();
$this->config['zip_url'] = 'https://github.com/woocommerce/storefront/releases/download/' . $this->config['new_release']. '/storefront.zip';
$this->config['screenshot'] = 'https://raw.githubusercontent.com/woocommerce/storefront/master/screenshot.png';
}
/**
* Check wether or not the transients need to be overruled and API needs to be called for every single page load
*
* @access public
* @return bool overrule or not
*/
public function overrule_transients() {
return ( defined( 'STOREFRONT_BETA_TESTER_FORCE_UPDATE' ) && STOREFRONT_BETA_TESTER_FORCE_UPDATE );
}
/**
* Get New Version from GitHub
*
* @since 1.0.0
* @version 1.0.2
* @access public
* @return int $version the version number
*/
public function get_latest_tag() {
$tagged_version = get_site_transient( md5( $this->config['slug'] ) . '_latest_tag' );
if ( $this->overrule_transients() || empty( $tagged_version ) ) {
$raw_response = wp_remote_get( trailingslashit( $this->config['api_url'] ) . 'releases' );
if ( is_wp_error( $raw_response ) ) {
return '<div id="message" class="error"><p>' . $raw_response->get_error_message() . '</p></div>';
}
$releases = json_decode( $raw_response['body'] );
$tagged_version = false;
if ( is_array( $releases ) ) {
foreach ( $releases as $release ) {
if ( $release->prerelease ) {
$tagged_version = $release->tag_name;
break;
}
}
}
// refresh every 6 hours
if ( ! empty( $tagged_version ) ) {
set_site_transient( md5( $this->config['slug'] ) . '_latest_tag', $tagged_version, 60*60*6 );
}
}
return $tagged_version;
}
/**
* Get GitHub Data from the specified repository
*
* @since 1.0.0
* @version 1.0.2
* @access public
* @return array $github_data the data
*/
public function get_github_data() {
if ( ! empty( $this->github_data ) ) {
$github_data = $this->github_data;
} else {
$github_data = get_site_transient( md5( $this->config['slug'] ) . '_github_data' );
if ( $this->overrule_transients() || ( ! isset( $github_data ) || ! $github_data || '' == $github_data ) ) {
$github_data = wp_remote_get( $this->config['api_url'] );
if ( is_wp_error( $github_data ) ) {
return '<div id="message" class="error"><p>' . $github_data->get_error_message() . '</p></div>';
}
$github_data = json_decode( $github_data['body'] );
// refresh every 6 hours
set_site_transient( md5( $this->config['slug'] ) . '_github_data', $github_data, 60*60*6 );
}
// Store the data in this class instance for future calls
$this->github_data = $github_data;
}
return $github_data;
}
/**
* Get update date
*
* @access public
* @return string $date the date
*/
public function get_date() {
$_date = $this->get_github_data();
return ! empty( $_date->updated_at ) ? date( 'Y-m-d', strtotime( $_date->updated_at ) ) : false;
}
/**
* Get theme description
*
* @access public
* @return string $description the description
*/
public function get_description() {
$_description = $this->get_github_data();
return ! empty( $_description->description ) ? $_description->description : __( 'Storefront is a robust and flexible WordPress theme, designed by WooCommerce to help you make the most out of using WooCommerce to power your online store.', 'storefront-beta-tester' );
}
/**
* Get Theme data
*
* @access public
* @return object $data the data
*/
public function get_theme_data() {
return wp_get_theme( $this->config['slug'] );
}
/**
* Hook into the theme update check and connect to GitHub
*
* @access public
* @param object $transient the theme data transient
* @return object $transient updated theme data transient
*/
public function api_check( $transient ) {
// Check if the transient contains the 'checked' information
// If not, just return its value without hacking it
if ( empty( $transient->checked ) ) {
return $transient;
}
// Clear our transient
delete_site_transient( md5( $this->config['slug'] ) . '_latest_tag' );
// Update tags
$this->set_update_args();
// check the version and decide if it's new
if ( version_compare( $this->config['new_version'], $this->config['version'], '>' ) ) {
$transient->response[ $this->config['slug'] ] = array(
'new_version' => $this->config['new_version'],
'package' => $this->config['zip_url'],
'url' => $this->config['github_url']
);
}
return $transient;
}
/**
* Get Theme info
*
* @access public
* @param bool $false always false
* @param string $action the API function being performed
* @param object $args theme arguments
* @return object $response the theme info
*/
public function get_theme_info( $false, $action, $response ) {
// Check if this call API is for the right theme
if ( ! isset( $response->slug ) || $response->slug != $this->config['slug'] ) {
return false;
}
// Update tags
$this->set_update_args();
$response->slug = $this->config['slug'];
$response->theme = $this->config['slug'];
$response->name = $this->config['theme_name'];
$response->theme_name = $this->config['theme_name'];
$response->version = $this->config['new_version'];
$response->author = $this->config['author'];
$response->homepage = $this->config['homepage'];
$response->requires = $this->config['requires'];
$response->tested = $this->config['tested'];
$response->screenshot_url = $this->config['screenshot'];
$response->downloaded = 0;
$response->last_updated = $this->config['last_updated'];
$response->sections = array( 'description' => $this->config['description'] );
$response->download_link = $this->config['zip_url'];
$response->package = $this->config['zip_url'];
$response->file_name = $this->config['slug'] . '.zip';
return $response;
}
/**
* Rename the downloaded zip
*
* @access public
* @global $wp_filesystem
*/
public function upgrader_source_selection( $source, $remote_source, $upgrader ) {
global $wp_filesystem;
if ( strstr( $source, '/woothemes-Storefront-' ) ) {
$corrected_source = trailingslashit( $remote_source ) . trailingslashit( $this->config[ 'proper_folder_name' ] );
if ( $wp_filesystem->move( $source, $corrected_source, true ) ) {
return $corrected_source;
} else {
return new WP_Error();
}
}
return $source;
}
/**
* Load textdomain.
*
* @access public
* @return void
*/
public function load_textdomain() {
load_plugin_textdomain( 'storefront-beta-tester', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Show row meta on the plugin screen.
*
* @access public
* @param mixed $links Plugin Row Meta
* @param mixed $file Plugin Base file
* @return array
*/
public function plugin_meta_links( $links, $file, $data, $status ) {
if ( $file == plugin_basename( __FILE__ ) ) {
$author1 = '<a href="' . $data[ 'AuthorURI' ] . '">' . $data[ 'Author' ] . '</a>';
$author2 = '<a href="http://jameskoster.co.uk/">James Koster</a>';
$author3 = '<a href="https://bradgriffin.me/">Brad Griffin</a>';
$links[1] = sprintf( __( 'By %s' ), sprintf( __( '%s and %s and %s' ), $author1, $author2, $author3 ) );
}
return $links;
}
} // END class
register_activation_hook( __FILE__, array( 'Storefront_Beta_Tester', 'activate' ) );
add_action( 'admin_init', array( 'Storefront_Beta_Tester', 'instance' ) );
} // END if theme installed / class exists
/**
* Storefront Not Installed Notice
*/
if ( ! function_exists( 'storefront_not_installed' ) ) {
function storefront_not_installed() {
echo '<div class="notice notice-error is-dismissible"><p>' . sprintf( __( 'Storefront Beta Tester requires %s to be installed.', 'storefront-beta-tester'), '<a href="' . esc_url( wp_nonce_url( self_admin_url( 'update.php?action=install-theme&theme=storefront' ), 'install-theme_storefront' ) ) . '">Storefront</a>' ) . '</p></div>';
}
}