From c8f16a3f6c2aff4dd81d7816b8d66461df82463b Mon Sep 17 00:00:00 2001 From: MaggieCabrera Date: Tue, 3 Sep 2024 12:35:03 +0200 Subject: [PATCH 01/12] adds skip links check for block themes --- checks/class-skip-links.php | 106 ++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 checks/class-skip-links.php diff --git a/checks/class-skip-links.php b/checks/class-skip-links.php new file mode 100644 index 00000000..747132e5 --- /dev/null +++ b/checks/class-skip-links.php @@ -0,0 +1,106 @@ +wp_theme = $data['theme']; + $theme_dir = $this->wp_theme->get_stylesheet_directory(); + // Check if the theme has all the required files. + if ( + file_exists($theme_dir.'/theme.json') || + ( + file_exists($theme_dir.'/templates/index.html') && + file_exists($theme_dir.'/block-templates/index.html') + ) + ) { + $this->is_block_theme = true; + } + } + } + + /** + * Check that return true for good/okay/acceptable, false for bad/not-okay/unacceptable. + * + * @param array $php_files File paths and content for PHP files. + * @param array $css_files File paths and content for CSS files. + * @param array $other_files Folder names, file paths and content for other files. + */ + public function check( $php_files, $css_files, $other_files ) { + + + $info = ''; + $templates_without_main_tag = []; + + $directory = 'templates'; // Path to the folder containing HTML files + $theme_dir = $this->wp_theme->get_stylesheet_directory(); + + // Get all HTML files in the directory + $files = glob($theme_dir . '/' . $directory . '/*.html'); + + foreach ($files as $file) { + $contents = file_get_contents($file); + $hasMainTag = strpos($contents, 'error[] = sprintf( + '%s %s ', + __( 'REQUIRED', 'theme-check' ), + sprintf( + __( 'Skip links are missing from the following templates: ' . $info . '. Please make sure the templates have a
tag', 'theme-check' ) + ) + ); + } + + return true; + } + + /** + * Get error messages from the checks. + * + * @return array Error message. + */ + public function getError() { + return $this->error; + } +} + +$themechecks[] = new Skip_Links_Check(); From db23a46b16fe11d49ac16d8c829c24712f564909 Mon Sep 17 00:00:00 2001 From: MaggieCabrera Date: Tue, 3 Sep 2024 12:39:09 +0200 Subject: [PATCH 02/12] fixed formatting --- checks/class-skip-links.php | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/checks/class-skip-links.php b/checks/class-skip-links.php index 747132e5..c90ac92b 100644 --- a/checks/class-skip-links.php +++ b/checks/class-skip-links.php @@ -33,13 +33,13 @@ class Skip_Links_Check implements themecheck { function set_context( $data ) { if ( isset( $data['theme'] ) ) { $this->wp_theme = $data['theme']; - $theme_dir = $this->wp_theme->get_stylesheet_directory(); + $theme_dir = $this->wp_theme->get_stylesheet_directory(); // Check if the theme has all the required files. if ( - file_exists($theme_dir.'/theme.json') || + file_exists( $theme_dir . '/theme.json' ) || ( - file_exists($theme_dir.'/templates/index.html') && - file_exists($theme_dir.'/block-templates/index.html') + file_exists( $theme_dir . '/templates/index.html' ) && + file_exists( $theme_dir . '/block-templates/index.html' ) ) ) { $this->is_block_theme = true; @@ -56,31 +56,30 @@ function set_context( $data ) { */ public function check( $php_files, $css_files, $other_files ) { - - $info = ''; - $templates_without_main_tag = []; + $info = ''; + $templates_without_main_tag = array(); $directory = 'templates'; // Path to the folder containing HTML files $theme_dir = $this->wp_theme->get_stylesheet_directory(); // Get all HTML files in the directory - $files = glob($theme_dir . '/' . $directory . '/*.html'); - - foreach ($files as $file) { - $contents = file_get_contents($file); - $hasMainTag = strpos($contents, 'error[] = sprintf( '%s %s ', __( 'REQUIRED', 'theme-check' ), From 382d1513b5a8bdb2c7307336da55381eeba5289a Mon Sep 17 00:00:00 2001 From: MaggieCabrera Date: Thu, 5 Sep 2024 17:08:18 +0200 Subject: [PATCH 03/12] check inside patterns for a main tag --- checks/class-skip-links.php | 53 ++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/checks/class-skip-links.php b/checks/class-skip-links.php index c90ac92b..75874041 100644 --- a/checks/class-skip-links.php +++ b/checks/class-skip-links.php @@ -70,11 +70,19 @@ public function check( $php_files, $css_files, $other_files ) { $hasMainTag = strpos( $contents, 'template_has_patterns($contents); + if( $pattern_slugs ) { + foreach($pattern_slugs as $slug){ + $hasMainTag = $this->pattern_has_tag($slug); + if ( ! $hasMainTag ) { + $templates_without_main_tag[] = $fileName; + } + } + }else { + $templates_without_main_tag[] = $fileName; + } } - // TODO: check on nested patterns!! } $info = implode( ', ', $templates_without_main_tag ); @@ -92,6 +100,45 @@ public function check( $php_files, $css_files, $other_files ) { return true; } + function template_has_patterns($contents) { + $pattern = '//'; + if (preg_match_all($pattern, $contents, $matches)) { + $slugs = $matches[1]; + return $slugs; + } else { + return false; + } + } + + function pattern_has_tag($slug){ + $directory = 'patterns'; + $theme_dir = $this->wp_theme->get_stylesheet_directory(); + + if(! is_dir($theme_dir . '/' . $directory)){ + $directory = 'block-patterns'; + } + if(! is_dir($theme_dir . '/' . $directory)){ + return false; + } + + $files = glob( $theme_dir . '/' . $directory . '/*.php' ); + + $has_tag = false; + + foreach ($files as $file) { + if (is_file($file)) { + $contents = file_get_contents($file); + $pattern = '/\* Slug: ' . preg_quote($slug, '/') . '\b/'; + if (preg_match($pattern, $contents)) { + $has_tag = strpos( $contents, ' Date: Thu, 5 Sep 2024 17:14:26 +0200 Subject: [PATCH 04/12] fix formatting --- checks/class-skip-links.php | 60 ++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/checks/class-skip-links.php b/checks/class-skip-links.php index 75874041..48e06a05 100644 --- a/checks/class-skip-links.php +++ b/checks/class-skip-links.php @@ -66,21 +66,21 @@ public function check( $php_files, $css_files, $other_files ) { $files = glob( $theme_dir . '/' . $directory . '/*.html' ); foreach ( $files as $file ) { - $contents = file_get_contents( $file ); - $hasMainTag = strpos( $contents, 'template_has_patterns($contents); - if( $pattern_slugs ) { - foreach($pattern_slugs as $slug){ - $hasMainTag = $this->pattern_has_tag($slug); - if ( ! $hasMainTag ) { - $templates_without_main_tag[] = $fileName; + $contents = file_get_contents( $file ); + $has_main_tag = strpos( $contents, 'template_has_patterns( $contents ); + if ( $pattern_slugs ) { + foreach ( $pattern_slugs as $slug ) { + $has_main_tag = $this->pattern_has_tag( $slug ); + if ( ! $has_main_tag ) { + $templates_without_main_tag[] = $file_name; } } - }else { - $templates_without_main_tag[] = $fileName; + } else { + $templates_without_main_tag[] = $file_name; } } } @@ -92,7 +92,8 @@ public function check( $php_files, $css_files, $other_files ) { '%s %s ', __( 'REQUIRED', 'theme-check' ), sprintf( - __( 'Skip links are missing from the following templates: ' . $info . '. Please make sure the templates have a
tag', 'theme-check' ) + __( 'Skip links are missing from the following templates: %s Please make sure the templates have a
tag', 'theme-check' ), + $info ) ); } @@ -100,9 +101,9 @@ public function check( $php_files, $css_files, $other_files ) { return true; } - function template_has_patterns($contents) { + function template_has_patterns( $contents ) { $pattern = '//'; - if (preg_match_all($pattern, $contents, $matches)) { + if ( preg_match_all( $pattern, $contents, $matches ) ) { $slugs = $matches[1]; return $slugs; } else { @@ -110,32 +111,31 @@ function template_has_patterns($contents) { } } - function pattern_has_tag($slug){ - $directory = 'patterns'; + function pattern_has_tag( $slug ) { + $directory = 'patterns'; $theme_dir = $this->wp_theme->get_stylesheet_directory(); - if(! is_dir($theme_dir . '/' . $directory)){ - $directory = 'block-patterns'; + if ( ! is_dir( $theme_dir . '/' . $directory ) ) { + $directory = 'block-patterns'; } - if(! is_dir($theme_dir . '/' . $directory)){ - return false; + if ( ! is_dir( $theme_dir . '/' . $directory ) ) { + return false; } $files = glob( $theme_dir . '/' . $directory . '/*.php' ); $has_tag = false; - - foreach ($files as $file) { - if (is_file($file)) { - $contents = file_get_contents($file); - $pattern = '/\* Slug: ' . preg_quote($slug, '/') . '\b/'; - if (preg_match($pattern, $contents)) { + + foreach ( $files as $file ) { + if ( is_file( $file ) ) { + $contents = file_get_contents( $file ); + $pattern = '/\* Slug: ' . preg_quote( $slug, '/' ) . '\b/'; + if ( preg_match( $pattern, $contents ) ) { $has_tag = strpos( $contents, ' Date: Wed, 11 Sep 2024 17:14:28 +0200 Subject: [PATCH 05/12] change is block theme check to use function --- checks/class-skip-links.php | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/checks/class-skip-links.php b/checks/class-skip-links.php index 48e06a05..e079fb12 100644 --- a/checks/class-skip-links.php +++ b/checks/class-skip-links.php @@ -33,17 +33,7 @@ class Skip_Links_Check implements themecheck { function set_context( $data ) { if ( isset( $data['theme'] ) ) { $this->wp_theme = $data['theme']; - $theme_dir = $this->wp_theme->get_stylesheet_directory(); - // Check if the theme has all the required files. - if ( - file_exists( $theme_dir . '/theme.json' ) || - ( - file_exists( $theme_dir . '/templates/index.html' ) && - file_exists( $theme_dir . '/block-templates/index.html' ) - ) - ) { - $this->is_block_theme = true; - } + $this->is_block_theme = wp_is_block_theme(); } } From 3b2ea37041b9713bc2a7bd439a206de8c2bf0cd2 Mon Sep 17 00:00:00 2001 From: MaggieCabrera Date: Wed, 11 Sep 2024 17:17:25 +0200 Subject: [PATCH 06/12] escape html tags --- checks/class-skip-links.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/checks/class-skip-links.php b/checks/class-skip-links.php index e079fb12..1e191e3e 100644 --- a/checks/class-skip-links.php +++ b/checks/class-skip-links.php @@ -82,7 +82,7 @@ public function check( $php_files, $css_files, $other_files ) { '%s %s ', __( 'REQUIRED', 'theme-check' ), sprintf( - __( 'Skip links are missing from the following templates: %s Please make sure the templates have a
tag', 'theme-check' ), + __( 'Skip links are missing from the following templates: %s Please make sure the templates have a <main> tag', 'theme-check' ), $info ) ); From 1aa08ceda2edfe65c86cc0d9d96dd1c3665c1793 Mon Sep 17 00:00:00 2001 From: MaggieCabrera Date: Wed, 11 Sep 2024 17:40:11 +0200 Subject: [PATCH 07/12] check for nested patterns --- checks/class-skip-links.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/checks/class-skip-links.php b/checks/class-skip-links.php index 1e191e3e..c504c8d8 100644 --- a/checks/class-skip-links.php +++ b/checks/class-skip-links.php @@ -32,7 +32,7 @@ class Skip_Links_Check implements themecheck { function set_context( $data ) { if ( isset( $data['theme'] ) ) { - $this->wp_theme = $data['theme']; + $this->wp_theme = $data['theme']; $this->is_block_theme = wp_is_block_theme(); } } @@ -122,6 +122,14 @@ function pattern_has_tag( $slug ) { $pattern = '/\* Slug: ' . preg_quote( $slug, '/' ) . '\b/'; if ( preg_match( $pattern, $contents ) ) { $has_tag = strpos( $contents, 'template_has_patterns( $contents ); + if ( $nested_patterns_slugs ) { + foreach ( $nested_patterns_slugs as $slug ) { + $has_tag = $this->pattern_has_tag( $slug ); + } + } + } } } } From 17ee4214ab4331440865f75ab27b0aa17e212fda Mon Sep 17 00:00:00 2001 From: Maggie Date: Mon, 16 Sep 2024 15:39:46 +0200 Subject: [PATCH 08/12] Update checks/class-skip-links.php Co-authored-by: Matias Benedetto --- checks/class-skip-links.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/checks/class-skip-links.php b/checks/class-skip-links.php index c504c8d8..cbfa5bd1 100644 --- a/checks/class-skip-links.php +++ b/checks/class-skip-links.php @@ -82,7 +82,7 @@ public function check( $php_files, $css_files, $other_files ) { '%s %s ', __( 'REQUIRED', 'theme-check' ), sprintf( - __( 'Skip links are missing from the following templates: %s Please make sure the templates have a <main> tag', 'theme-check' ), + __( 'Skip links are missing from the following templates: %s Please make sure the templates have a <main> tag.', 'theme-check' ), $info ) ); From 508a1510a307c021693ee05b78b089ecc1684af1 Mon Sep 17 00:00:00 2001 From: MaggieCabrera Date: Thu, 31 Oct 2024 10:35:55 +0100 Subject: [PATCH 09/12] return false when check fails --- checks/class-skip-links.php | 1 + 1 file changed, 1 insertion(+) diff --git a/checks/class-skip-links.php b/checks/class-skip-links.php index cbfa5bd1..8b38214a 100644 --- a/checks/class-skip-links.php +++ b/checks/class-skip-links.php @@ -86,6 +86,7 @@ public function check( $php_files, $css_files, $other_files ) { $info ) ); + return false; } return true; From ed39dbba1e76eaacceb04230d6d7559a09d65d1b Mon Sep 17 00:00:00 2001 From: MaggieCabrera Date: Thu, 31 Oct 2024 10:40:25 +0100 Subject: [PATCH 10/12] avoid duplicated file names --- checks/class-skip-links.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/checks/class-skip-links.php b/checks/class-skip-links.php index 8b38214a..0bf44206 100644 --- a/checks/class-skip-links.php +++ b/checks/class-skip-links.php @@ -66,11 +66,15 @@ public function check( $php_files, $css_files, $other_files ) { foreach ( $pattern_slugs as $slug ) { $has_main_tag = $this->pattern_has_tag( $slug ); if ( ! $has_main_tag ) { - $templates_without_main_tag[] = $file_name; + if ( ! in_array( $file_name, $templates_without_main_tag ) ) { + $templates_without_main_tag[] = $file_name; + } } } } else { - $templates_without_main_tag[] = $file_name; + if ( ! in_array( $file_name, $templates_without_main_tag ) ) { + $templates_without_main_tag[] = $file_name; + } } } } From e186040dfea97c164441fe4647e0e346361cf9ed Mon Sep 17 00:00:00 2001 From: MaggieCabrera Date: Thu, 31 Oct 2024 11:00:39 +0100 Subject: [PATCH 11/12] don't traverse file contents --- checks/class-skip-links.php | 43 +++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/checks/class-skip-links.php b/checks/class-skip-links.php index 0bf44206..b75f042f 100644 --- a/checks/class-skip-links.php +++ b/checks/class-skip-links.php @@ -49,31 +49,28 @@ public function check( $php_files, $css_files, $other_files ) { $info = ''; $templates_without_main_tag = array(); - $directory = 'templates'; // Path to the folder containing HTML files - $theme_dir = $this->wp_theme->get_stylesheet_directory(); - - // Get all HTML files in the directory - $files = glob( $theme_dir . '/' . $directory . '/*.html' ); - - foreach ( $files as $file ) { - $contents = file_get_contents( $file ); - $has_main_tag = strpos( $contents, 'template_has_patterns( $contents ); - if ( $pattern_slugs ) { - foreach ( $pattern_slugs as $slug ) { - $has_main_tag = $this->pattern_has_tag( $slug ); - if ( ! $has_main_tag ) { - if ( ! in_array( $file_name, $templates_without_main_tag ) ) { - $templates_without_main_tag[] = $file_name; + foreach ( $other_files as $php_key => $file ) { + //if the file is a template, print the name of the file + if ( strpos( $php_key, 'templates/' ) !== false ) { + + $file_name = tc_filename( $php_key ); + $has_main_tag = strpos( $file, 'template_has_patterns( $file ); + if ( $pattern_slugs ) { + foreach ( $pattern_slugs as $slug ) { + $has_main_tag = $this->pattern_has_tag( $slug ); + if ( ! $has_main_tag ) { + if ( ! in_array( $file_name, $templates_without_main_tag ) ) { + $templates_without_main_tag[] = $file_name; + } } } - } - } else { - if ( ! in_array( $file_name, $templates_without_main_tag ) ) { - $templates_without_main_tag[] = $file_name; + } else { + if ( ! in_array( $file_name, $templates_without_main_tag ) ) { + $templates_without_main_tag[] = $file_name; + } } } } From dabaf1d021cd4e44aebbe0deec35a5eda304ec6a Mon Sep 17 00:00:00 2001 From: MaggieCabrera Date: Thu, 31 Oct 2024 11:30:09 +0100 Subject: [PATCH 12/12] fix format --- checks/class-skip-links.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/checks/class-skip-links.php b/checks/class-skip-links.php index b75f042f..9aaa459b 100644 --- a/checks/class-skip-links.php +++ b/checks/class-skip-links.php @@ -50,10 +50,10 @@ public function check( $php_files, $css_files, $other_files ) { $templates_without_main_tag = array(); foreach ( $other_files as $php_key => $file ) { - //if the file is a template, print the name of the file + // if the file is a template, print the name of the file if ( strpos( $php_key, 'templates/' ) !== false ) { - $file_name = tc_filename( $php_key ); + $file_name = tc_filename( $php_key ); $has_main_tag = strpos( $file, '