status updates to Ping.fm or Twitter everytime you publish a post. Using Bit.ly or Tr.im for the permalinks (accounts for these services required). Author: Samuel Aguilera Version: 1.1.1 Author URI: http://www.samuelaguilera.com */ /* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License version 3 as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ /* Originally based on WordTwit 1.2 by Duane Storey. PingFM function based on one by Sold Out Activistclass for the pingPressFM plugin, and Bit.ly function by David Walsh & Jason Lengstorf. HISTORY: 1.0 - Initial release. 1.0.1 - Fixed a typo and Ping.fm developer approved. 1.0.2 - Replaced PHPingFM by Dmitri Gaskin for a single function based on the one by Sold Out Activist for the pingPressFM. - Removed 'includes' directory (now only one function for each service) and merged functions into shorten2ping.php file. 1.1 - Store settings in an array to reduce calls to database. - Some other minor improvements/cleanup in the code. - Added support for http://tr.im. - Added support for Twitter. - Added options for choose shorten and notification services, and to turn off them (so you can use Shorten2Ping only for notification if your domain is already short enough for you, or use Shorten2Ping only to get shortened urls for your posts). 1.1.1 - Changed action hook. Now the ping function is called only when post changed status from X to publish (where X can be new, draft, pending or future). So now, if you edit an old post (that you published before installing Shorten2Ping) this plugin does nothing. */ // setting some internal information $shorten2ping_plugin_prefix = 'shorten2ping_'; $shorten2ping_dirname = plugin_basename(dirname(__FILE__)); //load translation file if any for the current language load_plugin_textdomain('shorten2ping', PLUGINDIR . '/' . $shorten2ping_dirname . '/locale'); // simple function to use in your theme if you want to show the short url for the current post function short_permalink() { global $post; $short_permalink = get_post_meta($post->ID, 'short_url', 'true'); // Using rel="shorturl" as proposed at http://wiki.snaplog.com/short_url if (!empty($short_permalink)) echo "post_title\">" . __('Short URL','shorten2ping') . ""; } function short_url_head() { global $post; $short_permalink = get_post_meta($post->ID, 'short_url', 'true'); if (is_single($post->ID) && !empty($short_permalink)) { echo "\n"; echo "\n"; } } function shorten2ping_published_post($post) { global $shorten2ping_plugin_prefix; $post_id = $post->ID; $s2p_options = get_option($shorten2ping_plugin_prefix . 'options'); $post_url = get_permalink($post_id); $post_title = get_the_title($post_id); $has_been_pinged = get_post_meta($post_id, 'pinged', true); $short_url_exists = get_post_meta($post_id, 'short_url', true); if (!($has_been_pinged == 'yes')) { if (empty($short_url_exists)) { if ($s2p_options['shorten_service'] == 'bitly') { //acortamos la url del post con bit.ly $bitly_user = $s2p_options['bitly_user']; $bitly_key = $s2p_options['bitly_key']; $short_url = make_bitly_url($post_id,$post_url,$bitly_user,$bitly_key); } elseif ($s2p_options['shorten_service'] == 'trim') { $trim_user = $s2p_options['trim_user']; $trim_pass = $s2p_options['trim_pass']; $short_url = make_trim($post_id,$post_url,$trim_user,$trim_pass); } elseif ($s2p_options['shorten_service'] == 'none') { $short_url = $post_url; } } else { $short_url = $short_url_exists; } //get message from settings and process title and link $message = $s2p_options['message']; $message = str_replace('[title]', $post_title, $message); $message = str_replace('[link]', $short_url, $message); if ($s2p_options['ping_service'] == 'pingfm'){ $pingfm_user_key = $s2p_options['pingfm_key']; $post_data = Array( 'api_key' => '6f604abd220a79bcd443a4824354734d', 'user_app_key' => $pingfm_user_key, 'post_method' => 'status', 'body' => $message, // 'debug' => 1 ); send_pingfm($post_id,$post_data); } elseif ($s2p_options['ping_service'] == 'twitter') { send_twit($post_id,$s2p_options['twitter_user'], $s2p_options['twitter_pass'], $message); } elseif ($s2p_options['ping_service'] == 'none') { return; } } } function s2p_bnc_stripslashes_deep($value) { $value = is_array($value) ? array_map('s2p_bnc_stripslashes_deep', $value) : stripslashes($value); return $value; } function s2c_init_options() { // create options array. if options already exists add_option function does nothing. $s2p_options['message'] = "New Blog Entry, \"[title]\" - [link]"; $s2p_options['ping_service'] = "pingfm"; $s2p_options['pingfm_key'] = ""; $s2p_options['twitter_user'] = ""; $s2p_options['twitter_pass'] = ""; $s2p_options['shorten_service'] = "bitly"; $s2p_options['bitly_user'] = ""; $s2p_options['bitly_key'] = ""; $s2p_options['trim_user'] = ""; $s2p_options['trim_pass'] = ""; // check if old options exists and update to the new system foreach( $s2p_options as $key => $value ) { if($existing = get_option('shorten2ping_'. $key)) { $s2p_options[$key] = $existing; delete_option('shorten2ping_' . $key); } } add_option('shorten2ping_options', $s2p_options ); } function shorten2ping_options_subpanel() { global $shorten2ping_plugin_prefix; $s2p_options = get_option($shorten2ping_plugin_prefix . 'options'); if (get_magic_quotes_gpc()) { $_POST = array_map('s2p_bnc_stripslashes_deep', $_POST); $_GET = array_map('s2p_bnc_stripslashes_deep', $_GET); $_COOKIE = array_map('s2p_bnc_stripslashes_deep', $_COOKIE); $_REQUEST = array_map('s2p_bnc_stripslashes_deep', $_REQUEST); } if (isset($_POST['info_update'])) { foreach( $s2p_options as $key => $value ) { if(isset($_POST[$key])) { if ($_POST[$key] == $_POST['message']) { $s2p_options[$key] = stripslashes($_POST['message']); } else { $s2p_options[$key] = $_POST[$key]; } } else { $s2p_options[$key] = ''; } } update_option( $shorten2ping_plugin_prefix . 'options', $s2p_options ); echo '

