[wp-trac] [WordPress Trac] #50245: extend functionality to stop further actions on hook on a priority range

WordPress Trac noreply at wordpress.org
Mon May 25 12:08:59 UTC 2020


#50245: extend functionality to stop further actions on hook on a priority range
-------------------------------------------+-----------------------------
 Reporter:  Alignak                        |      Owner:  (none)
     Type:  enhancement                    |     Status:  assigned
 Priority:  normal                         |  Milestone:  Awaiting Review
Component:  Cache API                      |    Version:
 Severity:  normal                         |   Keywords:  dev-feedback
  Focuses:  performance, coding-standards  |
-------------------------------------------+-----------------------------
 I would like to be possible to stop further actions on a hook or filter,
 based on a priority range (or prevent further processing by preventing
 other actions from being added on upper priorities).

 For example,

 I would like to hook into the_content on priority -10 and prevent any
 other action / filter from being applied until priority 10.

 This would allow me for example, to cache a hook.

 Real case scenario:

 A site uses a theme builder that generates thousands of shortcodes. For
 every pageview that cannot be cached, it has to run the do_shortcode
 sometimes thousands of times.

 If I were to implement a transient on the_content using the max priority
 possible, I could then use the lowest priority possible next to check if a
 transient exists, and "prevent further processing on the_content" by
 returning the content that I cached earlier.

 Currently, all I could do would be to save the transient as a global,
 leave the_content empty (which doesn't avoid any other filters on the
 empty string), and then print it at the last priority.

 Or what makes sense, create a the_content alternative function that does
 this (what I am doing in a couple of projects where the_content takes
 almost 3 seconds to generate code from the Divi theme).

 So as a developer, a way to skip any further processing (either on a
 priority range or completely), would make performance tweaks much easier,
 instead of having to write new functions just for that.

 I am aware of the remove_all_filters action, which is great, but
 unfortunately, it doesn't work when called from inside itself.

 I cannot do for example:

 {{{#!php
 <?php
 add_filter( 'the_content', 'my_filter_start', PHP_INT_MIN);
 function my_filter_start( $content ) {

         $prefix_tkey = md5($content);
         global $prefix_tkey;
         $value = get_transient($prefix_tkey);
         if ( false !== $value ) {

                 # unfortunately, this doesn't work (feature request)
                 # and even if it did, I would like to limit it up to a
 certain priority level, or whitelist certain actions
                 remove_all_filters('the_content');

                 # return transient code, which will be cached on
 PHP_INT_MAX priority
                 return $value;

         }

     return $content;
 }

 add_filter( 'the_content', 'my_filter_end', PHP_INT_MAX);
 function my_filter_end( $content ) {

         global $prefix_tkey;

         # cache html if we are here
         if(isset($prefix_tkey) && strlen($prefix_tkey) == 32) {
                 set_transient( $prefix_tkey, $content, 24 *
 HOUR_IN_SECONDS );
         }

     return $content;
 }
 }}}

 I'm filling this under the cache api, because the main purpose for this is
 cache.
 I think it could be implemented on wordpress side, with an option to cache
 the_content, wp_head, and maybe menus.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/50245>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list