Categories
Ping.fm Programming Projects Technology Thought Experiments TweetSuite Twitter Wordpress

TweetSuite WordPress Plugin + Ping.fm (Preview)

A couple weeks ago, I found out that Twitter had a Search feature that showed real-time tweets for a keyword. I gave it a little thought, considered what it would take to actually write the software – and then wised up, and decided to see if someone already did the hard work.

Sure enough, Dan Zarrella over at danzarrella.com had. He wrote one for Tweetbacks, and then expanded on it with TweetSuite. So I gave them a shot.

I started with Tweetbacks on the FreeformFrog.com Blog and everything seemed to be working fine – until one day when the Tweetbacks stopped. It just stopped finding them – even though I knew they were getting tweeted – because I was using Ping.fm to syndicate my blog posts to the appropriate social networks.

I gave it a couple weeks, and then decided I was going to fix it. I was tired of not having my TweetBacks working – especially during my efforts pushing a Social Networking campaign at job.

So, I added @danzarrella, and asked…

@danzarrella do you have plans to integrate ping.fm posting in TweetSuite? If not, mind if I take a crack at it?
from @neotsn at  from web

A few minutes later, I got a response…

@neotsn go to town
from @danzarrella at

  1. Make it post through Ping.fm – I syndicate all my stuff through twitterfeed.com and it pushes everything out through Ping.fm for me. Upon further investigation, I noticed that TweetSuite used its own publishing code to push out the updates – after it created its own shortened urls and attached them. So of course it would never find my updates – it didn’t know what urls to search for, because it didn’t create them.
  2. Make it find my tweets that were pushed through Ping.fm – After realizing that it stopped searching for blog titles and started searching for the shortened urls as the unique identifier on the web, I realized that I needed it to create my urls and push them out with my Ping.fm update text. That was the only way to let TweetSuite know that there were tweets out there about my blog, and obtw here’s the link.
  3. Clean up some of the things that were a little messy – Once I got poking around in the code, I noticed that it some parts of it were written a little redundantly, and in order for me to maintain understanding of what was going on, I needed to clean it up a little.

Let’s Code…

Well, for starters, to accomplish #1, I had to write a function that would validate the user’s app key with Ping.fm:
{code type=php}
/* [[Neo]] */
//BEGIN Ping.fm functions
function pingfm_verify() {
// request app key verification
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, ‘http://api.ping.fm/v1/user.validate’);
curl_setopt($ch, CURLOPT_POSTFIELDS, Array(
‘api_key’ => get_option(‘tweetsuite_ping_api_key’),
‘user_app_key’ => get_option(‘tweetsuite_ping_app_key’)
));
$output = curl_exec($ch);
// update database with response
if (preg_match(‘/OK/’, $output)) {
echo ‘ <strong>Your key has been verified.</strong> Your can now post to your
<a href=”http://www.ping.fm” target=”_blank”>Ping.fm</a> account.’;
update_option(‘tweetsuite_ping_keyVerified’, 1);
} else {
echo (‘<strong>Your key could not be verified.</strong>.’);
update_option(‘tweetsuite_ping_keyVerified’, 0);
return false;
}
}

// END Ping.fm Functions
{/code}
Just your basic curl function to contact http://api.ping.fm/v1/user.validate and pass the api_key and user_app_key to the api, per the API Documentation on user.validate. Depending on the response, store it to the database, and display the appropriate message.

Next, I had to implement the fields to make that happen. We’ll start with the database…
{code type=php}
/* [[Neo]] */
//ADDED:
add_option(‘tweetsuite_ping_api_key’,’333333333333333333333333333333333′);
add_option(‘tweetsuite_ping_app_key’,”);
add_option(‘tweetsuite_use_ping’,0);
add_option(‘tweetsuite_ping_keyVerified’,0);
//END ADD
{/code}
This sticks those fields into the wordpress database table “wp_options” where all the config values go. My developer’s api key, a blank user application key, a field for the checkbox “Do you want to use Ping.fm?” and one for whether or not the user_app_key successfully verified.

