init.php
194 lines
| 1 | <?php |
| 2 | namespace ElementsKit_Lite\Modules\ElementsKit_Icon_Pack; |
| 3 | |
| 4 | defined('ABSPATH') || exit; |
| 5 | |
| 6 | class Init { |
| 7 | public static function get_url() { |
| 8 | return \ElementsKit_Lite::module_url() . 'elementskit-icon-pack/'; |
| 9 | } |
| 10 | |
| 11 | public static function get_dir() { |
| 12 | return \ElementsKit_Lite::module_dir() . 'elementskit-icon-pack/'; |
| 13 | } |
| 14 | |
| 15 | public function __construct() { |
| 16 | add_action( 'admin_enqueue_scripts', [$this, 'enqueue_admin_icon_css'] ); |
| 17 | |
| 18 | if (!self::is_svg_icon_experiment()) { |
| 19 | add_action('elementor/frontend/before_enqueue_scripts', array($this, 'enqueue_frontend')); |
| 20 | } |
| 21 | |
| 22 | add_action('elementor/preview/enqueue_styles', array($this, 'enqueue_frontend')); |
| 23 | add_filter('elementor/icons_manager/additional_tabs', array($this, 'register_icon_pack_to_elementor')); |
| 24 | add_filter('elementor/widget/render_content', array($this, 'filter_widget_content'), 10, 2); |
| 25 | } |
| 26 | |
| 27 | // Enqueue admin CSS for widget icons |
| 28 | public function enqueue_admin_icon_css( $hook ) { |
| 29 | if ( $hook !== 'elementor_page_elementor-element-manager' ) { |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | wp_enqueue_style( 'widget-icons', \ElementsKit_Lite::widget_url() . 'init/assets/css/editor.css', [], \ElementsKit_Lite::version() ); |
| 34 | |
| 35 | // Inline CSS to adjust icon display in Elementor's Element Manager |
| 36 | $css = 'td .ekit-widget-icon{max-width:13px;overflow:hidden;min-height:auto;font-size:inherit;}td .ekit-widget-icon:after{display:none}'; |
| 37 | wp_add_inline_style( 'widget-icons', $css ); |
| 38 | } |
| 39 | |
| 40 | public function enqueue_frontend() { |
| 41 | wp_enqueue_style( 'elementor-icons-ekiticons', self::get_url() . 'assets/css/ekiticons.css', array(), \ElementsKit_Lite::version() ); |
| 42 | } |
| 43 | |
| 44 | public function register_icon_pack_to_elementor($font) { |
| 45 | $font_new['ekiticons'] = array( |
| 46 | 'name' => 'ekiticons', |
| 47 | 'label' => esc_html__('ElementsKit Icon Pack', 'elementskit-lite'), |
| 48 | 'prefix' => 'icon-', |
| 49 | 'displayPrefix' => 'icon', |
| 50 | 'labelIcon' => 'icon icon-ekit', |
| 51 | 'ver' => \ElementsKit_Lite::version(), |
| 52 | 'fetchJson' => self::get_url() . 'assets/js/ekiticons.json', |
| 53 | 'native' => true, |
| 54 | ); |
| 55 | |
| 56 | if (!self::is_svg_icon_experiment()) { |
| 57 | $font_new['ekiticons']['url'] = self::get_url() . 'assets/css/ekiticons.css'; |
| 58 | } else { |
| 59 | $font_new['ekiticons']['enqueue'] = [self::get_url() . 'assets/css/ekiticons.css']; |
| 60 | } |
| 61 | |
| 62 | return array_merge($font, $font_new); |
| 63 | } |
| 64 | |
| 65 | public function filter_widget_content($widget_content, $widget) { |
| 66 | // Check if it's in Elementor editor |
| 67 | if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) { |
| 68 | return $widget_content; // return normal content |
| 69 | } |
| 70 | |
| 71 | $ekit_svg_icon = self::is_svg_icon_experiment(); |
| 72 | |
| 73 | // Match all <i> tags with class containing "icon icon-" |
| 74 | if ($ekit_svg_icon && strpos($widget_content, 'icon icon-') !== false) { |
| 75 | $widget_content = self::replace_icon_tags($widget_content); |
| 76 | } |
| 77 | |
| 78 | return $widget_content; |
| 79 | } |
| 80 | |
| 81 | public static function is_svg_icon_experiment() { |
| 82 | $elementskit_options = get_option( 'elementskit_options' ); |
| 83 | $inline_svg = $elementskit_options['user_data']['inline_svg']['is_enable'] ?? false; |
| 84 | return apply_filters( 'elementskit_font_icon_inline_svg', $inline_svg ); |
| 85 | } |
| 86 | |
| 87 | public static function replace_icon_tags( string $widget_content ): string { |
| 88 | return preg_replace_callback( |
| 89 | '/<i[^>]*class="([^"]*)"[^>]*><\/i>/', |
| 90 | function ( array $matches ): string { |
| 91 | $original_tag = $matches[0]; |
| 92 | $class_string = $matches[1]; |
| 93 | |
| 94 | // Bail early if this is not an ElementsKit icon tag |
| 95 | if ( strpos( $class_string, 'icon icon-' ) === false ) { |
| 96 | return $original_tag; |
| 97 | } |
| 98 | |
| 99 | // Extract the icon name e.g. "icon icon-down-arrow1" → "down-arrow1" |
| 100 | preg_match( '/icon icon-([^\s"]+)/', $class_string, $icon_matches ); |
| 101 | $icon_name = $icon_matches[1] ?? ''; |
| 102 | |
| 103 | // Generate the ElementsKit icon HTML |
| 104 | $icon_html = self::get_icon_html( [ |
| 105 | 'library' => 'ekiticons', |
| 106 | 'value' => 'icon icon-' . $icon_name, |
| 107 | ] ); |
| 108 | |
| 109 | if ( empty( $icon_html ) ) { |
| 110 | return $original_tag; |
| 111 | } |
| 112 | |
| 113 | // Strip the "icon icon-{name}" segment to get any remaining classes |
| 114 | $extra_classes = trim( preg_replace( '/\s*icon icon-[^\s"]+\s*/', ' ', $class_string ) ); |
| 115 | |
| 116 | // Prepend extra classes to the generated icon HTML if any exist |
| 117 | if ( ! empty( $extra_classes ) ) { |
| 118 | $icon_html = str_replace( 'class="', 'class="' . $extra_classes . ' ', $icon_html ); |
| 119 | } |
| 120 | |
| 121 | return $icon_html; |
| 122 | }, |
| 123 | $widget_content |
| 124 | ); |
| 125 | } |
| 126 | |
| 127 | public static function get_svg_icon($icon, $attributes = []) { |
| 128 | // go for ekit svg icon |
| 129 | $file = \ElementsKit_Lite::module_dir() . 'elementskit-icon-pack/' . 'assets/json/icons.json'; |
| 130 | $get_file_content = \Elementor\Utils::file_get_contents($file); |
| 131 | |
| 132 | // check condition for svg icon |
| 133 | if (!$get_file_content) { |
| 134 | return ''; |
| 135 | } |
| 136 | |
| 137 | $icons = json_decode($get_file_content, true); |
| 138 | $icon_name = str_replace('icon icon-', '', $icon['value']); |
| 139 | $svg = isset($icons[$icon_name]) ? $icons[$icon_name] : false; |
| 140 | |
| 141 | if ( empty($svg['paths']) ) { |
| 142 | return ''; |
| 143 | } |
| 144 | |
| 145 | $attributes['class'][] = 'ekit-svg-icon'; |
| 146 | $attributes['class'][] = sprintf('icon-%s', $icon_name); |
| 147 | $attributes['viewBox'] = $svg['viewBox']; |
| 148 | $attributes['xmlns'] = 'http://www.w3.org/2000/svg'; |
| 149 | |
| 150 | $svg_html = sprintf('<svg %s>', \Elementor\Utils::render_html_attributes($attributes)); |
| 151 | |
| 152 | foreach ($svg['paths'] as $path) { |
| 153 | $svg_html .= sprintf('<path d="%s"></path>', esc_attr($path)); |
| 154 | } |
| 155 | |
| 156 | $svg_html .= '</svg>'; |
| 157 | |
| 158 | return $svg_html; |
| 159 | } |
| 160 | |
| 161 | public static function get_icon_html($icon, $attributes = [], $tag = 'i') { |
| 162 | if ( empty( $icon['library'] ) ) { |
| 163 | return ''; |
| 164 | } |
| 165 | |
| 166 | if ( 'ekiticons' === $icon['library'] && self::is_svg_icon_experiment() ) { |
| 167 | $content = self::get_svg_icon($icon, $attributes); |
| 168 | |
| 169 | if ( ! empty( $content ) ) { |
| 170 | return $content; |
| 171 | } |
| 172 | } else { |
| 173 | return \Elementor\Icons_Manager::try_get_icon_html($icon, $attributes, $tag); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | public static function icon($icon, $attributes = [], $tag = 'i') { |
| 178 | if ( empty( $icon['library'] ) ) { |
| 179 | return ''; |
| 180 | } |
| 181 | |
| 182 | if ( 'ekiticons' === $icon['library'] && self::is_svg_icon_experiment() ) { |
| 183 | $content = self::get_svg_icon($icon, $attributes); |
| 184 | |
| 185 | if ( ! empty( $content ) ) { |
| 186 | \Elementor\Utils::print_unescaped_internal_string($content); |
| 187 | return true; |
| 188 | } |
| 189 | } else { |
| 190 | \Elementor\Icons_Manager::render_icon($icon, $attributes, $tag); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 |