'.esc_html__('Table of content shortcode can be used only once on a page, this page is using the shortcode more than once. Please remove the extra table of content shortcodes for this warning to go away.', 'siteseo').'

'; } $options = get_option('siteseo_advanced_option_name', []); $content = get_the_content(); if(empty($content)){ return; } $heading_type = (!empty($options['toc_heading_type']) ? $options['toc_heading_type'] : 'ul'); $dom = new DOMDocument(); $internalErrors = libxml_use_internal_errors(true); $dom->preserveWhiteSpace = false; $html = ''; if($dom->loadHTML('' . $content)){ $xpath = new DOMXPath($dom); $heading_list = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']; $headings_to_scan = []; foreach($heading_list as $h){ if(!empty($options['toc_excluded_headings']) && is_array($options['toc_excluded_headings']) && in_array($h, $options['toc_excluded_headings'])){ continue; } $headings_to_scan[] = '//'.$h; } if(empty($headings_to_scan)){ return; } $headings_to_scan = implode('|', $headings_to_scan); // The imploded string will look like this //h1|//h2|//h3|//h4|//h5|//h6 $headings = $xpath->query($headings_to_scan); if(empty($headings)){ return; } $last_h = 0; $open_ul = 0; $html .= '

'.(!empty($options['toc_label']) ? esc_html($options['toc_label']) : esc_html__('Table of Content', 'siteseo')).'

<'.esc_html($heading_type).'>'; foreach($headings as $heading){ $title = trim(strip_tags($heading->nodeValue)); $id = $heading->getAttribute('id'); $current_h = (int) substr($heading->tagName, 1); if(empty($id)){ $id = '#'.siteseo_title_to_id($title); } else { $id = '#'.$id; } if($current_h > $last_h){ $html .= '<'.esc_html($heading_type).'>'; $open_ul++; } else { while($current_h <= $open_ul){ $html .= ''; $open_ul--; } } $html .= '
  • '.esc_html($title).'
  • '; $last_h = $current_h; } $html .= '
    '; $siteseo_toc_run = true; } return $html; } // Converts heading text content to ID to be used as link function siteseo_title_to_id($title){ $id = trim(strip_tags($title)); $id = remove_accents($title); $id = sanitize_title_with_dashes($id); $id = urlencode($id); return $id; } function siteseo_add_ids_to_headings($content){ if(empty($content)){ return $content; } // If the page does not have the shortcode then we don't need to update the id's in the heading. if(!has_shortcode($content, 'siteseo_toc')){ return $content; } $dom = new DOMDocument(); $internalErrors = libxml_use_internal_errors(true); $dom->preserveWhiteSpace = false; $html = ''; if($dom->loadHTML('' . $content)){ $xpath = new DOMXPath($dom); $headings = $xpath->query('//h1|//h2|//h3|//h4|//h5|//h6'); if(empty($headings)){ return; } foreach($headings as $heading){ $title = trim(strip_tags($heading->nodeValue)); $id = $heading->getAttribute('id'); if(!empty($id)){ continue; } if(empty($title)){ continue; } $id = siteseo_title_to_id($title); $heading->setAttribute('id', $id); } $content = $dom->saveHTML($dom->documentElement); } return $content; }