Plugin Directory

Changeset 954161


Ignore:
Timestamp:
07/24/2014 02:22:58 PM (12 years ago)
Author:
SivaDu
Message:

version 0.2 update

Location:
anylink/trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • anylink/trunk/al_setting.php

    r780848 r954161  
    2121    </form>
    2222    <form action="<?php echo admin_url( 'options-general.php?page=anyLinkSetting' ); ?>" method="post">
     23        <span class="plain"><?php _e( 'For the first time running, you need scan all the exist comments manually.' ) ?></b></span>
     24        <div id="slug_bar">
     25            <div id="anylink_comment_proceeding"> </div>
     26        </div>
     27        <input name="action" value="anylink_comment_scan" type="hidden" />
     28        <?php submit_button( __( 'Regenerate comment slugs', 'anylink' ), 'secondary' ); ?>
     29    </form>
     30    <form action="<?php echo admin_url( 'options-general.php?page=anyLinkSetting' ); ?>" method="post">
    2331        <span class="plain"><?php _e( 'Allows you to generate slugs manually. Keep in mind that please do not regenerate slugs unless you changed slug settings. Search engines may think that you have modified your articles.', 'anylink' ); ?><br /><b><?php _e( 'Note: It won\'t work unless slug settings are changed', 'anylink' ); ?></b></span>
    2432        <div id="slug_bar">
     
    2937    </form>
    3038</div>
     39<?php
     40if( isset( $_POST['action'] ) && $_POST['action'] == 'anylink_comment_scan' ) {
     41    flush();
     42    set_time_limit( 0 );
     43    require_once( ANYLNK_PATH . "/classes/al_covert.php" );
     44    $objAllPost = new al_covert();
     45    $comment_IDs = $objAllPost -> get_all_comments();
     46    $j = count( $comment_IDs );
     47    $k = 0;
     48    foreach( $comment_IDs as $comment_ID ) {
     49        $objAllPost -> covertURLs( $comment_ID, true );
     50        $k += 1;
     51 ?>
     52 <script type="text/javascript">setDivStyle( "anylink_comment_proceeding", <?php echo round( $k / $j, 4 ); ?> ); </script>
     53 <?php
     54    }
     55}
     56?>
    3157<?php
    3258if( isset( $_POST['action'] ) && $_POST['action'] == 'anylink_scan' ) {
  • anylink/trunk/anyLink.php

    r898037 r954161  
    44Plugin URI: http://dudo.org/anylink
    55Description: anyLink is an external links management tool. It help you to covert all the external links in your posts into internal links automatically. It can prevent the website weight flow outside to others. It's absolutely SEO friendly.
    6 Version: 0.1.9
     6Version: 0.2
    77Author: dudo
    88Author URI: http://dudo.org/about
     
    2828add_filter( 'the_content', 'filterByType' );
    2929add_filter( 'rewrite_rules_array','anylink_rewrite_rules' );
    30 
     30add_action( 'wp_insert_comment', 'new_comment',1000, 2);
     31add_filter( 'comment_text', 'filter_comment', 100, 2 );
     32add_filter( 'get_comment_author_url', 'filter_comment_urls', 100 );
    3133/**
    3234 * Check rewrite rules and flush
     
    9698    if( ( is_string( $types ) && ( $types == $type ) ) || ( is_array( $types ) && array_search( $type, $types ) !== false ) ){
    9799        $filter = new al_filter();
    98         return $filter -> applyFilter( $content );
     100        $id = get_the_id();
     101        return $filter -> applyFilter( $content, $id );
    99102       
    100103    } else {
     
    102105    }
    103106}
     107
     108/**
     109 * Call actions when a new comment added
     110 * @param int $comment_id is the id of the comment
     111 * @param object $comment is the comment object
     112 * @since 0.1.9
     113 */
     114 function new_comment( $comment_id, $comment) {
     115    $covert = new al_covert();
     116    $covert -> covertURLs( $comment_id, true );
     117 }
     118 /**
     119  * 把评论内容中的外链转换成内容
     120  *
     121  * @param string $comment_text 评论内容
     122  * @param object $comment comment对象,默认为null
     123  * @return string $comment_text 返回处理后的评论内容以在页面上显示
     124  * @since 0.2
     125  */
     126function filter_comment( $comment_text, $comment = null ){
     127    $filter = new al_filter();
     128    if( is_null( $comment ) )
     129        return $comment_text;
     130    return $filter -> applyFilter( $comment_text, $comment -> comment_ID, true );
     131}
     132/**
     133 * 把留言者的链接转换成内链
     134 *
     135 * @param string $comment_url 评论者留下的链接地址
     136 * @since 0.2
     137 */
     138function filter_comment_urls( $comment_url ) {
     139    //如果没有留下链接,放弃处理
     140    if( empty( $comment_url ) )
     141        return $comment_url;
     142    $anylink_option = get_option( 'anylink_options' );
     143    $anylink_comment_option = $anylink_option['filter-comment'];
     144    //如果设置了不对评论中的链接进行处理则原样返回
     145    if( !$anylink_comment_option )
     146        return $comment_url;
     147    $filter = new al_filter();
     148    //通过链接查找对应的slug
     149    $new_comment_url_slug = $filter -> get_slug_by_url( $comment_url );
     150    if( !empty( $new_comment_url_slug ) ) {
     151        //根据slug产生内部链接,并返回给模板显示
     152        return $filter -> getInternalLinkBySlug( $new_comment_url_slug['al_slug'] );
     153    } else {    //如果链接表中没有索引这个链接,那么按原样返回
     154        return $comment_url;
     155    }
     156}
    104157?>
  • anylink/trunk/classes/al_covert.php

    r836042 r954161  
    77        global $wpdb;
    88        $arrPostIDs = array();
    9             $arrPostIDs += $wpdb -> get_col( $wpdb -> prepare(
     9            $arrPostIDs = $wpdb -> get_col( $wpdb -> prepare(
    1010                "SELECT ID
    1111                FROM " . $wpdb -> prefix . "posts
     
    1515        return $arrPostIDs;
    1616    }
     17    public function get_all_comments(){
     18        global $wpdb;
     19        $all_comment_IDs = array();
     20        $all_comment_IDs = $wpdb -> get_col( $wpdb -> prepare(
     21            "SELECT comment_ID
     22            FROM " . $wpdb -> prefix . "comments
     23            WHERE comment_approved = %d",
     24            1
     25        ));
     26        return $all_comment_IDs;
     27    }
     28   
    1729    public function arrGetAllLnks( $content ) {
    1830        $pattern = '/(?<=href=["\'])https?:\/\/[^"\']+/i';
     
    6678        return $urlIDs;
    6779    }
    68     public function storeRel ( $post_id, $arrUrlIDs ) {
     80    public function storeRel ( $post_id, $arrUrlIDs, $comment_id = 0 ) {
    6981        global $wpdb;
    7082        $arrOldIndex = array();
     
    7587                        "SELECT al_url_id
    7688                        FROM " . ANYLNK_DBINDEX . "
    77                         WHERE al_post_id = %d",
    78                         $post_id
     89                        WHERE al_post_id = %d
     90                        AND al_comm_id = %d",
     91                        $post_id,
     92                        $comment_id
    7993                        ), ARRAY_A );
    8094        foreach( $arrOldRel as $oldRel ){
     
    95109                                    'al_url_id'   => $urlID,
    96110                                    'al_post_id'  => $post_id,
     111                                    'al_comm_id'  => $comment_id,
    97112                                    ),
    98113                                array(
    99114                                    '%d',
    100115                                    '%d',
     116                                    '%d',
    101117                                    )
    102118                                );
     
    109125                                    'al_url_id'  => $urlID,
    110126                                    'al_post_id' => $post_id,
     127                                    'al_comm_id' => $comment_id,
    111128                                    ),
    112129                                array(
    113130                                    '%d',
    114131                                    '%d',
     132                                    '%d',
    115133                                    )
    116134                                );
     
    143161                        );
    144162    } //end regenerateSlugByID
    145     public function covertURLs( $id ) {
    146         $thePost = get_post( $id,  ARRAY_A );
    147         $content = $thePost['post_content']; //get post content
    148         $arrURLs = array();
    149         $arrIDs  = array();
    150         $arrURLs = $this -> arrGetAllLnks( $content );
    151         $arrURLs = $this -> filterLocalURL( $arrURLs );
    152         if( empty( $arrURLs ) )
    153             return;
    154         $arrIDs  = $this -> storeExtLnks( $arrURLs );
    155         $this -> storeRel( $id, $arrIDs );
     163    public function covertURLs( $id, $comment = false) {
     164       //if this is a post
     165        if( !$comment ) {
     166            $thePost = get_post( $id,  ARRAY_A );
     167            $content = $thePost['post_content']; //get post content
     168            $arrURLs = array();
     169            $arrIDs  = array();
     170            $arrURLs = $this -> arrGetAllLnks( $content );
     171            $arrURLs = $this -> filterLocalURL( $arrURLs );
     172            if( empty( $arrURLs ) )
     173                return;
     174            $arrIDs  = $this -> storeExtLnks( $arrURLs );
     175            $this -> storeRel( $id, $arrIDs );
     176        } else { //if this is a comment
     177            $the_comment = get_comment( $id, ARRAY_A );
     178            $comment_body = $the_comment['comment_content'];
     179            $comment_post_id = $the_comment['comment_post_ID'];
     180            $comment_body_urls = $this -> arrGetAllLnks( $comment_body );   //get all urls in comment body
     181            //if comment author's homepage url is not empty
     182            //add it to the array of urls
     183            if( !empty( $the_comment['comment_author_url'] ) ) {
     184                array_push( $comment_body_urls, $the_comment['comment_author_url'] );
     185                $comment_urls = array_unique( $comment_body_urls );
     186            }
     187            if( empty( $comment_urls ) )
     188                return;
     189            $comment_urls = $this -> filterLocalURL( $comment_urls );  //filte local urls
     190            if( empty ( $comment_urls ) )
     191                return;
     192            $comment_url_IDs = $this -> storeExtLnks( $comment_urls ); //insert all external links into data table and get IDs
     193            $this -> storeRel( $comment_post_id, $comment_url_IDs, $id );
     194        }
    156195    }
    157196}
  • anylink/trunk/classes/al_filter.php

    r898037 r954161  
    1111        $this -> rel = $anylinkOptions['rel'];
    1212    }
    13     //@post_id: get all links and slugs of a specified post
    14     public function getAllLnks( $post_id ) {
     13    /**
     14     * Get all links belongd to the specified post or comment
     15     *
     16     * @param int $post_id the id of the post
     17     * @param int $cimment_id the id of the comment, default 0, means it's a post rather than a comment
     18     * @since 0.1.9
     19     */
     20    public function getAllLnks( $id, $is_comment = false ) {
    1521        $arrURL = array();
    1622        global $wpdb;
     23        if( !$is_comment )
     24            $column = 'I.al_post_id';
     25        else
     26            $column = 'I.al_comm_id';
    1727        $arrURL = $wpdb -> get_results( $wpdb -> prepare(
    1828            "
     
    2131            LEFT JOIN " . ANYLNK_DBTB . " U
    2232            ON I.al_url_id = U.al_id
    23             WHERE I.al_post_id = %d",
    24             $post_id
     33            WHERE " . $column . "= %d",
     34            $id
    2535        ), ARRAY_A );
    2636        return $arrURL;
    2737    }
    2838    //restore all URL
    29     private function replaceURL( $matches ) {
     39    public function replaceURL( $matches ) {
    3040        $U2S = $this -> arrU2S;
    3141        $siteURL = home_url();
     
    5868        }
    5969    }
    60     public function applyFilter( $content ) {
     70    /**
     71     * Filter all external links which are exist in database
     72     *
     73     * @param String $content is the content which cotains external link(s)
     74     * @return string $content
     75     */
     76    public function applyFilter( $content, $id, $is_comment = false ) {
    6177        global $wp_query, $wp_rewrite;
    62         $post_id = get_the_id();
    63         $arrUrlSlug = $this -> getAllLnks( $post_id );
     78        $arrUrlSlug = $this -> getAllLnks( $id, $is_comment );
    6479        if( $arrUrlSlug ) {
    6580            foreach( $arrUrlSlug as $arrSlugs ) {
     
    129144        return $arrSlug;
    130145    }
     146    /**
     147     * 通过URL地址查找对应的slug
     148     *
     149     * @param string $url 给定的链接
     150     * @return arrary $arr_slug 返回查找到的数组
     151     * @since 0.2
     152     */
     153    public function get_slug_by_url( $url ) {
     154        global $wpdb;
     155        $arr_slug = array();
     156        $arr_slug = $wpdb -> get_row( $wpdb -> prepare(
     157            "SELECT *
     158            FROM " . ANYLNK_DBTB ."
     159            WHERE al_origURL = %s",
     160            $url
     161        ), ARRAY_A );
     162        return $arr_slug;
     163    }
    131164   
    132165    public function getInternalLinkBySlug( $slug ) {
  • anylink/trunk/classes/al_option.php

    r898037 r954161  
    3636        add_settings_field( 'al_post_types', __( 'Post Types', 'anylink' ), array( $this, 'dispPostTypes' ), 'anyLinkSetting', 'al_general_settings' );
    3737        add_settings_field( 'al_url_properties', __( 'Link properties', 'anylink' ), array( $this, 'dispLinkPro'), 'anyLinkSetting', 'al_general_settings' );
     38        add_settings_field(  'filter_comment_toggle', __( 'Turn on comment filter.' ), array( $this, 'display_comment_toggle'), 'anyLinkSetting', 'al_general_settings' );
    3839        load_plugin_textdomain( 'anylink', false, ANYLNK_PATH . '/i18n/' );
    3940    }
     
    120121        echo $html;
    121122    }
     123    public function display_comment_toggle() {
     124        if( !isset( $this -> anylinkOptions['filter-comment'] ) )
     125            $filter_comment = 1;
     126        else
     127            $filter_comment = $this -> anylinkOptions['filter-comment'];
     128        $html  = __( "Allow anylink to filter external url in comment." );
     129        $html .= "<br /><input type='radio' id='filter-comment-n' name='anylink_options[filter-comment]' value= '0'";
     130        $html .= $filter_comment == 0 ? ' checked' : '';
     131        $html .= "/><label for='filter-comment-n'>" . __( "Please leave comments unfiltered." ) . "</label><br />";
     132        $html .= "<input type='radio' id='filter-comment-y' name='anylink_options[filter-comment]' value= '1'";
     133        $html .= $filter_comment == 1 ? ' checked' : '';
     134        $html .= "/><label for='filter-comment-y'>" . __( "Filter link(s) in comment" ) . "</label>";
     135        echo $html;
     136    }
    122137    /*  I should put some validations here
    123138     *  a filter named "sanitize_option_$optionname" is applied when you can update_option
     
    137152        $oldOptions['slugChar'] = $input['slugChar'];
    138153        $oldOptions['postType'] = $input['postType'];
     154        $oldOptions['filter-comment'] = $input['filter-comment'];
    139155        $oldOptions['rel'] = sanitize_text_field( $input['rel'] );
    140156        return $oldOptions;
  • anylink/trunk/readme.txt

    r898037 r954161  
    55Requires at least: 3.4
    66Tested up to: 3.9
    7 Stable tag: 0.1.9
     7Stable tag: 0.2
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    4141
    4242== Changelog ==
     43
     44= 0.2 =
     45*   增加了对评论中出现的链接的转换
     46*   更新了部分代码
     47*   优化性能,减少资源开支
    4348
    4449= 0.1.9 =
Note: See TracChangeset for help on using the changeset viewer.