<?php
namespace Elementor;
use Elementor\Core\Files\File_Types\Svg;
use Elementor\Core\Page_Assets\Data_Managers\Font_Icon_Svg\Manager as Font_Icon_Svg_Data_Manager;
use Elementor\Utils;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Elementor icons manager.
*
* Elementor icons manager handler class
*
* @since 2.4.0
*/
class Icons_Manager {
const NEEDS_UPDATE_OPTION = 'icon_manager_needs_update';
const FONT_ICON_SVG_CLASS_NAME = 'e-font-icon-svg';
const LOAD_FA4_SHIM_OPTION_KEY = 'elementor_load_fa4_shim';
const ELEMENTOR_ICONS_VERSION = '5.40.0';
/**
* Tabs.
*
* Holds the list of all the tabs.
*
* @access private
* @static
* @since 2.4.0
* @var array
*/
private static $tabs;
private static $data_manager;
private static function get_needs_upgrade_option() {
return get_option( 'elementor_' . self::NEEDS_UPDATE_OPTION, null );
}
/**
* @param array $icon
* @param array $attributes
* @param string $tag
* @return bool|mixed|string
*/
public static function try_get_icon_html( $icon, $attributes = [], $tag = 'i' ) {
if ( empty( $icon['library'] ) ) {
return '';
}
return static::get_icon_html( $icon, $attributes, $tag );
}
/**
* @param array $icon
* @param array $attributes
* @param string $tag
* @return bool|mixed|string
*/
private static function get_icon_html( array $icon, array $attributes, $tag ) {
/**
* When the library value is svg it means that it's a SVG media attachment uploaded by the user.
* Otherwise, it's the name of the font family that the icon belongs to.
*/
if ( 'svg' === $icon['library'] ) {
$output = self::render_uploaded_svg_icon( $icon['value'] );
} else {
$output = self::render_font_icon( $icon, $attributes, $tag );
}
return $output;
}
/**
* Register styles
*
* Used to register all icon types stylesheets so they could be enqueued later by widgets
*/
public function register_styles() {
$config = self::get_icon_manager_tabs_config();
$shared_styles = [];
foreach ( $config as $type => $icon_type ) {
if ( ! isset( $icon_type['url'] ) ) {
continue;
}
$dependencies = [];
if ( ! empty( $icon_type['enqueue'] ) ) {
foreach ( (array) $icon_type['enqueue'] as $font_css_url ) {
if ( ! in_array( $font_css_url, array_keys( $shared_styles ), true ) ) {
$style_handle = 'elementor-icons-shared-' . count( $shared_styles );
wp_register_style(
$style_handle,
$font_css_url,
[],
$icon_type['ver']
);
$shared_styles[ $font_css_url ] = $style_handle;
}
$dependencies[] = $shared_styles[ $font_css_url ];
}
}
wp_register_style(
'elementor-icons-' . $icon_type['name'],
$icon_type['url'],
$dependencies,
$icon_type['ver']
);
}
}
/**
* Init Tabs
*
* Initiate Icon Manager Tabs.
*
* @access private
* @static
* @since 2.4.0
*/
private static function init_tabs() {
$initial_tabs = [
'fa-regular' => [
'name' => 'fa-regular',
'label' => esc_html__( 'Font Awesome - Regular', 'elementor' ),
'url' => self::get_fa_asset_url( 'regular' ),
'enqueue' => [ self::get_fa_asset_url( 'fontawesome' ) ],
'prefix' => 'fa-',
'displayPrefix' => 'far',
'labelIcon' => 'fab fa-font-awesome-alt',
'ver' => '5.15.3',
'fetchJson' => self::get_fa_asset_url( 'regular', 'js', false ),
'native' => true,
],
'fa-solid' => [
'name' => 'fa-solid',
'label' => esc_html__( 'Font Awesome - Solid', 'elementor' ),
'url' => self::get_fa_asset_url( 'solid' ),
'enqueue' => [ self::get_fa_asset_url( 'fontawesome' ) ],
'prefix' => 'fa-',
'displayPrefix' => 'fas',
'labelIcon' => 'fab fa-font-awesome',
'ver' => '5.15.3',
'fetchJson' => self::get_fa_asset_url( 'solid', 'js', false ),
'native' => true,
],
'fa-brands' => [
'name' => 'fa-brands',
'label' => esc_html__( 'Font Awesome - Brands', 'elementor' ),
'url' => self::get_fa_asset_url( 'brands' ),
'enqueue' => [ self::get_fa_asset_url( 'fontawesome' ) ],
'prefix' => 'fa-',
'displayPrefix' => 'fab',
'labelIcon' => 'fab fa-font-awesome-flag',
'ver' => '5.15.3',
'fetchJson' => self::get_fa_asset_url( 'brands', 'js', false ),
'native' => true,
],
];
/**
* Initial icon manager tabs.
*
* Filters the list of initial icon manager tabs.
*
* @param array $icon_manager_tabs Initial icon manager tabs.
*/
$initial_tabs = apply_filters( 'elementor/icons_manager/native', $initial_tabs );
self::$tabs = $initial_tabs;
}
/**
* Get Icon Manager Tabs
*
* @return array
*/
public static function get_icon_manager_tabs() {
if ( self::is_font_icon_inline_svg() && ! Plugin::$instance->editor->is_edit_mode() && ! Plugin::$instance->preview->is_preview_mode() ) {
self::$tabs = [];
} elseif ( ! self::$tabs ) {
self::init_tabs();
}
$additional_tabs = [];
/**
* Additional icon manager tabs.
*
* Filters additional icon manager tabs.
*
* @param array $additional_tabs Additional icon manager tabs. Default is an empty array.
*/
$additional_tabs = apply_filters( 'elementor/icons_manager/additional_tabs', $additional_tabs );
return array_replace( self::$tabs, $additional_tabs );
}
public static function enqueue_shim() {
//phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter
wp_enqueue_script(
'font-awesome-4-shim',
self::get_fa_asset_url( 'v4-shims', 'js' ),
[],
ELEMENTOR_VERSION
);
// Make sure that the CSS in the 'all' file does not override FA Pro's CSS
if ( ! wp_script_is( 'font-awesome-pro' ) ) {
wp_enqueue_style(
'font-awesome-5-all',
self::get_fa_asset_url( 'all' ),
[],
ELEMENTOR_VERSION
);
}
wp_enqueue_style(
'font-awesome-4-shim',
self::get_fa_asset_url( 'v4-shims' ),
[],
ELEMENTOR_VERSION
);
}
private static function get_fa_asset_url( $filename, $ext_type = 'css', $add_suffix = true ) {
static $is_test_mode = null;
if ( null === $is_test_mode ) {
$is_test_mode = Utils::is_script_debug() || Utils::is_elementor_tests();
}
$url = ELEMENTOR_ASSETS_URL . 'lib/font-awesome/' . $ext_type . '/' . $filename;
if ( ! $is_test_mode && $add_suffix ) {
$url .= '.min';
}
return $url . '.' . $ext_type;
}
public static function get_icon_manager_tabs_config() {
$tabs = [
'all' => [
'name' => 'all',
'label' => esc_html__( 'All Icons', 'elementor' ),
'labelIcon' => 'eicon-filter',
'native' => true,
],
];
return array_values( array_merge( $tabs, self::get_icon_manager_tabs() ) );
}
private static function is_font_icon_inline_svg() {
return Plugin::$instance->experiments->is_feature_active( 'e_font_icon_svg' );
}
/**
* @deprecated 3.8.0
*/
public static function render_svg_symbols() {}
public static function get_icon_svg_data( $icon ) {
return self::$data_manager->get_font_icon_svg_data( $icon );
}
/**
* Get font awesome svg.
*
* @param $icon array [ 'value' => string, 'library' => string ]
*
* @return bool|mixed|string
*/
public static function get_font_icon_svg( $icon, $attributes = [] ) {
// Load the SVG from the database.
$icon_data = self::get_icon_svg_data( $icon );
if ( ! $icon_data['path'] ) {
return '';
}
if ( ! empty( $attributes['class'] ) && ! is_array( $attributes['class'] ) ) {
$attributes['class'] = [ $attributes['class'] ];
}
$attributes['class'][] = self::FONT_ICON_SVG_CLASS_NAME;
$attributes['class'][] = 'e-' . $icon_data['key'];
$attributes['viewBox'] = '0 0 ' . $icon_data['width'] . ' ' . $icon_data['height'];
$attributes['xmlns'] = 'http://www.w3.org/2000/svg';
return (
'<svg ' . Utils::render_html_attributes( $attributes ) . '>' .
'<path d="' . esc_attr( $icon_data['path'] ) . '"></path>' .
'</svg>'
);
}
public static function render_uploaded_svg_icon( $value ) {
if ( ! isset( $value['id'] ) ) {
return '';
}
return Svg::get_inline_svg( $value['id'] );
}
public static function render_font_icon( $icon, $attributes = [], $tag = 'i' ) {
$icon_types