acf-gallery-update.php
5.0 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
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
<?php
/*
* acf_gallery_field_plugin_update
*
* this class will connect with and download updates from the ACF website
*
* IMPORTANT: This file must be removed from the add-on if you are distibuting this add-on within a plugin or theme.
* to read more about the terms & conditions regarding add-ons, please refer to the documentation here:
* http://www.advancedcustomfields.com/terms-conditions/
*
* @type class
* @date 13/07/13
*
*/
class acf_gallery_field_plugin_update
{
var $settings;
/*
* Constructor
*
* @description:
* @since 1.0.0
* @created: 23/06/12
*/
function __construct()
{
// vars
$this->settings = array(
'version' => '',
'remote' => 'http://download.advancedcustomfields.com/GF72-8ME6-JS15-3PZC/info/',
'basename' => plugin_basename( str_replace('-update.php', '.php', __FILE__) ),
'slug' => dirname( plugin_basename( str_replace('-update.php', '.php', __FILE__) ) )
);
// actions
add_action('in_plugin_update_message-' . $this->settings['basename'], array($this, 'in_plugin_update_message'), 10, 2 );
// filters
add_filter('pre_set_site_transient_update_plugins', array($this, 'check_update'));
add_filter('plugins_api', array($this, 'check_info'), 10, 3);
}
/*
* in_plugin_update_message
*
* Displays an update message for plugin list screens.
* Shows only the version updates from the current until the newest version
*
* @type function
* @date 5/06/13
*
* @param {array} $plugin_data
* @param {object} $r
*/
function in_plugin_update_message( $plugin_data, $r )
{
// vars
$readme = wp_remote_fopen( str_replace( '/info/' , '/trunk/readme.txt', $this->settings['remote'] ) );
$regexp = '/== Changelog ==(.*)= ' . $this->get_version() . ' =/sm';
$o = '';
// validate
if( !$readme )
{
return;
}
// regexp
preg_match( $regexp, $readme, $matches );
if( ! isset($matches[1]) )
{
return;
}
// add style
$o .= '<style type="text/css">';
$o .= '#advanced-custom-fields-gallery-field + .plugin-update-tr .update-message { background: #EAF2FA; border: #C7D7E2 solid 1px; padding: 10px; }';
$o .= '</style>';
// render changelog
$changelog = explode('*', $matches[1]);
array_shift( $changelog );
if( !empty($changelog) )
{
$o .= '<div class="acf-plugin-update-info">';
$o .= '<h3>' . __("What's new", 'acf') . '</h3>';
$o .= '<ul>';
foreach( $changelog as $item )
{
$o .= '<li>' . make_clickable( $item ) . '</li>';
}
$o .= '</ul></div>';
}
echo $o;
}
/*
* get_remote
*
* @description:
* @since: 3.6
* @created: 31/01/13
*/
function get_remote()
{
// vars
$info = false;
// Get the remote info
$request = wp_remote_post( $this->settings['remote'] );
if( !is_wp_error($request) || wp_remote_retrieve_response_code($request) === 200)
{
$info = @unserialize($request['body']);
$info->slug = $this->settings['slug'];
}
return $info;
}
/*
* check_update
*
* @description:
* @since: 3.6
* @created: 31/01/13
*/
function check_update( $transient )
{
if( empty($transient->checked) )
{
return $transient;
}
// vars
$info = $this->get_remote();
// validate
if( !$info )
{
return $transient;
}
// compare versions
if( version_compare($info->version, $this->get_version(), '<=') )
{
return $transient;
}
// create new object for update
$obj = new stdClass();
$obj->slug = $info->slug;
$obj->new_version = $info->version;
$obj->url = $info->homepage;
$obj->package = $info->download_link;
// add to transient
$transient->response[ $this->settings['basename'] ] = $obj;
return $transient;
}
/*
* check_info
*
* @description:
* @since: 3.6
* @created: 31/01/13
*/
function check_info( $false, $action, $arg )
{
// validate
if( !isset($arg->slug) || $arg->slug != $this->settings['slug'] )
{
return $false;
}
if( $action == 'plugin_information' )
{
$false = $this->get_remote();
}
return $false;
}
/*
* get_version
*
* This function will return the current version of this add-on
*
* @type function
* @date 27/08/13
*
* @param N/A
* @return (string)
*/
function get_version()
{
// populate only once
if( !$this->settings['version'] )
{
$plugin_data = get_plugin_data( str_replace('-update.php', '.php', __FILE__) );
$this->settings['version'] = $plugin_data['Version'];
}
// return
return $this->settings['version'];
}
}
// instantiate
if( is_admin() )
{
new acf_gallery_field_plugin_update();
}
?>