Make WordPress Core

Ticket #48022: 48022.1.diff

File 48022.1.diff, 2.2 KB (added by audrasjb, 5 years ago)

Adds 5.3.0 @since information to the new functions DocBlocks

  • src/wp-includes/formatting.php

    diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php
    index fd9fca04f0..d9dce6d48e 100644
    a b function wp_rel_nofollow_callback( $matches ) { 
    30753075        return "<a $text rel=\"" . esc_attr( $rel ) . '">';
    30763076}
    30773077
     3078/**
     3079 * Adds rel ugc string to all HTML A elements in content.
     3080 *
     3081 * @since 5.3.0
     3082 *
     3083 * @param string $text Content that may contain HTML A elements.
     3084 * @return string Converted content.
     3085 */
     3086function wp_rel_ugc( $text ) {
     3087        // This is a pre save filter, so text is already escaped.
     3088        $text = stripslashes( $text );
     3089        $text = preg_replace_callback( '|<a (.+?)>|i', 'wp_rel_ugc_callback', $text );
     3090        return wp_slash( $text );
     3091}
     3092
     3093/**
     3094 * Callback to add rel=ugc string to HTML A element.
     3095 *
     3096 * Will remove already existing rel="ugc" and rel='ugc' from the
     3097 * string to prevent from invalidating (X)HTML.
     3098 *
     3099 * @since 5.3.0
     3100 *
     3101 * @param array $matches Single Match
     3102 * @return string HTML A Element with rel ugc.
     3103 */
     3104function wp_rel_ugc_callback( $matches ) {
     3105        $text = $matches[1];
     3106        $atts = wp_kses_hair( $matches[1], wp_allowed_protocols() );
     3107        $rel  = 'ugc';
     3108
     3109        if ( ! empty( $atts['href'] ) ) {
     3110                if ( in_array( strtolower( wp_parse_url( $atts['href']['value'], PHP_URL_SCHEME ) ), array( 'http', 'https' ), true ) ) {
     3111                        if ( strtolower( wp_parse_url( $atts['href']['value'], PHP_URL_HOST ) ) === strtolower( wp_parse_url( home_url(), PHP_URL_HOST ) ) ) {
     3112                                return "<a $text>";
     3113                        }
     3114                }
     3115        }
     3116
     3117        if ( ! empty( $atts['rel'] ) ) {
     3118                $parts = array_map( 'trim', explode( ' ', $atts['rel']['value'] ) );
     3119                if ( false === array_search( 'ugc', $parts ) ) {
     3120                        $parts[] = 'ugc';
     3121                }
     3122                $rel = implode( ' ', $parts );
     3123                unset( $atts['rel'] );
     3124
     3125                $html = '';
     3126                foreach ( $atts as $name => $value ) {
     3127                        if ( isset( $value['vless'] ) && 'y' === $value['vless'] ) {
     3128                                $html .= $name . ' ';
     3129                        } else {
     3130                                $html .= "{$name}=\"" . esc_attr( $value['value'] ) . '" ';
     3131                        }
     3132                }
     3133                $text = trim( $html );
     3134        }
     3135        return "<a $text rel=\"" . esc_attr( $rel ) . '">';
     3136}
     3137
    30783138/**
    30793139 * Adds rel noreferrer and noopener to all HTML A elements that have a target.
    30803140 *