Changeset 42770 for trunk/src/wp-includes/formatting.php
- Timestamp:
- 03/02/2018 02:41:04 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/formatting.php
r42610 r42770 3025 3025 3026 3026 /** 3027 * Adds rel noreferrer and noopener to all HTML A elements that have a target. 3028 * 3029 * @param string $text Content that may contain HTML A elements. 3030 * @return string Converted content. 3031 */ 3032 function wp_targeted_link_rel( $text ) { 3033 // Don't run (more expensive) regex if no links with targets. 3034 if ( stripos( $text, 'target' ) !== false && stripos( $text, '<a ' ) !== false ) { 3035 $text = preg_replace_callback( '|<a\s([^>]*target\s*=[^>]*)>|i', 'wp_targeted_link_rel_callback', $text ); 3036 } 3037 3038 return $text; 3039 } 3040 3041 /** 3042 * Callback to add rel="noreferrer noopener" string to HTML A element. 3043 * 3044 * Will not duplicate existing noreferrer and noopener values 3045 * to prevent from invalidating the HTML. 3046 * 3047 * @param array $matches Single Match 3048 * @return string HTML A Element with rel noreferrer noopener in addition to any existing values 3049 */ 3050 function wp_targeted_link_rel_callback( $matches ) { 3051 $link_html = $matches[1]; 3052 $rel_match = array(); 3053 3054 /** 3055 * Filters the rel values that are added to links with `target` attribute. 3056 * 3057 * @since 5.0.0 3058 * 3059 * @param string The rel values. 3060 * @param string $link_html The matched content of the link tag including all HTML attributes. 3061 */ 3062 $rel = apply_filters( 'wp_targeted_link_rel', 'noopener noreferrer', $link_html ); 3063 3064 // Value with delimiters, spaces around are optional. 3065 $attr_regex = '|rel\s*=\s*?(\\\\{0,1}["\'])(.*?)\\1|i'; 3066 preg_match( $attr_regex, $link_html, $rel_match ); 3067 3068 if ( empty( $rel_match[0] ) ) { 3069 // No delimiters, try with a single value and spaces, because `rel = va"lue` is totally fine... 3070 $attr_regex = '|rel\s*=(\s*)([^\s]*)|i'; 3071 preg_match( $attr_regex, $link_html, $rel_match ); 3072 } 3073 3074 if ( ! empty( $rel_match[0] ) ) { 3075 $parts = preg_split( '|\s+|', strtolower( $rel_match[2] ) ); 3076 $parts = array_map( 'esc_attr', $parts ); 3077 $needed = explode( ' ', $rel ); 3078 $parts = array_unique( array_merge( $parts, $needed ) ); 3079 $delimiter = trim( $rel_match[1] ) ? $rel_match[1] : '"'; 3080 $rel = 'rel=' . $delimiter . trim( implode( ' ', $parts ) ) . $delimiter; 3081 $link_html = str_replace( $rel_match[0], $rel, $link_html ); 3082 } else { 3083 $link_html .= " rel=\"$rel\""; 3084 } 3085 3086 return "<a $link_html>"; 3087 } 3088 3089 /** 3027 3090 * Convert one smiley code to the icon graphic file equivalent. 3028 3091 *
Note: See TracChangeset
for help on using the changeset viewer.