|
5 | 5 | Description: Inserts the Northeastern University global header, footer, and TrustArc cookie consent manager. Requires wp_body_open() under the body tag to display the global header. |
6 | 6 | Author: Northeastern University ITS Web Solutions |
7 | 7 | Author URI: https://its.northeastern.edu |
8 | | -Version: 1.2.2 |
| 8 | +Version: 1.3.0 |
9 | 9 | */ |
10 | 10 |
|
| 11 | +if (!defined('ABSPATH')) { exit; } |
| 12 | + |
| 13 | +const NU_GLOBAL_ELEMENTS_PLUGIN_VER = "1.3.0"; |
| 14 | +const NU_GLOBAL_ELEMENTS_PLUGIN_MANIFEST_URL = "https://its-digital-technology.github.io/global-elements-wordpress/manifest/info.json"; |
| 15 | + |
11 | 16 | /** |
12 | 17 | * Get options values for plugin |
13 | 18 | */ |
@@ -222,3 +227,164 @@ function nu_global_elements_link( $links ) { |
222 | 227 | ); |
223 | 228 | return $links; |
224 | 229 | } |
| 230 | + |
| 231 | +/** |
| 232 | + * Check for updates to this plugin and if available allow updating through WP admin plugin manager |
| 233 | + */ |
| 234 | + |
| 235 | + |
| 236 | +if( ! class_exists( 'UpdateChecker' ) ) { |
| 237 | + |
| 238 | + class UpdateChecker{ |
| 239 | + |
| 240 | + public $plugin_slug; |
| 241 | + public $version; |
| 242 | + public $cache_key; |
| 243 | + public $cache_allowed; |
| 244 | + |
| 245 | + public function __construct() { |
| 246 | + |
| 247 | + $this->plugin_slug = plugin_basename( __DIR__ ); |
| 248 | + $this->version = NU_GLOBAL_ELEMENTS_PLUGIN_VER; |
| 249 | + $this->cache_key = 'global_elements_updater'; |
| 250 | + $this->cache_allowed = false; |
| 251 | + |
| 252 | + add_filter( 'plugins_api', array( $this, 'info' ), 20, 3 ); |
| 253 | + add_filter( 'site_transient_update_plugins', array( $this, 'update' ) ); |
| 254 | + add_action( 'upgrader_process_complete', array( $this, 'purge' ), 10, 2 ); |
| 255 | + |
| 256 | + } |
| 257 | + |
| 258 | + public function request(){ |
| 259 | + |
| 260 | + $remote = get_transient( $this->cache_key ); |
| 261 | + |
| 262 | + if( false === $remote || ! $this->cache_allowed ) { |
| 263 | + |
| 264 | + $remote = wp_remote_get( |
| 265 | + NU_GLOBAL_ELEMENTS_PLUGIN_MANIFEST_URL, |
| 266 | + array( |
| 267 | + 'timeout' => 10, |
| 268 | + 'headers' => array( |
| 269 | + 'Accept' => 'application/json' |
| 270 | + ) |
| 271 | + ) |
| 272 | + ); |
| 273 | + |
| 274 | + if( |
| 275 | + is_wp_error( $remote ) |
| 276 | + || 200 !== wp_remote_retrieve_response_code( $remote ) |
| 277 | + || empty( wp_remote_retrieve_body( $remote ) ) |
| 278 | + ) { |
| 279 | + return false; |
| 280 | + } |
| 281 | + |
| 282 | + set_transient( $this->cache_key, $remote, DAY_IN_SECONDS ); |
| 283 | + |
| 284 | + } |
| 285 | + |
| 286 | + $remote = json_decode( wp_remote_retrieve_body( $remote ) ); |
| 287 | + |
| 288 | + return $remote; |
| 289 | + |
| 290 | + } |
| 291 | + |
| 292 | + |
| 293 | + function info( $res, $action, $args ) { |
| 294 | + |
| 295 | + // do nothing if you're not getting plugin information right now |
| 296 | + if( 'plugin_information' !== $action ) { |
| 297 | + return $res; |
| 298 | + } |
| 299 | + |
| 300 | + // do nothing if it is not our plugin |
| 301 | + if( $this->plugin_slug !== $args->slug ) { |
| 302 | + return $res; |
| 303 | + } |
| 304 | + |
| 305 | + // get updates |
| 306 | + $remote = $this->request(); |
| 307 | + |
| 308 | + if( ! $remote ) { |
| 309 | + return $res; |
| 310 | + } |
| 311 | + |
| 312 | + $res = new stdClass(); |
| 313 | + |
| 314 | + $res->name = $remote->name; |
| 315 | + $res->slug = $remote->slug; |
| 316 | + $res->version = $remote->version; |
| 317 | + $res->tested = $remote->tested; |
| 318 | + $res->requires = $remote->requires; |
| 319 | + $res->author = $remote->author; |
| 320 | + $res->author_profile = $remote->author_profile; |
| 321 | + $res->download_link = $remote->download_url; |
| 322 | + $res->trunk = $remote->download_url; |
| 323 | + $res->requires_php = $remote->requires_php; |
| 324 | + $res->last_updated = $remote->last_updated; |
| 325 | + |
| 326 | + $res->sections = array( |
| 327 | + 'description' => $remote->sections->description, |
| 328 | + 'installation' => $remote->sections->installation, |
| 329 | + 'changelog' => $remote->sections->changelog |
| 330 | + ); |
| 331 | + |
| 332 | + if( ! empty( $remote->banners ) ) { |
| 333 | + $res->banners = array( |
| 334 | + 'low' => $remote->banners->low, |
| 335 | + 'high' => $remote->banners->high |
| 336 | + ); |
| 337 | + } |
| 338 | + |
| 339 | + return $res; |
| 340 | + |
| 341 | + } |
| 342 | + |
| 343 | + public function update( $transient ) { |
| 344 | + |
| 345 | + if ( empty($transient->checked ) ) { |
| 346 | + return $transient; |
| 347 | + } |
| 348 | + |
| 349 | + $remote = $this->request(); |
| 350 | + |
| 351 | + if( |
| 352 | + $remote |
| 353 | + && version_compare( $this->version, $remote->version, '<' ) |
| 354 | + && version_compare( $remote->requires, get_bloginfo( 'version' ), '<=' ) |
| 355 | + && version_compare( $remote->requires_php, PHP_VERSION, '<' ) |
| 356 | + ) { |
| 357 | + $res = new stdClass(); |
| 358 | + $res->slug = $this->plugin_slug; |
| 359 | + $res->plugin = plugin_basename( __FILE__ ); |
| 360 | + $res->new_version = $remote->version; |
| 361 | + $res->tested = $remote->tested; |
| 362 | + $res->package = $remote->download_url; |
| 363 | + |
| 364 | + $transient->response[ $res->plugin ] = $res; |
| 365 | + |
| 366 | + } |
| 367 | + |
| 368 | + return $transient; |
| 369 | + |
| 370 | + } |
| 371 | + |
| 372 | + public function purge( $upgrader, $options ){ |
| 373 | + |
| 374 | + if ( |
| 375 | + $this->cache_allowed |
| 376 | + && 'update' === $options['action'] |
| 377 | + && 'plugin' === $options[ 'type' ] |
| 378 | + ) { |
| 379 | + // just clean the cache when new plugin version is installed |
| 380 | + delete_transient( $this->cache_key ); |
| 381 | + } |
| 382 | + |
| 383 | + } |
| 384 | + |
| 385 | + |
| 386 | + } |
| 387 | + |
| 388 | + new UpdateChecker(); |
| 389 | + |
| 390 | +} |
0 commit comments