status updates to Ping.fm or Twitter everytime you publish a post, using own domain or others for shortened permalinks. Like it? Donate | Amazon Wishlist | Silk icons by FAMFAMFAM
Author: Lopo Lencastre de Almeida - iPublicis.com
Version: 1.3.1
Author URI: http://www.ipublicis.com
Donate link: http://smsh.me/7kit
*/
/*
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 .
*/
/*
Changelog:
From now on, see the readme for changelog.
*/
// setting some internal information
$shorten2ping_dirname = plugin_basename(dirname(__FILE__));
$shorten2ping_url = WP_PLUGIN_URL . '/' . $shorten2ping_dirname;
$donate = 'donation';
//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($linktext="") {
global $post;
$short_permalink = get_post_meta($post->ID, 'short_url', 'true');
if ($linktext == 'linktext') {
$linktext = $short_permalink;
} elseif (empty($linktext)) {
$linktext = __('Short URL','shorten2ping');
}
$post_title = strip_tags($post->post_title);
// Using rel="shorturl" as proposed at http://wiki.snaplog.com/short_url
if (!empty($short_permalink)) echo "" . $linktext . "";
}
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 fb_thumb_in_head() {
global $post;
$fb_thumbnail = get_post_meta($post->ID, 'fb_img', true);
if (is_single($post->ID) && !empty($fb_thumbnail)) {
echo "\n";
echo "\n";
} else {
//Get images attached to the post
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
$fb_thumbnail = wp_get_attachment_url( $attachment->ID );
}
echo "\n";
echo "\n";
}
}
}
function shorten2ping_published_post($post)
{
// get user ID to use in multi author blogs.
global $user_ID;
get_currentuserinfo();
if ( $post->post_type != 'post' ) return; // dont ping pages
$post_id = $post->ID;
$s2p_options = get_option('shorten2ping_options_'.$user_ID);
$pingfm_user_key = $s2p_options['pingfm_key'];
$post_url = get_permalink($post_id);
$post_title = strip_tags($post->post_title);
$short_url_exists = get_post_meta($post_id, 'short_url', true);
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'] == 'yourls') {
$yourls_api = $s2p_options['yourls_api'];
$yourls_user = $s2p_options['yourls_user'];
$yourls_pass = $s2p_options['yourls_pass'];
$short_url = make_yourls($post_id,$post_url,$yourls_api,$yourls_user,$yourls_pass);
} elseif ($s2p_options['shorten_service'] == 'none') {
$short_url = $post_url;
}
elseif ($s2p_options['shorten_service'] == 'supr') {
$supr_key = $s2p_options['supr_key'];
$supr_user = $s2p_options['supr_user'];
$short_url = make_supr($post_id,$post_url,$supr_key,$supr_user);
}
elseif ($s2p_options['shorten_service'] == 'selfdomain') {
$s2p_blog_url = get_bloginfo(url);
$short_url = $s2p_blog_url . '/?p=' . $post_id;
add_post_meta($post_id, 'short_url', $short_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'){
send_pingfm($pingfm_user_key,$post_id,$message);
} 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;
} elseif ($s2p_options['ping_service'] == 'both') {
send_pingfm($pingfm_user_key,$post_id,$message);
send_twit($post_id,$s2p_options['twitter_user'], $s2p_options['twitter_pass'], $message);
}
}
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() {
// get user ID to use in multi author blogs.
global $user_ID;
get_currentuserinfo();
// create options array. if options already exists add_option function does nothing.
$s2p_options['message'] = "New post, \"[title]\" - [link]";
$s2p_options['ping_service'] = "pingfm";
$s2p_options['pingfm_key'] = "";
$s2p_options['twitter_user'] = "";
$s2p_options['twitter_pass'] = "";
$s2p_options['shorten_service'] = "smsh";
$s2p_options['bitly_user'] = "";
$s2p_options['bitly_key'] = "";
$s2p_options['trim_user'] = "";
$s2p_options['trim_pass'] = "";
$s2p_options['yourls_api'] = "";
$s2p_options['yourls_user'] = "";
$s2p_options['yourls_pass'] = "";
$s2p_options['supr_user'] = "";
$s2p_options['supr_key'] = "";
// removed old 1.0 non array settings to array migration
add_option('shorten2ping_options_'.$user_ID, $s2p_options );
// check if plugin installed is previous to 1.2.x, and add new options in that case
$existing_s2p_options = get_option('shorten2ping_options');
$old_settings = count($existing_s2p_options);
if (empty($existing_s2p_options)) {
delete_option('shorten2ping_options');
add_option('shorten2ping_options_'.$user_ID, $s2p_options );
} elseif ($old_settings == 10) {
$s2p_new_options = array ("yourls_api" => "", "yourls_user" => "", "yourls_pass" => "", "supr_user" => "", "supr_key" => "");
$merged_options = array_merge($existing_s2p_options, $s2p_new_options);
delete_option ('shorten2ping_options');
add_option('shorten2ping_options_'.$user_ID, $merged_options );
}
}
function shorten2ping_options_subpanel()
{
// get user ID to use in multi author blogs.
global $donate, $user_ID;
get_currentuserinfo();
$s2p_options = get_option('shorten2ping_options_'.$user_ID);
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_options_'.$user_ID, $s2p_options );
echo '
' . __('Settings saved.') . '
';
}
?>
|
|
' . __('Settings','shorten2ping') . '';
array_unshift( $links, $settings_link ); // before other links
return $links;
}
// Funtion to send 'status' to Ping.fm. Based on the one by Sold Out Activist for the pingPressFM
function send_pingfm($pingfm_user_key,$post_id,$message) {
if (!$pingfm_user_key) return false;
$post_data = Array(
'api_key' => '6f604abd220a79bcd443a4824354734d',
'user_app_key' => $pingfm_user_key,
'post_method' => 'status',
'body' => $message,
// 'debug' => 1
);
// 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]));
//only for debugging. not needed to work.
// add_post_meta($post_id, 'pinged', $ping_result);
// 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']);
}
}
// 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']);
}
}
function make_yourls ($post_id,$post_url,$yourls_api,$yourls_user,$yourls_pass) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'Shorten2PING');
curl_setopt($ch, CURLOPT_URL, $yourls_api);
curl_setopt($ch, CURLOPT_HEADER, 0); // No header in the result
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return, do not echo result
curl_setopt($ch, CURLOPT_POST, 1); // This is a POST request
curl_setopt($ch, CURLOPT_POSTFIELDS, array( // Data to POST
'url' => $post_url,
'format' => 'json',
'action' => 'shorturl',
'username' => $yourls_user,
'password' => $yourls_pass
));
$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'] == 'success') {
add_post_meta($post_id, 'short_url', $json['shorturl']);
return $json['shorturl'];
} else {
add_post_meta($post_id, 'yourls_error', $json['message']);
}
}
function make_supr($post_id,$post_url,$supr_key,$supr_user) {
// create API URL
$supr_result = 'http://su.pr/api/shorten?longUrl='.$post_url.'&login='.$supr_user.'&apiKey='.$supr_key;
// get the surl
$ch=curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'Shorten2PING');
curl_setopt($ch,CURLOPT_URL, $supr_result);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$json = json_decode($supr_result,true);
// check if all goes ok, if not, return error message
if ($json['statusCode'] == 'OK') {
add_post_meta($post_id, 'short_url', $json['results'][$post_url]['shortUrl']);
return $json['results'][$post_url]['shortUrl'];
} else {
add_post_meta($post_id, 'supr_error', $json['errorMessage']);
}
}
// Another function for making tr.im, but using simple method -NOT USED, only for testing-
function make_simple_trim($url,$user,$pass) {
// create API URL
$trim_url = 'http://api.tr.im/api/trim_simple?url='.urlencode($url).'&username='.$user.'&password='.$pass;
// get the surl
$ch=curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'Shorten2PING');
curl_setopt($ch,CURLOPT_URL, $trim_url);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $trim_url;
}
// Function to shorten post URL using sm00sh at smsh.me
function make_sm00sh($url) {
// create API URL
$sm00sher = 'http://smsh.me/?id='.sha1(get_bloginfo('wpurl')).'&api=json&url='.urlencode($url);
// get the surl
$ch=curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'Shorten2PING');
curl_setopt($ch,CURLOPT_URL, $sm00sher);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// returns { "title": "HTTP/1.0 200 OK", "body": "http://smsh.me/7jk1" }
$json = json_decode($response,true);
// check if all goes ok, if not, return error message
if ($json['title'] == 'HTTP/1.0 200 OK') {
add_post_meta($post_id, 'short_url', $json['body']);
return $json['body'];
} else {
add_post_meta($post_id, 'smsh_error', $json['title']."\n".$json['body']);
}
}
// tabs for options page
function s2p_admin_js() { // options js
global $shorten2ping_url;
wp_enqueue_script('jquery-ui-tabs');
wp_enqueue_script('s2p_tabs_js', $shorten2ping_url . '/includes/s2p_admin.js', array('jquery-ui-tabs'));
}
function s2p_admin_css() { // options css
global $shorten2ping_url;
wp_enqueue_style('s2p_tabs_css', $shorten2ping_url . '/includes/s2p_admin.css');
}
// remove wordpress stats wp.me shorlink creation if present.
if ( !function_exists('remove_wpme') ) {
add_action( 'plugins_loaded', 'remove_wpme' );
function remove_wpme() {
if ( function_exists('shortlink_wp_head') ) {
remove_action('wp_head', 'shortlink_wp_head');
remove_action('wp', 'shortlink_header');
remove_filter( 'get_sample_permalink_html', 'get_shortlink_html');
}
}
}
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_filter( 'plugin_action_links_'.plugin_basename(__FILE__), 'shorten2ping_add_settings_link', -10);
add_action('wp_head', 'short_url_head');
add_action('wp_head', 'fb_thumb_in_head');
?>