> */ public static function for_post( WP_Post $post ): array { if ( ! function_exists( 'parse_blocks' ) || ! str_contains( (string) $post->post_content, 'wp:ai-rank-suite/' ) ) { return array(); } $faqs = array(); $nodes = array(); foreach ( self::flatten( parse_blocks( (string) $post->post_content ) ) as $block ) { $name = (string) ( $block['blockName'] ?? '' ); $attrs = (array) ( $block['attrs'] ?? array() ); if ( 'ai-rank-suite/faq' === $name ) { foreach ( Render::items( $attrs ) as $item ) { $faqs[] = array( '@type' => 'Question', 'name' => $item['question'], 'acceptedAnswer' => array( '@type' => 'Answer', 'text' => wp_strip_all_tags( $item['answer'] ), ), ); } } if ( 'ai-rank-suite/howto' === $name ) { $node = self::howto( $attrs, $post ); if ( null !== $node ) { $nodes[] = $node; } } } if ( ! empty( $faqs ) ) { // One FAQPage per page, however many blocks: the questions of a // second block belong to the same page, not to a second one. array_unshift( $nodes, array( '@type' => 'FAQPage', '@id' => get_permalink( $post ) . '#faq', 'mainEntity' => $faqs, ) ); } return $nodes; } /** * Which schema types the blocks of this post produce — so the builder can * let a hand-written item of the same type win. * * @return array */ public static function types( WP_Post $post ): array { return array_values( array_unique( array_column( self::for_post( $post ), '@type' ) ) ); } /** * @param array $attrs * @return array|null */ private static function howto( array $attrs, WP_Post $post ): ?array { $steps = Render::steps( $attrs ); if ( empty( $steps ) ) { return null; } $node = array( '@type' => 'HowTo', '@id' => get_permalink( $post ) . '#howto', 'name' => trim( (string) ( $attrs['name'] ?? '' ) ) !== '' ? trim( (string) $attrs['name'] ) : get_the_title( $post ), ); $description = trim( (string) ( $attrs['description'] ?? '' ) ); if ( '' !== $description ) { $node['description'] = $description; } $time = trim( (string) ( $attrs['totalTime'] ?? '' ) ); if ( preg_match( '/^P(?:T(?:\d+H)?(?:\d+M)?)?$/i', $time ) && 'PT' !== strtoupper( $time ) ) { $node['totalTime'] = strtoupper( $time ); } foreach ( array( 'supplies' => 'supply', 'tools' => 'tool' ) as $key => $property ) { $list = Render::strings( (array) ( $attrs[ $key ] ?? array() ) ); if ( empty( $list ) ) { continue; } $node[ $property ] = array_map( static fn( string $name ): array => array( '@type' => 'supply' === $property ? 'HowToSupply' : 'HowToTool', 'name' => $name, ), $list ); } $node['step'] = array(); foreach ( $steps as $i => $step ) { $entry = array( '@type' => 'HowToStep', 'position' => $i + 1, 'text' => wp_strip_all_tags( $step['text'] ), ); if ( '' !== $step['name'] ) { $entry['name'] = $step['name']; } if ( '' !== $step['image'] ) { $entry['image'] = $step['image']; } $node['step'][] = $entry; } return $node; } /** * @param array> $blocks * @return array> */ private static function flatten( array $blocks ): array { $out = array(); foreach ( $blocks as $block ) { $out[] = $block; if ( ! empty( $block['innerBlocks'] ) && is_array( $block['innerBlocks'] ) ) { $out = array_merge( $out, self::flatten( $block['innerBlocks'] ) ); } } return $out; } }