[wp-trac] [WordPress Trac] #53290: Prevent tagging certain links in comments with rel="nofollow ugc"

WordPress Trac noreply at wordpress.org
Wed Feb 1 04:29:42 UTC 2023


#53290: Prevent tagging certain links in comments with rel="nofollow ugc"
--------------------------+------------------------------
 Reporter:  galbaras      |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  Comments      |     Version:  5.7.2
 Severity:  normal        |  Resolution:
 Keywords:                |     Focuses:
--------------------------+------------------------------

Comment (by galbaras):

 I think your approach should work well.

 One point of efficiency may be to use a static variable for the internal
 hosts list, rather than applying a filter to a function within a function
 every time `wp_internal_hosts()` is called.

 Optimising even further, the code from `wp_internal_hosts()` can be used
 directly in `wp_is_internal_link()` to set a static `$internal_hosts`,
 instead of applying the `array_map()` on every call.

 So something like this:

 {{{#!php
 <?php
 function wp_is_internal_link( $link ) {
         static $internal_hosts;

         $link = strtolower( $link );
         if ( in_array( wp_parse_url( $link, PHP_URL_SCHEME ),
 wp_allowed_protocols(), true ) ) {
                 if ( empty( $internal_hosts ) {
                         $internal_hosts = array_map( 'strtolower',
 apply_filters( 'wp_internal_hosts', array( wp_parse_url( home_url(),
 PHP_URL_HOST ) ) ) );
                 }
                 return in_array( wp_parse_url( $link, PHP_URL_HOST ),
 $internal_hosts, true );
         }

         return false;

 }
 }}}

 Looking at `wp_allowed_protocols()`, that's exactly how protocols are
 stored, so this should be consistent, as well as efficient.

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


More information about the wp-trac mailing list