Skip to content

Commit a66e821

Browse files
Merge pull request #5 from ITS-Digital-Technology/feature/add-wp-plugin-update-functionality
Feature/add wp plugin update functionality
2 parents 89ad372 + 007407f commit a66e821

File tree

4 files changed

+195
-9
lines changed

4 files changed

+195
-9
lines changed

.nojekyll

Whitespace-only changes.

README.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33
## Introduction
44

5-
Simple Wordpress Plugin developed by the Northeastern University ITS Web Solutions team. Loads the latest version of [kernl(ui) Global Elements](https://northeastern.netlify.app/pattern-library/page-chrome/global-elements/) to your website, plus the code for the [TrustArc](https://trustarc.com/) cookie consent manager.
5+
A WordPress plugin developed by the Northeastern University ITS Web Solutions team. Loads the latest version of [kernl(ui) Global Elements](https://northeastern.netlify.app/pattern-library/page-chrome/global-elements/) to your website, including the code for the [TrustArc](https://trustarc.com/) cookie consent manager.
66

7-
The global elements package includes a few elements that should be included on almost all Northeastern sites. They have dynamic content and fixed designs for consistency across sites.
7+
The global elements package includes a few elements that should be included on almost all Northeastern sites. They have dynamic content and fixed designs for consistency across sites. TrustArc allows users to set their cookie preferences, and is now required on all Northestern University websites.
88

99
[Learn more about the kernl(ui) design system](https://northeastern.netlify.app/)
1010

11-
TrustArc allows users to set their cookie preferences, and is now required on all Northestern University websites.
12-
1311
## Requirements
1412
- **Global Header.** In order for the global header to display, the active theme must support a call to `wp_body_open()` after the opening `<body>` tag:
1513
```php
@@ -22,7 +20,8 @@ TrustArc allows users to set their cookie preferences, and is now required on al
2220

2321
## Installation
2422

25-
1. Download repository as a `.zip` file on GitHub
26-
2. Upload the `.zip` file to the WordPress site's plugins
27-
3. Activate the new Plugin once it has finished uploading
28-
4. If you need to disable this plugin adding either the global header, footer, or TrustArc code, you can do so from the settings page for this plugin in wp-admin.
23+
1. Download the latest version of the plugin from [GitHub](https://github.com/ITS-Digital-Technology/global-elements-wordpress/releases/latest/download/global-elements-wordpress.zip).
24+
2. Add the new plugin through WP Admin, uploading the `.zip` file from step 1.
25+
3. Activate the new plugin once it has finished uploading.
26+
4. If you need to stop this plugin from displaying the global header, footer, or TrustArc elements, you can do so from the settings page for this plugin in WP Admin.
27+
5. If you need more detailed instructions, see this [KB article](https://service.northeastern.edu/tech?id=kb_article_view&sysparm_article=KB000022192).

manifest/info.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name" : "Northeastern Global Elements",
3+
"slug" : "global-elements-wordpress",
4+
"author" : "<a href='https://its.northeastern.edu/'>ITS Digital Technology & Services</a>",
5+
"author_profile" : "https://its.northeastern.edu/",
6+
"version" : "1.3.0",
7+
"download_url" : "https://github.com/ITS-Digital-Technology/global-elements-wordpress/releases/download/v1.3.0/global-elements-wordpress.zip",
8+
"requires" : "5.6",
9+
"tested" : "6.6.1",
10+
"requires_php" : "5.6",
11+
"last_updated" : "2024-08-12 16:00:00",
12+
"sections" : {
13+
"description" : "A Wordpress plugin developed by the Northeastern University ITS Web Solutions team that inserts the Northeastern University global header, footer, and TrustArc cookie consent manager. Each elemenent can be enabled/disabled from the plugin's settings.",
14+
"installation" : "Click the activate button to install. In order for the global header to display, the active theme must support a call to wp_body_open() after the opening &lt;body&gt; tag.",
15+
"changelog" : "Fixed 'unexpected output' bug and tweaked TrustArc link formatting."
16+
},
17+
"banners" : {
18+
"low" : "",
19+
"high" : ""
20+
}
21+
}

nu_global_elements.php

Lines changed: 167 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55
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.
66
Author: Northeastern University ITS Web Solutions
77
Author URI: https://its.northeastern.edu
8-
Version: 1.2.2
8+
Version: 1.3.0
99
*/
1010

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+
1116
/**
1217
* Get options values for plugin
1318
*/
@@ -222,3 +227,164 @@ function nu_global_elements_link( $links ) {
222227
);
223228
return $links;
224229
}
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

Comments
 (0)