Changeset 954161
- Timestamp:
- 07/24/2014 02:22:58 PM (12 years ago)
- Location:
- anylink/trunk
- Files:
-
- 6 edited
-
al_setting.php (modified) (2 diffs)
-
anyLink.php (modified) (4 diffs)
-
classes/al_covert.php (modified) (7 diffs)
-
classes/al_filter.php (modified) (4 diffs)
-
classes/al_option.php (modified) (3 diffs)
-
readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
anylink/trunk/al_setting.php
r780848 r954161 21 21 </form> 22 22 <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"> 23 31 <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> 24 32 <div id="slug_bar"> … … 29 37 </form> 30 38 </div> 39 <?php 40 if( 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 ?> 31 57 <?php 32 58 if( isset( $_POST['action'] ) && $_POST['action'] == 'anylink_scan' ) { -
anylink/trunk/anyLink.php
r898037 r954161 4 4 Plugin URI: http://dudo.org/anylink 5 5 Description: 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.96 Version: 0.2 7 7 Author: dudo 8 8 Author URI: http://dudo.org/about … … 28 28 add_filter( 'the_content', 'filterByType' ); 29 29 add_filter( 'rewrite_rules_array','anylink_rewrite_rules' ); 30 30 add_action( 'wp_insert_comment', 'new_comment',1000, 2); 31 add_filter( 'comment_text', 'filter_comment', 100, 2 ); 32 add_filter( 'get_comment_author_url', 'filter_comment_urls', 100 ); 31 33 /** 32 34 * Check rewrite rules and flush … … 96 98 if( ( is_string( $types ) && ( $types == $type ) ) || ( is_array( $types ) && array_search( $type, $types ) !== false ) ){ 97 99 $filter = new al_filter(); 98 return $filter -> applyFilter( $content ); 100 $id = get_the_id(); 101 return $filter -> applyFilter( $content, $id ); 99 102 100 103 } else { … … 102 105 } 103 106 } 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 */ 126 function 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 */ 138 function 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 } 104 157 ?> -
anylink/trunk/classes/al_covert.php
r836042 r954161 7 7 global $wpdb; 8 8 $arrPostIDs = array(); 9 $arrPostIDs += $wpdb -> get_col( $wpdb -> prepare(9 $arrPostIDs = $wpdb -> get_col( $wpdb -> prepare( 10 10 "SELECT ID 11 11 FROM " . $wpdb -> prefix . "posts … … 15 15 return $arrPostIDs; 16 16 } 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 17 29 public function arrGetAllLnks( $content ) { 18 30 $pattern = '/(?<=href=["\'])https?:\/\/[^"\']+/i'; … … 66 78 return $urlIDs; 67 79 } 68 public function storeRel ( $post_id, $arrUrlIDs ) {80 public function storeRel ( $post_id, $arrUrlIDs, $comment_id = 0 ) { 69 81 global $wpdb; 70 82 $arrOldIndex = array(); … … 75 87 "SELECT al_url_id 76 88 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 79 93 ), ARRAY_A ); 80 94 foreach( $arrOldRel as $oldRel ){ … … 95 109 'al_url_id' => $urlID, 96 110 'al_post_id' => $post_id, 111 'al_comm_id' => $comment_id, 97 112 ), 98 113 array( 99 114 '%d', 100 115 '%d', 116 '%d', 101 117 ) 102 118 ); … … 109 125 'al_url_id' => $urlID, 110 126 'al_post_id' => $post_id, 127 'al_comm_id' => $comment_id, 111 128 ), 112 129 array( 113 130 '%d', 114 131 '%d', 132 '%d', 115 133 ) 116 134 ); … … 143 161 ); 144 162 } //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 } 156 195 } 157 196 } -
anylink/trunk/classes/al_filter.php
r898037 r954161 11 11 $this -> rel = $anylinkOptions['rel']; 12 12 } 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 ) { 15 21 $arrURL = array(); 16 22 global $wpdb; 23 if( !$is_comment ) 24 $column = 'I.al_post_id'; 25 else 26 $column = 'I.al_comm_id'; 17 27 $arrURL = $wpdb -> get_results( $wpdb -> prepare( 18 28 " … … 21 31 LEFT JOIN " . ANYLNK_DBTB . " U 22 32 ON I.al_url_id = U.al_id 23 WHERE I.al_post_id= %d",24 $ post_id33 WHERE " . $column . "= %d", 34 $id 25 35 ), ARRAY_A ); 26 36 return $arrURL; 27 37 } 28 38 //restore all URL 29 p rivatefunction replaceURL( $matches ) {39 public function replaceURL( $matches ) { 30 40 $U2S = $this -> arrU2S; 31 41 $siteURL = home_url(); … … 58 68 } 59 69 } 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 ) { 61 77 global $wp_query, $wp_rewrite; 62 $post_id = get_the_id(); 63 $arrUrlSlug = $this -> getAllLnks( $post_id ); 78 $arrUrlSlug = $this -> getAllLnks( $id, $is_comment ); 64 79 if( $arrUrlSlug ) { 65 80 foreach( $arrUrlSlug as $arrSlugs ) { … … 129 144 return $arrSlug; 130 145 } 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 } 131 164 132 165 public function getInternalLinkBySlug( $slug ) { -
anylink/trunk/classes/al_option.php
r898037 r954161 36 36 add_settings_field( 'al_post_types', __( 'Post Types', 'anylink' ), array( $this, 'dispPostTypes' ), 'anyLinkSetting', 'al_general_settings' ); 37 37 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' ); 38 39 load_plugin_textdomain( 'anylink', false, ANYLNK_PATH . '/i18n/' ); 39 40 } … … 120 121 echo $html; 121 122 } 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 } 122 137 /* I should put some validations here 123 138 * a filter named "sanitize_option_$optionname" is applied when you can update_option … … 137 152 $oldOptions['slugChar'] = $input['slugChar']; 138 153 $oldOptions['postType'] = $input['postType']; 154 $oldOptions['filter-comment'] = $input['filter-comment']; 139 155 $oldOptions['rel'] = sanitize_text_field( $input['rel'] ); 140 156 return $oldOptions; -
anylink/trunk/readme.txt
r898037 r954161 5 5 Requires at least: 3.4 6 6 Tested up to: 3.9 7 Stable tag: 0. 1.97 Stable tag: 0.2 8 8 License: GPLv2 or later 9 9 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 41 41 42 42 == Changelog == 43 44 = 0.2 = 45 * 增加了对评论中出现的链接的转换 46 * 更新了部分代码 47 * 优化性能,减少资源开支 43 48 44 49 = 0.1.9 =
Note: See TracChangeset
for help on using the changeset viewer.