WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 21 months ago

#19968 new enhancement

Use named regular expression groups to simplify shortcodes code

Reported by: nbachiyski Owned by:
Milestone: Awaiting Review Priority: normal
Severity: minor Version: 3.3.1
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 3 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 (5)

comment:1 @scribu3 years ago

  • Cc scribu added

@GaryJ3 years ago

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

comment:2 @GaryJ3 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.

comment:3 @c3mdigital21 months ago

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

comment:4 @SergeyBiryukov21 months ago

  • Description modified (diff)
Note: See TracTickets for help on using tickets.