font-awesome.php
115 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Font awesome 5 icons support. |
| 4 | * |
| 5 | * @package Menu_Icons |
| 6 | */ |
| 7 | final class Menu_Icons_Font_Awesome { |
| 8 | |
| 9 | /** |
| 10 | * Init hooks. |
| 11 | */ |
| 12 | public static function init() { |
| 13 | add_filter( 'icon_picker_icon_type_stylesheet_uri', array( __CLASS__, '_icon_type_stylesheet_uri' ), 10, 3 ); |
| 14 | add_filter( 'icon_picker_fa_items', array( __CLASS__, '_icon_picker_fa_items' ) ); |
| 15 | add_filter( 'icon_picker_font_media_templates', array( __CLASS__, '_icon_picker_font_media_templates' ) ); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Font Awesome's stylesheet. |
| 20 | * |
| 21 | * @param string $stylesheet_uri Icon type's stylesheet URI. |
| 22 | * @param string $icon_type_id Icon type's ID. |
| 23 | * @param Icon_Picker_Type_Font $icon_type Icon type's instance. |
| 24 | * |
| 25 | * @return string |
| 26 | */ |
| 27 | public static function _icon_type_stylesheet_uri( $stylesheet_uri, $icon_type_id, $icon_type ) { |
| 28 | if ( 'fa' === $icon_type_id ) { |
| 29 | $url = Menu_Icons::get( 'url' ); |
| 30 | |
| 31 | $stylesheet_uri = sprintf( |
| 32 | "{$url}css/fontawesome/css/all.min.css", |
| 33 | $icon_type->version |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | return $stylesheet_uri; |
| 38 | } |
| 39 | |
| 40 | |
| 41 | /** |
| 42 | * Icon picker fontawesome items. |
| 43 | * |
| 44 | * @param array $icons Icons. |
| 45 | * @return array Icons. |
| 46 | */ |
| 47 | public static function _icon_picker_fa_items( $icons ) { |
| 48 | if ( empty( $icons ) ) { |
| 49 | return $icons; |
| 50 | } |
| 51 | |
| 52 | $deprecated_icons = array_search( 'fa-tripadvisor', array_column( $icons, 'id' ), true ); |
| 53 | if ( false !== $deprecated_icons ) { |
| 54 | unset( $icons[ $deprecated_icons ] ); |
| 55 | $icons = array_values( $icons ); |
| 56 | } |
| 57 | |
| 58 | $font_awesome5 = font_awesome5_backward_compatible(); |
| 59 | foreach ( $icons as $key => $icon ) { |
| 60 | $old_fa_icon = sprintf( 'fa-%s', $icons[ $key ]['id'] ); |
| 61 | if ( array_key_exists( $old_fa_icon, $font_awesome5 ) ) { |
| 62 | $icons[ $key ]['id'] = trim( $font_awesome5[ $old_fa_icon ] ); |
| 63 | } else { |
| 64 | $icons[ $key ]['id'] = sprintf( 'fa %s', trim( $icons[ $key ]['id'] ) ); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // Fa5 extra icons support. |
| 69 | $global_settins = get_option( 'menu-icons', false ); |
| 70 | if ( ! empty( $global_settins['global']['fa5_extra_icons'] ) ) { |
| 71 | $fa5_extra_icons = $global_settins['global']['fa5_extra_icons']; |
| 72 | $fa5_extra_icons = explode( ',', $fa5_extra_icons ); |
| 73 | $fa5_extra_icons = array_map( 'trim', $fa5_extra_icons ); |
| 74 | if ( ! empty( $fa5_extra_icons ) ) { |
| 75 | foreach ( $fa5_extra_icons as $fa5_icon ) { |
| 76 | $icon_name = explode( '-', $fa5_icon ); |
| 77 | $icon_name = end( $icon_name ); |
| 78 | $icons[] = array( |
| 79 | 'group' => 'all', |
| 80 | 'id' => $fa5_icon, |
| 81 | 'name' => $icon_name, |
| 82 | ); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return $icons; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | /** |
| 92 | * Icon picker font media template. |
| 93 | * |
| 94 | * @param string $template Media template. |
| 95 | * @return string Media template. |
| 96 | */ |
| 97 | public static function _icon_picker_font_media_templates( $template ) { |
| 98 | $templates = array( |
| 99 | 'icon' => '<i class="_icon {{data.type}} {{ data.icon }}"></i>', |
| 100 | 'item' => sprintf( |
| 101 | '<div class="attachment-preview js--select-attachment"> |
| 102 | <div class="thumbnail"> |
| 103 | <span class="_icon"><i class="{{"fa" == data.type ? "" : data.type}} {{ data.id }}"></i></span> |
| 104 | <div class="filename"><div>{{ data.name }}</div></div> |
| 105 | </div> |
| 106 | </div> |
| 107 | <a class="check" href="#" title="%s"><div class="media-modal-icon"></div></a>', |
| 108 | esc_attr__( 'Deselect', 'icon-picker' ) |
| 109 | ), |
| 110 | ); |
| 111 | |
| 112 | return $templates; |
| 113 | } |
| 114 | } |
| 115 |