Skip to content

Instantly share code, notes, and snippets.

@mattkeys
Created August 9, 2016 03:27
Show Gist options
  • Save mattkeys/13e004734c1ed292f6d9f4883e79f0f0 to your computer and use it in GitHub Desktop.
Save mattkeys/13e004734c1ed292f6d9f4883e79f0f0 to your computer and use it in GitHub Desktop.
/**
* Get the Font Awesome CSS.
*
* @since 1.0.0
*
* @param string $url URL of the remote stylesheet.
* @param string $version Version of Font Awesome to fetch.
*
* @return string $response Font Awesome CSS, from either:
* 1. transient,
* 2. wp_remote_get(), or
* 3. fallback CSS.
*/
private function get_css( $url, $version ) {
// First try getting the transient CSS.
$response = $this->get_transient_css( $version );
/**
* Filter the force fallback flag.
*
* @since 1.0.4
*
* @param bool Whether or not to force the fallback CSS.
*/
$force_fallback = apply_filters( 'bfa_force_fallback', false );
// Next, try fetching the CSS from the remote jsDelivr CDN.
if ( ! $response && ! $force_fallback ) {
$response = $this->get_remote_css( $url, $version );
}
/**
* Use the local fallback if both the transient and wp_remote_get()
* methods fail, or if fallback is forced with bfa_force_fallback filter.
*/
if ( $force_fallback || is_wp_error( $response ) ) {
// Log the CSS fetch error.
if ( ! $force_fallback ) {
$this->set_error( 'css', $response->get_error_code(), $response->get_error_message() . " (URL: $url)" );
}
// Use the local fallback CSS.
$response = $this->fallback_data['css'];
// Update the version string to match the fallback version.
$this->font_awesome_version = $this->fallback_data['version'];
// Update the stylesheet URL to match the fallback version.
$this->stylesheet_url = $this->fallback_data['url'];
}
return $response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment