Last active
August 29, 2015 14:24
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Highlights: