viewing source for "shrinkylink.php"
last commit
commit 1bb94e432308d292dedf65144ae2ea0359c8178f
Date: Mon Jul 24 19:13:50 2006 +0000
source code
001: <?php
002: /*
003: Plugin Name: ShrinkyLink
004: Plugin URI: http://nymb.us/?p=54
005: Description: A simple filter that changes the links in comments/posts to be a certain size (char length) or a certain word, and optionally, displays the domain of the site after the link.
006: Author: Andrew Rader
007: Version: 0.2
008: Author URI: http://nymb.us
009:
010: Copyright 2006 Andrew Rader
011:
012: This file is part of Shrinky Link
013:
014: Shrinky Link is free software; you can redistribute it and/or modify
015: it under the terms of the GNU General Public License as published by
016: the Free Software Foundation; either version 2 of the License, or
017: (at your option) any later version.
018:
019: Shrinky Link is distributed in the hope that it will be useful,
020: but WITHOUT ANY WARRANTY; without even the implied warranty of
021: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
022: GNU General Public License for more details.
023:
024: You should have received a copy of the GNU General Public License
025: along with Shrinky Link; if not, write to the Free Software
026: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
027:
028: */
029:
030: add_filter('comment_text', array('shrinky','shrinky_comments'), 50);
031: add_filter('the_content', array('shrinky','shrinky_posts'), 50);
032:
033: add_action('activate_shrinkylink/shrinkylink.php', array('shrinky','shrinky_init'));
034: add_action('admin_menu', array('shrinky','shrinky_setup'));
035:
036: class shrinky {
037:
038: function shrinky_comments( $comment ) {
039: if( shrinky::get_comment_filter() ) {
040: return shrinky::shrinkylink($comment);
041: }
042: else {
043: return $comment;
044: }
045: }
046:
047: function shrinky_posts( $post ) {
048: if( shrinky::get_post_filter() ) {
049: return shrinky::shrinkylink($post);
050: }
051: else {
052: return $post;
053: }
054: }
055:
056: function shrinkylink( $comment ) {
057: if( shrinky::get_replace() ) {
058: $name = shrinky::get_replace_text();
059: if( shrinky::get_show_domain() ) {
060: $comment = preg_replace( '/<a([^>]*)href=?([^:]*:\/\/)?([^ \/"\']*)([^>]*)>([^<]*)<\/a>/', '<a$1href=$2$3$4>'.$name.'</a> [$3]', $comment );
061: }
062: else {
063: $comment = preg_replace( '/<a([^>]*)href=?([^:]*:\/\/)?([^ \/"\']*)([^>]*)>([^<]*)<\/a>/', '<a$1href=$2$3$4>'.$name.'</a>', $comment );
064: }
065: }
066: else {
067: $comment = preg_replace_callback( '/<a([^>]*)href=?([^:]*:\/\/)?([^ \/"\']*)([^>]*)>([^<]*)<\/a>/', array('shrinky','shrinky_trim'), $comment );
068: }
069: return $comment;
070: }
071:
072: function shrinky_trim( $comment ) {
073: $text = $comment[5];
074: if( shrinky::get_www_trim() ) {
075: $text = preg_replace( '/([^ :]*)www\.([^ ]*)/', '$1$2', $text );
076: }
077: if( shrinky::get_scheme_trim() ) {
078: $text = preg_replace( '/([^ :]*:\/\/)([^ ]*)/', '$2', $text );
079: }
080: if( shrinky::get_show_domain() ) {
081: $domain = ' ['.$comment[3].']';
082: }
083: else {
084: $domain = '';
085: }
086:
087: $len = strlen($text);
088: $text = substr( $text, 0, shrinky::get_trim_length() );
089:
090: if( $len > shrinky::get_trim_length() && shrinky::get_elipses() ) {
091: $text .= "...";
092: }
093:
094: return '<a'.$comment[1].'href='.$comment[2].$comment[3].$comment[4].'>'.$text.'</a>'.$domain;
095: }
096:
097: function shrinky_init() {
098: if( function_exists('add_option') ) {
099: add_option( 'shrinky_comments', 'yes' );
100: add_option( 'shrinky_posts', 'no' );
101: add_option( 'shrinky_replace', 'yes' );
102: add_option( 'shrinky_text', 'link' );
103: add_option( 'shrinky_trim', 'no' );
104: add_option( 'shrinky_size', '12' );
105: add_option( 'shrinky_scheme', 'no' );
106: add_option( 'shrinky_www', 'no' );
107: add_option( 'shrinky_elipse', 'yes');
108: add_option( 'shrinky_domain', 'yes' );
109: }
110: }
111:
112: function shrinky_setup() {
113: if( function_exists('add_options_page') ) {
114: add_options_page(__('Shrinky Link'),__('Shrinky Link'),1,basename(__FILE__),array('shrinky','shrinky_ui'));
115: }
116: }
117:
118: function shrinky_ui() {
119: include_once('shrinky-ui.php');
120: }
121:
122: function enable_comment_filter() {
123: if( function_exists('update_option') ) {
124: update_option( 'shrinky_comments', 'yes' );
125: }
126: }
127:
128: function disable_comment_filter() {
129: if( function_exists('update_option') ) {
130: update_option( 'shrinky_comments', 'no' );
131: }
132: }
133:
134: function get_comment_filter() {
135: if( function_exists('get_option') ) {
136: $ret = get_option( 'shrinky_comments' );
137: }
138: return ($ret == 'yes');
139: }
140:
141: function enable_post_filter() {
142: if( function_exists('update_option') ) {
143: update_option( 'shrinky_posts', 'yes' );
144: }
145: }
146:
147: function disable_post_filter() {
148: if( function_exists('update_option') ) {
149: update_option( 'shrinky_posts', 'no' );
150: }
151: }
152:
153: function get_post_filter() {
154: if( function_exists('get_option') ) {
155: $ret = get_option('shrinky_posts');
156: }
157: return ($ret == 'yes');
158: }
159:
160: function enable_replace() {
161: if( function_exists('update_option') ) {
162: update_option( 'shrinky_replace', 'yes' );
163: }
164: }
165:
166: function disable_replace() {
167: if( function_exists('update_option') ) {
168: update_option( 'shrinky_replace', 'no' );
169: }
170: }
171:
172: function get_replace() {
173: if( function_exists('get_option') ) {
174: $ret = get_option('shrinky_replace');
175: }
176: return ($ret == 'yes');
177: }
178:
179: function set_replace_text($text) {
180: if( function_exists('update_option') ) {
181: update_option('shrinky_text', $text);
182: }
183: }
184:
185: function get_replace_text() {
186: if( function_exists('get_option') ) {
187: $ret = get_option('shrinky_text');
188: }
189: return $ret;
190: }
191:
192: function enable_trim() {
193: if( function_exists('update_option') ) {
194: update_option('shrinky_trim', 'yes');
195: }
196: }
197:
198: function disable_trim() {
199: if( function_exists('update_option') ) {
200: update_option('shrinky_trim', 'no');
201: }
202: }
203:
204: function get_trim() {
205: if( function_exists('get_option') ) {
206: $ret = get_option('shrinky_trim');
207: }
208: return ($ret == 'yes');
209: }
210:
211: function set_trim_length($length) {
212: if( function_exists('update_option') ) {
213: update_option('shrinky_size',$length);
214: }
215: }
216:
217: function get_trim_length() {
218: if( function_exists('get_option') ) {
219: $ret = get_option('shrinky_size');
220: }
221: return $ret;
222: }
223:
224: function enable_scheme_trim() {
225: if( function_exists('update_option') ) {
226: update_option('shrinky_scheme','yes');
227: }
228: }
229:
230: function disable_scheme_trim() {
231: if( function_exists('update_option') ) {
232: update_option('shrinky_scheme','no');
233: }
234: }
235:
236: function get_scheme_trim() {
237: if( function_exists('get_option') ) {
238: $ret = get_option('shrinky_scheme');
239: }
240: return ($ret == 'yes');
241: }
242:
243: function enable_www_trim() {
244: if( function_exists('update_option') ) {
245: update_option('shrinky_www','yes');
246: }
247: }
248:
249: function disable_www_trim() {
250: if( function_exists('update_option') ) {
251: update_option('shrinky_www', 'no');
252: }
253: }
254:
255: function get_www_trim() {
256: if( function_exists('get_option') ) {
257: $ret = get_option('shrinky_www');
258: }
259: return ($ret == 'yes');
260: }
261:
262: function enable_elipses() {
263: if( function_exists('update_option') ) {
264: update_option('shrinky_elipse','yes');
265: }
266: }
267:
268: function disable_elipses() {
269: if( function_exists('update_option') ) {
270: update_option('shrinky_elipse','no');
271: }
272: }
273:
274: function get_elipses() {
275: if( function_exists('get_option') ) {
276: $ret = get_option('shrinky_elipse');
277: }
278: return ($ret == 'yes');
279: }
280:
281: function enable_show_domain() {
282: if( function_exists('update_option') ) {
283: update_option('shrinky_domain','yes');
284: }
285: }
286:
287: function disable_show_domain() {
288: if( function_exists('update_option') ) {
289: update_option('shrinky_domain','no');
290: }
291: }
292:
293: function get_show_domain() {
294: if( function_exists('get_option') ) {
295: $ret = get_option('shrinky_domain');
296: }
297: return ($ret == 'yes');
298: }
299: }
300: ?>