Make WordPress Core

Ticket #9959: wp_rel_nofollow.txt

File wp_rel_nofollow.txt, 1.4 KB (added by junsuijin, 15 years ago)
Line 
1<?php
2
3/* eliminates the need for function wp_rel_nofollow_callback */
4function wp_rel_nofollow( $text ) {
5        global $wpdb;
6        // This is a pre save filter, so text is already escaped.
7        $text = stripslashes($text);
8        $r = array(
9                "|(<a\b[^>]*?\srel=['\"])(?![^'\"]*\bnofollow\b)|i" => "$1nofollow ",
10                "|<a\b(?![^>]*\srel=['\"])|i" => '<a rel="nofollow"'
11        );
12        $text = preg_replace(array_keys($r), array_values($r), $text);
13        $text = $wpdb->escape($text);
14        return $text;
15}
16
17/* replace these */
18/**
19 * Adds rel nofollow string to all HTML A elements in content.
20 *
21 * @since 1.5.0
22 *
23 * @param string $text Content that may contain HTML A elements.
24 * @return string Converted content.
25 */
26function wp_rel_nofollow( $text ) {
27        global $wpdb;
28        // This is a pre save filter, so text is already escaped.
29        $text = stripslashes($text);
30        $text = preg_replace_callback('|<a (.+?)>|i', 'wp_rel_nofollow_callback', $text);
31        $text = $wpdb->escape($text);
32        return $text;
33}
34
35/**
36 * Callback to used to add rel=nofollow string to HTML A element.
37 *
38 * Will remove already existing rel="nofollow" and rel='nofollow' from the
39 * string to prevent from invalidating (X)HTML.
40 *
41 * @since 2.3.0
42 *
43 * @param array $matches Single Match
44 * @return string HTML A Element with rel nofollow.
45 */
46function wp_rel_nofollow_callback( $matches ) {
47        $text = $matches[1];
48        $text = str_replace(array(' rel="nofollow"', " rel='nofollow'"), '', $text);
49        return "<a $text rel=\"nofollow\">";
50}
51
52?>