diff --git a/checkbase.php b/checkbase.php index 7c03305..b27d649 100644 --- a/checkbase.php +++ b/checkbase.php @@ -386,6 +386,7 @@ function tc_adapt_checks_for_fse_themes( $php_files, $css_files, $other_files ) // Add FSE specific checks. $themechecks[] = new FSE_Required_Files_Check(); + $themechecks[] = new FSE_Templates_H1_Check(); return true; } diff --git a/checks/class-fse-templates-h1-check.php b/checks/class-fse-templates-h1-check.php new file mode 100644 index 0000000..4072d74 --- /dev/null +++ b/checks/class-fse-templates-h1-check.php @@ -0,0 +1,91 @@ +content ); + $h1_count = $this->count_h1_tags_recursively( $blocks ); + + if ( $h1_count > 1 ) { + $templates_with_multiple_h1_tags[] = $template->slug; + } + + if ( $h1_count === 0 ) { + $templates_with_no_h1_tags[] = $template->slug; + } + } + + if ( ! empty( $templates_with_multiple_h1_tags ) ) { + $this->error[] = sprintf( '' . __( 'WARNING', 'theme-check' ) . ': ' . __( 'The following templates have multiple h1 tags: %s', 'theme-check' ), '' . implode( ', ', $templates_with_multiple_h1_tags ) . '' ); + } + + if ( ! empty( $templates_with_no_h1_tags ) ) { + $this->error[] = sprintf( '' . __( 'WARNING', 'theme-check' ) . ': ' . __( 'The following templates have no h1 tags: %s', 'theme-check' ), '' . implode( ', ', $templates_with_no_h1_tags ) . '' ); + } + + return $ret; + } + + /** + * Recursively count h1 tags in blocks, including nested blocks. + * + * @param array $blocks Array of blocks to process. + * @return int Number of h1 tags found. + */ + private function count_h1_tags_recursively( $blocks ) { + $h1_count = 0; + foreach ( $blocks as $block ) { + if ( ! empty( $block['innerBlocks'] ) ) { + $h1_count += $this->count_h1_tags_recursively( $block['innerBlocks'] ); + } else { + if ( $block['blockName'] === 'core/heading' || $block['blockName'] === 'core/post-title' ) { + if ( isset( $block['attrs']['level'] ) && $block['attrs']['level'] === 1 ) { + $h1_count++; + } + } + } + } + return $h1_count; + } + + /** + * Get error messages from the checks. + * + * @return array Error message. + */ + public function getError() { + return $this->error; + } +}