WordPress.org

Make WordPress Core

#38766 closed defect (bug) (fixed)

Post type templates ignore the last post type if there is a trailing full stop.

Reported by: peterwilsoncc Owned by: peterwilsoncc
Milestone: 4.7 Priority: normal
Severity: normal Version: 4.7
Component: Posts, Post Types Keywords: has-patch has-unit-tests commit
Focuses: template Cc:

Description

Trailing full stops can cause the template post type comment to ignore the final post type.

// Works
Template post type: page, post

// Fails
Template post type: page, post.

Incoming patch modifies the regular expression to exclude any trailing full stops and trailing whitespace.

Attachments (3)

38766.diff (579 bytes) - added by peterwilsoncc 13 months ago.
38766.2.diff (2.6 KB) - added by peterwilsoncc 13 months ago.
38766.3.diff (4.7 KB) - added by peterwilsoncc 13 months ago.

Download all attachments as: .zip

Change History (11)

#1 @peterwilsoncc
13 months ago

  • Keywords has-unit-tests added

#2 @peterwilsoncc
13 months ago

  • Keywords needs-unit-tests added; has-unit-tests removed

Missed it by this much... :0

#3 @peterwilsoncc
13 months ago

  • Keywords has-unit-tests 2nd-opinion added; needs-unit-tests removed

Unit tests added in 38766.2.diff.

After a second opinion, is regex's u modifier bad for performance?

#4 @pento
13 months ago

  • Keywords needs-refresh needs-unit-tests added; has-unit-tests 2nd-opinion removed

Some things:

  • If we're going to change how this works, I think the correct way would be to use sanitize_key() on the post type, the same as register_post_type() does. So, instead of $type = trim( $type );, have $type = sanitize_key( $type );. That removes the need for changing the regex.
  • The tests don't test for trailing spaces. The tests could probably be expanded to test for other invalid characters.
  • The U modifier (invert greediness) is not the same as the u modifier (UTF-8 compatibility). The former is incorrect, and we don't need to use the latter, as only a subset of ASCII characters are allowed in post types.

#5 @peterwilsoncc
13 months ago

  • Keywords has-unit-tests added; needs-refresh needs-unit-tests removed

@pento thanks for the regex explanation.

In 38766.3.diff:

  • additional tests for trailing punctuation and white space.
  • switch to sanitize_key()
  • test to ensure invalid character ignored.

#6 @pento
13 months ago

  • Keywords commit added
  • Owner set to peterwilsoncc
  • Status changed from new to assigned

:thumbs-up:

This ticket was mentioned in Slack in #core by peterwilsoncc. View the logs.


13 months ago

#8 @peterwilsoncc
13 months ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 39236:

Posts, Post Types: Improve sanitisation of templates' post types.

Prevents post type templates ignoring post types due to invalid characters. Each entry in the Template Post Type comment is run through sanitize_key() to match the sanitisation used by register_post_type().

Fixes #38766.

Note: See TracTickets for help on using tickets.