Ticket #43187: 42770.diff
File 42770.diff, 5.7 KB (added by , 6 years ago) |
---|
-
src/wp-includes/default-filters.php
diff --git src/wp-includes/default-filters.php src/wp-includes/default-filters.php index 96d17e6ed7..61ebf0cedc 100644
foreach ( array( 'content_save_pre', 'excerpt_save_pre', 'comment_save_pre', 'pr 118 118 add_filter( $filter, 'balanceTags', 50 ); 119 119 } 120 120 121 // Add proper rel values for links with target. 122 foreach ( array( 123 'title_save_pre', 124 'content_save_pre', 125 'excerpt_save_pre', 126 'content_filtered_save_pre', 127 'pre_comment_content', 128 'pre_term_description', 129 'pre_link_description', 130 'pre_link_notes', 131 'pre_user_description', 132 ) as $filter ) { 133 add_filter( $filter, 'wp_targeted_link_rel' ); 134 }; 135 121 136 // Format strings for display. 122 137 foreach ( array( 'comment_author', 'term_name', 'link_name', 'link_description', 'link_notes', 'bloginfo', 'wp_title', 'widget_title' ) as $filter ) { 123 138 add_filter( $filter, 'wptexturize' ); -
src/wp-includes/formatting.php
diff --git src/wp-includes/formatting.php src/wp-includes/formatting.php index 0e175f24e7..fa44d2d5e6 100644
function wp_rel_nofollow_callback( $matches ) { 2773 2773 return "<a $text rel=\"$rel\">"; 2774 2774 } 2775 2775 2776 /** 2777 * Adds rel noreferrer and noopener to all HTML A elements that have a target. 2778 * 2779 * @param string $text Content that may contain HTML A elements. 2780 * @return string Converted content. 2781 */ 2782 function wp_targeted_link_rel( $text ) { 2783 // Don't run (more expensive) regex if no links with targets. 2784 if ( stripos( $text, 'target' ) !== false && stripos( $text, '<a ' ) !== false ) { 2785 $text = preg_replace_callback( '|<a\s([^>]*target\s*=[^>]*)>|i', 'wp_targeted_link_rel_callback', $text ); 2786 } 2787 2788 return $text; 2789 } 2790 2791 /** 2792 * Callback to add rel="noreferrer noopener" string to HTML A element. 2793 * 2794 * Will not duplicate existing noreferrer and noopener values 2795 * to prevent from invalidating the HTML. 2796 * 2797 * @param array $matches Single Match 2798 * @return string HTML A Element with rel noreferrer noopener in addition to any existing values 2799 */ 2800 function wp_targeted_link_rel_callback( $matches ) { 2801 $link_html = $matches[1]; 2802 $rel_match = array(); 2803 2804 /** 2805 * Filters the rel values that are added to links with `target` attribute. 2806 * 2807 * @since 5.0.0 2808 * 2809 * @param string The rel values. 2810 * @param string $link_html The matched content of the link tag including all HTML attributes. 2811 */ 2812 $rel = apply_filters( 'wp_targeted_link_rel', 'noopener noreferrer', $link_html ); 2813 2814 // Value with delimiters, spaces around are optional. 2815 $attr_regex = '|rel\s*=\s*?(\\\\{0,1}["\'])(.*?)\\1|i'; 2816 preg_match( $attr_regex, $link_html, $rel_match ); 2817 2818 if ( empty( $rel_match[0] ) ) { 2819 // No delimiters, try with a single value and spaces, because `rel = va"lue` is totally fine... 2820 $attr_regex = '|rel\s*=(\s*)([^\s]*)|i'; 2821 preg_match( $attr_regex, $link_html, $rel_match ); 2822 } 2823 2824 if ( ! empty( $rel_match[0] ) ) { 2825 $parts = preg_split( '|\s+|', strtolower( $rel_match[2] ) ); 2826 $parts = array_map( 'esc_attr', $parts ); 2827 $needed = explode( ' ', $rel ); 2828 $parts = array_unique( array_merge( $parts, $needed ) ); 2829 $delimiter = trim( $rel_match[1] ) ? $rel_match[1] : '"'; 2830 $rel = 'rel=' . $delimiter . trim( implode( ' ', $parts ) ) . $delimiter; 2831 $link_html = str_replace( $rel_match[0], $rel, $link_html ); 2832 } else { 2833 $link_html .= " rel=\"$rel\""; 2834 } 2835 2836 return "<a $link_html>"; 2837 } 2838 2776 2839 /** 2777 2840 * Convert one smiley code to the icon graphic file equivalent. 2778 2841 * -
tests/phpunit/tests/rest-api/rest-attachments-controller.php
diff --git tests/phpunit/tests/rest-api/rest-attachments-controller.php tests/phpunit/tests/rest-api/rest-attachments-controller.php index c1ddc4ed68..fa22a4a39d 100644
class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control 939 939 'rendered' => '<a href="#">link</a>', 940 940 ), 941 941 'description' => array( 942 'raw' => '<a href="#" target="_blank" >link</a>',943 'rendered' => '<p><a href="#" target="_blank" >link</a></p>',942 'raw' => '<a href="#" target="_blank" rel="noopener noreferrer">link</a>', 943 'rendered' => '<p><a href="#" target="_blank" rel="noopener noreferrer">link</a></p>', 944 944 ), 945 945 'caption' => array( 946 'raw' => '<a href="#" target="_blank" >link</a>',947 'rendered' => '<p><a href="#" target="_blank" >link</a></p>',946 'raw' => '<a href="#" target="_blank" rel="noopener noreferrer">link</a>', 947 'rendered' => '<p><a href="#" target="_blank" rel="noopener noreferrer">link</a></p>', 948 948 ), 949 949 ) 950 950 ), -
tests/phpunit/tests/rest-api/rest-posts-controller.php
diff --git tests/phpunit/tests/rest-api/rest-posts-controller.php tests/phpunit/tests/rest-api/rest-posts-controller.php index e2c3e5edef..7969a84b45 100644
class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 2924 2924 'rendered' => '<a href="#">link</a>', 2925 2925 ), 2926 2926 'content' => array( 2927 'raw' => '<a href="#" target="_blank" >link</a>',2928 'rendered' => '<p><a href="#" target="_blank" >link</a></p>',2927 'raw' => '<a href="#" target="_blank" rel="noopener noreferrer">link</a>', 2928 'rendered' => '<p><a href="#" target="_blank" rel="noopener noreferrer">link</a></p>', 2929 2929 ), 2930 2930 'excerpt' => array( 2931 'raw' => '<a href="#" target="_blank" >link</a>',2932 'rendered' => '<p><a href="#" target="_blank" >link</a></p>',2931 'raw' => '<a href="#" target="_blank" rel="noopener noreferrer">link</a>', 2932 'rendered' => '<p><a href="#" target="_blank" rel="noopener noreferrer">link</a></p>', 2933 2933 ), 2934 2934 ) 2935 2935 ),