| 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 | */ |
| 3086 | function 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 | */ |
| 3104 | function 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 | |
| 3138 | /** |