diff --git a/lib/better-font-awesome-library/README.md b/lib/better-font-awesome-library/README.md new file mode 100755 index 0000000..2185cdd --- /dev/null +++ b/lib/better-font-awesome-library/README.md @@ -0,0 +1,290 @@ +Better Font Awesome Library +=========================== + +*The easiest way to integrate Font Awesome into your WordPress project.* + +## Table of contents ## +1. [Introduction](https://github.com/MickeyKay/better-font-awesome-library#introduction) +1. [Features](https://github.com/MickeyKay/better-font-awesome-library#features) +1. [Installation](https://github.com/MickeyKay/better-font-awesome-library#installation) +1. [Usage](https://github.com/MickeyKay/better-font-awesome-library#usage) +1. [Initialization Parameters](https://github.com/MickeyKay/better-font-awesome-library#initialization-parameters-args) +1. [Shortcode](https://github.com/MickeyKay/better-font-awesome-library#shortcode) +1. [The Better Font Awesome Library Object](https://github.com/MickeyKay/better-font-awesome-library#the-better-font-awesome-library-object) +1. [Filters](https://github.com/MickeyKay/better-font-awesome-library#filters) +1. [To Do](https://github.com/MickeyKay/better-font-awesome-library#to-do) +1. [Credits](https://github.com/MickeyKay/better-font-awesome-library#credits) + +## Introduction ## +The Better Font Awesome Library allows you to automatically integrate the latest available version of [Font Awesome](http://fontawesome.io/) into your WordPress project, along with accompanying CSS, shortcode, and TinyMCE icon shortcode generator. Furthermore, it generates all the data you need to create new functionality of your own. + +## Features ## +* Automatically fetches the most recent available version of Font Awesome, meaning you no longer need to manually update the version included in your theme/plugin. +* Generates an easy-to-use [PHP object](#the-better-font-awesome-library-object) that contains all relevant info for the version of Font Awesome you're using, including: version, stylesheet URL, array of available icons, and prefix used (`icon` or `fa`). +* CDN speeds - Font Awesome CSS is pulled from the super-fast and reliable [jsDelivr CDN](http://www.jsdelivr.com/#!fontawesome). +* Includes a TinyMCE drop-down shortcode generator. +* Includes a local copy of Font Awesome to use as a fallback in case the remote fetch fails (or you can specify your own with the [`bfa_fallback_directory_path`](https://github.com/MickeyKay/better-font-awesome-library#bfa_fallback_directory_path) filter). +* Utilizes transients to optimize for speed and performance. + +## Installation ## +The Better Font Awesome Library contains a [Git submodule](http://git-scm.com/book/en/Git-Tools-Submodules) which will require you to recursively clone this repo: +``` +git clone --recursive https://github.com/MickeyKay/better-font-awesome-library.git +``` + +Alternately, if you've already cloned the repo and need to add the submodules, you can run the following command: +``` +// Initialize and update all submodules. +git submodule update --init --recursive +``` + +## Usage ## +1. Copy the /better-font-awesome-library folder into your project. + +2. Add the following code to your main plugin file or your theme's functions.php file. + ```php + add_action( 'init', 'my_prefix_load_bfa' ); + /** + * Initialize the Better Font Awesome Library. + * + * (see usage notes below on proper hook priority) + */ + function my_prefix_load_bfa() { + + // Include the main library file. Make sure to modify the path to match your directory structure. + require_once ( dirname( __FILE__ ) . '/better-font-awesome-library/better-font-awesome-library.php' ); + + // Set the library initialization args (defaults shown). + $args = array( + 'version' => 'latest', + 'minified' => true, + 'remove_existing_fa' => false, + 'load_styles' => true, + 'load_admin_styles' => true, + 'load_shortcode' => true, + 'load_tinymce_plugin' => true, + ); + + // Initialize the Better Font Awesome Library. + Better_Font_Awesome_Library::get_instance( $args ); + } +``` + +3. If desired, use the [Better Font Awesome Library object](#the-better-font-awesome-library-object) to manually include Font Awesome CSS, output lists of available icons, create your own shortcodes, and much more. + +#### Usage Notes #### +The Better Font Awesome Library is designed to work in conjunction with the [Better Font Awesome](https://wordpress.org/plugins/better-font-awesome/) WordPress plugin. The plugin initializes this library (with its initialization args) on the `init` hook, priority `5`. When using the Better Font Awesome Library in your project, you have two options: + +1. Initialize later, to ensure that any Better Font Awesome plugin settings override yours (this is the default behavior, shown above by initializing the library on the `init` hook with default priority `10`. +1. Initialize earlier, to "take over" and prevent Better Font Awesome settings from having an effect. + +## Initialization Parameters ($args) ## +The following arguments can be used to initialize the library using `Better_Font_Awesome_Library::get_instance( $args )`: + +#### $args['version'] #### +(string) Which version of Font Awesome you want to use. +* `'latest'` (default) - always use the latest available version. +* `'3.2.1'` - any existing Font Awesome version number. + +#### $args['minified'] #### +(boolean) Use minified Font Awesome CSS. +* `true` (default) - uses minifed CSS. +* `false` - uses unminified CSS. + +#### $args['remove_existing_fa'] #### +(boolean) Attempts to remove existing Font Awesome styles and shortcodes. This can be useful to prevent conflicts with other themes/plugins, but is no guarantee. +* `true` +* `false` (default) + +#### $args['load_styles'] #### +(boolean) Automatically loads Font Awesome CSS on the **front-end** of your site using `wp_enqueue_scripts()`. +* `true` (default) +* `false` - use this if you don't want to load the Font Awesome CSS on the front-end, or wish to do it yourself. + +#### $args['load_admin_styles'] #### +(boolean) Automatically loads Font Awesome CSS on the **admin** of your site using `admin_enqueue_scripts()`. +* `true` (default) +* `false` - use this if you don't want to load the Font Awesome CSS in the admin, or wish to do it yourself. + +#### $args['load_shortcode'] #### +(boolean) Loads the included `[icon]` [shortcode](https://github.com/MickeyKay/better-font-awesome-library#shortcode). +* `true` (default) +* `false` + +#### load_tinymce_plugin #### +(boolean) Loads a TinyMCE drop-down list of available icons (based on the active Font Awesome version), which generates an `[icon]` shortcode. +* `true` (default) +* `false` + +## Shortcode ## +If either the `$args['load_shortcode']` or `$args['load_tinymce_plugin']` initialization arg is set to `true`, then the Better Font Awesome Library will include an icon shortcode that can be used as follows: +``` +[icon name="star" class="2x spin" unprefixed_class="my-custom-class"] +``` + +**name** +The unprefixed icon name (e.g. star). The version-specific prefix will be automatically prepended. + +**class** +Unprefixed [Font Awesome icon classes](http://fortawesome.github.io/Font-Awesome/examples/). The version-specific prefix will be automatically prepended to each class. + +**unprefixed_class** +Any additional classes that you wish to remain unprefixed (e.g. my-custom-class). + +#### Shortcode Output #### +The example shortcode above would output the following depending on which version of Font Awesome you've selected: + +**Version 4+** +``` + +``` +**Version 3** +``` + +``` + +## The Better Font Awesome Library Object ## +The Better Font Awesome Library object can be accessed with the following code: +`Better_Font_Awesome_Library::get_instance();` + +The object has the following public methods: +#### get_version() #### +(string) Returns the active version of Font Awesome being used. + +#### get_stylesheet_url() #### +(string) Returns the active Font Awesome stylesheet URL. + +#### get_icons() #### +(array) Returns an associative array of icon hex values (index, e.g. \f000) and unprefixed icon names (values, e.g. rocket) for all available icons in the active Font Awesome version. + +#### get_prefix() #### +(string) Returns the version-dependent prefix ('fa' or 'icon') that is used in the icons' CSS classes. + +#### get_api_data() #### +(object) Returns version data for the remote jsDelivr CDN (uses [jsDelivr API](https://github.com/jsdelivr/api)). Includes all available versions and latest version. + +#### get_errors() #### +(array) Returns all library errors, including API and CDN fetch failures. + +#### Example: #### +```php +// Initialize the library with custom args. +Better_Font_Awesome_Library::get_instance( $args ); + +// Get the active Better Font Awesome Library Object. +$my_bfa = Better_Font_Awesome_Library::get_instance(); + +// Get info on the Better Font Awesome Library object. +$version = $my_bfa->get_version(); +$stylesheet_url = $my_bfa->get_stylesheet_url(); +$prefix = $my_bfa->get_prefix(); +$icons = $my_bfa->get_icons(); + +// Output all available icons. +foreach ( $icons as $icon ) { + echo $icon . '
'; +} +``` + +## Filters ## +The Better Font Awesome Library applies the following filters: + +#### bfa_init_args #### +Applied to the initialization arguments after they have been parsed with default args, but before they are used to fetch any Font Awesome data. + +**Parameters** + +* `$init_args` (array) + +#### bfa_wp_remote_get_args #### +Applied to arguments passed to all `wp_remote_get()` calls (useful for adjusting the timeout if needed). + +**Parameters** + +* `$wp_remote_get_args` (array) + +#### bfa_fallback_directory_path #### +Applied to the fallback directory path before setting up any fallback CSS info. Can be used to specify an alternate fallback directory to replace the default fallback directory. + +*The path must be to a local, non-remote, directory.* + +**Parameters** + +* `$path` (string) + +#### bfa_api_transient_expiration #### +Applied to the API (version information) transient [expiration](http://codex.wordpress.org/Transients_API#Using_Transients). Can be used to increase/decrease the expiration as desired. + +**Parameters** + +* `$api_expiration` (int) + +#### bfa_css_transient_expiration #### +Applied to the CSS stylesheet data transient [expiration](http://codex.wordpress.org/Transients_API#Using_Transients). Can be used to increase/decrease the expiration as desired. + +**Parameters** + +* `$css_expiration` (int) + +#### bfa_icon_list #### +Applied to the icon array after it has been generated from the Font Awesome stylesheet, and before it is assigned to the Better Font Awesome Library object's `$icons` property. + +**Parameters** + +* `$icons` (array) + +#### bfa_prefix #### +Applied to the Font Awesome prefix ('fa' or 'icon') before it is assigned to the Better Font Awesome Library object's `$prefix` property. + +**Parameters** + +* `$prefix` (string) + +#### bfa_icon_class #### +Applied to the classes that are output on each icon's `` element. + +**Parameters** + +* `$class` (string) + +#### bfa_icon #### +Applied to the entire `` element that is output for each icon. + +**Parameters** + +* `$output` (string) + +#### bfa_force_fallback #### +Applied to the boolean that determines whether or not to force the included fallback version of Font Awesome to load. This can be useful if you're having trouble with delays or timeouts. + +**Parameters** + +* `$force_fallback` (false) + +#### bfa_show_errors #### +Applied to the boolean that determines whether or not to suppress all Font Awesome warnings that normally display in the admin. + +**Parameters** + +* `$show_errors` (true) + + +## To Do ## +Ideas? File an issue or add a pull request! +* Add README section on manually updating the fallback version. +* Remove existing FA? - move to later hook so that it works for styles enqueued via shortcode (= wp_footer basically) + +## Credits ## +Special thanks to the following folks and their plugins for inspiration and support: +* [Font Awesome Icons](http://wordpress.org/plugins/font-awesome/ "Font Awesome Icons") by [Rachel Baker](http://rachelbaker.me/ "Rachel Baker") +* [Font Awesome More Icons](https://wordpress.org/plugins/font-awesome-more-icons/ "Font Awesome More Icons") by [Web Guys](http://webguysaz.com/ "Web Guys") +* [Font Awesome Shortcodes](https://wordpress.org/plugins/font-awesome-shortcodes/) by [FoolsRun](https://profiles.wordpress.org/foolsrun/ "FoolsRun") +* Dmitriy Akulov and the awesome folks at [jsDelivr](http://www.jsdelivr.com/) + +And many thanks to the following folks who helped with testing and QA: +* [Jeffrey Dubinksy](http://vanishingforests.org/) +* [Neil Gee](https://twitter.com/_neilgee) +* [Michael Beil](https://twitter.com/MichaelBeil) +* [Rob Neue](https://twitter.com/rob_neu) +* [Gary Jones](https://twitter.com/GaryJ) +* [Jan Hoek](https://twitter.com/JanHoekdotCom) diff --git a/lib/better-font-awesome-library/better-font-awesome-library.php b/lib/better-font-awesome-library/better-font-awesome-library.php new file mode 100755 index 0000000..ce1c09d --- /dev/null +++ b/lib/better-font-awesome-library/better-font-awesome-library.php @@ -0,0 +1,1290 @@ + 'latest', + 'minified' => true, + 'remove_existing_fa' => false, + 'load_styles' => true, + 'load_admin_styles' => true, + 'load_shortcode' => true, + 'load_tinymce_plugin' => true, + ); + + /** + * Root URL of the library. + * + * @since 1.0.4 + * + * @var string + */ + private $root_url; + + /** + * Args for wp_remote_get() calls. + * + * @since 1.0.0 + * + * @var array + */ + private $wp_remote_get_args = array( + 'timeout' => 10, + 'sslverify' => false, + ); + + /** + * Array to hold the jsDelivr API data. + * + * @since 1.0.0 + * + * @var string + */ + private $api_data = array(); + + /** + * Version of Font Awesome being used. + * + * @since 1.0.0 + * + * @var string + */ + private $font_awesome_version; + + /** + * Font Awesome stylesheet URL. + * + * @since 1.0.0 + * + * @var string + */ + private $stylesheet_url; + + /** + * Font Awesome CSS. + * + * @since 1.0.0 + * + * @var string + */ + private $css; + + /** + * Data associated with the local fallback version of Font Awesome. + * + * @since 1.0.0 + * + * @var string + */ + private $fallback_data = array( + 'directory' => 'lib/fallback-font-awesome/', + 'path' => '', + 'url' => '', + 'version' => '', + 'css' => '', + ); + + /** + * Array of available Font Awesome icon slugs. + * + * @since 1.0.0 + * + * @var string + */ + private $icons = array(); + + /** + * Font Awesome prefix to be used ('icon' or 'fa'). + * + * @since 1.0.0 + * + * @var string + */ + private $prefix; + + /** + * Array to track errors and wp_remote_get() failures. + * + * @since 1.0.0 + * + * @var array + */ + private $errors = array(); + + /** + * Instance of this class. + * + * @since 1.0.0 + * + * @var Better_Font_Awesome_Library + */ + private static $instance = null; + + /** + * Returns the instance of this class, and initializes + * the instance if it doesn't already exist. + * + * @since 1.0.0 + * + * @return Better_Font_Awesome_Library The BFAL object. + */ + public static function get_instance( $args = array() ) { + + // If the single instance hasn't been set, set it now. + if ( null == self::$instance ) { + self::$instance = new self( $args ); + } + + return self::$instance; + + } + + /** + * Better Font Awesome Library constructor. + * + * @since 1.0.0 + * + * @param array $args Initialization arguments. + */ + private function __construct( $args = array() ) { + + // Get initialization args. + $this->args = $args; + + // Load the library functionality. + $this->load(); + + } + + /** + * Set up all plugin library functionality. + * + * @since 1.0.0 + */ + public function load() { + + // Initialize library properties and actions as needed. + $this->initialize( $this->args ); + + // Use the jsDelivr API to fetch info on the jsDelivr Font Awesome CDN. + $this->setup_api_data(); + + // Set the version of Font Awesome to be used. + $this->set_active_version(); + + // Set the URL for the Font Awesome stylesheet. + $this->set_stylesheet_url( $this->font_awesome_version ); + + // Get stylesheet and generate list of available icons in Font Awesome stylesheet. + $this->setup_stylesheet_data(); + + // Add Font Awesome and/or custom CSS to the editor. + $this->add_editor_styles(); + + // Output any necessary admin notices. + add_action( 'admin_notices', array( $this, 'do_admin_notice' ) ); + + /** + * Remove existing Font Awesome CSS and shortcodes if needed. + * + * Use priority 15 to ensure this is done after other plugin + * CSS/shortcodes are loaded. This must run before any other + * style/script/shortcode actions so it doesn't accidentally + * remove them. + */ + if ( $this->args['remove_existing_fa'] ) { + + add_action( 'wp_enqueue_scripts', array( $this, 'remove_font_awesome_css' ), 15 ); + add_action( 'init', array( $this, 'remove_icon_shortcode' ), 20 ); + + } + + /** + * Load front-end scripts and styles. + * + * Use priority 15 to make sure styles/scripts load after other plugins. + */ + if ( $this->args['load_styles'] || $this->args['remove_existing_fa'] ) { + add_action( 'wp_enqueue_scripts', array( $this, 'register_font_awesome_css' ), 15 ); + } + + /** + * Load admin scripts and styles. + * + * Use priority 15 to make sure styles/scripts load after other plugins. + */ + if ( $this->args['load_admin_styles'] || $this->args['load_tinymce_plugin'] ) { + add_action( 'admin_enqueue_scripts', array( $this, 'register_font_awesome_css' ), 15 ); + } + + /** + * Add [icon] shortcode. + * + * Use priority 15 to ensure this is done after removing existing Font + * Awesome CSS and shortcodes. + */ + if ( $this->args['load_shortcode'] || $this->args['load_tinymce_plugin'] ) { + add_action( 'init', array( $this, 'add_icon_shortcode' ), 20 ); + } + + // Load TinyMCE functionality. + if ( $this->args['load_tinymce_plugin'] ) { + + add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) ); + + // Add shortcode insertion button. + add_action( 'media_buttons', array( $this, 'add_insert_shortcode_button' ), 99 ); + + } + + } + + /** + * Do necessary initialization actions. + * + * @since 1.0.0 + */ + private function initialize( $args ) { + + // Parse the initialization args with the defaults. + $this->parse_args( $args ); + + // Setup root URL, which differs for plugins vs. themes. + $this->setup_root_url(); + + // Set fallback stylesheet directory URL and path. + $this->setup_fallback_data(); + + } + + /** + * Parse the initialization args with the defaults and apply bfa_args filter. + * + * @since 1.0.0 + * + * @param array $args Args used to initialize BFAL. + */ + private function parse_args( $args = array() ) { + + // Parse initialization args with defaults. + $this->args = wp_parse_args( $args, $this->default_args ); + + /** + * Filter the initialization args. + * + * @since 1.0.0 + * + * @param array $this->args BFAL initialization args. + */ + $this->args = apply_filters( 'bfa_init_args', $this->args ); + + /** + * Filter the wp_remote_get args. + * + * @since 1.0.0 + * + * @param array $this->wp_remote_get_args BFAL wp_remote_get_args args. + */ + $this->wp_remote_get_args = apply_filters( 'bfa_wp_remote_get_args', $this->wp_remote_get_args ); + + } + + /** + * Set up root URL for library, which differs for plugins vs. themes. + * + * @since 1.0.4 + */ + function setup_root_url() { + + // Get BFA directory and theme root directory paths. + $bfa_directory = dirname(__FILE__); + $theme_directory = get_stylesheet_directory(); + $plugin_dir = plugin_dir_url( __FILE__ ); + + /** + * Check if we're inside a theme or plugin. + * + * If we're in a theme, than plugin_dir_url() will return a + * funky URL that includes the actual file path (e.g. + * /srv/www/site_name/wp-content/...) + */ + $is_theme = false; + if ( strpos( $plugin_dir, $bfa_directory ) !== false ) { + $is_theme = true; + } + + // First check if we're inside a theme. + if ( $is_theme ) { + + // Get relative BFA directory by removing theme root directory path. + $bfa_rel_path = str_replace( $theme_directory, '', $bfa_directory ); + $this->root_url = trailingslashit( get_stylesheet_directory_uri() . $bfa_rel_path ); + + } else { // Otherwise we're inside a plugin. + + $this->root_url = trailingslashit( plugin_dir_url( __FILE__ ) ); + + } + + } + + /** + * Set up data for the local fallback version of Font Awesome. + * + * @since 1.0.0 + */ + private function setup_fallback_data() { + + // Set fallback directory path. + $directory_path = plugin_dir_path( __FILE__ ) . $this->fallback_data['directory']; + + /** + * Filter directory path. + * + * @since 1.0.0 + * + * @param string $directory_path The path to the fallback Font Awesome directory. + */ + $directory_path = trailingslashit( apply_filters( 'bfa_fallback_directory_path', $directory_path ) ); + + // Set fallback path and URL. + $this->fallback_data['path'] = $directory_path . 'css/font-awesome' . $this->get_min_suffix() . '.css'; + $this->fallback_data['url'] = $this->root_url . $this->fallback_data['directory'] . 'css/font-awesome' . $this->get_min_suffix() . '.css'; + + // Get the fallback version based on package.json. + $fallback_json_file_path = $directory_path . 'package.json'; + $fallback_data = json_decode( $this->get_local_file_contents( $fallback_json_file_path ) ); + $this->fallback_data['version'] = $fallback_data->version; + + // Get the fallback CSS. + $this->fallback_data['css'] = $this->get_fallback_css(); + + } + + /** + * Set up data for all versions of Font Awesome available on the jsDelivr + * CDN. + * + * Uses the jsDelivr API. + * + * @since 1.0.0 + */ + private function setup_api_data() { + $this->api_data = $this->fetch_api_data( self::JSDELIVR_API_URL ); + } + + /** + * Fetch the jsDelivr API data. + * + * First check to see if the api-versions transient is set, and if not use + * the jsDelivr API to retrieve all available versions of Font Awesome. + * + * @since 1.0.0 + * + * @return array|WP_ERROR Available CDN Font Awesome versions, or a + * WP_ERROR if the fetch fails. + */ + private function fetch_api_data( $url ) { + + if ( false === ( $response = get_transient( self::SLUG . '-api-versions' ) ) ) { + + $response = wp_remote_get( $url, $this->wp_remote_get_args ); + + if ( 200 == wp_remote_retrieve_response_code( $response ) ) { + + // Decode the API data and grab the versions info. + $json_data = json_decode( wp_remote_retrieve_body( $response ) ); + $response = $json_data[0]; + + /** + * Filter the API transient expiration. + * + * @since 1.0.0 + * + * @param int Expiration for API transient. + */ + $transient_expiration = apply_filters( 'bfa_api_transient_expiration', 12 * HOUR_IN_SECONDS ); + + // Set the API transient. + set_transient( self::SLUG . '-api-versions', $response, $transient_expiration ); + + } elseif ( is_wp_error( $response ) ) { // Check for faulty wp_remote_get() + + $this->set_error( 'api', $response->get_error_code(), $response->get_error_message() . " (URL: $url)" ); + $response = ''; + + } elseif ( isset( $response['response'] ) ) { // Check for 404 and other non-WP_ERROR codes + + $this->set_error( 'api', $response['response']['code'], $response['response']['message'] . " (URL: $url)" ); + $response = ''; + + } else { // Total failsafe + + $this->set_error( 'api', 'Unknown', __( 'The jsDelivr API servers appear to be temporarily unavailable.', 'better-font-awesome' ) . " (URL: $url)" ); + $response = ''; + + } + + } + + return $response; + + } + + /** + * Set the version of Font Awesome to use. + * + * @since 1.0.0 + */ + private function set_active_version() { + + if ( 'latest' == $this->args['version'] ) { + $this->font_awesome_version = $this->get_latest_version(); + } else { + $this->font_awesome_version = $this->args['version']; + } + + } + + /** + * Get the latest available Font Awesome version. + * + * @since 1.0.0 + * + * @return string Latest available Font Awesome version, either via the + * jsDelivr API data (if available), or a best guess based on transient and + * fallback version data. + */ + private function get_latest_version() { + + if ( $this->api_data_exists() ) { + return $this->get_api_value( 'lastversion' ); + } else { + return $this->guess_latest_version(); + } + + } + + /** + * Guess the latest Font Awesome version. + * + * Check both the transient Font Awesome CSS array and the locally-hosted + * version of Font Awesome to determine the latest listed version. + * + * @since 1.0.0 + * + * @return string Latest transient or fallback version of Font Awesome CSS. + */ + private function guess_latest_version() { + + $css_transient_latest_version = $this->get_css_transient_latest_version(); + + if ( version_compare( $css_transient_latest_version, $this->fallback_data['version'], '>' ) ) { + return $css_transient_latest_version; + } else { + return $this->fallback_data['version']; + } + + } + + /** + * Get the latest version saved in the CSS transient. + * + * @since 1.0.0 + * + * @return string Latest version key in the CSS transient array. + * Return '0' if the CSS transient isn't set. + */ + private function get_css_transient_latest_version() { + + $transient_css_array = get_transient( self::SLUG . '-css' ); + + if ( ! empty( $transient_css_array ) ) { + return max( array_keys( $transient_css_array ) ); + } else { + return '0'; + } + + } + + /** + * Determine the remote Font Awesome stylesheet URL based on the selected + * version. + * + * @since 1.0.0 + * + * @param string $version Version of Font Awesome to use. + */ + private function set_stylesheet_url( $version ) { + $this->stylesheet_url = '//cdn.jsdelivr.net/fontawesome/' . $version . '/css/font-awesome' . $this->get_min_suffix() . '.css'; + } + + /** + * Get stylesheet CSS and populate icons array. + * + * @since 1.0.0 + */ + private function setup_stylesheet_data() { + + // Get the Font Awesome CSS. + $this->css = $this->get_css( $this->stylesheet_url, $this->font_awesome_version ); + + // Get the list of available icons from the Font Awesome CSS. + $this->icons = $this->setup_icon_array( $this->css ); + + // Set up prefix based on version ('fa' or 'icon'). + $this->prefix = $this->setup_prefix( $this->font_awesome_version ); + + } + + /** + * 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 ); + + // Next, try fetching the CSS from the remote jsDelivr CDN. + if ( ! $response ) { + $response = $this->get_remote_css( $url, $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 ); + + /** + * 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 ( is_wp_error( $response ) || $force_fallback ) { + + // 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; + + } + + /** + * Get the transient copy of the CSS for the specified version. + * + * @since 1.0.0 + * + * @param string $version Font Awesome version to check. + * + * @return string Transient CSS if it exists, otherwise null. + */ + private function get_transient_css( $version ) { + + $transient_css_array = get_transient( self::SLUG . '-css' ); + return isset( $transient_css_array[ $version ] ) ? $transient_css_array[ $version ] : ''; + + } + + /** + * Get the CSS from the remote jsDelivr CDN. + * + * @since 1.0.0 + * + * @param string $url URL for the remote stylesheet. + * @param string $version Font Awesome version to fetch. + * + * @return string|WP_ERROR $response Remote CSS, or WP_ERROR if + * wp_remote_get() fails. + */ + private function get_remote_css( $url, $version ) { + + // Get the remote Font Awesome CSS. + $url = set_url_scheme( $url ); + $response = wp_remote_get( $url, $this->wp_remote_get_args ); + + /** + * If fetch was successful, return the remote CSS, and set the CSS + * transient for this version. Otherwise, return a WP_Error object. + */ + if ( 200 == wp_remote_retrieve_response_code( $response ) ) { + + $response = wp_remote_retrieve_body( $response ); + $this->set_css_transient( $version, $response ); + + } elseif ( is_wp_error( $response ) ) { // Check for faulty wp_remote_get() + $response = $response; + } elseif ( isset( $response['response'] ) ) { // Check for 404 and other non-WP_ERROR codes + $response = new WP_Error( $response['response']['code'], $response['response']['message'] . " (URL: $url)" ); + } else { // Total failsafe + $response = ''; + } + + return $response; + + } + + /** + * Set the CSS transient array. + * + * @since 1.0.0 + * + * @param string $version Version of Font Awesome for which to set the + * transient. + * @param string $value CSS for corresponding version of Font Awesome. + */ + private function set_css_transient( $version, $value ) { + + /** + * Get the transient array, which contains data for multiple Font + * Awesome version. + */ + $transient_css_array = get_transient( self::SLUG . '-css' ); + + // Set the new value for the specified Font Awesome version. + $transient_css_array[ $version ] = $value; + + /** + * Filter the CSS transient expiration. + * + * @since 1.0.0 + * + * @param int Expiration for the CSS transient. + */ + $transient_expiration = apply_filters( 'bfa_css_transient_expiration', 30 * DAY_IN_SECONDS ); + + // Set the new CSS array transient. + set_transient( self::SLUG . '-css', $transient_css_array, $transient_expiration ); + + } + + /** + * Get the CSS of the local fallback Font Awesome version. + * + * @since 1.0.0 + * + * @return string Contents of the local fallback Font Awesome stylesheet. + */ + private function get_fallback_css() { + return $this->get_local_file_contents( $this->fallback_data['path'] ); + } + + /** + * Get array of icons from the Font Awesome CSS. + * + * @since 1.0.0 + * + * @param string $css The Font Awesome CSS. + * + * @return array All available icon names (e.g. adjust, car, pencil). + */ + private function setup_icon_array( $css ) { + + $icons = array(); + $hex_codes = array(); + + /** + * Get all CSS selectors that have a "content:" pseudo-element rule, + * as well as all associated hex codes. + */ + preg_match_all( '/\.(icon-|fa-)([^,}]*)\s*:before\s*{\s*(content:)\s*"(\\\\[^"]+)"/s', $css, $matches ); + $icons = $matches[2]; + $hex_codes = $matches[4]; + + // Add hex codes as icon array index. + $icons = array_combine( $hex_codes, $icons ); + + // Alphabetize the icons array by icon name. + asort( $icons ); + + /** + * Filter the array of available icons. + * + * @since 1.0.0 + * + * @param array $icons Array of all available icons. + */ + $icons = apply_filters( 'bfa_icon_list', $icons ); + + return $icons; + + } + + /** + * Get the Font Awesosome prefix ('fa' or 'icon'). + * + * @since 1.0.0 + * + * @param string $version Font Awesome version being used. + * + * @return string $prefix 'fa' or 'icon', depending on the version. + */ + private function setup_prefix( $version ) { + + if ( 0 <= version_compare( $version, '4' ) ) { + $prefix = 'fa'; + } else { + $prefix = 'icon'; + } + + /** + * Filter the Font Awesome prefix. + * + * @since 1.0.0 + * + * @param string $prefix Font Awesome prefix ('icon' or 'fa'). + */ + $prefix = apply_filters( 'bfa_prefix', $prefix ); + + return $prefix; + + } + + /** + * Remove styles that include 'fontawesome' or 'font-awesome' in their slug. + * + * @since 1.0.0 + */ + public function remove_font_awesome_css() { + + global $wp_styles; + + // Loop through all registered styles and remove any that appear to be Font Awesome. + foreach ( $wp_styles->registered as $script => $details ) { + + if ( false !== strpos( $script, 'fontawesome' ) || false !== strpos( $script, 'font-awesome' ) ) { + wp_dequeue_style( $script ); + } + + } + + } + + /** + * Remove [icon] shortcode. + * + * @since 1.0.0 + */ + public function remove_icon_shortcode() { + remove_shortcode( 'icon' ); + } + + /** + * Add [icon] shortcode. + * + * Usage: + * [icon name="flag" class="fw 2x spin" unprefixed_class="custom_class"] + * + * @since 1.0.0 + */ + public function add_icon_shortcode() { + add_shortcode( 'icon', array( $this, 'render_shortcode' ) ); + } + + /** + * Render [icon] shortcode. + * + * Usage: + * [icon name="flag" class="fw 2x spin" unprefixed_class="custom_class"] + * + * @param array $atts Shortcode attributes. + * @return string $output Icon HTML (e.g. ). + */ + public function render_shortcode( $atts ) { + + extract( shortcode_atts( array( + 'name' => '', + 'class' => '', + 'unprefixed_class' => '', + 'title' => '', /* For compatibility with other plugins */ + 'size' => '', /* For compatibility with other plugins */ + 'space' => '', + ), $atts ) + ); + + /** + * Include for backwards compatibility with Font Awesome More Icons plugin. + * + * @see https://wordpress.org/plugins/font-awesome-more-icons/ + */ + $title = $title ? 'title="' . $title . '" ' : ''; + $space = 'true' == $space ? ' ' : ''; + $size = $size ? ' '. $this->prefix . '-' . $size : ''; + + // Remove "icon-" and "fa-" from name + // This helps both: + // 1. Incorrect shortcodes (when user includes full class name including prefix) + // 2. Old shortcodes from other plugins that required prefixes + + /** + * Strip 'icon-' and 'fa-' from the BEGINNING of $name. + * + * This corrects for: + * 1. Incorrect shortcodes (when user includes full class name including prefix) + * 2. Old shortcodes from other plugins that required prefixes + */ + $prefixes = array( 'icon-', 'fa-' ); + foreach ( $prefixes as $prefix ) { + + if ( substr( $name, 0, strlen( $prefix ) ) == $prefix ) { + $name = substr( $name, strlen( $prefix ) ); + } + + } + + $name = str_replace( 'fa-', '', $name ); + + // Add the version-specific prefix back on to $name, if it exists. + $icon_name = $this->prefix ? $this->prefix . '-' . $name : $name; + + // Remove "icon-" and "fa-" from the icon class. + $class = str_replace( 'icon-', '', $class ); + $class = str_replace( 'fa-', '', $class ); + + // Remove extra spaces from the icon class. + $class = trim( $class ); + $class = preg_replace( '/\s{3,}/', ' ', $class ); + + // Add the version-specific prefix back on to each class. + $class_array = explode( ' ', $class ); + foreach ( $class_array as $index => $class ) { + $class_array[ $index ] = $this->prefix ? $this->prefix . '-' . $class : $class; + } + $class = implode( ' ', $class_array ); + + // Add unprefixed classes. + $class .= $unprefixed_class ? ' ' . $unprefixed_class : ''; + + /** + * Filter the icon class. + * + * @since 1.0.0 + * + * @param string $class Classes attached to the icon. + */ + $class = apply_filters( 'bfa_icon_class', $class, $name ); + + // Generate the HTML icon element output. + $output = sprintf( '%s', + $this->prefix, + $icon_name, + $class, + $size, + $title, + $space + ); + + /** + * Filter the icon output. + * + * @since 1.0.0 + * + * @param string $output Icon output. + */ + return apply_filters( 'bfa_icon', $output ); + + } + + /** + * Register and enqueue Font Awesome CSS. + */ + public function register_font_awesome_css() { + + wp_register_style( self::SLUG . '-font-awesome', $this->stylesheet_url, '', $this->font_awesome_version ); + wp_enqueue_style( self::SLUG . '-font-awesome' ); + + } + + /** + * Add Font Awesome CSS to TinyMCE. + * + * @since 1.0.0 + */ + public function add_editor_styles() { + add_editor_style( $this->stylesheet_url ); + } + + /** + * Load admin CSS. + * + * @since 1.0.0 + */ + public function enqueue_admin_scripts() { + + // Check whether to get minified or non-minified files. + $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; + + // Custom admin CSS. + wp_enqueue_style( self::SLUG . '-admin', $this->root_url . 'css/admin-styles.css' ); + + // Custom admin JS. + wp_enqueue_script( self::SLUG . '-admin', $this->root_url . 'js/admin.js' ); + + // Icon picker JS and CSS. + wp_enqueue_style( 'fontawesome-iconpicker', $this->root_url . 'lib/fontawesome-iconpicker/css/fontawesome-iconpicker' . $suffix . '.css' ); + wp_enqueue_script( 'fontawesome-iconpicker', $this->root_url . 'lib/fontawesome-iconpicker/js/fontawesome-iconpicker' . $suffix . '.js' ); + + // Output PHP variables to JS. + $bfa_vars = array( + 'fa_prefix' => $this->prefix, + 'fa_icons' => $this->get_icons(), + ); + wp_localize_script( self::SLUG . '-admin', 'bfa_vars', $bfa_vars ); + + } + + /** + * [add_insert_shortcode_button description] + * + * @since 1.3.0 + */ + public function add_insert_shortcode_button() { + + ob_start(); + ?> + + +   + + + + + errors ) && apply_filters( 'bfa_show_errors', true ) ) : + ?> +
+

