Please update your WordPress version!';
if ( version_compare( $wp_version,"2.5","<" )) {
exit ($exit_msg);
}
// Function checks for an alternate URL to be tweeted. Contribution by Bill Berry.
function external_or_permalink( $post_ID ) {
$wtb_extlink_custom_field = get_option('jd_twit_custom_url');
$perma_link = get_permalink( $post_ID );
$ex_link = get_post_meta($post_ID, $wtb_extlink_custom_field, true);
return ( $ex_link ) ? $ex_link : $perma_link;
}
// This function used to perform the API post to Twitter and now serves as a fallback.
function jd_old_doTwitterAPIPost( $twit, $authID=FALSE, $twitterURI="/statuses/update.xml" ) {
$host = 'twitter.com';
$port = 80;
$fp = @fsockopen($host, $port, $err_num, $err_msg, 10);
//check if user login details have been entered on admin page
if ($authID === FALSE || ( get_option( 'jd_individual_twitter_users' ) != '1' ) ) {
$thisLoginDetails = get_option( 'twitterlogin_encrypted' );
} else {
if ( ( get_usermeta( $authID, 'wp-to-twitter-enable-user' ) == 'true' || get_usermeta( $authID, 'wp-to-twitter-enable-user' ) == 'userTwitter' || get_usermeta( $authID, 'wp-to-twitter-enable-user' ) == 'userAtTwitter' ) && get_usermeta( $authID, 'wp-to-twitter-encrypted' )!="" ) {
$thisLoginDetails = get_usermeta( $authID, 'wp-to-twitter-encrypted' );
} else {
$thisLoginDetails = get_option( 'twitterlogin_encrypted' );
}
}
if ( $thisLoginDetails != '' ) {
if ( !is_resource($fp) ) {
#echo "$err_msg ($err_num)
\n"; // Fail Silently, but you could turn these back on...
return FALSE;
} else {
if (!fputs( $fp, "POST $twitterURI HTTP/1.1\r\n" )) {
return FALSE;
}
fputs( $fp, "Authorization: Basic ".$thisLoginDetails."\r\n" );
fputs( $fp, "User-Agent: ".$agent."\n" );
fputs( $fp, "Host: $host\n" );
fputs( $fp, "Content-type: application/x-www-form-urlencoded\n" );
fputs( $fp, "Content-length: ".strlen( $twit )."\n" );
fputs( $fp, "Connection: close\n\n" );
fputs( $fp, $twit );
for ( $i = 1; $i < 10; $i++ ) {
$reply = fgets( $fp, 256 );
}
fclose( $fp );
return TRUE;
}
} else {
// no username/password: return an empty string
return FALSE;
}
}
// This function performs the API post to Twitter
function jd_doTwitterAPIPost( $twit, $authID=FALSE ) {
global $version, $jd_plugin_url;
//check if user login details have been entered on admin page
if ($authID == FALSE || ( get_option( 'jd_individual_twitter_users' ) != '1' ) ) {
$thisuser = get_option( 'twitterlogin' );
$thispass = get_option( 'twitterpw' );
} else {
if ( ( get_usermeta( $authID, 'wp-to-twitter-enable-user' ) == 'true' || get_usermeta( $authID, 'wp-to-twitter-enable-user' ) == 'userTwitter' || get_usermeta( $authID, 'wp-to-twitter-enable-user' ) == 'userAtTwitter' ) && ( get_usermeta( $authID, 'wp-to-twitter-user-username' ) != "" && get_usermeta( $authID, 'wp-to-twitter-user-password' ) != "" ) ) {
$thisuser = get_usermeta( $authID, 'wp-to-twitter-user-username' );
$thispass = get_usermeta( $authID, 'wp-to-twitter-user-password' );
} else {
$thisuser = get_option( 'twitterlogin' );
$thispass = get_option( 'twitterpw' );
}
}
if ($thisuser == '' || $thispass == '' || $twit == '' ) {
return FALSE;
} else {
$twit = urldecode($twit);
$tweet = new Snoopy;
if (!empty($tweet)) {
$tweet->agent = 'WP to Twitter $jd_plugin_url';
$tweet->rawheaders = array(
'X-Twitter-Client' => 'WP to Twitter'
, 'X-Twitter-Client-Version' => $version
, 'X-Twitter-Client-URL' => 'http://www.joedolson.com/scripts/wp-to-twitter.xml'
);
$tweet->user = $thisuser;
$tweet->pass = $thispass;
$tweet->submit(
JDWP_API_POST_STATUS
, array(
'status' => $twit
, 'source' => 'wptotwitter'
)
);
if (strpos($tweet->response_code, '200')) {
return TRUE;
} else {
if (jd_old_doTwitterAPIPost( $twit, $authID ) == TRUE) {
return TRUE;
} else {
return FALSE;
}
}
} else {
if (jd_old_doTwitterAPIPost( $twit ) == TRUE) {
return TRUE;
} else {
// If you're attempting to solve the "settings page doesn't display" problem, you're in the wrong file.
// Please open /wp-to-twitter-manager.php to make your edits.
return FALSE;
}
}
}
}
// cURL query contributed by Thor Erik (http://thorerik.net)
function getfilefromurl($url) {
$ch = curl_init();
curl_setopt( $ch, CURLOPT_HEADER, 0 );
curl_setopt( $ch, CURLOPT_VERBOSE, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_URL, $url );
$output = curl_exec( $ch );
curl_close( $ch );
return $output;
}
function jd_truncate_tweet( $sentence, $thisposttitle, $thisblogtitle, $authID=FALSE ) {
if ( get_usermeta( $authID, 'wp-to-twitter-enable-user' ) == 'userAtTwitter' ) {
$at_append = "@" . get_option('twitterlogin');
} else if ( get_usermeta( $authID, 'wp-to-twitter-enable-user' ) == 'mainAtTwitter' ) {
$at_append = "@" . get_usermeta( $authID, 'wp-to-twitter-user-username' );
} else {
$at_append = "";
}
$sentence = $at_append . " " . $sentence;
if ( get_option( 'jd_twit_prepend' ) != "" ) {
$sentence = get_option( 'jd_twit_prepend' ) . " " . $sentence;
}
if ( get_option( 'jd_twit_append' ) != "" ) {
$sentence = $sentence . " " . get_option( 'jd_twit_append' );
}
$twit_length = strlen( $sentence );
$title_length = strlen( $thisposttitle );
$blog_length = strlen( $thisblogtitle );
if ( ( ( $twit_length + $title_length ) - 7 ) < 140 ) {
$sentence = str_ireplace( '#title#', $thisposttitle, $sentence );
$twit_length = strlen( $sentence );
} else {
$thisposttitle = substr( $thisposttitle, 0, ( 140- ( $twit_length-3 ) ) ) . "...";
$sentence = str_ireplace ( '#title#', $thisposttitle, $sentence );
$twit_length = strlen( $sentence );
}
if ( ( ( $twit_length + $blog_length ) - 6 ) < 140 ) {
$sentence = str_ireplace ( '#blog#',$thisblogtitle,$sentence );
$twit_length = strlen( $sentence );
} else {
$thisblogtitle = substr( $thisblogtitle, 0, ( 140-( $twit_length-3 ) ) ) . "...";
$sentence = str_ireplace ( '#blog#',$thisblogtitle,$sentence );
}
return $sentence;
}
function jd_shorten_link( $thispostlink, $thisposttitle ) {
$cligsapi = get_option( 'cligsapi' );
$bitlyapi = get_option( 'bitlyapi' );
$bitlylogin = get_option( 'bitlylogin' );
$snoopy = new Snoopy;
if ( ( get_option('twitter-analytics-campaign') != '' ) && ( get_option('use-twitter-analytics') == 1 ) ) {
$this_campaign = get_option('twitter-analytics-campaign');
if ( strpos( $thispostlink,"%3F" ) === FALSE) {
$thispostlink .= urlencode("?");
} else {
$thispostlink .= urlencode("&");
}
$thispostlink .= urlencode("utm_campaign=$this_campaign&utm_medium=twitter&utm_source=twitter");
}
// Generate and grab the clig using the Cli.gs API
// cURL alternative contributed by Thor Erik (http://thorerik.net)
switch ( get_option( 'jd_shortener' ) ) {
case 0:
case 1:
if ( $snoopy->fetchtext( "http://cli.gs/api/v1/cligs/create?t=snoopy&appid=WP-to-Twitter&url=".$thispostlink."&title=".$thisposttitle."&key=".$cligsapi ) ) {
$shrink = trim($snoopy->results);
} else {
$shrink = @file_get_contents( "http://cli.gs/api/v1/cligs/create?t=fgc&appid=WP-to-Twitter&url=".$thispostlink."&title=".$thisposttitle."&key=".$cligsapi);
}
if ( $shrink === FALSE ) {
$shrink = getfilefromurl( "http://cli.gs/api/v1/cligs/create?t=gffu&appid=WP-to-Twitter&url=".$thispostlink."&title=".$thisposttitle."&key=".$cligsapi);
}
break;
case 2:
if ( $snoopy->fetch( "http://api.bit.ly/shorten?version=2.0.1&longUrl=".$thispostlink."&login=".$bitlylogin."&apiKey=".$bitlyapi."&history=1" ) ) {
$shrink = $snoopy->results;
} else {
$shrink = @file_get_contents( "http://api.bit.ly/shorten?version=2.0.1&longUrl=".$thispostlink."&login=".$bitlylogin."&apiKey=".$bitlyapi."&history=1" );
}
if ( $shrink === FALSE ) {
$shrink = getfilefromurl( "http://api.bit.ly/shorten?version=2.0.1&longUrl=".$thispostlink."&login=".$bitlylogin."&apiKey=".$bitlyapi."&history=1" );
}
$decoded = json_decode($shrink,TRUE);
$shrink = $decoded['results'][urldecode($thispostlink)]['shortUrl'];
//die(print_r($decoded['results'][urldecode($thispostlink)]));
break;
case 3:
$shrink = $thispostlink;
}
if ( $shrink === FALSE || ( stristr( $shrink, "http://" ) === FALSE )) {
update_option( 'wp_url_failure','1' );
$shrink = $thispostlink;
}
return $shrink;
}
function jd_twit( $post_ID ) {
$jd_tweet_this = get_post_meta( $post_ID, 'jd_tweet_this', TRUE);
if ( $jd_tweet_this == "yes" ) {
$get_post_info = get_post( $post_ID );
$authID = $get_post_info->post_author;
$thisposttitle = urlencode( stripcslashes( strip_tags( $_POST['post_title'] ) ) );
if ($thisposttitle == "") {
$thisposttitle = urlencode( stripcslashes( strip_tags( $_POST['title'] ) ) );
}
$thispostlink = urlencode( external_or_permalink( $post_ID ) );
$thisblogtitle = urlencode( get_bloginfo( 'name' ) );
$cligsapi = get_option( 'cligsapi' );
$sentence = '';
$customTweet = stripcslashes( $_POST['jd_twitter'] );
$oldClig = get_post_meta( $post_ID, 'wp_jd_clig', TRUE );
if (($get_post_info->post_status == 'publish' || $_POST['publish'] == 'Publish') && ($_POST['prev_status'] == 'draft' || $_POST['original_post_status'] == 'draft')) {
// publish new post
if ( get_option( 'newpost-published-update' ) == '1' ) {
$sentence = stripcslashes( get_option( 'newpost-published-text' ) );
if ( get_option( 'newpost-published-showlink' ) == '1' ) {
if ($oldClig != '') {
$shrink = $oldClig;
} else {
$shrink = jd_shorten_link( $thispostlink, $thisposttitle, $cligsapi );
}
//$sentence = $sentence . " " . $shrink;
if ( stripos( $sentence, "#url#" ) === FALSE ) {
$sentence = $sentence . " " . $shrink;
} else {
$sentence = str_ireplace( "#url#", $shrink, $sentence );
}
if ( $customTweet != "" ) {
if ( get_option( 'newpost-published-showlink' ) == '1' ) {
if ( stripos( $customTweet, "#url#" ) === FALSE ) {
$sentence = $customTweet . " " . $shrink;
} else {
$sentence = str_ireplace( "#url#", $shrink, $customTweet );
}
} else {
$sentence = $customTweet;
}
}
$sentence = jd_truncate_tweet( $sentence, $thisposttitle, $thisblogtitle, $authID );
// Stores the posts CLIG in a custom field for later use as needed.
store_url( $post_ID, $shrink );
} else {
$sentence = jd_truncate_tweet( $sentence, $thisposttitle, $thisblogtitle, $authID );
}
}
} else if ( (( $_POST['originalaction'] == "editpost" ) && ( ( $_POST['prev_status'] == 'publish' ) || ($_POST['original_post_status'] == 'publish') ) ) && $get_post_info->post_status == 'publish') {
// if this is an old post and editing updates are enabled
if ( get_option( 'oldpost-edited-update') == '1' || get_option( 'jd_twit_edited_pages' ) == '1' ) {
$sentence = stripcslashes( get_option( 'oldpost-edited-text' ) );
if ( get_option( 'oldpost-edited-showlink') == '1') {
if ( $oldClig != '' ) {
$old_post_link = $oldClig;
} else {
$old_post_link = jd_shorten_link( $thispostlink, $thisposttitle );
}
store_url( $post_ID, $old_post_link );
if ( stripos( $sentence, "#url#" ) === FALSE ) {
$sentence = $sentence . " " . $old_post_link;
} else {
$sentence = str_ireplace( "#url#", $old_post_link, $sentence );
}
if ( $customTweet != "" ) {
if ( get_option( 'oldpost-edited-showlink') == '1' ) {
if ( stripos( $customTweet, "#url#" ) === FALSE ) {
$sentence = $customTweet . " " . $old_post_link;
} else {
$sentence = str_ireplace( "#url#", $old_post_link, $customTweet );
}
} else {
$sentence = $customTweet;
}
}
$sentence = jd_truncate_tweet( $sentence, $thisposttitle, $thisblogtitle, $authID );
}
}
}
if ( get_option( 'use_tags_as_hashtags' ) == '1' ) {
$sentence = $sentence . " " . generate_hash_tags( $post_ID );
}
if ( $sentence != '' ) {
$sendToTwitter = jd_doTwitterAPIPost( $sentence, $authID );
if ( $sendToTwitter === FALSE ) {
add_post_meta( $post_ID,'jd_wp_twitter',urldecode( $sentence ) );
update_option( 'wp_twitter_failure','1' );
}
}
}
return $post_ID;
}
// Add Tweets on links in Blogroll
function jd_twit_link( $link_ID ) {
global $version;
$thislinkprivate = $_POST['link_visible'];
if ($thislinkprivate != 'N') {
$thislinkname = urlencode( stripcslashes( $_POST['link_name'] ) );
$thispostlink = urlencode( $_POST['link_url'] ) ;
$thislinkdescription = urlencode( stripcslashes( $_POST['link_description'] ) );
$sentence = '';
# || (get_option( 'jd-use-link-title' ) == '1' && $thislinkname == '')
if ( (get_option( 'jd-use-link-description' ) == '1' && $thislinkdescription == '') ) {
$sentence = stripcslashes( get_option( 'newlink-published-text' ) );
} else {
if ( get_option( 'jd-use-link-description' ) == '1' && get_option ( 'jd-use-link-title' ) == '0' ) {
$sentence = $thislinkdescription;
} else if ( get_option( 'jd-use-link-description' ) == '0' && get_option ( 'jd-use-link-title' ) == '1' ) {
$sentence = $thislinkname;
}
}
if (strlen($sentence) > 120) {
$sentence = substr($sentence,0,116) . '...';
}
// Generate and grab the clig using the Cli.gs API
// cURL alternative contributed by Thor Erik (http://thorerik.net)
$shrink = jd_shorten_link( $thispostlink, $thislinkname );
if ( stripos($sentence,"#url#") === FALSE ) {
$sentence = $sentence . " " . $shrink;
} else {
$sentence = str_ireplace("#url#",$shrink,$sentence);
}
if ( $sentence != '' ) {
$sendToTwitter = jd_doTwitterAPIPost( $sentence );
if ($sendToTwitter === FALSE) {
update_option('wp_twitter_failure','2');
}
}
return $link_ID;
} else {
return '';
}
}
// HANDLES SCHEDULED POSTS
function jd_twit_future( $post_ID ) {
$post_ID = $post_ID->ID;
$get_post_info = get_post( $post_ID );
$jd_tweet_this = get_post_meta( $post_ID, 'jd_tweet_this', TRUE );
$post_status = $get_post_info->post_status;
if ( $jd_tweet_this == "yes" ) {
$thispostlink = urlencode( external_or_permalink( $post_ID ) );
$thisposttitle = urlencode( strip_tags( $get_post_info->post_title ) );
$authID = $get_post_info->post_author;
$thisblogtitle = urlencode( get_bloginfo( 'name' ) );
$sentence = '';
$customTweet = get_post_meta( $post_ID, 'jd_twitter', TRUE );
$sentence = stripcslashes(get_option( 'newpost-published-text' ));
if ( get_option( 'newpost-published-showlink' ) == '1' ) {
$shrink = jd_shorten_link( $thispostlink, $thisposttitle );
if ( stripos($sentence,"#url#") === FALSE ) {
$sentence = $sentence . " " . $shrink;
} else {
$sentence = str_ireplace("#url#",$shrink,$sentence);
}
if ( $customTweet != "" ) {
// Get the custom Tweet message if it's been supplied. Truncate it to fit if necessary.
if ( get_option( 'newpost-published-showlink' ) == '1' ) {
if ( ( strlen( $customTweet ) + 21) > 140 ) {
$customTweet = substr( $customTweet, 0, 119 );
}
} else {
if ( strlen( $customTweet ) > 140 ) {
$customTweet = substr( $customTweet, 0, 140 );
}
}
if ( get_option( 'newpost-published-showlink' ) == '1' ) {
if ( stripos( $customTweet, "#url#" ) === FALSE ) {
$sentence = $customTweet . " " . $shrink;
} else {
$sentence = str_ireplace( "#url#", $shrink, $customTweet );
}
} else {
$sentence = $customTweet;
}
}
$sentence = jd_truncate_tweet( $sentence, $thisposttitle, $thisblogtitle, $authID );
// Stores the post's short URL in a custom field for later use as needed.
store_url($post_ID, $shrink);
} else {
$sentence = jd_truncate_tweet( $sentence, $thisposttitle, $thisblogtitle, $authID );
}
if ( get_option( 'use_tags_as_hashtags' ) == '1' ) {
$sentence = $sentence . " " . generate_hash_tags( $post_ID );
}
if ( $sentence != '' ) {
$sendToTwitter = jd_doTwitterAPIPost( $sentence, $authID );
if ($sendToTwitter === FALSE) {
add_post_meta( $post_ID,'jd_wp_twitter',urldecode($sentence) );
update_option( 'wp_twitter_failure','1' );
}
}
return $post_ID;
}
} // END jd_twit_future
// Tweet from QuickPress (no custom fields, so can't control whether to tweet.)
function jd_twit_quickpress( $post_ID ) {
$post_ID = $post_ID->ID;
$get_post_info = get_post( $post_ID );
$post_status = $get_post_info->post_status;
$thispostlink = urlencode( external_or_permalink( $post_ID ) );
$thisposttitle = urlencode( strip_tags( $get_post_info->post_title ) );
$authID = $get_post_info->post_author;
$thisblogtitle = urlencode( get_bloginfo( 'name' ) );
$sentence = '';
$customTweet = get_post_meta( $post_ID, 'jd_twitter', TRUE );
$sentence = stripcslashes(get_option( 'newpost-published-text' ));
if ( get_option( 'newpost-published-showlink' ) == '1' ) {
$shrink = jd_shorten_link( $thispostlink, $thisposttitle );
if ( stripos($sentence,"#url#") === FALSE ) {
$sentence = $sentence . " " . $shrink;
} else {
$sentence = str_ireplace("#url#",$shrink,$sentence);
}
$sentence = jd_truncate_tweet($sentence, $thisposttitle, $thisblogtitle, $authID);
// Stores the posts CLIG in a custom field for later use as needed.
store_url($post_ID, $shrink);
} else {
$sentence = jd_truncate_tweet( $sentence, $thisposttitle, $thisblogtitle, $authID );
}
if ( get_option( 'use_tags_as_hashtags' ) == '1' ) {
$sentence = $sentence . " " . generate_hash_tags( $post_ID );
}
if ( $sentence != '' ) {
$sendToTwitter = jd_doTwitterAPIPost( $sentence, $authID );
if ($sendToTwitter === FALSE) {
add_post_meta( $post_ID,'jd_wp_twitter',urldecode($sentence) );
update_option( 'wp_twitter_failure','1' );
}
}
return $post_ID;
} // END jd_twit_quickpress
// HANDLES xmlrpc POSTS
function jd_twit_xmlrpc( $post_ID ) {
$get_post_info = get_post( $post_ID );
$post_status = $get_post_info->post_status;
if ( get_option('oldpost-edited-update') != 1 && get_post_meta ( $post_ID, 'wp_jd_clig', TRUE ) != '' ) {
return;
} else {
if ( get_option('jd_tweet_default') != '1' && get_option('jd_twit_remote') == '1' ) {
$authID = $get_post_info->post_author;
$thispostlink = urlencode( external_or_permalink( $post_ID ) );
$thisposttitle = urlencode( strip_tags( $get_post_info->post_title ) );
$thisblogtitle = urlencode( get_bloginfo( 'name' ) );
$sentence = '';
$sentence = stripcslashes(get_option( 'newpost-published-text' ));
if ( get_option( 'newpost-published-showlink' ) == '1' ) {
$shrink = jd_shorten_link( $thispostlink, $thisposttitle );
if ( stripos($sentence,"#url#") === FALSE) {
$sentence = $sentence . " " . $shrink;
} else {
$sentence = str_ireplace("#url#",$shrink,$sentence);
}
// Check the length of the tweet and truncate parts as necessary.
$sentence = jd_truncate_tweet( $sentence, $thisposttitle, $thisblogtitle, $authID );
// Stores the posts CLIG in a custom field for later use as needed.
store_url($post_ID, $shrink);
} else {
$sentence = jd_truncate_tweet( $sentence, $thisposttitle, $thisblogtitle, $authID );
}
if ( get_option( 'use_tags_as_hashtags' ) == '1' ) {
$sentence = $sentence . " " . generate_hash_tags( $post_ID );
}
if ( $sentence != '' ) {
$sendToTwitter = jd_doTwitterAPIPost( $sentence, $authID );
$sendToTwitter = jd_doTwitterAPIPost( $sentence, $authID );
if ($sendToTwitter === FALSE) {
add_post_meta( $post_ID,'jd_wp_twitter',urldecode($sentence));
update_option('wp_twitter_failure','1');
}
}
}
return $post_ID;
}
} // END jd_twit_xmlrpc
// NEW IF ADD
add_action('admin_menu','jd_add_twitter_outer_box');
function store_url($post_ID, $url) {
if ( get_option( 'jd_shortener' ) == 0 || get_option( 'jd_shortener' ) == 1) {
if ( get_post_meta ( $post_ID, 'wp_jd_clig', TRUE ) != $url ) {
add_post_meta ( $post_ID, 'wp_jd_clig', $url );
}
} elseif ( get_option( 'jd_shortener' ) == 2 ) {
if ( get_post_meta ( $post_ID, 'wp_jd_bitly', TRUE ) != $url ) {
add_post_meta ( $post_ID, 'wp_jd_bitly', $url );
}
}
}
function generate_hash_tags($post_ID) {
if ( version_compare( $wp_version,"2.8",">=" )) {
$tags = $_POST['tax_input']['post_tag'] . "," . $_POST['newtag']['post_tag'];
} else {
$tags = $_POST['tags_input'];
}
$tags = explode(",",$tags);
foreach ($tags as $value) {
$value = trim($value);
if ($value != "Add new tag") {
$hashtags .= "#$value ";
}
}
if (strlen($hashtags) <= 1) {
$hashtags = "";
}
return $hashtags;
}
function jd_add_twitter_old_box() {
?>