web/wp-content/plugins/twitter-tools/twitter-tools-bitly.php
branchwordpress
changeset 109 03b0d1493584
equal deleted inserted replaced
-1:000000000000 109:03b0d1493584
       
     1 <?php
       
     2 /*
       
     3 Plugin Name: Twitter Tools - Bit.ly URLs 
       
     4 Plugin URI: http://crowdfavorite.com/wordpress/ 
       
     5 Description: Use Bit.ly for URL shortening with Twitter Tools. This plugin relies on Twitter Tools, configure it on the Twitter Tools settings page.
       
     6 Version: 2.0 
       
     7 Author: Crowd Favorite
       
     8 Author URI: http://crowdfavorite.com
       
     9 */
       
    10 
       
    11 // ini_set('display_errors', '1'); ini_set('error_reporting', E_ALL);
       
    12 
       
    13 if (!defined('PLUGINDIR')) {
       
    14 	define('PLUGINDIR','wp-content/plugins');
       
    15 }
       
    16 
       
    17 load_plugin_textdomain('twitter-tools-bitly');
       
    18 
       
    19 define('AKTT_BITLY_API_SHORTEN_URL', 'http://api.bit.ly/shorten');
       
    20 define('AKTT_BITLY_API_VERSION', '2.0.1');
       
    21 
       
    22 function aktt_bitly_shorten_url($url) {
       
    23 	$parts = parse_url($url);
       
    24 	if ($parts['host'] != 'bit.ly') {
       
    25 		$snoop = get_snoopy();
       
    26 		$json = new Services_JSON();
       
    27 		$api = AKTT_BITLY_API_SHORTEN_URL.'?version='.AKTT_BITLY_API_VERSION.'&longUrl='.urlencode($url);
       
    28 		$login = get_option('aktt_bitly_api_login');
       
    29 		$key = get_option('aktt_bitly_api_key');
       
    30 		if (!empty($login) && !empty($key)) {
       
    31 			$api .= '&login='.urlencode($login).'&apiKey='.urlencode($key).'&history=1';
       
    32 		}
       
    33 		$snoop->agent = 'Twitter Tools http://alexking.org/projects/wordpress';
       
    34 		$snoop->fetch($api);
       
    35 		$result = $json->decode($snoop->results);
       
    36 		$url = $result->results->{$url}->shortUrl;
       
    37 	}
       
    38 	return $url;
       
    39 }
       
    40 add_filter('tweet_blog_post_url', 'aktt_bitly_shorten_url');
       
    41 
       
    42 function aktt_bitly_shorten_tweet($tweet) {
       
    43 	if (strpos($tweet->tw_text, 'http') !== false) {
       
    44 		preg_match_all('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', $tweet->tw_text, $urls);
       
    45 		if (isset($urls[0]) && count($urls[0])) {
       
    46 			foreach ($urls[0] as $url) {
       
    47 // borrowed from WordPress's make_clickable code
       
    48 				if ( in_array(substr($url, -1), array('.', ',', ';', ':', ')')) === true ) {
       
    49 					$url = substr($url, 0, strlen($url)-1);
       
    50 				}
       
    51 				$tweet->tw_text = str_replace($url, aktt_bitly_shorten_url($url), $tweet->tw_text);
       
    52 			}
       
    53 		}
       
    54 	}
       
    55 	return $tweet;
       
    56 }
       
    57 add_filter('aktt_do_tweet', 'aktt_bitly_shorten_tweet');
       
    58 
       
    59 function aktt_bitly_request_handler() {
       
    60 	if (!empty($_GET['cf_action'])) {
       
    61 		switch ($_GET['cf_action']) {
       
    62 
       
    63 		}
       
    64 	}
       
    65 	if (!empty($_POST['cf_action'])) {
       
    66 		switch ($_POST['cf_action']) {
       
    67 
       
    68 			case 'aktt_bitly_update_settings':
       
    69 				aktt_bitly_save_settings();
       
    70 				wp_redirect(trailingslashit(get_bloginfo('wpurl')).'wp-admin/options-general.php?page=twitter-tools.php&updated=true');
       
    71 				die();
       
    72 				break;
       
    73 		}
       
    74 	}
       
    75 }
       
    76 add_action('init', 'aktt_bitly_request_handler');
       
    77 
       
    78 $aktt_bitly_settings = array(
       
    79 	'aktt_bitly_api_login' => array(
       
    80 		'type' => 'string',
       
    81 		'label' => __('Bit.ly Username', 'twitter-tools-bitly'),
       
    82 		'default' => '',
       
    83 		'help' => '',
       
    84 	),
       
    85 	'aktt_bitly_api_key' => array(
       
    86 		'type' => 'string',
       
    87 		'label' => __('Bit.ly API key', 'twitter-tools-bitly'),
       
    88 		'default' => '',
       
    89 		'help' => '',
       
    90 	),
       
    91 );
       
    92 
       
    93 function aktt_bitly_setting($option) {
       
    94 	$value = get_option($option);
       
    95 	if (empty($value)) {
       
    96 		global $aktt_bitly_settings;
       
    97 		$value = $aktt_bitly_settings[$option]['default'];
       
    98 	}
       
    99 	return $value;
       
   100 }
       
   101 
       
   102 if (!function_exists('cf_settings_field')) {
       
   103 	function cf_settings_field($key, $config) {
       
   104 		$option = get_option($key);
       
   105 		if (empty($option) && !empty($config['default'])) {
       
   106 			$option = $config['default'];
       
   107 		}
       
   108 		$label = '<label for="'.$key.'">'.$config['label'].'</label>';
       
   109 		$help = '<span class="help">'.$config['help'].'</span>';
       
   110 		switch ($config['type']) {
       
   111 			case 'select':
       
   112 				$output = $label.'<select name="'.$key.'" id="'.$key.'">';
       
   113 				foreach ($config['options'] as $val => $display) {
       
   114 					$option == $val ? $sel = ' selected="selected"' : $sel = '';
       
   115 					$output .= '<option value="'.$val.'"'.$sel.'>'.htmlspecialchars($display).'</option>';
       
   116 				}
       
   117 				$output .= '</select>'.$help;
       
   118 				break;
       
   119 			case 'textarea':
       
   120 				$output = $label.'<textarea name="'.$key.'" id="'.$key.'">'.htmlspecialchars($option).'</textarea>'.$help;
       
   121 				break;
       
   122 			case 'string':
       
   123 			case 'int':
       
   124 			default:
       
   125 				$output = $label.'<input name="'.$key.'" id="'.$key.'" value="'.htmlspecialchars($option).'" />'.$help;
       
   126 				break;
       
   127 		}
       
   128 		return '<div class="option">'.$output.'<div class="clear"></div></div>';
       
   129 	}
       
   130 }
       
   131 
       
   132 function aktt_bitly_settings_form() {
       
   133 	global $aktt_bitly_settings;
       
   134 
       
   135 	print('
       
   136 <div class="wrap">
       
   137 	<h2>'.__('Bit.ly for Twitter Tools', 'twitter-tools-bitly').'</h2>
       
   138 	<form id="aktt_bitly_settings_form" name="aktt_bitly_settings_form" action="'.get_bloginfo('wpurl').'/wp-admin/options-general.php" method="post">
       
   139 		<input type="hidden" name="cf_action" value="aktt_bitly_update_settings" />
       
   140 		<fieldset class="options">
       
   141 	');
       
   142 	foreach ($aktt_bitly_settings as $key => $config) {
       
   143 		echo cf_settings_field($key, $config);
       
   144 	}
       
   145 	print('
       
   146 		</fieldset>
       
   147 		<p class="submit">
       
   148 			<input type="submit" name="submit" class="button-primary" value="'.__('Save Settings', 'twitter-tools-bitly').'" />
       
   149 		</p>
       
   150 	</form>
       
   151 </div>
       
   152 	');
       
   153 }
       
   154 add_action('aktt_options_form', 'aktt_bitly_settings_form');
       
   155 
       
   156 function aktt_bitly_save_settings() {
       
   157 	if (!current_user_can('manage_options')) {
       
   158 		return;
       
   159 	}
       
   160 	global $aktt_bitly_settings;
       
   161 	foreach ($aktt_bitly_settings as $key => $option) {
       
   162 		$value = '';
       
   163 		switch ($option['type']) {
       
   164 			case 'int':
       
   165 				$value = intval($_POST[$key]);
       
   166 				break;
       
   167 			case 'select':
       
   168 				$test = stripslashes($_POST[$key]);
       
   169 				if (isset($option['options'][$test])) {
       
   170 					$value = $test;
       
   171 				}
       
   172 				break;
       
   173 			case 'string':
       
   174 			case 'textarea':
       
   175 			default:
       
   176 				$value = stripslashes($_POST[$key]);
       
   177 				break;
       
   178 		}
       
   179 		update_option($key, $value);
       
   180 	}
       
   181 }
       
   182 
       
   183 
       
   184 if (!function_exists('get_snoopy')) {
       
   185 	function get_snoopy() {
       
   186 		include_once(ABSPATH.'/wp-includes/class-snoopy.php');
       
   187 		return new Snoopy;
       
   188 	}
       
   189 }
       
   190 
       
   191 //a:21:{s:11:"plugin_name";s:29:"Bit.ly URLs for Twitter Tools";s:10:"plugin_uri";s:35:"http://crowdfavorite.com/wordpress/";s:18:"plugin_description";s:49:"Use Bit.ly for URL shortening with Twitter Tools.";s:14:"plugin_version";s:3:"1.0";s:6:"prefix";s:10:"aktt_bitly";s:12:"localization";s:19:"twitter-tools-bitly";s:14:"settings_title";s:24:"Bit.ly for Twitter Tools";s:13:"settings_link";s:24:"Bit.ly for Twitter Tools";s:4:"init";b:0;s:7:"install";b:0;s:9:"post_edit";b:0;s:12:"comment_edit";b:0;s:6:"jquery";b:0;s:6:"wp_css";b:0;s:5:"wp_js";b:0;s:9:"admin_css";b:0;s:8:"admin_js";b:0;s:15:"request_handler";s:1:"1";s:6:"snoopy";s:1:"1";s:11:"setting_cat";b:0;s:14:"setting_author";b:0;}
       
   192 
       
   193 ?>