Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * NEW: Added icon name
- * class-modal-elements.php / icon output in iconfont function.
- */
- // New: get the name of the character in question.
- $name = avia_font_manager::get_char_name( $key, $font );
- // New attribute: data-element-name
- $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>";
- /**
- * NEW: added JavaScript to filter the icons. This should be moved to an external file, really.
- * class-modal-elements.php / added on the end of static public function iconfont to $output
- */
- $output .= "
- <script>
- jQuery(document).ready(function($) {
- $('.av-iconselect-filter').on('keyup', function() {
- let searchValue = $(this).val().toLowerCase();
- let font = $(this).data('element-font');
- $(this).closest('.avia_icon_select_container').find('span.avia-attach-element-select').each(function() {
- let name = $(this).data('element-name').toLowerCase();
- let spanFont = $(this).data('element-font');
- if (spanFont === font && name.indexOf(searchValue) !== -1) {
- $(this).show();
- } else {
- $(this).hide();
- }
- });
- });
- });
- </script>
- ";
- /**
- * NEW: stuff to extract names from a(n icon) font.
- * class-modal-elements.php / added to avia_font_manager class
- */
- protected static array $char_names_cache = [];
- public static function get_char_name($char, $font): mixed {
- // Check if we've already parsed the CSS and cached the results...
- if (empty(self::$char_names_cache[$font])) {
- self::$char_names_cache[$font] = self::parse_font_json($font);
- }
- // Handle both string and integer character codes.
- $css_code = is_int($char) ? '\\' . strtoupper(dechex($char)) : '\\' . $char;
- // Remove backslash to ensure we can find this as a key in the array.
- $css_code =ltrim($css_code, '\\');
- // Return the icon name if it exists, otherwise return a generic name.
- return self::$char_names_cache[$font][$css_code] ?? 'Unknown Icon';
- }
- protected static function parse_font_json($font): array {
- $icon_names = [];
- $font_configs = self::load_iconfont_list();
- // Attempt to locate the JSON config file.
- if (!empty($font_configs[$font])) {
- $font_config = $font_configs[$font];
- $json_file_path = null;
- // Check if the font config includes a path to the JSON file...
- if (!empty($font_config['include'])) {
- $json_file_path = rtrim($font_config['include'], '/') . '/config.json';
- } elseif (!empty($font_config['folder'])) {
- $folder_path = str_replace(WP_CONTENT_URL, WP_CONTENT_DIR, $font_config['folder']);
- $json_file_path = rtrim($folder_path, '/') . '/config.json';
- }
- // Check if the config.json file exists and parse it
- if ($json_file_path && file_exists($json_file_path)) {
- $config_content = file_get_contents($json_file_path);
- $config_json = json_decode($config_content, true);
- if (!empty($config_json['glyphs'])) {
- foreach ($config_json['glyphs'] as $glyph) {
- // Correctly format the character code to hex and ensure it matches CSS notation
- $css_code = 'u' . str_pad(dechex($glyph['code']), 4, '0', STR_PAD_LEFT);
- $icon_names[$css_code] = $glyph['css'];
- }
- }
- } else {
- error_log("Font config file not found for font '{$font}' at expected path: {$json_file_path}");
- }
- }
- return $icon_names;
- }
Advertisement
Add Comment
Please, Sign In to add comment