Plugin Directory

Changeset 3212532

Timestamp:
12/24/2024 10:20:52 AM (15 months ago)
Author:
fesomia
Message:

Version 1.25

Location:
fsm-custom-featured-image-caption/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • fsm-custom-featured-image-caption/trunk/fsm-custom-featured-image-caption.php

    r2806695 r3212532  
    33* Plugin Name: FSM Custom Featured Image Caption
    44* Description: Allows adding custom captions to the featured image of posts and pages
    5 * Version: 1.24
     5* Version: 1.2
    66* Author: Fesomia
    77* Author URI: http://wp.fesomia.cat
     
    757757    // Generates the html for the figure and caption
    758758    $figure_class = current_filter() == 'divi_thumbnail_html'?'wp-caption-divi':'wp-caption';
    759     $html = '<figure class="' . $figure_class . ' featured">' . $html . $figcaption . '</figure>';
     759    $html = '<figure class="' . $figure_class . ' featured">' . $html . $figcaption . '</figure>';
    760760
    761761    return $html;
     
    796796    if (!$is_list && has_shortcode($post->post_content, 'FSM_featured_image')) {return ''; }
    797797
    798    
     798
    799799    return FSMCFIC_add_caption($html, $post_id);
    800800   
    801801   
    802 }   
     802}
     803
     804
     805
     806
     807
     808
     809function 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   
    803888   
    804889add_filter( 'post_thumbnail_html', 'FSMCFIC_post_featured_image_filter',20,2 );
    805890add_filter( 'divi_thumbnail_html', 'FSMCFIC_post_featured_image_filter',20,2 );
    806 
     891add_filter('render_block', 'FSMCFIC_featured_image_block_fix', 10, 2);
    807892
    808893
  • fsm-custom-featured-image-caption/trunk/readme.txt

    r2806695 r3212532  
    33Tags: featured image, caption, images, credits, copyright
    44Requires at least: 4
    5 Tested up to: 6.1
     5Tested up to: 6.
    66Stable tag: trunk
    77License: GPLv2 or later
     
    127127== Changelog ==
    128128
     129
     130
     131
    129132= 1.24 =
    130133* 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.