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

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