| 1 |
<?php |
| 2 |
|
| 3 |
if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 4 |
|
| 5 |
if( ! class_exists('acf_updates') ) : |
| 6 |
|
| 7 |
class acf_updates { |
| 8 |
|
| 9 |
// vars |
| 10 |
var $version = '2.2', |
| 11 |
$plugins = array(), |
| 12 |
$updates = false, |
| 13 |
$dev = 0; |
| 14 |
|
| 15 |
|
| 16 |
/* |
| 17 |
* __construct |
| 18 |
* |
| 19 |
* This function will setup the class functionality |
| 20 |
* |
| 21 |
* @type function |
| 22 |
* @date 5/03/2014 |
| 23 |
* @since 5.0.0 |
| 24 |
* |
| 25 |
* @param n/a |
| 26 |
* @return n/a |
| 27 |
*/ |
| 28 |
|
| 29 |
function __construct() { |
| 30 |
|
| 31 |
// append update information to transient |
| 32 |
add_filter('pre_set_site_transient_update_plugins', array($this, 'modify_plugins_transient'), 10, 1); |
| 33 |
|
| 34 |
|
| 35 |
// modify plugin data visible in the 'View details' popup |
| 36 |
add_filter('plugins_api', array($this, 'modify_plugin_details'), 10, 3); |
| 37 |
|
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
/* |
| 42 |
* add_plugin |
| 43 |
* |
| 44 |
* This function will register a plugin |
| 45 |
* |
| 46 |
* @type function |
| 47 |
* @date 8/4/17 |
| 48 |
* @since 5.5.10 |
| 49 |
* |
| 50 |
* @param $plugin (array) |
| 51 |
* @return n/a |
| 52 |
*/ |
| 53 |
|
| 54 |
function add_plugin( $plugin ) { |
| 55 |
|
| 56 |
// validate |
| 57 |
$plugin = wp_parse_args($plugin, array( |
| 58 |
'id' => '', |
| 59 |
'key' => '', |
| 60 |
'slug' => '', |
| 61 |
'basename' => '', |
| 62 |
'version' => '', |
| 63 |
)); |
| 64 |
|
| 65 |
|
| 66 |
// Check if is_plugin_active() function exists. This is required on the front end of the |
| 67 |
// site, since it is in a file that is normally only loaded in the admin. |
| 68 |
if( !function_exists( 'is_plugin_active' ) ) { |
| 69 |
|
| 70 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
// bail early if not active plugin (included in theme) |
| 76 |
if( !is_plugin_active($plugin['basename']) ) return; |
| 77 |
|
| 78 |
|
| 79 |
// add custom message in plugin update row |
| 80 |
// removed: decided this message will have a negative impact on user |
| 81 |
// if( is_admin() ) { |
| 82 |
// |
| 83 |
// add_action('in_plugin_update_message-' . $plugin['basename'], array($this, 'modify_plugin_update_message'), 10, 2 ); |
| 84 |
// |
| 85 |
// } |
| 86 |
|
| 87 |
|
| 88 |
// append |
| 89 |
$this->plugins[ $plugin['basename'] ] = $plugin; |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
/* |
| 95 |
* request |
| 96 |
* |
| 97 |
* This function will make a request to the ACF update server |
| 98 |
* |
| 99 |
* @type function |
| 100 |
* @date 8/4/17 |
| 101 |
* @since 5.5.10 |
| 102 |
* |
| 103 |
* @param $query (string) |
| 104 |
* @param $body (array) |
| 105 |
* @return (mixed) |
| 106 |
*/ |
| 107 |
|
| 108 |
function request( $query = 'index.php', $body = null ) { |
| 109 |
|
| 110 |
// vars |
| 111 |
$url = 'https://connect.advancedcustomfields.com/' . $query; |
| 112 |
|
| 113 |
|
| 114 |
// test |
| 115 |
if( $this->dev ) $url = 'http://connect/' . $query; |
| 116 |
|
| 117 |
|
| 118 |
// log |
| 119 |
//acf_log('acf connect: '. $url, $body); |
| 120 |
|
| 121 |
|
| 122 |
// post |
| 123 |
$raw_response = wp_remote_post( $url, array( |
| 124 |
'timeout' => 10, |
| 125 |
'body' => $body |
| 126 |
)); |
| 127 |
|
| 128 |
|
| 129 |
// wp error |
| 130 |
if( is_wp_error($raw_response) ) { |
| 131 |
|
| 132 |
return $raw_response; |
| 133 |
|
| 134 |
// http error |
| 135 |
} elseif( wp_remote_retrieve_response_code($raw_response) != 200 ) { |
| 136 |
|
| 137 |
return new WP_Error( 'server_error', wp_remote_retrieve_response_message($raw_response) ); |
| 138 |
|
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
// decode response |
| 143 |
$json = json_decode( wp_remote_retrieve_body($raw_response), true ); |
| 144 |
|
| 145 |
|
| 146 |
// allow non json value |
| 147 |
if( $json === null ) { |
| 148 |
|
| 149 |
return wp_remote_retrieve_body($raw_response); |
| 150 |
|
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
// return |
| 155 |
return $json; |
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
/* |
| 161 |
* get_plugin_info |
| 162 |
* |
| 163 |
* This function will get plugin info and save as transient |
| 164 |
* |
| 165 |
* @type function |
| 166 |
* @date 9/4/17 |
| 167 |
* @since 5.5.10 |
| 168 |
* |
| 169 |
* @param $id (string) |
| 170 |
* @return (mixed) |
| 171 |
*/ |
| 172 |
|
| 173 |
function get_plugin_info( $id = '' ) { |
| 174 |
|
| 175 |
// var |
| 176 |
$transient_name = 'acf_plugin_info_'.$id; |
| 177 |
|
| 178 |
|
| 179 |
// delete transient (force-check is used to refresh) |
| 180 |
if( !empty($_GET['force-check']) ) { |
| 181 |
|
| 182 |
delete_transient($transient_name); |
| 183 |
|
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
// try transient |
| 188 |
$transient = get_transient($transient_name); |
| 189 |
if( $transient !== false ) return $transient; |
| 190 |
|
| 191 |
|
| 192 |
// connect |
| 193 |
$response = $this->request('v2/plugins/get-info?p='.$id); |
| 194 |
|
| 195 |
|
| 196 |
// ensure response is expected JSON array (not string) |
| 197 |
if( is_string($response) ) { |
| 198 |
$response = new WP_Error( 'server_error', esc_html($response) ); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
// update transient |
| 203 |
set_transient($transient_name, $response, HOUR_IN_SECONDS ); |
| 204 |
|
| 205 |
|
| 206 |
// return |
| 207 |
return $response; |
| 208 |
|
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
/* |
| 213 |
* refresh_plugins_transient |
| 214 |
* |
| 215 |
* This function will refresh plugin update info to the transient |
| 216 |
* |
| 217 |
* @type function |
| 218 |
* @date 11/4/17 |
| 219 |
* @since 5.5.10 |
| 220 |
* |
| 221 |
* @param n/a |
| 222 |
* @return n/a |
| 223 |
*/ |
| 224 |
|
| 225 |
function refresh_plugins_transient() { |
| 226 |
|
| 227 |
// vars |
| 228 |
$transient = get_site_transient('update_plugins'); |
| 229 |
|
| 230 |
|
| 231 |
// bail early if no transient |
| 232 |
if( empty($transient) ) return; |
| 233 |
|
| 234 |
|
| 235 |
// update (will trigger modify function) |
| 236 |
set_site_transient( 'update_plugins', $transient ); |
| 237 |
|
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
/* |
| 242 |
* modify_plugins_transient |
| 243 |
* |
| 244 |
* This function will connect to the ACF website and find update information |
| 245 |
* |
| 246 |
* @type function |
| 247 |
* @date 16/01/2014 |
| 248 |
* @since 5.0.0 |
| 249 |
* |
| 250 |
* @param $transient (object) |
| 251 |
* @return $transient |
| 252 |
*/ |
| 253 |
|
| 254 |
function modify_plugins_transient( $transient ) { |
| 255 |
|
| 256 |
// bail early if no response (error) |
| 257 |
if( !isset($transient->response) ) return $transient; |
| 258 |
|
| 259 |
|
| 260 |
// fetch updates once (this filter is called multiple times during a single page load) |
| 261 |
if( !$this->updates ) { |
| 262 |
|
| 263 |
// vars |
| 264 |
$post = array( |
| 265 |
'plugins' => wp_json_encode($this->plugins), |
| 266 |
'wp' => wp_json_encode(array( |
| 267 |
'wp_name' => get_bloginfo('name'), |
| 268 |
'wp_url' => home_url(), |
| 269 |
'wp_version' => get_bloginfo('version'), |
| 270 |
'wp_language' => get_bloginfo('language'), |
| 271 |
'wp_timezone' => get_option('timezone_string'), |
| 272 |
)), |
| 273 |
'acf' => wp_json_encode(array( |
| 274 |
'acf_version' => get_option('acf_version'), |
| 275 |
'acf_pro' => (defined('ACF_PRO') && ACF_PRO), |
| 276 |
)), |
| 277 |
); |
| 278 |
|
| 279 |
|
| 280 |
// connect |
| 281 |
$this->updates = $this->request('v2/plugins/update-check', $post); |
| 282 |
|
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
// append |
| 287 |
if( is_array($this->updates) ) { |
| 288 |
|
| 289 |
foreach( $this->updates['plugins'] as $basename => $update ) { |
| 290 |
|
| 291 |
$transient->response[ $basename ] = (object) $update; |
| 292 |
|
| 293 |
} |
| 294 |
|
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
// return |
| 299 |
return $transient; |
| 300 |
|
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
/* |
| 305 |
* modify_plugin_details |
| 306 |
* |
| 307 |
* This function will populate the plugin data visible in the 'View details' popup |
| 308 |
* |
| 309 |
* @type function |
| 310 |
* @date 17/01/2014 |
| 311 |
* @since 5.0.0 |
| 312 |
* |
| 313 |
* @param $result (bool|object) |
| 314 |
* @param $action (string) |
| 315 |
* @param $args (object) |
| 316 |
* @return $result |
| 317 |
*/ |
| 318 |
|
| 319 |
function modify_plugin_details( $result, $action = null, $args = null ) { |
| 320 |
|
| 321 |
// vars |
| 322 |
$plugin = false; |
| 323 |
|
| 324 |
|
| 325 |
// only for 'plugin_information' action |
| 326 |
if( $action !== 'plugin_information' ) return $result; |
| 327 |
|
| 328 |
|
| 329 |
// find plugin via slug |
| 330 |
foreach( $this->plugins as $p ) { |
| 331 |
|
| 332 |
if( $args->slug == $p['slug'] ) $plugin = $p; |
| 333 |
|
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
// bail early if plugin not found |
| 338 |
if( !$plugin ) return $result; |
| 339 |
|
| 340 |
|
| 341 |
// connect |
| 342 |
$response = $this->get_plugin_info($plugin['id']); |
| 343 |
|
| 344 |
|
| 345 |
// bail early if no response |
| 346 |
if( !is_array($response) ) return $result; |
| 347 |
|
| 348 |
|
| 349 |
// remove tags (different context) |
| 350 |
unset($response['tags']); |
| 351 |
|
| 352 |
|
| 353 |
// convert to object |
| 354 |
$response = (object) $response; |
| 355 |
|
| 356 |
|
| 357 |
// sections |
| 358 |
$sections = array( |
| 359 |
'description' => '', |
| 360 |
'installation' => '', |
| 361 |
'changelog' => '', |
| 362 |
'upgrade_notice' => '' |
| 363 |
); |
| 364 |
|
| 365 |
foreach( $sections as $k => $v ) { |
| 366 |
|
| 367 |
$sections[ $k ] = $response->$k; |
| 368 |
|
| 369 |
} |
| 370 |
|
| 371 |
$response->sections = $sections; |
| 372 |
|
| 373 |
|
| 374 |
// return |
| 375 |
return $response; |
| 376 |
|
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
/* |
| 381 |
* modify_plugin_update_message |
| 382 |
* |
| 383 |
* Displays an update message for plugin list screens. |
| 384 |
* Shows only the version updates from the current until the newest version |
| 385 |
* |
| 386 |
* @type function |
| 387 |
* @date 14/06/2016 |
| 388 |
* @since 5.3.8 |
| 389 |
* |
| 390 |
* @param $plugin_data (array) |
| 391 |
* @param $r (object) |
| 392 |
* @return n/a |
| 393 |
*/ |
| 394 |
|
| 395 |
/* |
| 396 |
function modify_plugin_update_message( $plugin_data, $response ) { |
| 397 |
|
| 398 |
// show notice if exists in transient data |
| 399 |
if( isset($response->notice) ) { |
| 400 |
|
| 401 |
echo '<div class="acf-plugin-upgrade-notice">' . $response->notice . '</div>'; |
| 402 |
|
| 403 |
} |
| 404 |
|
| 405 |
} |
| 406 |
*/ |
| 407 |
|
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
/* |
| 412 |
* acf_updates |
| 413 |
* |
| 414 |
* The main function responsible for returning the one true acf_updates instance to functions everywhere. |
| 415 |
* Use this function like you would a global variable, except without needing to declare the global. |
| 416 |
* |
| 417 |
* Example: <?php $acf_updates = acf_updates(); ?> |
| 418 |
* |
| 419 |
* @type function |
| 420 |
* @date 9/4/17 |
| 421 |
* @since 5.5.12 |
| 422 |
* |
| 423 |
* @param n/a |
| 424 |
* @return (object) |
| 425 |
*/ |
| 426 |
|
| 427 |
function acf_updates() { |
| 428 |
|
| 429 |
global $acf_updates; |
| 430 |
|
| 431 |
if( !isset($acf_updates) ) { |
| 432 |
|
| 433 |
$acf_updates = new acf_updates(); |
| 434 |
|
| 435 |
} |
| 436 |
|
| 437 |
return $acf_updates; |
| 438 |
|
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
/* |
| 443 |
* acf_register_plugin_update |
| 444 |
* |
| 445 |
* alias of acf_updates()->add_plugin() |
| 446 |
* |
| 447 |
* @type function |
| 448 |
* @date 12/4/17 |
| 449 |
* @since 5.5.10 |
| 450 |
* |
| 451 |
* @param $post_id (int) |
| 452 |
* @return $post_id (int) |
| 453 |
*/ |
| 454 |
|
| 455 |
function acf_register_plugin_update( $plugin ) { |
| 456 |
|
| 457 |
acf_updates()->add_plugin( $plugin ); |
| 458 |
|
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
endif; // class_exists check |
| 463 |
|
| 464 |
?> |