From dc35a55ec5866c3ec0e798baeabfc0ae0f104ed7 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 26 Sep 2024 13:44:49 -0300 Subject: [PATCH 1/3] Checks the number of h1 elements on block theme templates --- checkbase.php | 1 + checks/class-fse-templates-h1-check.php | 92 +++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 checks/class-fse-templates-h1-check.php 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..b4c4d9e --- /dev/null +++ b/checks/class-fse-templates-h1-check.php @@ -0,0 +1,92 @@ +content ); + $h1_count = $this->count_h1_tags_recursively( $blocks ); + + if ( $h1_count > 1 ) { + $ret = false; + $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( '' . __( 'REQUIRED', '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; + } +} From 282e051ff0a72a67c9aaa29166c3d563cb99b314 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 26 Sep 2024 13:50:08 -0300 Subject: [PATCH 2/3] lint --- checks/class-fse-templates-h1-check.php | 54 ++++++++++++------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/checks/class-fse-templates-h1-check.php b/checks/class-fse-templates-h1-check.php index b4c4d9e..823098e 100644 --- a/checks/class-fse-templates-h1-check.php +++ b/checks/class-fse-templates-h1-check.php @@ -29,32 +29,32 @@ class FSE_Templates_H1_Check implements themecheck { */ public function check( $php_files, $css_files, $other_files ) { - $ret = true; - $templates = get_block_templates(); - $templates_with_multiple_h1_tags = array(); - $templates_with_no_h1_tags = array(); + $ret = true; + $templates = get_block_templates(); + $templates_with_multiple_h1_tags = array(); + $templates_with_no_h1_tags = array(); - foreach ($templates as $template) { - $blocks = parse_blocks( $template->content ); - $h1_count = $this->count_h1_tags_recursively( $blocks ); + foreach ( $templates as $template ) { + $blocks = parse_blocks( $template->content ); + $h1_count = $this->count_h1_tags_recursively( $blocks ); - if ( $h1_count > 1 ) { - $ret = false; - $templates_with_multiple_h1_tags[] = $template->slug; - } + if ( $h1_count > 1 ) { + $ret = false; + $templates_with_multiple_h1_tags[] = $template->slug; + } - if ( $h1_count === 0 ) { - $templates_with_no_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( '' . __( 'REQUIRED', 'theme-check' ) . ': ' . __( 'The following templates have multiple h1 tags: %s', 'theme-check' ), '' . implode( ', ', $templates_with_multiple_h1_tags ) . '' ); - } + if ( ! empty( $templates_with_multiple_h1_tags ) ) { + $this->error[] = sprintf( '' . __( 'REQUIRED', '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 ) . '' ); - } + 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; } @@ -71,12 +71,12 @@ private function count_h1_tags_recursively( $blocks ) { 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++; - } - } - } + if ( $block['blockName'] === 'core/heading' || $block['blockName'] === 'core/post-title' ) { + if ( isset( $block['attrs']['level'] ) && $block['attrs']['level'] === 1 ) { + $h1_count++; + } + } + } } return $h1_count; } From c05ac275cabdbfab3be25ccbefb4908c3e54d0a5 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 27 Sep 2024 11:00:07 -0300 Subject: [PATCH 3/3] h1 checks output only warnings --- checks/class-fse-templates-h1-check.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/checks/class-fse-templates-h1-check.php b/checks/class-fse-templates-h1-check.php index 823098e..4072d74 100644 --- a/checks/class-fse-templates-h1-check.php +++ b/checks/class-fse-templates-h1-check.php @@ -39,7 +39,6 @@ public function check( $php_files, $css_files, $other_files ) { $h1_count = $this->count_h1_tags_recursively( $blocks ); if ( $h1_count > 1 ) { - $ret = false; $templates_with_multiple_h1_tags[] = $template->slug; } @@ -49,7 +48,7 @@ public function check( $php_files, $css_files, $other_files ) { } if ( ! empty( $templates_with_multiple_h1_tags ) ) { - $this->error[] = sprintf( '' . __( 'REQUIRED', 'theme-check' ) . ': ' . __( 'The following templates have multiple h1 tags: %s', 'theme-check' ), '' . implode( ', ', $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 ) ) {