WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 7 months ago

#19968 closed enhancement (wontfix)

Use named regular expression groups to simplify shortcodes code

Reported by: nbachiyski Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.3
Component: Shortcodes Keywords: has-patch needs-refresh
Focuses: Cc:

Description (last modified by SergeyBiryukov)

Currently in get_shortcode_regex() in trunk/src/wp-includes/shortcodes.php you are welcomed by:

// WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcode_tag()

because these functions use the regular expression match from the shortcode parser.

Instead, we could use named regex groups: (?P<year>\d\d\d\d) or (?'year')\d\d\d\d which appear indexed by name in the matches array: $matches['year'] instead of matches[1].

Benefits:

  1. Future-proof code. Won't rely on indices, but on names, which we can control. Now, if we want to add a group we will have to offset all the indices, scattered all over the place.
  1. Less ugly and unreadable code like:
if (!empty($m[1]))
    $atts[strtolower($m[1])] = stripcslashes($m[2]);
elseif (!empty($m[3]))
    $atts[strtolower($m[3])] = stripcslashes($m[4]);

Attachments (1)

19968.diff (3.3 KB) - added by GaryJ 4 years ago.
Named groups defined for get_shortcode_regex(), used in do_shortcode_tags() and strip_shortcode_tag()

Download all attachments as: .zip

Change History (8)

#1 @scribu
4 years ago

  • Cc scribu added

@GaryJ
4 years ago

Named groups defined for get_shortcode_regex(), used in do_shortcode_tags() and strip_shortcode_tag()

#2 @GaryJ
4 years ago

19968.diff adds named groups for one of the shortcodes functions that defines a regex, but not the one that parses the attributes.

I've not named the group labelled as 4. as that didn't seem to currently be in use anywhere. If it is, then I'd suggest a name of self-closing.

All untested.

#3 @c3mdigital
3 years ago

  • Keywords has-patch needs-refresh added; needs-patch removed

#4 @SergeyBiryukov
3 years ago

  • Description modified (diff)

#5 @chriscct7
10 months ago

  • Severity changed from minor to normal
  • Version changed from 3.3.1 to 3.3

#6 @miqrogroove
9 months ago

  • Keywords close added

According to http://www.regular-expressions.info/refext.html the proposed enhancement would work in PHP, but would be incompatible with Javascript parsing. Participants at the October 14 roadmap meeting recommended closing this ticket as "maybelater". Please add any final comments now.

#7 @swissspidy
7 months ago

  • Keywords close removed
  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.