' . __('Settings saved.') . '

'; } // $s2p_options = get_option($shorten2ping_plugin_prefix . 'options'); echo('
'); ?>





API key here.','shorten2ping') ?>

Twitter doesn\'t have API keys for users, so you must put here your user login and password if you want to use this service.','shorten2ping') ?>

Bit.ly API key.','shorten2ping') ?>

Tr.im doesn\'t have API keys for users, so you must put here your user login and password if you want to use this service.','shorten2ping') ?>

'); } function shorten2ping_add_plugin_option() { $shorten2ping_plugin_name = 'Shorten2Ping'; if (function_exists('add_options_page')) { add_options_page($shorten2ping_plugin_name, $shorten2ping_plugin_name, 0, basename(__FILE__), 'shorten2ping_options_subpanel'); } } // Funtion to send 'status' to Ping.fm. Based on the one by Sold Out Activist for the pingPressFM function send_pingfm($post_id,$post_data=false) { if (!$post_data) return false; // send data to ping.fm $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, 'Shorten2Ping'); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_URL, 'http://api.ping.fm/v1/'. 'user.post'); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); $output = curl_exec($ch); // if ok, stores the ping_id if (preg_match('/OK/', $output)) { preg_match('/\([^\<]*)\<\/transaction\>/', $output, $match); $ping_result = addslashes(trim($match[1])); add_post_meta($post_id, 'pinged', 'yes'); // if not ok, stores the error message } else { preg_match('/\([^\<]*)\<\/message\>/', $output, $match); $ping_result = addslashes(trim($match[1])); add_post_meta($post_id, 'pingfm_error', $ping_result); } } function send_twit ($post_id,$twitter_user,$twitter_pass,$message) { $twitter_host = "http://twitter.com/statuses/update.json?status=" . urlencode(stripslashes(urldecode($message))); $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, 'Shorten2Ping'); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); //Use basic authentication curl_setopt($ch, CURLOPT_USERPWD, "$twitter_user:$twitter_pass"); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //Do not check SSL certificate (but use SSL). curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_URL, $twitter_host); $result = curl_exec($ch); $json = json_decode($result,true); if ($json['error']) { add_post_meta($post_id, 'twitter_error', $json['error']); } else { add_post_meta($post_id, 'pinged', 'yes'); } } // Original code by David Walsh (http://davidwalsh.name/bitly-php), improved by Jason Lengstorf (http://www.ennuidesign.com/). function make_bitly_url($post_id, $url, $login, $appkey, $history=1, $version='2.0.1') { //create the URL $bitly = 'http://api.bit.ly/shorten'; $param = 'version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$appkey.'&format=json&history='.$history; //get the url $ch = curl_init(); curl_setopt($ch, CURLOPT_USERAGENT, 'Shorten2Ping'); curl_setopt($ch, CURLOPT_URL, $bitly . "?" . $param); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); curl_close($ch); $json = json_decode($response,true); // check if all goes ok, if not, return error message if ($json['statusCode'] == 'OK') { add_post_meta($post_id, 'short_url', $json['results'][$url]['shortUrl']); return $json['results'][$url]['shortUrl']; } else { add_post_meta($post_id, 'bitly_error', $json['errorMessage']); } } // Function to shorten post URL using tr.im function make_trim($post_id, $url, $trim_user, $trim_pass) { //create the URL $trim = 'http://api.tr.im/api/trim_url.json'; $param = '?url='.urlencode($url); //get the url $ch = curl_init(); curl_setopt($ch, CURLOPT_USERAGENT, 'Shorten2Ping'); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); //Use basic authentication curl_setopt($ch,CURLOPT_USERPWD,$trim_user . ":" . $trim_pass); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //Do not check SSL certificate (but use SSL). curl_setopt($ch, CURLOPT_URL, $trim . $param); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); curl_close($ch); $json = json_decode($response,true); // check if all goes ok, if not, return error message if ($json['status']['result'] == 'OK') { add_post_meta($post_id, 'short_url', $json['url']); return $json['url']; } else { add_post_meta($post_id, 'trim_error', $json['status']['message']); } } // Another function for making tr.im, but using simple method -NOT USED, only for testing- function make_simple_trim($url,$user,$pass) { $trim_url = file_get_contents('http://api.tr.im/api/trim_simple?url='.urlencode($url).'&username='.$user.'&password='.$pass); return $trim_url; } register_activation_hook( __FILE__, 's2c_init_options' ); add_action('new_to_publish', 'shorten2ping_published_post'); add_action('draft_to_publish', 'shorten2ping_published_post'); add_action('pending_to_publish', 'shorten2ping_published_post'); add_action('future_to_publish', 'shorten2ping_published_post'); add_action('admin_menu', 'shorten2ping_add_plugin_option'); add_action('wp_head', 'short_url_head'); ?>