| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- <?php
- /**
- * Helper class for working with icons.
- *
- * @since 1.4.6
- */
- final class FLBuilderIcons {
- /**
- * An array of data for each icon set.
- *
- * @since 1.4.6
- * @access private
- * @var array $sets
- */
- static private $sets = null;
- /**
- * Gets an array of data for core and custom icon sets.
- *
- * @since 1.4.6
- * @return array An array of data for each icon set.
- */
- static public function get_sets() {
- $switched = false;
- // Return the sets if already registered.
- if ( self::$sets ) {
- return self::$sets;
- }
- global $blog_id;
- // Check to see if we should pull sets from the main site.
- if ( is_multisite() ) {
- $id = defined( 'BLOG_ID_CURRENT_SITE' ) ? BLOG_ID_CURRENT_SITE : 1;
- $enabled_icons = get_option( '_fl_builder_enabled_icons' );
- if ( ( $id != $blog_id ) && empty( $enabled_icons ) ) {
- switch_to_blog( $id );
- $switched = true;
- }
- }
- // Register the icon sets.
- self::register_custom_sets();
- self::register_core_sets();
- // Revert to the current site if we pulled from the main site.
- if ( is_multisite() && $switched ) {
- restore_current_blog();
- }
- // Filter the sets
- self::$sets = apply_filters( 'fl_builder_icon_sets', self::$sets );
- // Return the sets.
- return self::$sets;
- }
- /**
- * Gets an array of data for icon sets of the current
- * site on a multisite install.
- *
- * @since 1.4.6
- * @return array An array of data for each icon set.
- */
- static public function get_sets_for_current_site() {
- if ( ! is_multisite() ) {
- return self::get_sets();
- }
- // Store the original sets.
- $original_sets = self::$sets;
- // Register the icon sets.
- self::register_custom_sets();
- self::register_core_sets();
- // Get the new sets.
- $sets = self::$sets;
- // Revert to the original sets.
- self::$sets = $original_sets;
- // Return the sets.
- return $sets;
- }
- /**
- * Remove an icon set from the internal sets array.
- *
- * @since 1.4.6
- * @param string $key The key for the set to remove.
- * @return void
- */
- static public function remove_set( $key ) {
- if ( self::$sets && isset( self::$sets[ $key ] ) ) {
- unset( self::$sets[ $key ] );
- }
- }
- /**
- * Get the key for an icon set from the path to an icon set stylesheet.
- *
- * @since 1.4.6
- * @param string $path The path to retrieve a key for.
- * @return string The icon set key.
- */
- static public function get_key_from_path( $path ) {
- $sets = self::get_sets();
- foreach ( $sets as $key => $set ) {
- if ( $path == $set['path'] ) {
- return $key;
- }
- }
- }
- /**
- * Register core icon set data in the internal sets array.
- *
- * @since 1.4.6
- * @access private
- * @return void
- */
- static private function register_core_sets() {
- $enabled_icons = FLBuilderModel::get_enabled_icons();
- $core_sets = apply_filters( 'fl_builder_core_icon_sets', array(
- 'font-awesome' => array(
- 'name' => 'Font Awesome 4',
- 'prefix' => 'fa',
- ),
- 'font-awesome-5-solid' => array(
- 'name' => 'Font Awesome 5 Solid',
- 'prefix' => 'fas',
- ),
- 'font-awesome-5-regular' => array(
- 'name' => 'Font Awesome 5 Regular',
- 'prefix' => 'far',
- ),
- 'font-awesome-5-light' => array(
- 'name' => 'Font Awesome 5 Light (pro only)',
- 'prefix' => 'fal',
- ),
- 'font-awesome-5-brands' => array(
- 'name' => 'Font Awesome 5 Brands',
- 'prefix' => 'fab',
- ),
- 'foundation-icons' => array(
- 'name' => 'Foundation Icons',
- 'prefix' => '',
- ),
- 'dashicons' => array(
- 'name' => 'WordPress Dashicons',
- 'prefix' => 'dashicons dashicons-before',
- ),
- ) );
- if ( ! apply_filters( 'fl_enable_fa5_pro', false ) ) {
- unset( $core_sets['font-awesome-5-light'] );
- }
- // Add the core sets.
- foreach ( $core_sets as $set_key => $set_data ) {
- if ( is_admin() || in_array( $set_key, $enabled_icons ) ) {
- self::$sets[ $set_key ] = array(
- 'name' => $set_data['name'],
- 'prefix' => $set_data['prefix'],
- 'type' => 'core',
- );
- }
- }
- // if there are no registered sets stop here.
- if ( ! is_array( self::$sets ) ) {
- return;
- }
- // Loop through core sets and add icons.
- foreach ( self::$sets as $set_key => $set_data ) {
- if ( 'core' == $set_data['type'] ) {
- $key = $set_key;
- if ( apply_filters( 'fl_enable_fa5_pro', false ) ) {
- switch ( $set_key ) {
- case 'font-awesome-5-light' :
- $key = 'font-awesome-5-light-pro';
- break;
- case 'font-awesome-5-regular' :
- $key = 'font-awesome-5-regular-pro';
- break;
- case 'font-awesome-5-solid' :
- $key = 'font-awesome-5-solid-pro';
- break;
- }
- }
- $config_path = apply_filters( 'fl_builder_core_icon_set_config', FL_BUILDER_DIR . 'json/' . $key . '.json', $set_data );
- $icons = json_decode( file_get_contents( $config_path ) );
- self::$sets[ $set_key ]['icons'] = $icons;
- }
- }
- }
- /**
- * Register custom icon set data in the internal sets array.
- *
- * @since 1.4.6
- * @access private
- * @return void
- */
- static private function register_custom_sets() {
- // Get uploaded sets.
- $enabled_icons = FLBuilderModel::get_enabled_icons();
- $upload_info = FLBuilderModel::get_cache_dir( 'icons' );
- $folders = glob( $upload_info['path'] . '*' );
- // Make sure we have an array.
- if ( ! is_array( $folders ) ) {
- return;
- }
- // Loop through uploaded sets.
- foreach ( $folders as $folder ) {
- // Make sure we have a directory.
- if ( ! fl_builder_filesystem()->is_dir( $folder ) ) {
- continue;
- }
- $folder = trailingslashit( $folder );
- // This is an Icomoon font.
- if ( fl_builder_filesystem()->file_exists( $folder . 'selection.json' ) ) {
- $data = json_decode( fl_builder_filesystem()->file_get_contents( $folder . 'selection.json' ) );
- $key = basename( $folder );
- $url = str_ireplace( $upload_info['path'], $upload_info['url'], $folder );
- if ( isset( $data->icons ) ) {
- if ( is_admin() || in_array( $key, $enabled_icons ) ) {
- self::$sets[ $key ] = array(
- 'name' => $data->metadata->name,
- 'prefix' => '',
- 'type' => 'icomoon',
- 'path' => $folder,
- 'url' => $url,
- 'stylesheet' => $url . 'style.css',
- 'icons' => array(),
- );
- foreach ( $data->icons as $icon ) {
- $prefs = $data->preferences->fontPref;
- $postfix = isset( $prefs->postfix ) ? $prefs->postfix : '';
- if ( isset( $prefs->selector ) && 'class' == $prefs->selector ) {
- // @codingStandardsIgnoreLine
- $selector = trim( str_replace( '.', ' ', $prefs->classSelector ) ) . ' ';
- } else {
- $selector = '';
- }
- self::$sets[ $key ]['icons'][] = $selector . $prefs->prefix . $icon->properties->name . $postfix;
- }
- }
- }
- } elseif ( fl_builder_filesystem()->file_exists( $folder . 'config.json' ) ) {
- $data = json_decode( fl_builder_filesystem()->file_get_contents( $folder . 'config.json' ) );
- $key = basename( $folder );
- $name = empty( $data->name ) ? 'Fontello' : $data->name;
- $url = str_ireplace( $upload_info['path'], $upload_info['url'], $folder );
- $style = empty( $data->name ) ? 'fontello' : $data->name;
- // Append the date to the name?
- if ( empty( $data->name ) ) {
- $time = str_replace( 'icon-', '', $key );
- $date_format = get_option( 'date_format' );
- $time_format = get_option( 'time_format' );
- $date = date( $date_format . ' ' . $time_format );
- $name .= ' (' . $date . ')';
- }
- if ( isset( $data->glyphs ) ) {
- if ( is_admin() || in_array( $key, $enabled_icons ) ) {
- self::$sets[ $key ] = array(
- 'name' => $name,
- 'prefix' => '',
- 'type' => 'fontello',
- 'path' => $folder,
- 'url' => $url,
- 'stylesheet' => $url . 'css/' . $style . '.css',
- 'icons' => array(),
- );
- foreach ( $data->glyphs as $icon ) {
- if ( $data->css_use_suffix ) {
- self::$sets[ $key ]['icons'][] = $icon->css . $data->css_prefix_text;
- } else {
- self::$sets[ $key ]['icons'][] = $data->css_prefix_text . $icon->css;
- }
- }
- }
- }
- }
- }
- }
- /**
- * Enqueue the stylesheets for all icon sets.
- *
- * @since 1.4.6
- * @return void
- */
- static public function enqueue_all_custom_icons_styles() {
- $sets = self::get_sets();
- foreach ( (array) $sets as $key => $data ) {
- // Don't enqueue core icons.
- if ( 'core' == $data['type'] ) {
- continue;
- }
- // Enqueue the custom icon styles.
- self::enqueue_custom_styles_by_key( $key );
- }
- }
- /**
- * Enqueue the stylesheet(s) for icons in a module.
- *
- * @since 1.4.6
- * @param object $module The module to enqueue for.
- * @return void
- */
- static public function enqueue_styles_for_module( $module ) {
- $fields = FLBuilderModel::get_settings_form_fields( $module->form );
- foreach ( $fields as $name => $field ) {
- if ( isset( $field['form'] ) ) {
- $form = FLBuilderModel::$settings_forms[ $field['form'] ];
- self::enqueue_styles_for_nested_module_form( $module, $form['tabs'], $name );
- } elseif ( 'icon' == $field['type'] && isset( $module->settings->$name ) ) {
- self::enqueue_styles_for_icon( $module->settings->$name );
- }
- }
- }
- /**
- * Enqueue the stylesheet(s) for icons in a nested form field.
- *
- * @since 1.4.6
- * @access private
- * @param object $module The module to enqueue for.
- * @param array $form The nested form.
- * @param string $setting The nested form setting key.
- * @return void
- */
- static private function enqueue_styles_for_nested_module_form( $module, $form, $setting ) {
- $fields = FLBuilderModel::get_settings_form_fields( $form );
- foreach ( $fields as $name => $field ) {
- if ( 'icon' == $field['type'] && ! empty( $module->settings->$setting ) ) {
- foreach ( $module->settings->$setting as $key => $val ) {
- if ( isset( $val->$name ) ) {
- self::enqueue_styles_for_icon( $val->$name );
- } elseif ( $name == $key && ! empty( $val ) ) {
- self::enqueue_styles_for_icon( $val );
- }
- }
- }
- }
- }
- /**
- * Enqueue the stylesheet for an icon.
- *
- * @since 1.4.6
- * @access private
- * @param string $icon The icon CSS classname.
- * @return void
- */
- static private function enqueue_styles_for_icon( $icon ) {
- do_action( 'fl_builder_enqueue_styles_for_icon', $icon );
- // Is this a core icon?
- if ( stristr( $icon, 'fa fa-' ) ) {
- wp_enqueue_style( 'font-awesome' );
- } elseif ( stristr( $icon, 'far fa-' ) || stristr( $icon, 'fas fa-' ) || stristr( $icon, 'fab fa-' ) || stristr( $icon, 'fal fa-' ) ) {
- wp_enqueue_style( 'font-awesome-5' );
- } elseif ( stristr( $icon, 'fi-' ) ) {
- wp_enqueue_style( 'foundation-icons' );
- } elseif ( stristr( $icon, 'dashicon' ) ) {
- wp_enqueue_style( 'dashicons' );
- } else {
- $sets = self::get_sets();
- foreach ( (array) $sets as $key => $data ) {
- if ( in_array( $icon, $data['icons'] ) ) {
- self::enqueue_custom_styles_by_key( $key );
- }
- }
- }
- }
- /**
- * Enqueue the stylesheet for an icon set by key.
- *
- * @since 1.4.6
- * @access private
- * @param string $key The icon set key.
- * @return void
- */
- static private function enqueue_custom_styles_by_key( $key ) {
- if ( apply_filters( 'fl_builder_enqueue_custom_styles_by_key', true, $key ) ) {
- $sets = self::get_sets();
- if ( isset( $sets[ $key ] ) ) {
- $set = $sets[ $key ];
- if ( 'icomoon' == $set['type'] ) {
- wp_enqueue_style( $key, $set['stylesheet'], array(), FL_BUILDER_VERSION );
- }
- if ( 'fontello' == $set['type'] ) {
- wp_enqueue_style( $key, $set['stylesheet'], array(), FL_BUILDER_VERSION );
- }
- }
- }
- }
- }
|