PluginProbe
Elementor Website Builder – more than just a page builder / 4.2.2
Elementor Website Builder – more than just a page builder v4.2.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 4.2.2, at includes/managers/icons.php

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