Changeset 3212532
- Timestamp:
- 12/24/2024 10:20:52 AM (15 months ago)
- Location:
- fsm-custom-featured-image-caption/trunk
- Files:
-
- 2 edited
-
fsm-custom-featured-image-caption.php (modified) (3 diffs)
-
readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
fsm-custom-featured-image-caption/trunk/fsm-custom-featured-image-caption.php
r2806695 r3212532 3 3 * Plugin Name: FSM Custom Featured Image Caption 4 4 * Description: Allows adding custom captions to the featured image of posts and pages 5 * Version: 1.2 45 * Version: 1.2 6 6 * Author: Fesomia 7 7 * Author URI: http://wp.fesomia.cat … … 757 757 // Generates the html for the figure and caption 758 758 $figure_class = current_filter() == 'divi_thumbnail_html'?'wp-caption-divi':'wp-caption'; 759 $html = '<figure class="' . $figure_class . ' f eatured">' . $html . $figcaption . '</figure>';759 $html = '<figure class="' . $figure_class . ' featured">' . $html . $figcaption . '</figure>'; 760 760 761 761 return $html; … … 796 796 if (!$is_list && has_shortcode($post->post_content, 'FSM_featured_image')) {return ''; } 797 797 798 798 799 799 return FSMCFIC_add_caption($html, $post_id); 800 800 801 801 802 } 802 } 803 804 805 806 807 808 809 function FSMCFIC_featured_image_block_fix ($block_content, $block) { 810 // Check if the block is the featured image block and DOMDocument is enabled 811 if ($block['blockName'] === 'core/post-featured-image' && class_exists('DOMDocument')) { 812 // Detect the encoding of the original content 813 814 // Detect if there's a bested fsmcfi figure, if not, return 815 if (strpos($block_content, '<figure') === false || 816 strpos($block_content, 'fsmcfi-fig') === false) { 817 return $block_content; 818 } 819 820 $current_encoding = mb_detect_encoding($block_content, mb_list_encodings(), true); 821 822 // If not in UTF-8, convert to UTF-8 for processing 823 if ($current_encoding !== 'UTF-8') { 824 $block_content = mb_convert_encoding($block_content, 'UTF-8', $current_encoding); 825 } 826 827 828 829 // Load the HTML content into a DOMDocument object 830 $dom = new DOMDocument(); 831 832 // To avoid warnings for HTML5, use libxml to handle malformed tags 833 libxml_use_internal_errors(true); 834 835 // Load the block content 836 $dom->loadHTML(mb_convert_encoding($block_content, 'HTML-ENTITIES', 'UTF-8')); 837 838 // Find all <figure> elements 839 $figures = $dom->getElementsByTagName('figure'); 840 841 // Iterate over the found <figure> elements 842 foreach ($figures as $figure) { 843 // Check if this <figure> is inside another <figure> 844 $parent = $figure->parentNode; 845 846 // If the <figure> is inside another and has the 'featured' class 847 if ($parent && $parent->nodeName === 'figure' && $figure->hasAttribute('class') && strpos($figure->getAttribute('class'), 'featured') !== false) { 848 // Move the children of the inner <figure> to the outer 849 $children = iterator_to_array($figure->childNodes); 850 851 foreach ($children as $child) { 852 $parent->insertBefore($child, $figure); 853 } 854 855 // Transfer the classes from the inner <figure> 856 $parent_classes = $parent->getAttribute('class'); 857 $figure_classes = $figure->getAttribute('class'); 858 $new_classes = trim($parent_classes . ' ' . $figure_classes); 859 $parent->setAttribute('class', $new_classes); 860 861 // Remove the inner <figure> 862 $parent->removeChild($figure); 863 } 864 } 865 866 $modified_content = $dom->saveHTML(); 867 868 // If the original encoding was not UTF-8, convert back to the original encoding 869 if ($current_encoding !== 'UTF-8') { 870 $modified_content = mb_convert_encoding($modified_content, $current_encoding, 'UTF-8'); 871 } 872 873 874 libxml_clear_errors(); 875 876 877 return $modified_content; 878 } 879 880 return $block_content; 881 } 882 883 884 885 886 887 803 888 804 889 add_filter( 'post_thumbnail_html', 'FSMCFIC_post_featured_image_filter',20,2 ); 805 890 add_filter( 'divi_thumbnail_html', 'FSMCFIC_post_featured_image_filter',20,2 ); 806 891 add_filter('render_block', 'FSMCFIC_featured_image_block_fix', 10, 2); 807 892 808 893 -
fsm-custom-featured-image-caption/trunk/readme.txt
r2806695 r3212532 3 3 Tags: featured image, caption, images, credits, copyright 4 4 Requires at least: 4 5 Tested up to: 6. 15 Tested up to: 6. 6 6 Stable tag: trunk 7 7 License: GPLv2 or later … … 127 127 == Changelog == 128 128 129 130 131 129 132 = 1.24 = 130 133 * Added a global option in the general settings page to ignore the image captions defined for the image in the Media library. Tested compatibility up to wp 6.1
Note: See TracChangeset
for help on using the changeset viewer.