Then I create the user interface for those fields (as well as rearrange the rest of the fields since these have to flow right to make sense…
{code type=php}
/* [[Neo]] */
//ADDED:
update_option(‘tweetsuite_ping_app_key’, $_POST[‘tweetsuite_ping_app_key’]);
//END ADD

//tweetsuite_use_ping
($_POST[‘tweetsuite_use_ping’]) ? update_option(‘tweetsuite_use_ping’, 1) : update_option(‘tweetsuite_use_ping’, 0);
{/code}

That part allows the database to be updated with the data from the fields below…
{code type=html}
<!– /* [[Neo]] */ –>
<!– BEGIN ADD –>
<tr valign=”top”>
<th scope=”row”>Ping.fm :: Use Ping.fm to publish new posts (via your default method)?:</th>
<td><INPUT TYPE=CHECKBOX NAME=”tweetsuite_use_ping” <?php if(get_option(‘tweetsuite_use_ping’)) { echo “checked”; } ?>>
<br /><b>[“Send a Tweet when you publish” is required for this to work]</b>
</td>
</tr>
<tr valign=”top”>
<th scope=”row”>Ping.fm :: Your Ping.fm <a href=”http://ping.fm/key/” target=”_blank”>Application Key</a>:</th>
<td><INPUT TYPE=text NAME=”tweetsuite_ping_app_key” value='<?php echo get_option(‘tweetsuite_ping_app_key’); ?>’ /><br /><?php if(get_option(‘tweetsuite_ping_app_key’)) { echo pingfm_verify(); } ?>
</td>
</tr>
<!– END ADD –>
{/code}
This is below the checkbox for “Send a Tweet when you publish a post?”…if that is checked, then if you check “Use Ping.fm to publish new posts (via your default method)?” and fill out the next field for “Your Ping.fm Application Key” then it will send your post details on Publish through Ping.fm instead of Twitter itself, appending the tinyurl associated with the post to your Ping.fm message. All this is done by editing the function that sends the tweets…
{code type=php}
function tweetsuite_send($msg) {
/* [[Neo]] */
//REMOVED:
//$prefix = urlencode(get_option(‘tweetsuite_prefix’).’ ‘);
//REPLACED:
$prefix = (get_option(tweetsuite_use_ping) and get_option(tweetsuite_ping_keyVerified)) ? get_option(‘tweetsuite_prefix’).’ ‘ : urlencode(get_option(‘tweetsuite_prefix’).’ ‘);
$msg = $prefix.$msg;

/* [[Neo]] */
//ADDED
if(get_option(tweetsuite_use_ping) and get_option(tweetsuite_ping_keyVerified)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, ‘http://api.ping.fm/v1/user.post’);
curl_setopt($ch, CURLOPT_POSTFIELDS, Array(
‘api_key’ => get_option(‘tweetsuite_ping_api_key’),
‘user_app_key’ => get_option(‘tweetsuite_ping_app_key’),
‘post_method’ => “default”,
‘body’ => $msg)
);
$output = curl_exec($ch);
} else {
//END ADD
$username = get_option(‘tweetsuite_twitter_username’);
$password = get_option(‘tweetsuite_twitter_password’);
if(($username) and ($password)) {
$url = ‘http://twitter.com/statuses/update.xml’;
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, “$url”);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, “status=$msg”);
curl_setopt($curl_handle, CURLOPT_USERPWD, “$username:$password”);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
}
/* [[Neo]] */
//ADDED
}
//END ADD
}
{/code}
This starts off by swapping out the original urlencode() function for a conditional statement deciding if this is a ping.fm post or a twitter post. If it’s a ping.fm post, then we don’t want to urlencode() the prefix, because it sticks + signs where there should be spaces in the prefix to the title. However, if we don’t want to use ping.fm, then don’t mess with the original code.

Following that, it splits which method we used to post, based on the same criteria – make sure the checkbox is true for “tweetsuite_use_ping” and that the user’s app key is verified in “tweetsuite_ping_keyVerified”. If that’s the case, then post it through ping.fm – if any of that is not met, then go ahead and try to post it through twitter. If there is no username and password supplied on the options page, then we just won’t post anything.

Now…about the whole $msg – that part got skipped in the logic of everything. This was the tricky part, for me. Originally, I stumbled across an API Documentation page for the ping.fm method “url.shorten” and that was the very thing I needed – something to grab a ping.fm url, without passing it through ping.fm yet. However, I found out, after much frustration, that this method was mysteriously “depreciated” (which should be read as “edited off the page, and 404’d when you try to use it). But, I found another solution in the knowledgebase at Ping.fm…
{code type=php}
function ts_send_tweet($postID) {
global $wpdb;
if(!is_int($postID))
$postID = $postID->ID;

$table_name = $wpdb->prefix . “shorturls”;
$line = $wpdb->get_row(“select * from $table_name where postID=$postID”);
if($line->postID==$postID) {
/* [[Neo]] */
// ADDED: “*”.
$shorturl = “*”.$line->tinyurl;
$post = get_post($postID);
if(get_option(‘tweetsuite_send_posts’))
tweetsuite_send(trim($post->post_title).’ ‘.$shorturl);
}
}
{/code}
The article said you can prepend a * to the front of a url and it would not shorten it (in addition to the fact that they said they wouldn’t re-shorten any URLs 24 characters or smaller). So, I went with the * solution, because we’re already providing short URLs, but don’t want to risk the functionality in future updates to allow for custom url shortening services.

And that accomplishes both #1 and #2. We have the TweetSuite posting its updates through the Ping.fm API, and pushing a shortened url out with the post. TweetSuite then becomes aware of this url at publish, and stores it in its database. Then the cron job that runs every 5 minutes searches on any version of the URL that was stored and pulls back all the entries from the search.twitter.com Atom feed – parses them, and updates the database with them.

The rest of the stuff I did in the plugin was merely cleaning up…for example, reading the shortened urls from the Atom feed. The original code was:
{code type=php}
if(stristr($tweet, ‘http://bit.ly’)) {
if(strstr($tweet, $bitly)) {
$add = true;
} else {
$add = false;
}
}
{/code}
Because the urls are using alphanumeric sequences to track what link is what – and the url’s are case-sensitive, it was returning links that were not related to the post. For example, I got someone’s tweet about Rush Limbaugh because the bit.ly url (“http://bit.ly/fyhz”)was the same letters, but in a different case then my original “http://bit.ly/FyhZ”. So I changed the order of the search, did it for all the services currently supported, and cleaned up the code a little:
{code type=php}…
if(strstr($tweet, ‘http://bit.ly’)) { $add = (stristr($tweet, $bitly)) ? true : false; } else {

{/code}
The function strstr() is not case-sensitive, and that’s ok for a search on the domain name – we want anything that looks close to “bit.ly” to return positive. However, once it does, it’s imperitive that we use the case-sensitive stristr() function to return a positive result only if the full url matches what we have in the database case-for-case.

Wrapping up…

I made a couple more additions to the code beyond that, some of them required for functionality, some of them for cosmetics. I’ve submitted my plugin version to Ping.fm to approve it, and take my Developer’s Key out of “Pending” status. Once that’s done, I’ll send it off to Dan Zarrella to take a look and see how he wants to move forward. Then I’ll be able to post the plugin for download (definitely here, but also on the WordPress Plugin directory.

Continuing on…

I have read through the comments on Dan’s blog from the people, and have taken note of the things that they are asking for. After reading the code, he had already been working on some of the features. There are 5 main ones that people are asking for, and I’ve already done one of them in this release:

  1. TweetThis link opens in a new window.
  2. AutoUpgrade via WordPress Plugin Directory
  3. Allow customized URL Shortening Services
  4. TweetThis links for pre-installation posts
  5. TweetThis links on the home page.

I’ll be looking more into the other options and see what I can do.

One More Thing…

This post was syndicated through the TweetSuite + Ping.fm plugin 🙂 .

By [[Neo]]

I am a web programmer, system integrator, and photographer. I have been writing code since high school, when I had only a TI-83 calculator. I enjoy getting different systems to talk to each other, coming up with ways to mimic human processes using technology, and explaining how complicated things work.

Of my many blogs, this one is purely about the technology projects, ideas, and solutions that I have come across in my internet travels. It's also the place for technical updates related to my other sites that are part of The-Spot.Network.

2 replies on “TweetSuite WordPress Plugin + Ping.fm (Preview)”

Unfortunately, I did not. However, the folks working on the PingPressFM plugin have said they fixed the PingPressFM plugin, which does everything but the tweetsuite portion, in one of my other posts [here]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.