WordPress / WordPress/WordPress-Coding-Standards
New sniff "NamingConventions.ValidNavMenuSlug"
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 2.8k
- Forks
- 521
- Avg merge
- 5d 20h
- Merged PRs (30d)
- 1
Description
Is your feature request related to a problem?
As of WP 5.3, integer menu location slugs will no longer be supported.
Refs:
- https://make.wordpress.org/core/2019/09/18/integer-menu-slugs-are-no-longer-supported-from-wordpress-5-3/
- https://core.trac.wordpress.org/ticket/45361
- https://developer.wordpress.org/reference/functions/register_nav_menus/
- https://developer.wordpress.org/reference/functions/register_nav_menu/
Describe the solution you'd like
I'd like to suggest we add a sniff to the NamingConventions category to check code for this.
While this sniff could be a candidate for the Core ruleset, at the very least, it should go into the Extra ruleset (and the WPThemeReview ruleset /cc @dingo-d ).
// OK.
register_nav_menus(
array(
'primary' => 'Primary',
'first' => 'First',
'se' . 'cond' => 'Second',
)
);
// Ignore.
register_nav_menus( $locations );
register_nav_menus( $obj->get_locations() );
register_nav_menus(
array(
MENU_ONE => 'First',
$second => 'Second',
)
);
// Bad.
register_nav_menus(
array(
1 => 'First',
'Second',
2.5 => 'Third',
false => 'Fourth',
)
);
To find inspiration for additional unit tests: https://wpdirectory.net/search/01DN3RPKFHVQ712NG72GP99D5D
Additional context (optional)
Considerations to keep in mind when writing the sniff:
- If
$locationsis passed as a variable, constant or via a function call, the sniff should just bow out as it will be very difficult to figure out the value in a reliable manner. - The sniff should check for both array values without key, as well as array values with a non-string key.
Keys set via a variable or constant should be ignored. - The
register_nav_menu()function should be checked as well, as, while the$locationparameter is documented as astring, the function does no variable type checking.
To be researched
Are there any other restrictions to menu slugs ? And if so, should those be covered in the same sniff ?
Think along the lines of:
- Names which shouldn't be used (reserved).
- Any restrictions to the slug name ? allowed characters, slug length, etc
Input welcome!
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the register_nav_menus() and register_nav_menu() examples and the linked WordPress references, then review the proposed NamingConventions category and Extra, Core, and WPThemeReview rulesets. Define the supported menu-slug restrictions and test cases for literal arrays, before considering implementation complete.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 28/100