+ +

+ + + get_error('api') ) ) : ?> +

+
+ ' . $this->get_error('api')->get_error_code() . ': ' . $this->get_error('api')->get_error_message() . '' + ); + ?> +

+ + + + get_error('css') ) ) : ?> +

+
+ ' . $this->get_error('css')->get_error_code() . ': ' . $this->get_error('css')->get_error_message() . '', + '
', + '' . $this->font_awesome_version . '' + ); + ?> +

+ + + +

Don\'t worry! Better Font Awesome will still render using the included fallback version: ', 'better-font-awesome' ) . '' . $this->fallback_data['version'] . '' ; ?>

+ + +

+
+ ', + '' + ); + ?> +

+
+ args['minified'] ) ? '.min' : ''; + } + + /** + * Add an error to the $this->errors array. + * + * @since 1.0.0 + * + * @param string $error_type Type of error (api, css, etc). + * @param string $code Error code. + * @param string $message Error message. + */ + private function set_error( $error_type, $code, $message ) { + $this->errors[ $error_type ] = new WP_Error( $code, $message ); + } + + /** + * Retrieve a library error. + * + * @since 1.0.0 + * + * @param string $process Slug of the process to check (e.g. 'api'). + * + * @return WP_ERROR The error for the specified process. + */ + public function get_error( $process ) { + return isset( $this->errors[ $process ] ) ? $this->errors[ $process ] : ''; + } + + /** + * Check if API version data has been retrieved. + * + * @since 1.0.0 + * + * @return boolean Whether or not the API version info was successfully fetched. + */ + public function api_data_exists() { + + if ( $this->api_data ) { + return true; + } else { + return false; + } + + } + + /** + * Get a specific API value. + * + * @since 1.0.0 + * + * @param string $key Array key of the API data to get. + * + * @return mixed $value Value associated with specified key. + */ + public function get_api_value( $key ) { + + if ( $this->api_data ) { + $value = $this->api_data->$key; + } else { + $value = ''; + } + + return $value; + + } + + /*----------------------------------------------------------------------------* + * Public User Functions + *----------------------------------------------------------------------------*/ + + /** + * Get the version of Font Awesome currently in use. + * + * @since 1.0.0 + * + * @return string Font Awesome version. + */ + public function get_version() { + return $this->font_awesome_version; + } + + /** + * Get the fallback version of Font Awesome included locally. + * + * @since 1.0.0 + * + * @return string Font Awesome fallback version. + */ + public function get_fallback_version() { + return $this->fallback_data['version']; + } + + /** + * Get the stylesheet URL. + * + * @since 1.0.0 + * + * @return string Stylesheet URL. + */ + public function get_stylesheet_url() { + return $this->stylesheet_url; + } + + /** + * Get the array of available icons. + * + * @since 1.0.0 + * + * @return array Available Font Awesome icons. + */ + public function get_icons() { + return $this->icons; + } + + /** + * Get the icon prefix ('fa' or 'icon'). + * + * @since 1.0.0 + * + * @return string Font Awesome prefix. + */ + public function get_prefix() { + return $this->prefix; + } + + /** + * Get version data for the remote jsDelivr CDN. + * + * @since 1.0.0 + * + * @return object jsDelivr API data. + */ + public function get_api_data() { + return $this->api_data; + } + + /** + * Get errors. + * + * @since 1.0.0 + * + * @return array All library errors that have occured. + */ + public function get_errors() { + return $this->errors; + } + +} +endif; diff --git a/lib/better-font-awesome-library/css/admin-styles.css b/lib/better-font-awesome-library/css/admin-styles.css new file mode 100755 index 0000000..92f0bdf --- /dev/null +++ b/lib/better-font-awesome-library/css/admin-styles.css @@ -0,0 +1,60 @@ +/** + * Better Font Awesome admin styles + * + * @package Better Font Awesome Library + * @author MIGHTYminnow & Mickey Kay + * @license GPL-2.0+ + * @link https://github.com/MickeyKay/better-font-awesome-library + * @copyright 2014 MIGHTYminnow & Mickey Kay + */ + +/** + * TinyMCE Button & Popup + */ + +.bfa-iconpicker .iconpicker-popover { + position: absolute; + top: 100%; + left: 0; + z-index: 1000; + display: none; + float: left; + min-width: 160px; + padding: 5px 0; + margin: 2px 0 0; + list-style: none; + font-size: 14px; + background-color: #fff; + border: 1px solid #ccc; + border: 1px solid rgba(0,0,0,.15); + border-radius: 4px; + -webkit-box-shadow: 0 6px 12px rgba(0,0,0,.175); + box-shadow: 0 6px 12px rgba(0,0,0,.175); + background-clip: padding-box; +} + +.bfa-iconpicker .iconpicker-popover .arrow { + display: none !important; +} + +.bfa-iconpicker .iconpicker-item { + /* Override some unfortunate WP default admin mobile styles */ + width: 14px !important; + height: 14px !important; + padding: 14px !important; + font-size: 14px !important; + line-height: 1 !important; + box-shadow: 0 0 0 1px #ddd !important; +} + +.bfa-iconpicker input[type="search"] { + width: 100%; +} + +.bfa-iconpicker .iconpicker-popover.popover { + width: 253px; +} + +.bfa-iconpicker .iconpicker-component i { + display: none; +}