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

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