[wp-trac] [WordPress Trac] #50568: Improve WP_Term's sanitization calls

WordPress Trac noreply at wordpress.org
Tue Mar 12 17:49:50 UTC 2024


#50568: Improve WP_Term's sanitization calls
----------------------------------------------+----------------------------
 Reporter:  Cybr                              |       Owner:  (none)
     Type:  defect (bug)                      |      Status:  new
 Priority:  normal                            |   Milestone:  Future
                                              |  Release
Component:  Taxonomy                          |     Version:  5.5
 Severity:  normal                            |  Resolution:
 Keywords:  has-patch needs-unit-tests early  |     Focuses:  performance
----------------------------------------------+----------------------------

Comment (by Cybr):

 Here's a snippet of a bypass I've been using for the past 4 years.
 I put in a collective fix-up plugin called "WP Fix: Unified Core Kit,"
 which I might publish here one day.

 {{{#!php
 add_filter(
         'get_term',
         /**
          * Properly primes term cache. Otherwise, terms get resanitized
 every time they're called.
          *
          * @since 1.0.0
          * @link <https://core.trac.wordpress.org/ticket/50568>
          *
          * @param WP_Term $term     Term object.
          * @param string  $taxonomy The taxonomy slug.
          */
         function( $term, $taxonomy ) {

                 $_term = wp_cache_get( $term->term_id, 'terms' );

                 if ( $_term && empty( $_term->filter ) ) {
                         $term = sanitize_term( $term, $taxonomy, 'raw' );
                         wp_cache_replace( $term->term_id, $term, 'terms'
 );
                 }

                 return $term;
         },
         -100, // Low, for others might add data to the term object, which
 we don't want to filter out...
         2
 );
 }}}

 Saves 2 redundant database requests on my tiny homepage (running WP
 6.4.3).

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/50568#comment:10>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list