PluginProbe
Elementor Website Builder – more than just a page builder / 3.6.2
Elementor Website Builder – more than just a page builder v3.6.2
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / includes / managers / icons.php

icons.php in Elementor Website Builder – more than just a page builder 3.6.2, at includes/managers/icons.php

592 lines 17.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor;
3
4 use Elementor\Core\Files\File_Types\Svg;
5 use Elementor\Core\Page_Assets\Data_Managers\Font_Icon_Svg\Manager as Font_Icon_Svg_Data_Manager;
6
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit; // Exit if accessed directly.
9 }
10
11 /**
12 * Elementor icons manager.
13 *
14 * Elementor icons manager handler class
15 *
16 * @since 2.4.0
17 */
18 class Icons_Manager {
19
20 const NEEDS_UPDATE_OPTION = 'icon_manager_needs_update';
21
22 const FONT_ICON_SVG_CLASS_NAME = 'e-font-icon-svg';
23
24 const LOAD_FA4_SHIM_OPTION_KEY = 'elementor_load_fa4_shim';
25 /**
26 * Tabs.
27 *
28 * Holds the list of all the tabs.
29 *
30 * @access private
31 * @static
32 * @since 2.4.0
33 * @var array
34 */
35 private static $tabs;
36
37 private static $data_manager;
38
39 private static $font_icon_svg_symbols = [];
40
41 private static function get_needs_upgrade_option() {
42 return get_option( 'elementor_' . self::NEEDS_UPDATE_OPTION, null );
43 }
44
45 /**
46 * register styles
47 *
48 * Used to register all icon types stylesheets so they could be enqueued later by widgets
49 */
50 public function register_styles() {
51 $config = self::get_icon_manager_tabs_config();
52
53 $shared_styles = [];
54
55 foreach ( $config as $type => $icon_type ) {
56 if ( ! isset( $icon_type['url'] ) ) {
57 continue;
58 }
59 $dependencies = [];
60 if ( ! empty( $icon_type['enqueue'] ) ) {
61 foreach ( (array) $icon_type['enqueue'] as $font_css_url ) {
62 if ( ! in_array( $font_css_url, array_keys( $shared_styles ) ) ) {
63 $style_handle = 'elementor-icons-shared-' . count( $shared_styles );
64 wp_register_style(
65 $style_handle,
66 $font_css_url,
67 [],
68 $icon_type['ver']
69 );
70 $shared_styles[ $font_css_url ] = $style_handle;
71 }
72 $dependencies[] = $shared_styles[ $font_css_url ];
73 }
74 }
75 wp_register_style(
76 'elementor-icons-' . $icon_type['name'],
77 $icon_type['url'],
78 $dependencies,
79 $icon_type['ver']
80 );
81 }
82 }
83
84 /**
85 * Init Tabs
86 *
87 * Initiate Icon Manager Tabs.
88 *
89 * @access private
90 * @static
91 * @since 2.4.0
92 */
93 private static function init_tabs() {
94 $initial_tabs = [
95 'fa-regular' => [
96 'name' => 'fa-regular',
97 'label' => esc_html__( 'Font Awesome - Regular', 'elementor' ),
98 'url' => self::get_fa_asset_url( 'regular' ),
99 'enqueue' => [ self::get_fa_asset_url( 'fontawesome' ) ],
100 'prefix' => 'fa-',
101 'displayPrefix' => 'far',
102 'labelIcon' => 'fab fa-font-awesome-alt',
103 'ver' => '5.15.3',
104 'fetchJson' => self::get_fa_asset_url( 'regular', 'js', false ),
105 'native' => true,
106 ],
107 'fa-solid' => [
108 'name' => 'fa-solid',
109 'label' => esc_html__( 'Font Awesome - Solid', 'elementor' ),
110 'url' => self::get_fa_asset_url( 'solid' ),
111 'enqueue' => [ self::get_fa_asset_url( 'fontawesome' ) ],
112 'prefix' => 'fa-',
113 'displayPrefix' => 'fas',
114 'labelIcon' => 'fab fa-font-awesome',
115 'ver' => '5.15.3',
116 'fetchJson' => self::get_fa_asset_url( 'solid', 'js', false ),
117 'native' => true,
118 ],
119 'fa-brands' => [
120 'name' => 'fa-brands',
121 'label' => esc_html__( 'Font Awesome - Brands', 'elementor' ),
122 'url' => self::get_fa_asset_url( 'brands' ),
123 'enqueue' => [ self::get_fa_asset_url( 'fontawesome' ) ],
124 'prefix' => 'fa-',
125 'displayPrefix' => 'fab',
126 'labelIcon' => 'fab fa-font-awesome-flag',
127 'ver' => '5.15.3',
128 'fetchJson' => self::get_fa_asset_url( 'brands', 'js', false ),
129 'native' => true,
130 ],
131 ];
132
133 /**
134 * Initial icon manager tabs.
135 *
136 * Filters the list of initial icon manager tabs.
137 *
138 * @param array $icon_manager_tabs Initial icon manager tabs.
139 */
140 $initial_tabs = apply_filters( 'elementor/icons_manager/native', $initial_tabs );
141
142 self::$tabs = $initial_tabs;
143 }
144
145 /**
146 * Get Icon Manager Tabs
147 * @return array
148 */
149 public static function get_icon_manager_tabs() {
150 if ( self::is_font_icon_inline_svg() && ! Plugin::$instance->editor->is_edit_mode() && ! Plugin::$instance->preview->is_preview_mode() ) {
151 self::$tabs = [];
152 } elseif ( ! self::$tabs ) {
153 self::init_tabs();
154 }
155
156 $additional_tabs = [];
157
158 /**
159 * Additional icon manager tabs.
160 *
161 * Filters additional icon manager tabs.
162 *
163 * @param array $additional_tabs Additional icon manager tabs. Default is an empty array.
164 */
165 $additional_tabs = apply_filters( 'elementor/icons_manager/additional_tabs', $additional_tabs );
166
167 return array_merge( self::$tabs, $additional_tabs );
168 }
169
170 public static function enqueue_shim() {
171 wp_enqueue_script(
172 'font-awesome-4-shim',
173 self::get_fa_asset_url( 'v4-shims', 'js' ),
174 [],
175 ELEMENTOR_VERSION
176 );
177 // Make sure that the CSS in the 'all' file does not override FA Pro's CSS
178 if ( ! wp_script_is( 'font-awesome-pro' ) ) {
179 wp_enqueue_style(
180 'font-awesome-5-all',
181 self::get_fa_asset_url( 'all' ),
182 [],
183 ELEMENTOR_VERSION
184 );
185 }
186 wp_enqueue_style(
187 'font-awesome-4-shim',
188 self::get_fa_asset_url( 'v4-shims' ),
189 [],
190 ELEMENTOR_VERSION
191 );
192 }
193
194 private static function get_fa_asset_url( $filename, $ext_type = 'css', $add_suffix = true ) {
195 static $is_test_mode = null;
196 if ( null === $is_test_mode ) {
197 $is_test_mode = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || defined( 'ELEMENTOR_TESTS' ) && ELEMENTOR_TESTS;
198 }
199 $url = ELEMENTOR_ASSETS_URL . 'lib/font-awesome/' . $ext_type . '/' . $filename;
200 if ( ! $is_test_mode && $add_suffix ) {
201 $url .= '.min';
202 }
203
204 return $url . '.' . $ext_type;
205 }
206
207 public static function get_icon_manager_tabs_config() {
208 $tabs = [
209 'all' => [
210 'name' => 'all',
211 'label' => esc_html__( 'All Icons', 'elementor' ),
212 'labelIcon' => 'eicon-filter',
213 'native' => true,
214 ],
215 ];
216
217 return array_values( array_merge( $tabs, self::get_icon_manager_tabs() ) );
218 }
219
220 /**
221 * is_font_awesome_inline
222 *
223 * @return bool
224 */
225 private static function is_font_icon_inline_svg() {
226 return Plugin::$instance->experiments->is_feature_active( 'e_font_icon_svg' );
227 }
228
229 /**
230 * render_svg_symbols
231 *
232 */
233 public static function render_svg_symbols() {
234 if ( ! self::$font_icon_svg_symbols ) {
235 return;
236 }
237
238 $svg = '<svg xmlns="http://www.w3.org/2000/svg" id="e-font-icon-svg-symbols" style="display: none;">';
239
240 foreach ( self::$font_icon_svg_symbols as $symbol_id => $symbol ) {
241 $svg .= '<symbol id="' . $symbol_id . '" viewBox="0 0 ' . esc_attr( $symbol['width'] ) . ' ' . esc_attr( $symbol['height'] ) . '">';
242 $svg .= '<path d="' . esc_attr( $symbol['path'] ) . '"></path>';
243 $svg .= '</symbol>';
244 }
245
246 $svg .= '</svg>';
247
248 Utils::print_unescaped_internal_string( $svg );
249 }
250
251 public static function get_icon_svg_data( $icon ) {
252 return self::$data_manager->get_font_icon_svg_data( $icon );
253 }
254
255 /**
256 * Get font awesome svg.
257 * @param $icon array [ 'value' => string, 'library' => string ]
258 *
259 * @return bool|mixed|string
260 */
261 public static function get_font_icon_svg( $icon, $attributes = [] ) {
262 // Load the SVG from the database.
263 $icon_data = self::get_icon_svg_data( $icon );
264
265 if ( ! $icon_data['path'] ) {
266 return '';
267 }
268
269 // Add the icon data to the symbols array for later use in page rendering process.
270 if ( ! in_array( $icon_data['key'], self::$font_icon_svg_symbols, true ) ) {
271 self::$font_icon_svg_symbols[ $icon_data['key'] ] = $icon_data;
272 }
273
274 if ( ! empty( $attributes['class'] ) && ! is_array( $attributes['class'] ) ) {
275 $attributes['class'] = [ $attributes['class'] ];
276 }
277
278 $attributes['class'][] = self::FONT_ICON_SVG_CLASS_NAME;
279 $attributes['class'][] = 'e-' . $icon_data['key'];
280
281 /**
282 * If in edit mode inline the full svg, otherwise use the symbol.
283 * Will be displayed only after page update or widget "blur".
284 */
285 if ( Plugin::$instance->editor->is_edit_mode() ) {
286 return '<svg xmlns="http://www.w3.org/2000/svg" ' . Utils::render_html_attributes( $attributes ) . '
287 viewBox="0 0 ' . esc_attr( $icon_data['width'] ) . ' ' . esc_attr( $icon_data['height'] ) . '">
288 <path d="' . esc_attr( $icon_data['path'] ) . '"></path>
289 </svg>';
290 }
291
292 return '<svg ' . Utils::render_html_attributes( $attributes ) . '><use xlink:href="#' . esc_attr( $icon_data['key'] ) . '" /></svg>';
293 }
294
295 public static function render_uploaded_svg_icon( $value ) {
296 if ( ! isset( $value['id'] ) ) {
297 return '';
298 }
299
300 return Svg::get_inline_svg( $value['id'] );
301 }
302
303 public static function render_font_icon( $icon, $attributes = [], $tag = 'i' ) {
304 $icon_types = self::get_icon_manager_tabs();
305
306 if ( isset( $icon_types[ $icon['library'] ]['render_callback'] ) && is_callable( $icon_types[ $icon['library'] ]['render_callback'] ) ) {
307 return call_user_func_array( $icon_types[ $icon['library'] ]['render_callback'], [ $icon, $attributes, $tag ] );
308 }
309
310 $content = '';
311
312 $font_icon_svg_family = self::is_font_icon_inline_svg() ? Font_Icon_Svg_Data_Manager::get_font_family( $icon['library'] ) : '';
313
314 if ( $font_icon_svg_family ) {
315 $icon['font_family'] = $font_icon_svg_family;
316
317 $content = self::get_font_icon_svg( $icon, $attributes );
318
319 if ( $content ) {
320 return $content;
321 }
322 }
323
324 if ( ! $content ) {
325 if ( empty( $attributes['class'] ) ) {
326 $attributes['class'] = $icon['value'];
327 } else {
328 if ( is_array( $attributes['class'] ) ) {
329 $attributes['class'][] = $icon['value'];
330 } else {
331 $attributes['class'] .= ' ' . $icon['value'];
332 }
333 }
334 }
335
336 return '<' . $tag . ' ' . Utils::render_html_attributes( $attributes ) . '>' . $content . '</' . $tag . '>';
337 }
338
339 /**
340 * Render Icon
341 *
342 * Used to render Icon for \Elementor\Controls_Manager::ICONS
343 * @param array $icon Icon Type, Icon value
344 * @param array $attributes Icon HTML Attributes
345 * @param string $tag Icon HTML tag, defaults to <i>
346 *
347 * @return mixed|string
348 */
349 public static function render_icon( $icon, $attributes = [], $tag = 'i' ) {
350 if ( empty( $icon['library'] ) ) {
351 return false;
352 }
353
354 $output = '';
355
356 /**
357 * When the library value is svg it means that it's a SVG media attachment uploaded by the user.
358 * Otherwise, it's the name of the font family that the icon belongs to.
359 */
360 if ( 'svg' === $icon['library'] ) {
361 $output = self::render_uploaded_svg_icon( $icon['value'] );
362 } else {
363 $output = self::render_font_icon( $icon, $attributes, $tag );
364 }
365
366 Utils::print_unescaped_internal_string( $output );
367
368 return true;
369 }
370
371 /**
372 * Font Awesome 4 to font Awesome 5 Value Migration
373 *
374 * used to convert string value of Icon control to array value of Icons control
375 * ex: 'fa fa-star' => [ 'value' => 'fas fa-star', 'library' => 'fa-solid' ]
376 *
377 * @param $value
378 *
379 * @return array
380 */
381 public static function fa4_to_fa5_value_migration( $value ) {
382 static $migration_dictionary = false;
383 if ( '' === $value ) {
384 return [
385 'value' => '',
386 'library' => '',
387 ];
388 }
389 if ( false === $migration_dictionary ) {
390 $migration_dictionary = json_decode( file_get_contents( ELEMENTOR_ASSETS_PATH . 'lib/font-awesome/migration/mapping.js' ), true );
391 }
392 if ( isset( $migration_dictionary[ $value ] ) ) {
393 return $migration_dictionary[ $value ];
394 }
395
396 return [
397 'value' => 'fas ' . str_replace( 'fa ', '', $value ),
398 'library' => 'fa-solid',
399 ];
400 }
401
402 /**
403 * on_import_migration
404 * @param array $element settings array
405 * @param string $old_control old control id
406 * @param string $new_control new control id
407 * @param bool $remove_old boolean weather to remove old control or not
408 *
409 * @return array
410 */
411 public static function on_import_migration( array $element, $old_control = '', $new_control = '', $remove_old = false ) {
412
413 if ( ! isset( $element['settings'][ $old_control ] ) || isset( $element['settings'][ $new_control ] ) ) {
414 return $element;
415 }
416
417 // Case when old value is saved as empty string
418 $new_value = [
419 'value' => '',
420 'library' => '',
421 ];
422
423 // Case when old value needs migration
424 if ( ! empty( $element['settings'][ $old_control ] ) && ! self::is_migration_allowed() ) {
425 $new_value = self::fa4_to_fa5_value_migration( $element['settings'][ $old_control ] );
426 }
427
428 $element['settings'][ $new_control ] = $new_value;
429
430 //remove old value
431 if ( $remove_old ) {
432 unset( $element['settings'][ $old_control ] );
433 }
434
435 return $element;
436 }
437
438 /**
439 * is_migration_allowed
440 * @return bool
441 */
442 public static function is_migration_allowed() {
443 static $migration_allowed = false;
444 if ( false === $migration_allowed ) {
445 $migration_allowed = null === self::get_needs_upgrade_option();
446
447 /**
448 * Is icon migration allowed.
449 *
450 * Filters whther the icons migration allowed.
451 *
452 * @param bool $migration_allowed Is icon migration is allowed.
453 */
454 $migration_allowed = apply_filters( 'elementor/icons_manager/migration_allowed', $migration_allowed );
455 }
456 return $migration_allowed;
457 }
458
459 /**
460 * Register_Admin Settings
461 *
462 * adds Font Awesome migration / update admin settings
463 * @param Settings $settings
464 */
465 public function register_admin_settings( Settings $settings ) {
466 $settings->add_field(
467 Settings::TAB_ADVANCED,
468 Settings::TAB_ADVANCED,
469 'load_fa4_shim',
470 [
471 'label' => esc_html__( 'Load Font Awesome 4 Support', 'elementor' ),
472 'field_args' => [
473 'type' => 'select',
474 'std' => '',
475 'options' => [
476 '' => esc_html__( 'No', 'elementor' ),
477 'yes' => esc_html__( 'Yes', 'elementor' ),
478 ],
479 'desc' => esc_html__( 'Font Awesome 4 support script (shim.js) is a script that makes sure all previously selected Font Awesome 4 icons are displayed correctly while using Font Awesome 5 library.', 'elementor' ),
480 ],
481 ]
482 );
483 }
484
485 public function register_admin_tools_settings( Tools $settings ) {
486 $settings->add_tab( 'fontawesome4_migration', [ 'label' => esc_html__( 'Font Awesome Upgrade', 'elementor' ) ] );
487
488 $settings->add_section( 'fontawesome4_migration', 'fontawesome4_migration', [
489 'callback' => function() {
490 echo '<h2>' . esc_html__( 'Font Awesome Upgrade', 'elementor' ) . '</h2>';
491 echo '<p>' . // PHPCS - Plain Text
492 esc_html__( 'Access 1,500+ amazing Font Awesome 5 icons and enjoy faster performance and design flexibility.', 'elementor' ) . '<br>' . // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
493 esc_html__( 'By upgrading, whenever you edit a page containing a Font Awesome 4 icon, Elementor will convert it to the new Font Awesome 5 icon.', 'elementor' ) . // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
494 '</p><p><strong>' .
495 esc_html__( 'Please note that the upgrade process may cause some of the previously used Font Awesome 4 icons to look a bit different due to minor design changes made by Font Awesome.', 'elementor' ) . // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
496 '</strong></p><p>' .
497 esc_html__( 'The upgrade process includes a database update', 'elementor' ) . ' - ' . // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
498 esc_html__( 'We highly recommend backing up your database before performing this upgrade.', 'elementor' ) . // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
499 '</p>' .
500 esc_html__( 'This action is not reversible and cannot be undone by rolling back to previous versions.', 'elementor' ) . // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
501 '</p>';
502 },
503 'fields' => [
504 [
505 'label' => esc_html__( 'Font Awesome Upgrade', 'elementor' ),
506 'field_args' => [
507 'type' => 'raw_html',
508 'html' => sprintf( '<span data-action="%s" data-_nonce="%s" class="button" id="elementor_upgrade_fa_button">%s</span>',
509 self::NEEDS_UPDATE_OPTION . '_upgrade',
510 wp_create_nonce( self::NEEDS_UPDATE_OPTION ),
511 esc_html__( 'Upgrade To Font Awesome 5', 'elementor' )
512 ),
513 ],
514 ],
515 ],
516 ] );
517 }
518
519 /**
520 * Ajax Upgrade to FontAwesome 5
521 */
522 public function ajax_upgrade_to_fa5() {
523 check_ajax_referer( self::NEEDS_UPDATE_OPTION, '_nonce' );
524
525 delete_option( 'elementor_' . self::NEEDS_UPDATE_OPTION );
526
527 wp_send_json_success( [ 'message' => '<p>' . esc_html__( 'Hurray! The upgrade process to Font Awesome 5 was completed successfully.', 'elementor' ) . '</p>' ] );
528 }
529
530 /**
531 * Add Update Needed Flag
532 * @param array $settings
533 *
534 * @return array;
535 */
536 public function add_update_needed_flag( $settings ) {
537 $settings['icons_update_needed'] = true;
538 return $settings;
539 }
540
541 public function enqueue_fontawesome_css() {
542 if ( ! self::is_migration_allowed() ) {
543 wp_enqueue_style( 'font-awesome' );
544 } else {
545 $current_filter = current_filter();
546 $load_shim = get_option( self::LOAD_FA4_SHIM_OPTION_KEY, false );
547 if ( 'elementor/editor/after_enqueue_styles' === $current_filter ) {
548 self::enqueue_shim();
549 } else if ( 'yes' === $load_shim ) {
550 self::enqueue_shim();
551 }
552 }
553 }
554
555 /**
556 * @deprecated 3.1.0
557 */
558 public function add_admin_strings() {
559 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( __METHOD__, '3.1.0' );
560
561 return [];
562 }
563
564 /**
565 * Icons Manager constructor
566 */
567 public function __construct() {
568 if ( is_admin() ) {
569 // @todo: remove once we deprecate fa4
570 add_action( 'elementor/admin/after_create_settings/' . Settings::PAGE_ID, [ $this, 'register_admin_settings' ], 100 );
571 }
572
573 if ( self::is_font_icon_inline_svg() ) {
574 self::$data_manager = new Font_Icon_Svg_Data_Manager();
575
576 add_action( 'wp_footer', [ $this, 'render_svg_symbols' ], 10 );
577 }
578
579 add_action( 'elementor/frontend/after_enqueue_styles', [ $this, 'enqueue_fontawesome_css' ] );
580 add_action( 'elementor/frontend/after_register_styles', [ $this, 'register_styles' ] );
581
582 if ( ! self::is_migration_allowed() ) {
583 add_filter( 'elementor/editor/localize_settings', [ $this, 'add_update_needed_flag' ] );
584 add_action( 'elementor/admin/after_create_settings/' . Tools::PAGE_ID, [ $this, 'register_admin_tools_settings' ], 100 );
585
586 if ( ! empty( $_POST ) ) { // phpcs:ignore -- nonce validation done in callback
587 add_action( 'wp_ajax_' . self::NEEDS_UPDATE_OPTION . '_upgrade', [ $this, 'ajax_upgrade_to_fa5' ] );
588 }
589 }
590 }
591 }
592