'',
'levels' => 'h2-h3',
'numbered' => '0',
'sticky' => '0',
'collapsible' => '0',
'min' => '2',
'post' => '0',
'layout' => 'list',
'class' => '',
), (array) $atts, self::SHORTCODE );
self::enqueue_assets();
return self::render( array(
'title' => (string) $a['title'],
'levels' => (string) $a['levels'],
'numbered' => self::truthy( $a['numbered'] ),
'sticky' => self::truthy( $a['sticky'] ),
'collapsible' => self::truthy( $a['collapsible'] ),
'min' => (int) $a['min'],
'post' => (int) $a['post'],
'layout' => (string) $a['layout'],
'wrapper' => 'class="aieo-toc-block' . ( '' !== $a['class'] ? ' ' . esc_attr( $a['class'] ) : '' ) . '"',
) );
}
public static function enqueue_assets() {
if ( ! function_exists( 'wp_enqueue_block_style' ) || ! class_exists( 'WP_Block_Type_Registry' ) ) { return; }
$type = WP_Block_Type_Registry::get_instance()->get_registered( 'aieo/toc' );
if ( ! $type ) { return; }
foreach ( (array) $type->view_style_handles as $h ) { wp_enqueue_style( $h ); }
foreach ( (array) $type->view_script_handles as $h ) { wp_enqueue_script( $h ); }
}
public static function allowed_html() {
$wrap = array( 'class' => true, 'id' => true, 'style' => true, 'data-*' => true );
return array(
'div' => $wrap,
'nav' => array( 'class' => true, 'data-offset' => true, 'aria-label' => true ),
'p' => array( 'class' => true ),
'span' => array(),
'button' => array( 'type' => true, 'class' => true, 'aria-expanded' => true, 'aria-controls' => true ),
'ol' => array( 'class' => true ),
'li' => array( 'class' => true ),
'a' => array( 'href' => true ),
);
}
public static function render( array $o ) {
$post_id = ! empty( $o['post'] ) ? (int) $o['post'] : self::current_post_id();
if ( $post_id && ! self::readable( $post_id ) ) {
return '';
}
$content = $post_id ? (string) get_post_field( 'post_content', $post_id ) : '';
$levels = ( isset( $o['levels'] ) && 'h2' === $o['levels'] ) ? array( 2 ) : array( 2, 3 );
$items = self::headings( $content, $levels );
if ( ! $items && ! empty( $o['sample'] ) ) {
$items = array(
array( 'level' => 2, 'id' => 'sample-1', 'text' => __( 'First section', 'ai-eshop-optimizer' ) ),
array( 'level' => 3, 'id' => 'sample-1-1', 'text' => __( 'A sub-point', 'ai-eshop-optimizer' ) ),
array( 'level' => 2, 'id' => 'sample-2', 'text' => __( 'Second section', 'ai-eshop-optimizer' ) ),
);
}
$min = isset( $o['min'] ) ? max( 1, (int) $o['min'] ) : 2;
$top = 0;
foreach ( $items as $it ) { if ( 2 === $it['level'] ) { ++$top; } }
if ( $top < $min ) { return ''; }
$title = isset( $o['title'] ) && '' !== trim( (string) $o['title'] ) ? (string) $o['title'] : __( 'In this article', 'ai-eshop-optimizer' );
$inline = isset( $o['layout'] ) && 'inline' === $o['layout'];
if ( $inline ) {
$items = array_values( array_filter( $items, static function ( $it ) { return 2 === $it['level']; } ) );
$o['collapsible'] = false;
}
$classes = array( 'aieo-toc' );
if ( $inline ) { $classes[] = 'is-inline'; }
if ( ! empty( $o['numbered'] ) ) { $classes[] = 'is-numbered'; }
if ( ! empty( $o['sticky'] ) ) { $classes[] = 'is-sticky'; }
if ( ! empty( $o['collapsible'] ) ) { $classes[] = 'is-collapsible'; }
$offset = (int) apply_filters( 'aieo_toc_scroll_offset', 90 );
$wrapper = isset( $o['wrapper'] ) ? (string) $o['wrapper'] : 'class="aieo-toc-block"';
$html = '
';
$html .= '
';
return $html;
}
public static function add_heading_ids( $content ) {
if ( ! is_singular() || ! in_the_loop() || ! is_main_query() ) { return $content; }
if ( false === stripos( $content, ']*)>(.*?)#is', static function ( $m ) use ( &$seen ) {
if ( preg_match( '/\sid\s*=\s*["\']([^"\']+)["\']/i', $m[2], $id ) ) {
return $m[0];
}
$id = self::unique_id( self::slug( $m[3] ), $seen );
return '' . $m[3] . '';
}, $content );
}
private static function ids_in( $html ) {
preg_match_all( '/\sid\s*=\s*(["\'])(.*?)\1/i', (string) $html, $m );
return array_values( array_unique( $m[2] ) );
}
private static $on_page = array();
private static $in_template = array();
public static function on_page( $post_id ) {
$post_id = (int) $post_id;
if ( isset( self::$on_page[ $post_id ] ) ) {
return self::$on_page[ $post_id ];
}
$post = $post_id ? get_post( $post_id ) : null;
$found = false;
if ( $post instanceof WP_Post ) {
$found = has_block( 'aieo/toc', $post ) || has_shortcode( (string) $post->post_content, self::SHORTCODE );
}
if ( ! $found && ! empty( $GLOBALS['_wp_current_template_content'] ) ) {
$template = md5( (string) $GLOBALS['_wp_current_template_content'] );
if ( ! isset( self::$in_template[ $template ] ) ) {
self::$in_template[ $template ] = self::in_template( (string) $GLOBALS['_wp_current_template_content'], 0 );
}
$found = self::$in_template[ $template ];
}
if ( ! $found && $post_id && class_exists( 'AIEO_Elementor_Blocks', false ) ) {
$data = get_post_meta( $post_id, '_elementor_data', true );
$found = is_string( $data ) && str_contains( $data, '"widgetType":"aieo-toc"' );
}
self::$on_page[ $post_id ] = (bool) apply_filters( 'aieo_toc_on_page', $found, $post_id );
return self::$on_page[ $post_id ];
}
private static function in_template( $markup, $depth ) {
if ( has_block( 'aieo/toc', $markup ) ) {
return true;
}
if ( $depth > 2 ) {
return false;
}
if ( class_exists( 'WP_Block_Patterns_Registry' ) && preg_match_all( '//s', $markup, $patterns ) ) {
foreach ( $patterns[1] as $json ) {
$attrs = json_decode( $json, true );
$pattern = ( is_array( $attrs ) && ! empty( $attrs['slug'] ) ) ? WP_Block_Patterns_Registry::get_instance()->get_registered( (string) $attrs['slug'] ) : null;
if ( is_array( $pattern ) && isset( $pattern['content'] ) && self::in_template( (string) $pattern['content'], $depth + 1 ) ) {
return true;
}
}
}
if ( ! function_exists( 'get_block_template' ) || ! preg_match_all( '//s', $markup, $parts ) ) {
return false;
}
foreach ( $parts[1] as $json ) {
$attrs = json_decode( $json, true );
if ( ! is_array( $attrs ) || empty( $attrs['slug'] ) ) {
continue;
}
$theme = ! empty( $attrs['theme'] ) ? (string) $attrs['theme'] : get_stylesheet();
$part = get_block_template( $theme . '//' . (string) $attrs['slug'], 'wp_template_part' );
if ( $part && self::in_template( (string) $part->content, $depth + 1 ) ) {
return true;
}
}
return false;
}
private static function readable( $post_id ) {
$post = get_post( (int) $post_id );
if ( ! $post instanceof WP_Post ) {
return false;
}
if ( post_password_required( $post ) ) {
return false;
}
return 'publish' === get_post_status( $post ) || current_user_can( 'read_post', $post->ID );
}
public static function headings( $content, array $levels ) {
$out = array();
if ( '' === $content ) { return $out; }
if ( ! preg_match_all( '#]*)>(.*?)#is', $content, $all, PREG_SET_ORDER ) ) { return $out; }
$seen = self::ids_in( $content );
foreach ( $all as $m ) {
$level = (int) $m[1];
if ( preg_match( '/\sid\s*=\s*["\']([^"\']+)["\']/i', $m[2], $idm ) ) {
$id = $idm[1];
} else {
$id = self::unique_id( self::slug( $m[3] ), $seen );
}
if ( ! in_array( $level, $levels, true ) ) { continue; }
$text = trim( html_entity_decode( wp_strip_all_tags( $m[3] ), ENT_QUOTES, 'UTF-8' ) );
if ( '' === $text ) { continue; }
$out[] = array( 'level' => $level, 'id' => $id, 'text' => $text );
}
return $out;
}
public static function slug( $html ) {
$text = html_entity_decode( wp_strip_all_tags( (string) $html ), ENT_QUOTES, 'UTF-8' );
$text = mb_strtolower( trim( $text ), 'UTF-8' );
$text = strtr( $text, array(
'ά' => 'α', 'έ' => 'ε', 'ή' => 'η', 'ί' => 'ι', 'ϊ' => 'ι', 'ΐ' => 'ι', 'ό' => 'ο', 'ύ' => 'υ', 'ϋ' => 'υ', 'ΰ' => 'υ', 'ώ' => 'ω', 'ς' => 'σ',
) );
$text = strtr( $text, array( 'ου' => 'oy', 'αι' => 'ai', 'ει' => 'ei', 'οι' => 'oi', 'αυ' => 'ay', 'ευ' => 'ey', 'μπ' => 'mp', 'ντ' => 'nt', 'γκ' => 'gk', 'γγ' => 'gg' ) );
$text = strtr( $text, array(
'α' => 'a', 'β' => 'v', 'γ' => 'g', 'δ' => 'd', 'ε' => 'e', 'ζ' => 'z', 'η' => 'i', 'θ' => 'th', 'ι' => 'i', 'κ' => 'k', 'λ' => 'l', 'μ' => 'm', 'ν' => 'n', 'ξ' => 'x',
'ο' => 'o', 'π' => 'p', 'ρ' => 'r', 'σ' => 's', 'τ' => 't', 'υ' => 'y', 'φ' => 'f', 'χ' => 'ch', 'ψ' => 'ps', 'ω' => 'o',
) );
$text = remove_accents( $text );
$text = preg_replace( '/[^a-z0-9]+/', '-', $text );
$text = trim( (string) $text, '-' );
if ( strlen( $text ) > 60 ) {
$text = rtrim( substr( $text, 0, 60 ), '-' );
}
return '' === $text ? 'section' : $text;
}
private static function unique_id( $base, array &$seen ) {
$id = $base;
$n = 1;
while ( in_array( $id, $seen, true ) ) {
$id = $base . '-' . ( ++$n );
}
$seen[] = $id;
return $id;
}
private static function current_post_id() {
$id = get_queried_object_id();
if ( $id && is_singular() ) { return (int) $id; }
$id = get_the_ID();
return $id ? (int) $id : 0;
}
private static function truthy( $v ) {
return in_array( strtolower( trim( (string) $v ) ), array( '1', 'true', 'yes', 'on' ), true );
}
}
}