Guest User

Enfold iconfont icon search

a guest
Feb 5th, 2024
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.75 KB | Source Code | 0 0
  1. /**
  2.  * NEW: Added icon name
  3.  * class-modal-elements.php / icon output in iconfont function.
  4.  */
  5. // New: get the name of the character in question.
  6. $name = avia_font_manager::get_char_name( $key, $font );
  7.  
  8. // New attribute: data-element-name
  9. $output .= "<span title='{$charcode_prefix}{$key}' data-element-nr='{$key}' data-element-name='{$name}' data-element-font='{$font}' class='avia-attach-element-select avia_icon_preview avia-font-{$font} {$active_char}'>{$char}</span>";
  10.  
  11. /**
  12.   * NEW: added JavaScript to filter the icons. This should be moved to an external file, really.
  13.   * class-modal-elements.php / added on the end of static public function iconfont to $output
  14.   */
  15. $output .= "
  16. <script>
  17. jQuery(document).ready(function($) {
  18.    $('.av-iconselect-filter').on('keyup', function() {
  19.        let searchValue = $(this).val().toLowerCase();
  20.        let font = $(this).data('element-font');
  21.        
  22.        $(this).closest('.avia_icon_select_container').find('span.avia-attach-element-select').each(function() {
  23.            let name = $(this).data('element-name').toLowerCase();
  24.            let spanFont = $(this).data('element-font');
  25.            
  26.            if (spanFont === font && name.indexOf(searchValue) !== -1) {
  27.                $(this).show();
  28.            } else {
  29.                $(this).hide();
  30.            }
  31.        });
  32.    });
  33. });
  34. </script>
  35. ";
  36.  
  37.  
  38. /**
  39.  * NEW: stuff to extract names from a(n icon) font.
  40.  * class-modal-elements.php / added to avia_font_manager class
  41.  */
  42. protected static array $char_names_cache = [];
  43.  
  44. public static function get_char_name($char, $font): mixed {
  45.  
  46.     // Check if we've already parsed the CSS and cached the results...
  47.     if (empty(self::$char_names_cache[$font])) {
  48.         self::$char_names_cache[$font] = self::parse_font_json($font);
  49.     }
  50.  
  51.     // Handle both string and integer character codes.
  52.     $css_code = is_int($char) ? '\\' . strtoupper(dechex($char)) : '\\' . $char;
  53.  
  54.     // Remove backslash to ensure we can find this as a key in the array.
  55.     $css_code =ltrim($css_code, '\\');
  56.  
  57.     // Return the icon name if it exists, otherwise return a generic name.
  58.     return self::$char_names_cache[$font][$css_code] ?? 'Unknown Icon';
  59. }
  60.  
  61. protected static function parse_font_json($font): array {
  62.     $icon_names = [];
  63.     $font_configs = self::load_iconfont_list();
  64.  
  65.     // Attempt to locate the JSON config file.
  66.     if (!empty($font_configs[$font])) {
  67.         $font_config = $font_configs[$font];
  68.         $json_file_path = null;
  69.  
  70.         // Check if the font config includes a path to the JSON file...
  71.         if (!empty($font_config['include'])) {
  72.             $json_file_path = rtrim($font_config['include'], '/') . '/config.json';
  73.         } elseif (!empty($font_config['folder'])) {
  74.             $folder_path = str_replace(WP_CONTENT_URL, WP_CONTENT_DIR, $font_config['folder']);
  75.             $json_file_path = rtrim($folder_path, '/') . '/config.json';
  76.         }
  77.  
  78.         // Check if the config.json file exists and parse it
  79.         if ($json_file_path && file_exists($json_file_path)) {
  80.             $config_content = file_get_contents($json_file_path);
  81.             $config_json = json_decode($config_content, true);
  82.             if (!empty($config_json['glyphs'])) {
  83.                 foreach ($config_json['glyphs'] as $glyph) {
  84.                     // Correctly format the character code to hex and ensure it matches CSS notation
  85.                     $css_code = 'u' . str_pad(dechex($glyph['code']), 4, '0', STR_PAD_LEFT);
  86.                     $icon_names[$css_code] = $glyph['css'];
  87.                 }
  88.             }
  89.         } else {
  90.             error_log("Font config file not found for font '{$font}' at expected path: {$json_file_path}");
  91.         }
  92.     }
  93.  
  94.     return $icon_names;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment