*/ namespace RankMath\Tools; use RankMath\Helpers\HTML; defined( 'ABSPATH' ) || exit; /** * Yoast_TOC_Converter class. */ class Yoast_TOC_Converter { /** * Convert TOC blocks to Rank Math. * * @param array $block Block to convert. * * @return array */ public function convert( $block ) { $exclude_headings = []; if ( ! empty( $block['attrs']['maxHeadingLevel'] ) ) { for ( $i = $block['attrs']['maxHeadingLevel'] + 1; $i <= 6; $i++ ) { $exclude_headings[] = "h{$i}"; } } $new_block = [ 'blockName' => 'rank-math/toc-block', 'attrs' => [ 'title' => $this->get_toc_title( $block['innerHTML'] ), 'headings' => $this->get_headings( $block['innerHTML'] ), 'listStyle' => 'ul', 'titleWrapper' => 'h2', 'excludeHeadings' => $exclude_headings, ], ]; $new_block['innerContent'][] = $this->get_html( $block['innerHTML'] ); return $new_block; } /** * Replace block in content. * * @param string $post_content Post content. * @param array $blocks Blocks. * * @return string */ public function replace( $post_content, $blocks ) { preg_match_all( '/.*/iUs', $post_content, $matches ); foreach ( $matches[0] as $index => $match ) { $post_content = \str_replace( $match, $blocks[ $index ], $post_content ); } return $post_content; } /** * Extract headings from the block content. * * @param string $content Block content. * * @return array */ public function get_headings( $content ) { preg_match_all( '|]+>([^<]+)|', $content, $matches ); if ( empty( $matches ) || empty( $matches[0] ) ) { return []; } $headings = []; foreach ( $matches[0] as $link ) { $attrs = HTML::extract_attributes( $link ); $headings[] = [ 'key' => uniqid( 'toc-' ), 'link' => $attrs['href'] ?? '', 'content' => wp_strip_all_tags( $link ), 'level' => $attrs['data-level'] ?? '', 'disable' => false, ]; } return $headings; } /** * Get TOC title. * * @param string $html Block HTML. * * @return string */ public function get_toc_title( $html ) { preg_match( '#