- 19-11-2009 - added configuration option for URL shortener - added configuration option for post prefix - Replace WP smart quotes by plain ones. (from WP Laconica Tools) - Don't resend a post that was already submitted (from WP Laconica Tools) Version 1.1.1: Patrick Vande Walle - 19-05-2010 - added configuration option for Twitter hash tags */ function hellotxtpost_install() { global $wpdb; $table_name = $wpdb->prefix."hellotxt"; if($wpdb->get_var("show tables like '$table_name'") != $table_name) { $sql = "CREATE TABLE " . $table_name . " (id mediumint(9) NOT NULL AUTO_INCREMENT, hellotxt_user_key VARCHAR(100) NOT NULL, hellotxt_group VARCHAR(20) NOT NULL, );"; } require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } function send_to_hellotxt($post_ID) { // don't resend $has_been_sent = get_post_meta($post_ID, 'sent_to_hellotxt', true); if ($has_been_sent != 'yes') { $app_key = '1GNntqlC6X7UtVwj'; $user_key = get_option('hellotxt_user_key'); $group = get_option('hellotxt_group'); $posted = get_post($post_ID); $ch = curl_init(); // initialize curl handle curl_setopt($ch, CURLOPT_URL,"http://hellotxt.com/api/v1/method/user.post"); // set url to post to curl_setopt($ch, CURLOPT_FAILONERROR, 1); //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable curl_setopt($ch, CURLOPT_TIMEOUT, 3); // times out after 4s curl_setopt($ch, CURLOPT_POST, 1); // set POST method curl_setopt($ch, CURLOPT_POSTFIELDS, "app_key=".$app_key. "&user_key=".$user_key. "&body=".get_option('hellotxt_post_prefix').fix_quote_entities($posted->post_title).' - *'.shorten_url($posted->guid).' '.get_option('hellotxt_post_hashtags'). "&group=".$group. "&"); // add POST fields $result = curl_exec($ch); // run the whole process curl_close($ch); add_post_meta($post_ID, 'sent_to_hellotxt', 'yes'); //add meta to say it was posted } } function shorten_url($long_url) { $shortened_url = ''; $url = get_option('hellotxt_url_stem').urlencode($long_url); $chst = curl_init(); curl_setopt($chst, CURLOPT_URL, $url); # return the transfer as a string curl_setopt($chst, CURLOPT_RETURNTRANSFER, 1); $shortened_url = curl_exec($chst); curl_close($chst); return $shortened_url; } /* * In the database, quotes are stored as is but when pulled out, WP replaces with cute * named entities, like left and right quotes. This introduces ampersands which truncate * the post field sent to Laconica. Just urlencoding these isn't helpful because they * are escaped in Laconica and appear as things like “ literally on the page. * Changing them into plain old quotes, single and double, works best. */ function fix_quote_entities($title) { // this only handles the common entities, the ones that are guessable from // their name $title = html_entity_decode($title, ENT_QUOTES); // WordPress uses unicode escapes, preferentially $title = str_replace("’", "'", $title); $title = str_replace("“", '"', $title); $title = str_replace("”", '"', $title); $title = str_replace("…", '...', $title); $title = str_replace("–", '--', $title); $title = str_replace("&", '&', $title); return $title; } function hellotxtpost_menu() { add_options_page('HelloTxt Post Options', 'HelloTxt Post', 8, __FILE__, 'hellotxtpost_options'); } function hellotxtpost_options() { if (get_option('hellotxt_url_stem')== FALSE){ add_option('hellotxt_url_stem', 'http://pra.im/api.php?url='); } if (get_option('hellotxt_post_prefix')== FALSE){ add_option('hellotxt_post_prefix','New blog post: '); } if (get_option('hellotxt_post_hashtags')== FALSE){ add_option('hellotxt_post_hashtags','#fb'); } echo '
'; echo '

HelloTxt post

'; echo '
'; echo wp_nonce_field('update-options'); echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo '
HelloTxt User Key
Group
URL shortener string:
Post prefix:
Post hash tags:
'; echo ''; echo ''; echo '

'; echo ''; echo '

'; echo '
'; echo '
'; } // Create the tables // register_activation_hook(__FILE__,'hellotxtpost_install'); // Create the admin menu add_action('admin_menu', 'hellotxtpost_menu'); // Create the publish post action add_action('publish_post', 'send_to_hellotxt'); ?>