WordPress / WordPress/WordPress-Coding-Standards

New sniff "NamingConventions.ValidNavMenuSlug"

Open
#1,798 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Focus: WP interoperability Status: Needs investigation Type: Enhancement
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:

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 $locations is 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 $location parameter is documented as a string, 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.