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

620 lines 17.9 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.29.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 $icon
49 * @param $attributes
50 * @param $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 $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 * @return array
183 */
184 public static function get_icon_manager_tabs() {
185 if ( self::is_font_icon_inline_svg() && ! Plugin::$instance->editor->is_edit_mode() && ! Plugin::$instance->preview->is_preview_mode() ) {
186 self::$tabs = [];
187 } elseif ( ! self::$tabs ) {
188 self::init_tabs();
189 }
190
191 $additional_tabs = [];
192
193 /**
194 * Additional icon manager tabs.
195 *
196 * Filters additional icon manager tabs.
197 *
198 * @param array $additional_tabs Additional icon manager tabs. Default is an empty array.
199 */
200 $additional_tabs = apply_filters( 'elementor/icons_manager/additional_tabs', $additional_tabs );
201
202 return array_replace( self::$tabs, $additional_tabs );
203 }
204
205 public static function enqueue_shim() {
206 //phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter
207 wp_enqueue_script(
208 'font-awesome-4-shim',
209 self::get_fa_asset_url( 'v4-shims', 'js' ),
210 [],
211 ELEMENTOR_VERSION
212 );
213 // Make sure that the CSS in the 'all' file does not override FA Pro's CSS
214 if ( ! wp_script_is( 'font-awesome-pro' ) ) {
215 wp_enqueue_style(
216 'font-awesome-5-all',
217 self::get_fa_asset_url( 'all' ),
218 [],
219 ELEMENTOR_VERSION
220 );
221 }
222 wp_enqueue_style(
223 'font-awesome-4-shim',
224 self::get_fa_asset_url( 'v4-shims' ),
225 [],
226 ELEMENTOR_VERSION
227 );
228 }
229
230 private static function get_fa_asset_url( $filename, $ext_type = 'css', $add_suffix = true ) {
231 static $is_test_mode = null;
232 if ( null === $is_test_mode ) {
233 $is_test_mode = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || defined( 'ELEMENTOR_TESTS' ) && ELEMENTOR_TESTS;
234 }
235 $url = ELEMENTOR_ASSETS_URL . 'lib/font-awesome/' . $ext_type . '/' . $filename;
236 if ( ! $is_test_mode && $add_suffix ) {
237 $url .= '.min';
238 }
239
240 return $url . '.' . $ext_type;
241 }
242
243 public static function get_icon_manager_tabs_config() {
244 $tabs = [
245 'all' => [
246 'name' => 'all',
247 'label' => esc_html__( 'All Icons', 'elementor' ),
248 'labelIcon' => 'eicon-filter',
249 'native' => true,
250 ],
251 ];
252
253 return array_values( array_merge( $tabs, self::get_icon_manager_tabs() ) );
254 }
255
256 /**
257 * is_font_awesome_inline
258 *
259 * @return bool
260 */
261 private static function is_font_icon_inline_svg() {
262 return Plugin::$instance->experiments->is_feature_active( 'e_font_icon_svg' );
263 }
264
265 /**
266 * @deprecated 3.8.0
267 */
268 public static function render_svg_symbols() {}
269
270 public static function get_icon_svg_data( $icon ) {
271 return self::$data_manager->get_font_icon_svg_data( $icon );
272 }
273
274 /**
275 * Get font awesome svg.
276 * @param $icon array [ 'value' => string, 'library' => string ]
277 *
278 * @return bool|mixed|string
279 */
280 public static function get_font_icon_svg( $icon, $attributes = [] ) {
281 // Load the SVG from the database.
282 $icon_data = self::get_icon_svg_data( $icon );
283
284 if ( ! $icon_data['path'] ) {
285 return '';
286 }
287
288 if ( ! empty( $attributes['class'] ) && ! is_array( $attributes['class'] ) ) {
289 $attributes['class'] = [ $attributes['class'] ];
290 }
291
292 $attributes['class'][] = self::FONT_ICON_SVG_CLASS_NAME;
293 $attributes['class'][] = 'e-' . $icon_data['key'];
294 $attributes['viewBox'] = '0 0 ' . $icon_data['width'] . ' ' . $icon_data['height'];
295 $attributes['xmlns'] = 'http://www.w3.org/2000/svg';
296
297 return (
298 '<svg ' . Utils::render_html_attributes( $attributes ) . '>' .
299 '<path d="' . esc_attr( $icon_data['path'] ) . '"></path>' .
300 '</svg>'
301 );
302 }
303
304 public static function render_uploaded_svg_icon( $value ) {
305 if ( ! isset( $value['id'] ) ) {
306 return '';
307 }
308
309 return Svg::get_inline_svg( $value['id'] );
310 }
311
312 public static function render_font_icon( $icon, $attributes = [], $tag = 'i' ) {
313 $icon_types = self::get_icon_manager_tabs();
314
315 if ( isset( $icon_types[ $icon['library'] ]['render_callback'] ) && is_callable( $icon_types[ $icon['library'] ]['render_callback'] ) ) {
316 return call_user_func_array( $icon_types[ $icon['library'] ]['render_callback'], [ $icon, $attributes, $tag ] );
317 }
318
319 $content = '';
320
321 $font_icon_svg_family = self::is_font_icon_inline_svg() ? Font_Icon_Svg_Data_Manager::get_font_family( $icon['library'] ) : '';
322
323 if ( $font_icon_svg_family ) {
324 $icon['font_family'] = $font_icon_svg_family;
325
326 $content = self::get_font_icon_svg( $icon, $attributes );
327
328 if ( $content ) {
329 return $content;
330 }
331 }
332
333 if ( ! $content ) {
334 if ( empty( $attributes['class'] ) ) {
335 $attributes['class'] = $icon['value'];
336 } else {
337 if ( is_array( $attributes['class'] ) ) {
338 $attributes['class'][] = $icon['value'];
339 } else {
340 $attributes['class'] .= ' ' . $icon['value'];
341 }
342 }
343 }
344
345 return '<' . $tag . ' ' . Utils::render_html_attributes( $attributes ) . '>' . $content . '</' . $tag . '>';
346 }
347
348 /**
349 * Render Icon
350 *
351 * Used to render Icon for \Elementor\Controls_Manager::ICONS
352 * @param array $icon Icon Type, Icon value
353 * @param array $attributes Icon HTML Attributes
354 * @param string $tag Icon HTML tag, defaults to <i>
355 *
356 * @return mixed|string
357 */
358 public static function render_icon( $icon, $attributes = [], $tag = 'i' ) {
359 if ( empty( $icon['library'] ) ) {
360 return false;
361 }
362
363 $output = static::get_icon_html( $icon, $attributes, $tag );
364
365 Utils::print_unescaped_internal_string( $output );
366
367 return true;
368 }
369
370 /**
371 * Font Awesome 4 to font Awesome 5 Value Migration
372 *
373 * used to convert string value of Icon control to array value of Icons control
374 * ex: 'fa fa-star' => [ 'value' => 'fas fa-star', 'library' => 'fa-solid' ]
375 *
376 * @param $value
377 *
378 * @return array
379 */
380 public static function fa4_to_fa5_value_migration( $value ) {
381 static $migration_dictionary = false;
382 if ( '' === $value ) {
383 return [
384 'value' => '',
385 'library' => '',
386 ];
387 }
388 if ( false === $migration_dictionary ) {
389 $migration_dictionary = json_decode( Utils::file_get_contents( ELEMENTOR_ASSETS_PATH . 'lib/font-awesome/migration/mapping.js' ), true );
390 }
391 if ( isset( $migration_dictionary[ $value ] ) ) {
392 return $migration_dictionary[ $value ];
393 }
394
395 return [
396 'value' => 'fas ' . str_replace( 'fa ', '', $value ),
397 'library' => 'fa-solid',
398 ];
399 }
400
401 /**
402 * on_import_migration
403 * @param array $element settings array
404 * @param string $old_control old control id
405 * @param string $new_control new control id
406 * @param bool $remove_old boolean weather to remove old control or not
407 *
408 * @return array
409 */
410 public static function on_import_migration( array $element, $old_control = '', $new_control = '', $remove_old = false ) {
411
412 if ( ! isset( $element['settings'][ $old_control ] ) || isset( $element['settings'][ $new_control ] ) ) {
413 return $element;
414 }
415
416 // Case when old value is saved as empty string
417 $new_value = [
418 'value' => '',
419 'library' => '',
420 ];
421
422 // Case when old value needs migration
423 if ( ! empty( $element['settings'][ $old_control ] ) && ! self::is_migration_allowed() ) {
424 $new_value = self::fa4_to_fa5_value_migration( $element['settings'][ $old_control ] );
425 }
426
427 $element['settings'][ $new_control ] = $new_value;
428
429 //remove old value
430 if ( $remove_old ) {
431 unset( $element['settings'][ $old_control ] );
432 }
433
434 return $element;
435 }
436
437 /**
438 * is_migration_allowed
439 * @return bool
440 */
441 public static function is_migration_allowed() {
442 static $migration_allowed = false;
443 if ( false === $migration_allowed ) {
444 $migration_allowed = null === self::get_needs_upgrade_option();
445
446 /**
447 * Is icon migration allowed.
448 *
449 * Filters whether the icons migration allowed.
450 *
451 * @param bool $migration_allowed Is icon migration is allowed.
452 */
453 $migration_allowed = apply_filters( 'elementor/icons_manager/migration_allowed', $migration_allowed );
454 }
455 return $migration_allowed;
456 }
457
458 /**
459 * Register_Admin Settings
460 *
461 * adds Font Awesome migration / update admin settings
462 * @param Settings $settings
463 */
464 public function register_admin_settings( Settings $settings ) {
465 $settings->add_field(
466 Settings::TAB_ADVANCED,
467 Settings::TAB_ADVANCED,
468 'load_fa4_shim',
469 [
470 'label' => esc_html__( 'Load Font Awesome 4 Support', 'elementor' ),
471 'field_args' => [
472 'type' => 'select',
473 'std' => '',
474 'options' => [
475 '' => esc_html__( 'No', 'elementor' ),
476 'yes' => esc_html__( 'Yes', 'elementor' ),
477 ],
478 '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' ),
479 ],
480 ]
481 );
482 }
483
484 public function register_admin_tools_settings( Tools $settings ) {
485 $settings->add_tab( 'fontawesome4_migration', [ 'label' => esc_html__( 'Font Awesome Upgrade', 'elementor' ) ] );
486
487 $settings->add_section( 'fontawesome4_migration', 'fontawesome4_migration', [
488 'callback' => function() {
489 echo '<h2>' . esc_html__( 'Font Awesome Upgrade', 'elementor' ) . '</h2>';
490 echo '<p>' . // PHPCS - Plain Text
491 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
492 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
493 '</p><p><strong>' .
494 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
495 '</strong></p><p>' .
496 esc_html__( 'The upgrade process includes a database update', 'elementor' ) . ' - ' . // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
497 esc_html__( 'We highly recommend backing up your database before performing this upgrade.', 'elementor' ) . // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
498 '</p>' .
499 esc_html__( 'This action is not reversible and cannot be undone by rolling back to previous versions.', 'elementor' ) . // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
500 '</p>';
501 },
502 'fields' => [
503 [
504 'label' => esc_html__( 'Font Awesome Upgrade', 'elementor' ),
505 'field_args' => [
506 'type' => 'raw_html',
507 'html' => sprintf( '<span data-action="%s" data-_nonce="%s" data-redirect-url="%s" class="button" id="elementor_upgrade_fa_button">%s</span>',
508 self::NEEDS_UPDATE_OPTION . '_upgrade',
509 wp_create_nonce( self::NEEDS_UPDATE_OPTION ),
510 esc_url( $this->get_upgrade_redirect_url() ),
511 esc_html__( 'Upgrade To Font Awesome 5', 'elementor' )
512 ),
513 ],
514 ],
515 ],
516 ] );
517 }
518
519 /**
520 * Get redirect URL when upgrading font awesome.
521 *
522 * @return string
523 */
524 public function get_upgrade_redirect_url() {
525
526 if ( ! wp_verify_nonce( Utils::get_super_global_value( $_GET, '_wpnonce' ), 'tools-page-from-editor' ) ) {
527 return '';
528 }
529
530 $document_id = ! empty( $_GET['redirect_to_document'] ) ? absint( $_GET['redirect_to_document'] ) : null;
531
532 if ( ! $document_id ) {
533 return '';
534 }
535
536 $document = Plugin::$instance->documents->get( $document_id );
537
538 if ( ! $document ) {
539 return '';
540 }
541
542 return $document->get_edit_url();
543 }
544
545 /**
546 * Ajax Upgrade to FontAwesome 5
547 */
548 public function ajax_upgrade_to_fa5() {
549 check_ajax_referer( self::NEEDS_UPDATE_OPTION, '_nonce' );
550
551 if ( ! current_user_can( 'manage_options' ) ) {
552 wp_send_json_error( 'Permission denied' );
553 }
554
555 delete_option( 'elementor_' . self::NEEDS_UPDATE_OPTION );
556
557 wp_send_json_success( [ 'message' => esc_html__( 'Hurray! The upgrade process to Font Awesome 5 was completed successfully.', 'elementor' ) ] );
558 }
559
560 /**
561 * Add Update Needed Flag
562 * @param array $settings
563 *
564 * @return array;
565 */
566 public function add_update_needed_flag( $settings ) {
567 $settings['icons_update_needed'] = true;
568 return $settings;
569 }
570
571 public function enqueue_fontawesome_css() {
572 if ( ! self::is_migration_allowed() ) {
573 wp_enqueue_style( 'font-awesome' );
574 } else {
575 $current_filter = current_filter();
576 $load_shim = get_option( self::LOAD_FA4_SHIM_OPTION_KEY, false );
577 if ( 'elementor/editor/after_enqueue_styles' === $current_filter ) {
578 self::enqueue_shim();
579 } elseif ( 'yes' === $load_shim ) {
580 self::enqueue_shim();
581 }
582 }
583 }
584
585 /**
586 * @deprecated 3.1.0
587 */
588 public function add_admin_strings() {
589 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( __METHOD__, '3.1.0' );
590
591 return [];
592 }
593
594 /**
595 * Icons Manager constructor
596 */
597 public function __construct() {
598 if ( is_admin() ) {
599 // @todo: remove once we deprecate fa4
600 add_action( 'elementor/admin/after_create_settings/' . Settings::PAGE_ID, [ $this, 'register_admin_settings' ], 100 );
601 }
602
603 if ( self::is_font_icon_inline_svg() ) {
604 self::$data_manager = new Font_Icon_Svg_Data_Manager();
605 }
606
607 add_action( 'elementor/frontend/after_enqueue_styles', [ $this, 'enqueue_fontawesome_css' ] );
608 add_action( 'elementor/frontend/after_register_styles', [ $this, 'register_styles' ] );
609
610 if ( ! self::is_migration_allowed() ) {
611 add_filter( 'elementor/editor/localize_settings', [ $this, 'add_update_needed_flag' ] );
612 add_action( 'elementor/admin/after_create_settings/' . Tools::PAGE_ID, [ $this, 'register_admin_tools_settings' ], 100 );
613
614 if ( ! empty( $_POST ) ) { // phpcs:ignore -- nonce validation done in callback
615 add_action( 'wp_ajax_' . self::NEEDS_UPDATE_OPTION . '_upgrade', [ $this, 'ajax_upgrade_to_fa5' ] );
616 }
617 }
618 }
619 }
620