Skip to content

Commit

Permalink
Use name instead of keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
IanDelMar committed Sep 10, 2024
1 parent 5ec64ac commit 4e6f7f7
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 43 deletions.
2 changes: 1 addition & 1 deletion WordPress-Extra/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
<!-- Verify that everything in the global namespace is prefixed. -->
<rule ref="WordPress.NamingConventions.PrefixAllGlobals"/>

<!-- Validates post type slugs for valid characters, length and reserved keywords. -->
<!-- Validates post type slugs for valid characters, length and reserved names. -->
<rule ref="WordPress.NamingConventions.ValidPostTypeSlug"/>

<!-- Validates taxonomy slugs for valid characters, length and reserved names. -->
Expand Down
2 changes: 1 addition & 1 deletion WordPress/AbstractValidSlugSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* Validates names passed to a function call.
*
* Checks slugs for the presence of invalid characters, excessive length,
* and reserved keywords.
* and reserved names.
*/
abstract class AbstractValidSlugSniff extends AbstractFunctionParameterSniff {

Expand Down
64 changes: 32 additions & 32 deletions WordPress/Docs/NamingConventions/ValidPostTypeSlugStandard.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
<code_comparison>
<code title="Valid: Short post type slug.">
<![CDATA[
register_post_type(
<em>'my_short_slug'</em>,
array()
register_post_type(
<em>'my_short_slug'</em>,
array()
);
]]>
</code>
<code title="Invalid: Too long post type slug.">
<![CDATA[
register_post_type(
<em>'my_own_post_type_too_long'</em>,
array()
register_post_type(
<em>'my_own_post_type_too_long'</em>,
array()
);
]]>
</code>
Expand All @@ -34,17 +34,17 @@ register_post_type(
<code_comparison>
<code title="Valid: No special characters in post type slug.">
<![CDATA[
register_post_type(
<em>'my_post_type_slug'</em>,
array()
register_post_type(
<em>'my_post_type_slug'</em>,
array()
);
]]>
</code>
<code title="Invalid: Invalid characters in post type slug.">
<![CDATA[
register_post_type(
<em>'my/post/type/slug'</em>,
array()
register_post_type(
<em>'my/post/type/slug'</em>,
array()
);
]]>
</code>
Expand All @@ -57,40 +57,40 @@ register_post_type(
<code_comparison>
<code title="Valid: Static post type slug.">
<![CDATA[
register_post_type(
<em>'my_post_active'</em>,
array()
register_post_type(
<em>'my_post_active'</em>,
array()
);
]]>
</code>
<code title="Invalid: Dynamic post type slug.">
<![CDATA[
register_post_type(
<em>"my_post_{$status}"</em>,
array()
register_post_type(
<em>"my_post_{$status}"</em>,
array()
);
]]>
</code>
</code_comparison>
<standard>
<![CDATA[
The post type slug used in register_post_type() can not use reserved keywords, such as the ones used by WordPress itself.
The post type slug used in register_post_type() can not use reserved names, such as the ones used by WordPress itself.
]]>
</standard>
<code_comparison>
<code title="Valid: Prefixed post slug.">
<![CDATA[
register_post_type(
<em>'prefixed_author'</em>,
array()
register_post_type(
<em>'prefixed_author'</em>,
array()
);
]]>
</code>
<code title="Invalid: Using a reserved keyword as slug.">
<code title="Invalid: Using a reserved name as slug.">
<![CDATA[
register_post_type(
<em>'author'</em>,
array()
register_post_type(
<em>'author'</em>,
array()
);
]]>
</code>
Expand All @@ -103,17 +103,17 @@ register_post_type(
<code_comparison>
<code title="Valid: Custom prefix post slug.">
<![CDATA[
register_post_type(
<em>'prefixed_author'</em>,
array()
register_post_type(
<em>'prefixed_author'</em>,
array()
);
]]>
</code>
<code title="Invalid: Using a reserved prefix.">
<![CDATA[
register_post_type(
<em>'wp_author'</em>,
array()
register_post_type(
<em>'wp_author'</em>,
array()
);
]]>
</code>
Expand Down
12 changes: 5 additions & 7 deletions WordPress/Helpers/WPReservedNamesHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace WordPressCS\WordPress\Helpers;

/**
* Helper utilities for recognizing WP reserved keywords.
* Helper utilities for recognizing WP reserved names.
*/
final class WPReservedNamesHelper {
/**
Expand Down Expand Up @@ -143,26 +143,24 @@ final class WPReservedNamesHelper {
);

/**
* Verify if a given keyword is a reserved post type name.
* Verify if a given name is a reserved post type name.
*
* @param string $name The keyword to be checked.
* @param string $name The name to be checked.
*
* @return bool
*/
public static function is_reserved_post_type( $name ) {

return isset( self::$post_types[ $name ] );
}

/**
* Verify if a given keyword is a reserved taxonomy name.
* Verify if a given name is a reserved taxonomy name.
*
* @param string $name The keyword to be checked.
* @param string $name The name to be checked.
*
* @return bool
*/
public static function is_reserved_term( $name ) {

return isset( self::$terms[ $name ] )
|| isset( self::$post_types[ $name ] );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ EOD
register_post_type( "my-own-post-type-too-long-{$i}" ); // 1x Error, Too long. 1x Warning, post type may or may not get too long with dynamic contents in the id.
register_post_type( 'my/own/post/type/too/long', array() ); // Bad. Invalid chars: "/" and too long.

register_post_type( 'wp_block', array() ); // Bad. Must only error on reserved keyword, not invalid prefix.
register_post_type( 'wp_block', array() ); // Bad. Must only error on reserved name, not invalid prefix.

// Test handling of more complex embedded variables and expressions.
register_post_type("testing123-${(foo)}-test");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ EOD
register_taxonomy( "my-own-taxonomy-toooooooooo-long-{$i}" ); // 1x Error, Too long. 1x Warning, post type may or may not get too long with dynamic contents in the id.
register_taxonomy( 'my/own/taxonomy/tooooooooooo/long', array() ); // Bad. Invalid chars: "/" and too long.

register_taxonomy( 'wp_block', array() ); // Bad. Must only error on reserved keyword, not invalid prefix.
register_taxonomy( 'wp_block', array() ); // Bad. Must only error on reserved name, not invalid prefix.

// Test handling of more complex embedded variables and expressions.
register_taxonomy("testing123-${(foo)}-test");
Expand Down

0 comments on commit 4e6f7f7

Please sign in to comment.