-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdater.php
More file actions
178 lines (140 loc) · 5.74 KB
/
updater.php
File metadata and controls
178 lines (140 loc) · 5.74 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
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
<?php
/**
* Theme Updater
*
* @package Package Name
* @author Jobyaer Arman
*/
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
if ( ! class_exists( 'Theme_Updater' ) ) {
class Theme_Updater {
/**
* @var $config the config for the updater
* @access public
*/
var $config;
/**
* @var $github_data temporiraly store the data fetched from GitHub, allows us to only load the data once per class instance
* @access private
*/
private $github_data;
/**
* Class Constructor
*
* @since 1.0
* @param array $config the configuration required for the updater to work
* @return void
*/
public function __construct( $config = array() ) {
$defaults = array(
'slug' => wp_get_theme()->get_template(),
'owner' => 'jobayerarman',
'access_token' => '',
);
$this->config = wp_parse_args( $config, $defaults );
$this->set_defaults();
// Check for updates
add_filter( 'pre_set_site_transient_update_themes', array( $this, 'check_for_update' ), 10, 1 );
// Rename this zip file to the accurate theme folder
add_filter( 'upgrader_source_selection', array( $this, 'upgrader_source_selection_filter' ), 10, 3 );
}
/**
* Set defaults
*
* @since 2.6.1
* @return void
*/
public function set_defaults() {
// Store the data in this class instance for future calls
$update_data = $this->get_github_data();
if ( ! isset( $this->config['new_version'] ) )
$this->config['new_version'] = $update_data['tag_name'];
if ( ! isset( $this->config['zip_url'] ) )
$this->config['zip_url'] = $update_data['zipball_url'];
if ( ! isset( $this->config['github_url'] ) )
$this->config['github_url'] = $update_data['html_url'];
$theme_data = wp_get_theme();
if ( ! isset( $this->config['theme_name'] ) )
$this->config['theme_name'] = $theme_data['Name'];
if ( ! isset( $this->config['version'] ) )
$this->config['version'] = $theme_data['Version'];
}
/**
* Get GitHub Data from the specified repository
*
* @since 1.0
* @return array $github_data the data
*/
public function get_github_data() {
if ( isset( $this->github_data ) && ! empty( $this->github_data ) ) {
$github_data = $this->github_data;
} else {
$github_data = get_site_transient( $this->config['slug'].'_github_data' );
if ( ( ! isset( $github_data ) || ! $github_data || '' == $github_data ) ) {
$query = $this->config['api_url'];
$github_data = wp_remote_get( $query );
if ( is_wp_error( $github_data ) )
return false;
$github_data = json_decode( wp_remote_retrieve_body( $github_data ), true );
if( is_array( $github_data ) ) {
$github_data = current( $github_data );
}
// refresh every 6 hours
set_site_transient( $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;
}
/**
* [check_for_update description]
* @param [type] $transient [description]
* @return [type] [description]
*/
public function check_for_update( $transient ) {
// Check if the transient contains the 'checked' information
// If not, just return its value without hacking it
if ( empty( $transient->checked ) ) {
return $transient;
}
// check the version and decide if it's new
$update = version_compare( $this->config['new_version'], $this->config['version'] );
if ( 1 === $update ) {
$response = array(
'theme' => $this->config['slug'],
'new_version' => $this->config['new_version'],
'package' => $this->config['zip_url'],
'url' => $this->config['github_url']
);
// If response is false, don't alter the transient
if ( false !== $response )
$transient->response[ $this->config['slug'] ] = $response;
}
return $transient;
}
/**
* Used for renaming of sources to ensure correct directory name.
*
* @since WordPress 4.4.0 The $hook_extra parameter became available.
*
* @param string $source
* @param string $remote_source
* @param \Theme_Upgrader $upgrader
* @param array $hook_extra
*
* @return string
*/
public function upgrader_source_selection_filter( $source ) {
global $wp_filesystem;
/*
* Rename theme.
*/
$proper_source = dirname( $source ) . '/' . $this->config['slug'] . '/';
if ( ! $wp_filesystem->move($source, $proper_source, true) )
return new \WP_Error();
$source = $proper_source;
return $source;
}
}
}