| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | Plugin Name: Autolink URI |
|---|
| 4 | Plugin URI: http://www.semiologic.com/software/autolink-uri/ |
|---|
| 5 | Description: Automatically wraps unhyperlinked uri with html anchors. |
|---|
| 6 | Version: 2.0 RC |
|---|
| 7 | Author: Denis de Bernardy |
|---|
| 8 | Author URI: http://www.getsemiologic.com |
|---|
| 9 | Text Domain: sem-autolink-uri |
|---|
| 10 | Domain Path: /lang |
|---|
| 11 | */ |
|---|
| 12 | |
|---|
| 13 | /* |
|---|
| 14 | Terms of use |
|---|
| 15 | ------------ |
|---|
| 16 | |
|---|
| 17 | This software is copyright Mesoconcepts (http://www.mesoconcepts.com), and is distributed under the terms of the GPL license, v.2. |
|---|
| 18 | |
|---|
| 19 | http://www.opensource.org/licenses/gpl-2.0.php |
|---|
| 20 | **/ |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | /** |
|---|
| 24 | * autolink_uri |
|---|
| 25 | * |
|---|
| 26 | * @package Autolink URI |
|---|
| 27 | **/ |
|---|
| 28 | |
|---|
| 29 | // after shortcodes |
|---|
| 30 | add_filter('the_content', array('autolink_uri', 'filter'), 12); |
|---|
| 31 | add_filter('the_excerpt', array('autolink_uri', 'filter'), 12); |
|---|
| 32 | |
|---|
| 33 | class autolink_uri { |
|---|
| 34 | /** |
|---|
| 35 | * filter() |
|---|
| 36 | * |
|---|
| 37 | * @param string $text |
|---|
| 38 | * @return string $text |
|---|
| 39 | **/ |
|---|
| 40 | |
|---|
| 41 | function filter($text) { |
|---|
| 42 | global $escape_autolink_uri; |
|---|
| 43 | |
|---|
| 44 | $escape_autolink_uri = array(); |
|---|
| 45 | |
|---|
| 46 | $text = autolink_uri::escape($text); |
|---|
| 47 | |
|---|
| 48 | $text = preg_replace_callback("/ |
|---|
| 49 | \b |
|---|
| 50 | ( # protocol or www. |
|---|
| 51 | [a-z]{3,}:\/\/ |
|---|
| 52 | | |
|---|
| 53 | www\. |
|---|
| 54 | ) |
|---|
| 55 | (?: # domain |
|---|
| 56 | localhost |
|---|
| 57 | | |
|---|
| 58 | [a-z0-9%_|~-]+ |
|---|
| 59 | (?:\.[a-z0-9%_|~-]+)+ |
|---|
| 60 | ) |
|---|
| 61 | (?: # path |
|---|
| 62 | \/[a-z0-9%_|~.-]* |
|---|
| 63 | (?:\/[a-z0-9%_|~.-]*)+ |
|---|
| 64 | )? |
|---|
| 65 | (?: # attributes |
|---|
| 66 | \?[a-z0-9%_|~.-]* |
|---|
| 67 | (?:&[a-z0-9%_|~.=&#;-]*)+ |
|---|
| 68 | )? |
|---|
| 69 | (?: # anchor |
|---|
| 70 | \#[a-z0-9%_|~.=&#;-]* |
|---|
| 71 | )? |
|---|
| 72 | /ix", array('autolink_uri', 'url_callback'), $text); |
|---|
| 73 | |
|---|
| 74 | $text = preg_replace_callback("/ |
|---|
| 75 | \b |
|---|
| 76 | (?:mailto:)? |
|---|
| 77 | ( |
|---|
| 78 | [a-z0-9%_|~-]+ |
|---|
| 79 | (?:\.[a-z0-9%_|~-]+)* |
|---|
| 80 | @ |
|---|
| 81 | [a-z0-9%_|~-]+ |
|---|
| 82 | (?:\.[a-z0-9%_|~-]+)+ |
|---|
| 83 | ) |
|---|
| 84 | /ix", array('autolink_uri', 'email_callback'), $text); |
|---|
| 85 | |
|---|
| 86 | $text = autolink_uri::unescape($text); |
|---|
| 87 | |
|---|
| 88 | return $text; |
|---|
| 89 | } # filter() |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | /** |
|---|
| 93 | * url_callback() |
|---|
| 94 | * |
|---|
| 95 | * @param array $match |
|---|
| 96 | * @return string $text |
|---|
| 97 | **/ |
|---|
| 98 | |
|---|
| 99 | function url_callback($match) { |
|---|
| 100 | $url = $match[0]; |
|---|
| 101 | $href = $url; |
|---|
| 102 | |
|---|
| 103 | if ( strtolower($match[1]) === 'www.' ) |
|---|
| 104 | $href = 'http://' . $href; |
|---|
| 105 | |
|---|
| 106 | $href = esc_url($href); |
|---|
| 107 | |
|---|
| 108 | return '<a href="' . $href . '">' . $url . '</a>'; |
|---|
| 109 | } # url_callback() |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | /** |
|---|
| 113 | * email_callback() |
|---|
| 114 | * |
|---|
| 115 | * @param array $match |
|---|
| 116 | * @return string $text |
|---|
| 117 | **/ |
|---|
| 118 | |
|---|
| 119 | function email_callback($match) { |
|---|
| 120 | $email = end($match); |
|---|
| 121 | return '<a href="' . esc_url('mailto:' . $email) . '">' . $email . '</a>'; |
|---|
| 122 | } # email_callback() |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | /** |
|---|
| 126 | * escape() |
|---|
| 127 | * |
|---|
| 128 | * @param string $text |
|---|
| 129 | * @return string $text |
|---|
| 130 | **/ |
|---|
| 131 | |
|---|
| 132 | function escape($text) { |
|---|
| 133 | global $escape_autolink_uri; |
|---|
| 134 | |
|---|
| 135 | if ( !isset($escape_autolink_uri) ) |
|---|
| 136 | $escape_autolink_uri = array(); |
|---|
| 137 | |
|---|
| 138 | foreach ( array( |
|---|
| 139 | 'head' => "/ |
|---|
| 140 | .*? |
|---|
| 141 | <\s*\/\s*head\s*> |
|---|
| 142 | /isx", |
|---|
| 143 | 'blocks' => "/ |
|---|
| 144 | <\s*(script|style|object|textarea)(?:\s.*?)?> |
|---|
| 145 | .*? |
|---|
| 146 | <\s*\/\s*\\1\s*> |
|---|
| 147 | /isx", |
|---|
| 148 | 'smart_links' => "/ |
|---|
| 149 | \[.+?\] |
|---|
| 150 | /x", |
|---|
| 151 | 'anchors' => "/ |
|---|
| 152 | <\s*a\s.+?>.+?<\s*\/\s*a\s*> |
|---|
| 153 | /isx", |
|---|
| 154 | 'tags' => "/ |
|---|
| 155 | <[^<>]+?(?:src|href|codebase|archive|usemap|data|value)=[^<>]+?> |
|---|
| 156 | /ix", |
|---|
| 157 | ) as $regex ) { |
|---|
| 158 | $text = preg_replace_callback($regex, array('autolink_uri', 'escape_callback'), $text); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | return $text; |
|---|
| 162 | } # escape() |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | /** |
|---|
| 166 | * escape_callback() |
|---|
| 167 | * |
|---|
| 168 | * @param array $match |
|---|
| 169 | * @return string $tag_id |
|---|
| 170 | **/ |
|---|
| 171 | |
|---|
| 172 | function escape_callback($match) { |
|---|
| 173 | global $escape_autolink_uri; |
|---|
| 174 | |
|---|
| 175 | $tag_id = "----escape_autolink_uri:" . md5($match[0]) . "----"; |
|---|
| 176 | $escape_autolink_uri[$tag_id] = $match[0]; |
|---|
| 177 | |
|---|
| 178 | return $tag_id; |
|---|
| 179 | } # escape_callback() |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | /** |
|---|
| 183 | * unescape() |
|---|
| 184 | * |
|---|
| 185 | * @param string $text |
|---|
| 186 | * @return string $text |
|---|
| 187 | **/ |
|---|
| 188 | |
|---|
| 189 | function unescape($text) { |
|---|
| 190 | global $escape_autolink_uri; |
|---|
| 191 | |
|---|
| 192 | if ( !$escape_autolink_uri ) |
|---|
| 193 | return $text; |
|---|
| 194 | |
|---|
| 195 | $text = preg_replace_callback("/ |
|---|
| 196 | ----escape_autolink_uri:[a-f0-9]{32}---- |
|---|
| 197 | /x", array('autolink_uri', 'unescape_callback'), $text); |
|---|
| 198 | |
|---|
| 199 | return $text; |
|---|
| 200 | } # unescape() |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | /** |
|---|
| 204 | * unescape_callback() |
|---|
| 205 | * |
|---|
| 206 | * @param array $match |
|---|
| 207 | * @return string $text |
|---|
| 208 | **/ |
|---|
| 209 | |
|---|
| 210 | function unescape_callback($match) { |
|---|
| 211 | global $escape_autolink_uri; |
|---|
| 212 | |
|---|
| 213 | return $escape_autolink_uri[$match[0]]; |
|---|
| 214 | |
|---|
| 215 | return $match[0]; |
|---|
| 216 | } # unescape_callback() |
|---|
| 217 | } # autolink_uri |
|---|
| 218 | ?> |
|---|