|
1 <?php /* |
|
2 |
|
3 ************************************************************************** |
|
4 |
|
5 Plugin Name: Twitter Tools: bit.ly Links |
|
6 Plugin URI: http://www.viper007bond.com/wordpress-plugins/twitter-tools-bitly-links/ |
|
7 Version: 1.1.2 |
|
8 Description: Makes the links that <a href="http://wordpress.org/extend/plugins/twitter-tools/">Twitter Tools</a> posts to Twitter be API-created <a href="http://bit.ly/">bit.ly</a> links so you can track the number of clicks and such via your bit.ly account. Requires PHP 5.2.0+. |
|
9 Author: Viper007Bond |
|
10 Author URI: http://www.viper007bond.com/ |
|
11 |
|
12 **************************************************************************/ |
|
13 |
|
14 class TwitterToolsBitlyLinks { |
|
15 |
|
16 // Initalize the plugin by registering the hooks |
|
17 function __construct() { |
|
18 // Twitter Tools v2.0+ supports this natively. Don't do anything if that sub-plugin is active. |
|
19 if ( function_exists('aktt_bitly_shorten_url') ) |
|
20 return false; |
|
21 |
|
22 // Load localization domain |
|
23 load_plugin_textdomain( 'twitter-tools-bitly-links', false, '/twitter-tools-bitly-links/localization' ); |
|
24 |
|
25 // This plugin requires PHP 5.2.0+ and WordPress 2.7+ |
|
26 if ( function_exists('json_decode') && function_exists('wp_remote_retrieve_body') ) { |
|
27 // Register options |
|
28 add_option( 'viper_ttbl_login' ); |
|
29 add_option( 'viper_ttbl_apikey' ); |
|
30 |
|
31 // Register hooks |
|
32 add_action( 'admin_menu', array(&$this, 'register_settings_page') ); |
|
33 add_action( 'wp_ajax_viper_ttbl_check', array(&$this, 'ajax_authentication_check') ); |
|
34 add_filter( 'whitelist_options', array(&$this, 'whitelist_options') ); |
|
35 add_filter( 'tweet_blog_post_url', array(&$this, 'modify_url') ); |
|
36 |
|
37 // Make sure the user has filled in their login and API key |
|
38 $login = trim( get_option('viper_ttbl_login') ); |
|
39 $apikey = trim( get_option('viper_ttbl_apikey') ); |
|
40 if ( ( !$login || !$apikey ) && current_user_can('manage_options') ) |
|
41 add_action( 'admin_notices', array(&$this, 'settings_warn') ); |
|
42 } |
|
43 |
|
44 // Old version of PHP or WordPress? Let the user know. |
|
45 elseif ( current_user_can('activate_plugins') ) { |
|
46 add_action( 'admin_notices', array(&$this, 'old_version_warning') ); |
|
47 } |
|
48 } |
|
49 |
|
50 |
|
51 // Register the settings page |
|
52 function register_settings_page() { |
|
53 add_options_page( __('Twitter Tools: bit.ly Links', 'twitter-tools-bitly-links'), __('Twitter Tools: bit.ly', 'twitter-tools-bitly-links'), 'manage_options', 'twitter-tools-bitly-links', array(&$this, 'settings_page') ); |
|
54 } |
|
55 |
|
56 |
|
57 // Whitelist the options to allow saving via options.php |
|
58 function whitelist_options( $whitelist_options ) { |
|
59 $whitelist_options['twitter-tools-bitly-links'] = array( 'viper_ttbl_login', 'viper_ttbl_apikey' ); |
|
60 |
|
61 return $whitelist_options; |
|
62 } |
|
63 |
|
64 |
|
65 // Warn about an old version of PHP or WordPress |
|
66 function old_version_warning() { |
|
67 global $wp_version; |
|
68 echo '<div class="error"><p>' . sprintf( __( '<strong>Twitter Tools: bit.ly Links:</strong> You do not meet the minimum requirements of PHP %1$s and WordPress %2$s. You are currently using PHP %3$s and WordPress %4$s.', 'vipers-video-quicktags' ), '5.2.0', '2.7', PHP_VERSION, $wp_version ) . "</p></div>\n"; |
|
69 } |
|
70 |
|
71 |
|
72 // Display a notice telling the user to fill in their bit.ly details |
|
73 function settings_warn() { |
|
74 echo '<div class="error"><p>' . sprintf( __( '<strong>Twitter Tools: bit.ly Links:</strong> You must fill in your bit.ly details on the <a href="%s">settings page</a> in order for this plugin to function.', 'vipers-video-quicktags' ), admin_url('options-general.php?page=twitter-tools-bitly-links') ) . "</p></div>\n"; |
|
75 } |
|
76 |
|
77 |
|
78 // Modify the URL being sent to Twitter by Twitter Tools |
|
79 function modify_url( $url ) { |
|
80 |
|
81 // Make sure the user has filled in their login and API key |
|
82 $login = urlencode( strtolower( trim( get_option('viper_ttbl_login') ) ) ); |
|
83 $apikey = urlencode( trim( get_option('viper_ttbl_apikey') ) ); |
|
84 if ( empty($login) || empty($apikey) ) |
|
85 return $url; |
|
86 |
|
87 // Tell bit.ly to shorten the URL for us |
|
88 $response = wp_remote_retrieve_body( wp_remote_get( "http://api.bit.ly/shorten?version=2.0.1&format=json&history=1&login={$login}&apiKey={$apikey}&longUrl=" . urlencode( $url ) ) ); |
|
89 |
|
90 if ( empty($response) ) |
|
91 return $url; |
|
92 |
|
93 // Decode the response from bit.ly |
|
94 if ( !$response = json_decode( $response, true ) ) |
|
95 return $url; |
|
96 |
|
97 if ( !isset($response['errorCode']) || 0 != $response['errorCode'] || empty($response['results']) || empty($response['results'][$url]) || empty($response['results'][$url]['shortUrl']) ) |
|
98 return $url; |
|
99 |
|
100 return $response['results'][$url]['shortUrl']; |
|
101 } |
|
102 |
|
103 |
|
104 // Settings page |
|
105 function settings_page() { ?> |
|
106 |
|
107 <script type="text/javascript"> |
|
108 // <![CDATA[ |
|
109 function viper_ttbl_ajax() { |
|
110 jQuery("#viper_ttbl_status").load("<?php echo admin_url('admin-ajax.php'); ?>?nocache=" + Math.random(), { action: "viper_ttbl_check", login: jQuery("#viper_ttbl_login").val(), apikey: jQuery("#viper_ttbl_apikey").val() }); |
|
111 } |
|
112 jQuery(document).ready(function(){ viper_ttbl_ajax() }); |
|
113 jQuery("body").change(function(){ viper_ttbl_ajax() }); // I couldn't get anything but "body" to work for some reason |
|
114 // ]]> |
|
115 </script> |
|
116 |
|
117 <div class="wrap"> |
|
118 <?php screen_icon(); ?> |
|
119 <h2><?php _e( 'Twitter Tools: bit.ly Links Settings', 'twitter-tools-bitly-links' ); ?></h2> |
|
120 |
|
121 <form id="viper_ttbl_form" method="post" action="options.php"> |
|
122 <?php settings_fields('twitter-tools-bitly-links'); ?> |
|
123 |
|
124 <table class="form-table"> |
|
125 <tr valign="top"> |
|
126 <th scope="row"><label for="viper_ttbl_login"><?php _e( 'bit.ly Login', 'twitter-tools-bitly-links' ); ?></label></th> |
|
127 <td><input type="text" name="viper_ttbl_login" id="viper_ttbl_login" value="<?php form_option('viper_ttbl_login'); ?>" class="regular-text" /></td> |
|
128 </tr> |
|
129 <tr valign="top"> |
|
130 <th scope="row"><label for="viper_ttbl_apikey"><?php _e( 'bit.ly API Key', 'twitter-tools-bitly-links' ); ?></label></th> |
|
131 <td> |
|
132 <input type="text" name="viper_ttbl_apikey" id="viper_ttbl_apikey" value="<?php form_option('viper_ttbl_apikey'); ?>" class="regular-text" /> |
|
133 <span class="description"><?php printf( __( 'This can be found on your <a href="%s">account page</a>.', 'twitter-tools-bitly-links' ), 'http://bit.ly/account/' ); ?></span> |
|
134 </td> |
|
135 </tr> |
|
136 <tr valign="top" class="hide-if-no-js"> |
|
137 <th scope="row"><?php _e( 'API Status', 'twitter-tools-bitly-links' ); ?></th> |
|
138 <td style="font-size:1em"><span id="viper_ttbl_status"><em>Checking...</em></span></td> |
|
139 </tr> |
|
140 </table> |
|
141 |
|
142 <p class="submit"> |
|
143 <input type="submit" name="twitter-tools-bitly-links-submit" class="button-primary" value="<?php _e('Save Changes') ?>" /> |
|
144 </p> |
|
145 |
|
146 </form> |
|
147 </div> |
|
148 |
|
149 <?php |
|
150 } |
|
151 |
|
152 |
|
153 // Check the authentication details via AJAX |
|
154 function ajax_authentication_check() { |
|
155 // Make sure the user has filled in their login and API key |
|
156 $login = $apikey = false; |
|
157 if ( !empty($_POST['login']) ) |
|
158 $login = urlencode( strtolower( trim( $_POST['login'] ) ) ); |
|
159 if ( !empty($_POST['apikey']) ) |
|
160 $apikey = urlencode( trim( $_POST['apikey'] ) ); |
|
161 if ( empty($login) || empty($apikey) ) |
|
162 exit(); |
|
163 |
|
164 // Ask bit.ly for details about a random shortened URL in order to test the authentication details |
|
165 $response = wp_remote_retrieve_body( wp_remote_get( "http://api.bit.ly/expand?version=2.0.1&shortUrl=http://bit.ly/mnvj7&login={$login}&apiKey={$apikey}" ) ); |
|
166 |
|
167 if ( empty($response) ) |
|
168 exit( '<strong style="color:red">' . __('Failed to test credentials. Hmm.', 'twitter-tools-bitly-links') . '</strong>' ); |
|
169 |
|
170 // Decode the response from bit.ly |
|
171 if ( !$response = json_decode( $response, true ) ) |
|
172 exit( '<strong style="color:red">' . __('Failed to parse bit.ly API response. Hmm.', 'twitter-tools-bitly-links') . '</strong>' ); |
|
173 |
|
174 if ( !isset($response['errorCode']) || 0 != $response['errorCode'] ) |
|
175 exit( '<strong style="color:red">' . __('Your credentials are invalid. Please double-check them.', 'twitter-tools-bitly-links') . '</strong>' ); |
|
176 |
|
177 exit( '<strong style="color:green">' . __('Your credentials are valid.', 'twitter-tools-bitly-links') . '</strong>' ); |
|
178 } |
|
179 } |
|
180 |
|
181 // Start this plugin once all other plugins are fully loaded |
|
182 add_action( 'init', 'TwitterToolsBitlyLinks' ); function TwitterToolsBitlyLinks() { global $TwitterToolsBitlyLinks; $TwitterToolsBitlyLinks = new TwitterToolsBitlyLinks(); } |
|
183 |
|
184 ?> |