diff --git a/checks/class-skip-links.php b/checks/class-skip-links.php new file mode 100644 index 00000000..9aaa459b --- /dev/null +++ b/checks/class-skip-links.php @@ -0,0 +1,152 @@ +wp_theme = $data['theme']; + $this->is_block_theme = wp_is_block_theme(); + } + } + + /** + * 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 = array(); + + 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; + } + } + } + } + } + + $info = implode( ', ', $templates_without_main_tag ); + + if ( $info !== '' ) { + $this->error[] = sprintf( + '%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' ), + $info + ) + ); + return false; + } + + 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, 'template_has_patterns( $contents ); + if ( $nested_patterns_slugs ) { + foreach ( $nested_patterns_slugs as $slug ) { + $has_tag = $this->pattern_has_tag( $slug ); + } + } + } + } + } + } + + return $has_tag; + } + + /** + * Get error messages from the checks. + * + * @return array Error message. + */ + public function getError() { + return $this->error; + } +} + +$themechecks[] = new Skip_Links_Check();