WordPress.org

Plugin Directory

Changeset 599789


Ignore:
Timestamp:
09/16/12 19:57:05 (19 months ago)
Author:
aaroncampbell
Message:

Update handling of links including FTP and E-Mail links

Location:
twitter-widget-pro/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • twitter-widget-pro/trunk/readme.txt

    r599141 r599789  
    131131== Upgrade Notice == 
    132132 
     133= 2.3.11 = 
     134Better linking to fix issues with links followed by punctuation 
     135 
    133136= 2.3.10 = 
    134137Update feeds and links to Ran.ge, moves screenshots outside of plugin zip 
     
    165168 
    166169== Changelog == 
     170 
     171= 2.3.11 = 
     172* Better linking of URLs, especially handling of punctuation at end of URLs 
     173* More efficient regex for link parsing 
     174* Link FTP links 
     175* Link E-Mail addresses 
     176 
    167177 
    168178= 2.3.10 = 
  • twitter-widget-pro/trunk/wp-twitter-widget.php

    r599141 r599789  
    44 * Plugin URI: http://bluedogwebservices.com/wordpress-plugin/twitter-widget-pro/ 
    55 * Description: A widget that properly handles twitter feeds, including @username, #hashtag, and link parsing.  It can even display profile images for the users.  Requires PHP5. 
    6  * Version: 2.3.10 
     6 * Version: 2.3.11-beta 
    77 * Author: Aaron D. Campbell 
    88 * Author URI: http://ran.ge/ 
     
    3131require_once( 'tlc-transients.php' ); 
    3232require_once( 'xavisys-plugin-framework.php' ); 
    33 define( 'TWP_VERSION', '2.3.10' ); 
     33define( 'TWP_VERSION', '2.3.11' ); 
    3434 
    3535/** 
     
    472472     */ 
    473473    public function linkUrls( $text ) { 
    474         /** 
    475          * match protocol://address/path/file.extension?some=variable&another=asf% 
    476          * $1 is a possible space, this keeps us from linking href="[link]" etc 
    477          * $2 is the whole URL 
    478          * $3 is protocol:// 
    479          * $4 is the URL without the protocol:// 
    480          * $5 is the URL parameters 
    481          */ 
    482         $text = preg_replace_callback("/(^|\s)(([a-zA-Z]+:\/\/)([a-z][a-z0-9_\..-]*[a-z]{2,6})([a-zA-Z0-9~\/*-?&%]*))/i", array($this, '_linkUrlsCallback'), $text); 
    483  
    484         /** 
    485          * match www.something.domain/path/file.extension?some=variable&another=asf% 
    486          * $1 is a possible space, this keeps us from linking href="[link]" etc 
    487          * $2 is the whole URL that was matched.  The protocol is missing, so we assume http:// 
    488          * $3 is www. 
    489          * $4 is the URL matched without the www. 
    490          * $5 is the URL parameters 
    491          */ 
    492         $text = preg_replace_callback("/(^|\s)(www\.([a-z][a-z0-9_\..-]*[a-z]{2,6})([a-zA-Z0-9~\/*-?&%]*))/i", array($this, '_linkUrlsCallback'), $text); 
     474        $text = " {$text} "; // Pad with whitespace to simplify the regexes 
     475 
     476        $url_clickable = '~ 
     477            ([\\s(<.,;:!?])                                        # 1: Leading whitespace, or punctuation 
     478            (                                                      # 2: URL 
     479                [\\w]{1,20}+://                                # Scheme and hier-part prefix 
     480                (?=\S{1,2000}\s)                               # Limit to URLs less than about 2000 characters long 
     481                [\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]*+         # Non-punctuation URL character 
     482                (?:                                            # Unroll the Loop: Only allow puctuation URL character if followed by a non-punctuation URL character 
     483                    [\'.,;:!?)]                            # Punctuation URL character 
     484                    [\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]++ # Non-punctuation URL character 
     485                )* 
     486            ) 
     487            (\)?)                                                  # 3: Trailing closing parenthesis (for parethesis balancing post processing) 
     488        ~xS'; 
     489        // The regex is a non-anchored pattern and does not have a single fixed starting character. 
     490        // Tell PCRE to spend more time optimizing since, when used on a page load, it will probably be used several times. 
     491 
     492        $text = preg_replace_callback( $url_clickable, array($this, '_make_url_clickable_cb'), $text ); 
     493 
     494        $text = preg_replace_callback( '#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]+)#is', array($this, '_make_web_ftp_clickable_cb' ), $text ); 
     495        $text = preg_replace_callback( '#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', array($this, '_make_email_clickable_cb' ), $text ); 
     496 
     497        $text = substr( $text, 1, -1 ); // Remove our whitespace padding. 
    493498 
    494499        return $text; 
    495500    } 
    496501 
    497     private function _linkUrlsCallback ( $matches ) { 
     502    function _make_web_ftp_clickable_cb($matches) { 
     503        $ret = ''; 
     504        $dest = $matches[2]; 
     505        $dest = 'http://' . $dest; 
     506        $dest = esc_url($dest); 
     507        if ( empty($dest) ) 
     508            return $matches[0]; 
     509 
     510        // removed trailing [.,;:)] from URL 
     511        if ( in_array( substr($dest, -1), array('.', ',', ';', ':', ')') ) === true ) { 
     512            $ret = substr($dest, -1); 
     513            $dest = substr($dest, 0, strlen($dest)-1); 
     514        } 
     515        $linkAttrs = array( 
     516            'href'  => $dest 
     517        ); 
     518        return $matches[1] . $this->_buildLink( $dest, $linkAttrs ) . $ret; 
     519    } 
     520 
     521    private function _make_email_clickable_cb( $matches ) { 
     522        $email = $matches[2] . '@' . $matches[3]; 
     523        $linkAttrs = array( 
     524            'href'  => 'mailto:' . $email 
     525        ); 
     526        return $matches[1] . $this->_buildLink( $email, $linkAttrs ); 
     527    } 
     528 
     529    private function _make_url_clickable_cb ( $matches ) { 
    498530        $linkAttrs = array( 
    499531            'href'  => $matches[2] 
     
    510542        $attributes = apply_filters( 'widget_twitter_link_attributes', $attributes ); 
    511543        $attributes = wp_parse_args( $attributes ); 
    512         if ( strtolower( 'www' == substr( $attributes['href'], 0, 3 ) ) ) 
    513             $attributes['href'] = 'http://' . $attributes['href']; 
    514544 
    515545        $text = apply_filters( 'widget_twitter_link_text', $text ); 
Note: See TracChangeset for help on using the changeset viewer.