WordPress.org

Make WordPress Core

Opened 14 months ago

Closed 14 months ago

Last modified 14 months ago

#37906 closed enhancement (fixed)

Filter to short-circuit do_shortcode_tag

Reported by: ideag Owned by: ideag
Milestone: 4.7 Priority: normal
Severity: normal Version:
Component: Shortcodes Keywords: has-patch
Focuses: Cc:

Description

Working for a client I noticed that there is no way for plugins to modify or short-circuit shortcode output.

My use case - I wanted to fragment-cache some resource-intensive shortcodes from other plugins. In my case I solved it by introducing new shortcode and calling do_shortcode from inside, but I think a general filter for this would be good to have. We do have similar pre_ type of filters for http requests, get_option and elswhere, so introducing one for shortcodes would be good for consistency, to.

My suggestion is to introduce a pre_do_shortcode_tag filter that would allow plugins to short-circuit and return their own value for any shortcode they choose. I am attaching a possible patch for this.

Attachments (3)

pre_do_shortcode_tag.diff (1006 bytes) - added by ideag 14 months ago.
a patch to introduce pre_do_shortcode_tag filter
pre_do_shortcode_tag_tests.diff (1.6 KB) - added by ideag 14 months ago.
unit tests for pre_do_shortcode_tag filter
37906_pre_do_shortcode_tag_v2.diff (3.3 KB) - added by ideag 14 months ago.

Download all attachments as: .zip

Change History (12)

@ideag
14 months ago

a patch to introduce pre_do_shortcode_tag filter

#1 follow-ups: @pento
14 months ago

  • Keywords has-patch needs-unit-tests added
  • Milestone changed from Awaiting Review to 4.7
  • Version trunk deleted

Thanks for the suggestion, @ideag!

I like it! If you have some time, would you mind writing some unit tests for this new filter?

#2 in reply to: ↑ 1 @ideag
14 months ago

Replying to pento:

If you have some time, would you mind writing some unit tests for this new filter?

I have the time, but I have no experience writing unit tests. I guess this could be a good chance to learn that :)

#3 @ideag
14 months ago

I think #32790 might be related to this too, somehow I didn't see it in my search before.

@ideag
14 months ago

unit tests for pre_do_shortcode_tag filter

#4 in reply to: ↑ 1 @ideag
14 months ago

Replying to pento

I've added some unit tests for this patch. Please keep in mind this is my first time writing unit tests, so I might be doing things wrong.

#5 @pento
14 months ago

  • Keywords needs-refresh added; needs-unit-tests removed
  • Owner set to ideag
  • Status changed from new to assigned

Nice work, @ideag!

There are a handful of small changes I'd like you to make.

  • Please put the tests in tests/shortcode.php, not tests/filters.php. filters.php is just for testing the filter handling methods, testing an actual filter belongs in the file related to the area of code where the filter runs.
  • Don't use rand_str() to generate the shortcode. There's a chance (however small), that it could collide with an existing shortcode, unit tests should be as deterministic as possible. Naming the shortcode something related to the new filter will be unique, in the context of the tests.
  • We still support PHP 5.2, so the shortcode lambda functions actually need to be methods. The general naming convention for filter helper methods is to prefix them with _filter_, like test methods are prefixed test_.
  • Clean up at the end of the test - remove the filters and shortcode. We try to keep unit tests as self-contained as possible, to avoid side effects on tests that run later.
  • Please put both the tests and the new filter in the same patch, it makes them easier for folks to apply to their dev installs.

All up, it's looking quite good, thank you for taking the time to get it done! :-)

#6 @ideag
14 months ago

Thanks for feedback - I'll clean it up and have it ready by tomorrow.

#7 @ideag
14 months ago

Modified unit tests as per @pento recommendations and packed everything into a single patch.

#8 @pento
14 months ago

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

In 38506:

Shortcodes: Add the pre_do_shortcode_tag filter.

This filter allows the shortcode generation process to be short-circuited, so expensive short codes can be cached and returned immediately.

Props ideag.
Fixes #37906.

#9 @pento
14 months ago

  • Keywords needs-refresh removed

Thank you very much for your work, @ideag!

I made some minor naming and formatting adjustments to the tests, but otherwise, it all looked good!

Note: See TracTickets for help on using tickets.