diff --git a/WordPress/Sniffs/Theme/NoFaviconSniff.php b/WordPress/Sniffs/Theme/NoFaviconSniff.php new file mode 100644 index 00000000..b8975fb8 --- /dev/null +++ b/WordPress/Sniffs/Theme/NoFaviconSniff.php @@ -0,0 +1,97 @@ + array( + 'icon', + 'shortcut icon', + 'bookmark icon', + 'apple-touch-icon', + 'apple-touch-icon-precomposed', + ), + 'name' => array( + 'msapplication-config', + 'msapplication-TileImage', + 'msapplication-square70x70logo', + 'msapplication-square150x150logo', + 'msapplication-wide310x150logo', + 'msapplication-square310x310logo', + ), + ); + + /** + * The regex to catch the blacklisted attributes. + * + * @var string + */ + protected $favicon_regex; + + /** + * Returns an array of tokens this test wants to listen for. + * + * @return array + */ + public function register() { + // Build the regex to be used only once. + $regex_parts = array(); + + foreach ( $this->attribute_blacklist as $key => $values ) { + $values = array_map( 'preg_quote', $values, array_fill( 0, count( $values ), '`' ) ); + $values = implode( '|', $values ); + $regex_parts[] = sprintf( self::REGEX_ATTR_TEMPLATE, preg_quote( $key ), $values ); + } + + $this->favicon_regex = sprintf( self::REGEX_TEMPLATE, implode( '|', $regex_parts ) ); + + $tokens = PHP_CodeSniffer_Tokens::$stringTokens; + $tokens[] = T_INLINE_HTML; + + return $tokens; + } + + /** + * Processes this test, when one of its tokens is encountered. + * + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token + * in the stack passed in $tokens. + * + * @return void + */ + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { + $tokens = $phpcsFile->getTokens(); + $token = $tokens[ $stackPtr ]; + + if ( preg_match( $this->favicon_regex, $token['content'] ) > 0 ) { + $phpcsFile->addError( 'Code for favicon found. Favicons are handled by the "Site Icon" setting in the customizer since version 4.3.' , $stackPtr, 'NoFavicon' ); + } + + } + +} diff --git a/WordPress/Tests/Theme/NoFaviconUnitTest.inc b/WordPress/Tests/Theme/NoFaviconUnitTest.inc new file mode 100644 index 00000000..760211fd --- /dev/null +++ b/WordPress/Tests/Theme/NoFaviconUnitTest.inc @@ -0,0 +1,21 @@ + + class="no-js"> // OK. + // OK. + // OK. + // OK. + + + // Error. + // Error. + // Error. + // Error. + // Error. + + + + + + +'; diff --git a/WordPress/Tests/Theme/NoFaviconUnitTest.php b/WordPress/Tests/Theme/NoFaviconUnitTest.php new file mode 100644 index 00000000..7222c16f --- /dev/null +++ b/WordPress/Tests/Theme/NoFaviconUnitTest.php @@ -0,0 +1,48 @@ + => + */ + public function getErrorList() { + return array( + 8 => 1, + 9 => 1, + 10 => 1, + 11 => 1, + 12 => 1, + 16 => 1, + 17 => 1, + 18 => 1, + 19 => 1, + 20 => 1, + 21 => 1, + ); + } + + /** + * Returns the lines where warnings should occur. + * + * @return array => + */ + public function getWarningList() { + return array(); + } + +} // End class.