Skip to content

Instantly share code, notes, and snippets.

@ideag
Last active August 29, 2015 14:24
Show Gist options
  • Save ideag/a8affd669869611676ee to your computer and use it in GitHub Desktop.
Save ideag/a8affd669869611676ee to your computer and use it in GitHub Desktop.
a concept snippet for WordPress plugin to ensure it always enqueues the latest available version of FontAwesome via BootstrapCDN. Makes use of jsDelivr API to find the latest version of FontAwesome.
<?php
add_action( 'plugins_loaded', array( 'tinyConcept', 'init' ) );
register_activation_hook( __FILE__, array( 'tinyConcept', 'activate' ) );
register_deactivation_hook( __FILE__, array( 'tinyConcept', 'deactivate' ) );
class tinyConcept {
private static $fontawesome = '4.2.0';
// init plugin
public static function init() {
// get FA version from transient
self::$fontawesome = self::get_fontawesome_version( self::$fontawesome, false );
// hook into cron
add_action( 'tinyconcept_daily', array( 'tinyConcept', 'get_fontawesome_version' ) );
// enqueue FA style
add_action( 'wp_enqueue_scripts', array( 'tinyConcept', 'style' ), 9 );
}
// get FA version from transient, if transient is empty and $fetch === true, get latest version via API
private static function get_fontawesome_version( $current= false, $fetch = true ) {
$version = get_transient('tinyconcept_fontawesome');
if ( false === $version && true === $fetch ) {
$response = wp_remote_get( 'http://api.jsdelivr.com/v1/bootstrap/libraries/font-awesome' );
if ( !is_wp_error($response) ) {
$response = wp_remote_retrieve_body( $response );
if ( !is_wp_error($response) ) {
$response = json_decode( $response, true );
if ( isset( $response[0]['lastversion']) ) {
$version = $response[0]['lastversion'];
set_transient( 'tinyconcept_fontawesome', $version, DAY_IN_SECONDS + HOUR_IN_SECONDS );
}
}
}
}
if ( 1 == version_compare( $version, $current) ) {
$version = $version;
} else {
$version = $current;
}
$version = apply_filters( 'tinyconcept_fontawesome_version', $version );
return $version;
}
// create WP-Cron hook on activation
public static function activate() {
wp_schedule_event( time(), 'daily', 'tinyconcept_daily' );
}
// remove WP-Cron hook on deactivation
public static function deactivate() {
wp_clear_scheduled_hook( 'tinyconcept_daily' );
}
// enqueue FA style via BootstrapCDN, protocol-agnostic.
public static function style() {
wp_register_style( 'font-awesome', '//netdna.bootstrapcdn.com/font-awesome/' . self::$fontawesome . '/css/font-awesome.css' );
wp_enqueue_style( 'font-awesome' );
}
}
?>
@ideag
Copy link
Author

ideag commented Jul 12, 2015

Highlights:

  • it uses jsDelivr API to discover the latest version of FontAwesome. It is the recomended method by FontAwesome people [ https://github.com/FortAwesome/Font-Awesome/issues/3115 ];
  • remote calls are made via WP Cron, not on the usual pageloads, to avoid affecting performance;
  • latest version is stored in transient that has a higher expiration (25 hours) than the frequency of cron task (24 hours);
  • if transient is not set, version defaults to the one that was hardcoded into the plugin;
  • plugin provides a filter for the version number.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment