diff --git a/includes/utilities.php b/includes/utilities.php index 415887d..0b51d82 100644 --- a/includes/utilities.php +++ b/includes/utilities.php @@ -144,18 +144,19 @@ function build_footnotes_index_from_content($content) } $footnotes = []; - $dom = new DOMDocument(); - // On supprime les erreurs de parsing pour le HTML5 - @$dom->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); + // Trouve les balises avec classe footnote-reference ET attribut footnote-content (ordre flexible) + $pattern = '/]*class="[^"]*footnote-reference[^"]*"[^>]*footnote-content="([^"]*)"[^>]*>|]*footnote-content="([^"]*)"[^>]*class="[^"]*footnote-reference[^"]*"[^>]*>/i'; + $has_footnotes = preg_match_all($pattern, $content, $matches, PREG_SET_ORDER); - $links = $dom->getElementsByTagName('a'); - foreach ($links as $key => $link) { - if ($link->hasAttribute('class') && strpos($link->getAttribute('class'), 'footnote-reference') !== false) { - $footnote_content = $link->getAttribute('footnote-content'); + if ($has_footnotes) { + foreach ($matches as $index => $match) { + write_log($match); + // Le contenu peut être dans match[1] ou match[2] selon l'ordre des attributs + $footnote_content = !empty($match[1]) ? $match[1] : $match[2]; if (!empty($footnote_content)) { $footnotes[] = array( - 'key' => $key + 1, - 'anchorID' => $key + 1, + 'key' => $index + 1, + 'anchorID' => $index + 1, 'content' => $footnote_content ); } @@ -165,27 +166,25 @@ function build_footnotes_index_from_content($content) return $footnotes; } -add_filter('the_content', 'apply_footnotes_urls_to_content', 10); function apply_footnotes_urls_to_content($content) { $post_type = get_post_type(); - if ($post_type !== 'articles' && !is_admin()) return $content; + // Compter les références de notes de bas de page + $footnote_count = 0; - $footnotes = build_footnotes_index_from_content($content); - $dom = new DOMDocument(); - @$dom->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); + // Utiliser preg_replace_callback pour traiter chaque occurrence individuellement + $content = preg_replace_callback( + '/]*class="[^"]*footnote-reference[^"]*"[^>]*)>/i', + function ($matches) use (&$footnote_count) { + $footnote_count++; + return ''; + }, + $content + ); - $links = $dom->getElementsByTagName('a'); - - foreach ($links as $key => $link) { - - if ($link->hasAttribute('class') && strpos($link->getAttribute('class'), 'footnote-reference') !== false) { - $link->setAttribute('id', 'footnote-' . ($key + 1)); - } - } - - return $dom->saveHTML(); + return $content; } +add_filter('the_content', 'apply_footnotes_urls_to_content', 10);