|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * WordPress Coding Standard. |
| 4 | + * |
| 5 | + * @package WPCS\WordPressCodingStandards |
| 6 | + * @link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards |
| 7 | + * @license https://opensource.org/licenses/MIT MIT |
| 8 | + */ |
| 9 | + |
| 10 | +namespace WordPressCS\WordPress\Sniffs\NamingConventions; |
| 11 | + |
| 12 | +use WordPressCS\WordPress\AbstractFunctionParameterSniff; |
| 13 | +use PHP_CodeSniffer\Util\Tokens; |
| 14 | + |
| 15 | +/** |
| 16 | + * Validates post type names. |
| 17 | + * |
| 18 | + * Checks the post type slug for invalid characters, long function names |
| 19 | + * and reserved names. |
| 20 | + * |
| 21 | + * @link https://developer.wordpress.org/reference/functions/register_post_type/ |
| 22 | + * |
| 23 | + * @package WPCS\WordPressCodingStandards |
| 24 | + * |
| 25 | + * @since 2.2.0 |
| 26 | + */ |
| 27 | +class ValidPostTypeSlugSniff extends AbstractFunctionParameterSniff { |
| 28 | + |
| 29 | + /** |
| 30 | + * Max length of a post type name is limited by the SQL field. |
| 31 | + * |
| 32 | + * @since 2.2.0 |
| 33 | + * |
| 34 | + * @var int |
| 35 | + */ |
| 36 | + const POST_TYPE_MAX_LENGTH = 20; |
| 37 | + |
| 38 | + /** |
| 39 | + * Regex that whitelists characters that can be used as the post type slug. |
| 40 | + * |
| 41 | + * @link https://developer.wordpress.org/reference/functions/register_post_type/ |
| 42 | + * @since 2.2.0 |
| 43 | + * |
| 44 | + * @var string |
| 45 | + */ |
| 46 | + const POST_TYPE_CHARACTER_WHITELIST = '/^[a-z0-9_-]+$/'; |
| 47 | + |
| 48 | + /** |
| 49 | + * Array of functions that must be checked. |
| 50 | + * |
| 51 | + * @since 2.2.0 |
| 52 | + * |
| 53 | + * @var array List of function names as keys. Value irrelevant. |
| 54 | + */ |
| 55 | + protected $target_functions = array( |
| 56 | + 'register_post_type' => true, |
| 57 | + ); |
| 58 | + |
| 59 | + /** |
| 60 | + * Array of reserved post type names which can not be used by themes and plugins. |
| 61 | + * |
| 62 | + * @since 2.2.0 |
| 63 | + * |
| 64 | + * @var array |
| 65 | + */ |
| 66 | + protected $reserved_names = array( |
| 67 | + 'post' => true, |
| 68 | + 'page' => true, |
| 69 | + 'attachment' => true, |
| 70 | + 'revision' => true, |
| 71 | + 'nav_menu_item' => true, |
| 72 | + 'custom_css' => true, |
| 73 | + 'customize_changeset' => true, |
| 74 | + 'oembed_cache' => true, |
| 75 | + 'user_request' => true, |
| 76 | + 'wp_block' => true, |
| 77 | + 'action' => true, |
| 78 | + 'author' => true, |
| 79 | + 'order' => true, |
| 80 | + 'theme' => true, |
| 81 | + ); |
| 82 | + |
| 83 | + /** |
| 84 | + * All valid tokens for in the first parameter of register_post_type(). |
| 85 | + * |
| 86 | + * Set in `register()`. |
| 87 | + * |
| 88 | + * @since 2.2.0 |
| 89 | + * |
| 90 | + * @var string |
| 91 | + */ |
| 92 | + private $valid_tokens = array(); |
| 93 | + |
| 94 | + /** |
| 95 | + * Returns an array of tokens this test wants to listen for. |
| 96 | + * |
| 97 | + * @since 2.2.0 |
| 98 | + * |
| 99 | + * @return array |
| 100 | + */ |
| 101 | + public function register() { |
| 102 | + $this->valid_tokens = Tokens::$textStringTokens + Tokens::$heredocTokens + Tokens::$emptyTokens; |
| 103 | + return parent::register(); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Process the parameter of a matched function. |
| 108 | + * |
| 109 | + * Errors on invalid post type names when reserved keywords are used, |
| 110 | + * the post type is too long, or contains invalid characters. |
| 111 | + * |
| 112 | + * @since 2.2.0 |
| 113 | + * |
| 114 | + * @param int $stackPtr The position of the current token in the stack. |
| 115 | + * @param array $group_name The name of the group which was matched. |
| 116 | + * @param string $matched_content The token content (function name) which was matched. |
| 117 | + * @param array $parameters Array with information about the parameters. |
| 118 | + * |
| 119 | + * @return void |
| 120 | + */ |
| 121 | + public function process_parameters( $stackPtr, $group_name, $matched_content, $parameters ) { |
| 122 | + |
| 123 | + $string_pos = $this->phpcsFile->findNext( Tokens::$textStringTokens, $parameters[1]['start'], ( $parameters[1]['end'] + 1 ) ); |
| 124 | + $has_invalid_tokens = $this->phpcsFile->findNext( $this->valid_tokens, $parameters[1]['start'], ( $parameters[1]['end'] + 1 ), true ); |
| 125 | + if ( false !== $has_invalid_tokens || false === $string_pos ) { |
| 126 | + // Check for non string based slug parameter (we cannot determine if this is valid). |
| 127 | + $this->phpcsFile->addWarning( |
| 128 | + 'The post type slug is not a string literal. It is not possible to automatically determine the validity of this slug. Found: %s.', |
| 129 | + $stackPtr, |
| 130 | + 'NotStringLiteral', |
| 131 | + array( |
| 132 | + $parameters[1]['raw'], |
| 133 | + ), |
| 134 | + 3 |
| 135 | + ); |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + $post_type = $this->strip_quotes( $this->tokens[ $string_pos ]['content'] ); |
| 140 | + |
| 141 | + if ( strlen( $post_type ) === 0 ) { |
| 142 | + // Error for using empty slug. |
| 143 | + $this->phpcsFile->addError( |
| 144 | + 'register_post_type() called without a post type slug. The slug must be a non-empty string.', |
| 145 | + $parameters[1]['start'], |
| 146 | + 'Empty' |
| 147 | + ); |
| 148 | + return; |
| 149 | + } |
| 150 | + |
| 151 | + $data = array( |
| 152 | + $this->tokens[ $string_pos ]['content'], |
| 153 | + ); |
| 154 | + |
| 155 | + // Warn for dynamic parts in the slug parameter. |
| 156 | + if ( 'T_DOUBLE_QUOTED_STRING' === $this->tokens[ $string_pos ]['type'] || ( 'T_HEREDOC' === $this->tokens[ $string_pos ]['type'] && strpos( $this->tokens[ $string_pos ]['content'], '$' ) !== false ) ) { |
| 157 | + $this->phpcsFile->addWarning( |
| 158 | + 'The post type slug may, or may not, get too long with dynamic contents and could contain invalid characters. Found: %s.', |
| 159 | + $string_pos, |
| 160 | + 'PartiallyDynamic', |
| 161 | + $data |
| 162 | + ); |
| 163 | + $post_type = $this->strip_interpolated_variables( $post_type ); |
| 164 | + } |
| 165 | + |
| 166 | + if ( preg_match( self::POST_TYPE_CHARACTER_WHITELIST, $post_type ) === 0 ) { |
| 167 | + // Error for invalid characters. |
| 168 | + $this->phpcsFile->addError( |
| 169 | + 'register_post_type() called with invalid post type %s. Post type contains invalid characters. Only lowercase alphanumeric characters, dashes, and underscores are allowed.', |
| 170 | + $string_pos, |
| 171 | + 'InvalidCharacters', |
| 172 | + $data |
| 173 | + ); |
| 174 | + } |
| 175 | + |
| 176 | + if ( isset( $this->reserved_names[ $post_type ] ) ) { |
| 177 | + // Error for using reserved slug names. |
| 178 | + $this->phpcsFile->addError( |
| 179 | + 'register_post_type() called with reserved post type %s. Reserved post types should not be used as they interfere with the functioning of WordPress itself.', |
| 180 | + $string_pos, |
| 181 | + 'Reserved', |
| 182 | + $data |
| 183 | + ); |
| 184 | + } elseif ( stripos( $post_type, 'wp_' ) === 0 ) { |
| 185 | + // Error for using reserved slug prefix. |
| 186 | + $this->phpcsFile->addError( |
| 187 | + 'The post type passed to register_post_type() uses a prefix reserved for WordPress itself. Found: %s.', |
| 188 | + $string_pos, |
| 189 | + 'ReservedPrefix', |
| 190 | + $data |
| 191 | + ); |
| 192 | + } |
| 193 | + |
| 194 | + // Error for slugs that are too long. |
| 195 | + if ( strlen( $post_type ) > self::POST_TYPE_MAX_LENGTH ) { |
| 196 | + $this->phpcsFile->addError( |
| 197 | + 'A post type slug must not exceed %d characters. Found: %s (%d characters).', |
| 198 | + $string_pos, |
| 199 | + 'TooLong', |
| 200 | + array( |
| 201 | + self::POST_TYPE_MAX_LENGTH, |
| 202 | + $this->tokens[ $string_pos ]['content'], |
| 203 | + strlen( $post_type ), |
| 204 | + ) |
| 205 | + ); |
| 206 | + } |
| 207 | + } |
| 208 | +} |
0 commit comments