Twitpress を改造して Wassrpress (仮)にする

2008年7月4日

現在、マイクロブログは Twitter をメインに呟いていますが、なんだか今日あたり、Wassr への流れができていたので、アカウント作ってみました。

WordPress から Twitter への更新通知のポストは、Twitpress で行えていますが、Wassr も API が公開されているので、これを改造して対応できないかといじってみたらできたみたいなので、パッチを置いておきます。

適当にやったので関数名とかDB名の頭が大文字になっちゃってますが、気持ち悪いようなら適宜小文字に変更するなどして下さい。というか、置換で大文字小文字区別しなかったせいですががが。

ちょっと長くなったので、続きを読むの後に置いておきます。素人が適当にいじったせいで動作保証も何もあったものではないので、書き換えた場所だけチェックして自力で改造するのが良いと思います(^^;

一応、動くことは確認してありますが、ご利用の際は注意してください。

Wassbacker

Wassbacker を使えば、ブログツールから Ping を送信するだけで通知できるので、こちらのサービスにパスワード等を預けることを許容できるなら、こちらを利用するのも良いかもしれません。

— twitpress.php 2008-07-03 21:17:32.000000000 +0900
+++ wassrpress.php 2008-07-03 21:36:15.000000000 +0900
@@ -1,8 +1,8 @@
post_status );
+ Wassrpress_db_update_post( $postID, $post->post_status );
}
//process the posts, including twittering newly published posts
– twitpress_process_posts();
+ Wassrpress_process_posts();
}

-//Install the twitpress database
-function twitpress_install() {
+//Install the Wassrpress database
+function Wassrpress_install() {

global $wpdb;
– $table_name = “twitpress";
+ $table_name = “Wassrpress";

– twitpress_db_drop_table();
+ Wassrpress_db_drop_table();

if( $wpdb->get_var( “SHOW TABLES LIKE '$table_name'" ) != $table_name ) {
$sql = “CREATE TABLE " . $table_name . " (
id mediumint(9) NOT NULL,
– status enum( 'publish’, 'draft’, 'private’, 'static’, 'object’, 'attachment’, 'inherit’, 'future’, 'twittered’ ) NOT NULL,
+ status enum( 'publish’, 'draft’, 'private’, 'static’, 'object’, 'attachment’, 'inherit’, 'future’, 'wassrered’ ) NOT NULL,
UNIQUE KEY id (id)
);";

@@ -70,7 +70,7 @@

dbDelta( $sql );

– add_option( “twitpress_db_version", “0.3.2" );
+ add_option( “Wassrpress_db_version", “0.3.2" );
}

//Populate table to avoid twittering previously published posts when they are edited
@@ -78,17 +78,17 @@
$posts = $wpdb->get_results( $sql, OBJECT );
foreach ($posts as $post){
if ( $post->post_status == “publish" ){
– twitpress_db_update_post( $post->ID, “twittered" );
+ Wassrpress_db_update_post( $post->ID, “wassrered" );
} else {
– twitpress_db_update_post( $post->ID, $post->post_status );
+ Wassrpress_db_update_post( $post->ID, $post->post_status );
}
}
}

-//Delete twitpress table
-function twitpress_db_drop_table() {
+//Delete Wassrpress table
+function Wassrpress_db_drop_table() {
global $wpdb;
– $table_name = “twitpress";
+ $table_name = “Wassrpress";
if( $wpdb->get_var( “SHOW TABLES LIKE '$table_name'" ) != $table_name ) {
$sql = “DROP TABLE " . $table_name;
}
@@ -101,13 +101,13 @@
dbDelta( $sql );
}

-//Add / update a twitpress record of a post
-function twitpress_db_update_post( $postID, $status ){
+//Add / update a Wassrpress record of a post
+function Wassrpress_db_update_post( $postID, $status ){
global $wpdb;
– $table_name = “twitpress";
+ $table_name = “Wassrpress";

//delete any old record of this entry (could use update instead)
– twitpress_db_delete_post( $postID );
+ Wassrpress_db_delete_post( $postID );

//insert the new version of the record
$query = “INSERT INTO " . $table_name .
@@ -118,9 +118,9 @@
}

//Get a wordpress post status
-function twitpress_db_get_post_status( $postID ){
+function Wassrpress_db_get_post_status( $postID ){
global $wpdb;
– $table_name = “twitpress";
+ $table_name = “Wassrpress";

//find post by id
$query = “SELECT id, status from " . $wpdb->posts .
@@ -129,65 +129,65 @@
}

//Check if a post was previously twittered
-function twitpress_was_twittered( $postID ){
+function Wassrpress_was_wassrered( $postID ){
global $wpdb;
– $table_name = “twitpress";
+ $table_name = “Wassrpress";

$query = “SELECT id, status from " . $table_name .
" WHERE id = " . $wpdb->escape( $postID );

– return $wpdb->get_var( $query, 1 ) == 'twittered’;
+ return $wpdb->get_var( $query, 1 ) == 'wassrered’;
}

-//Delete a twitpress post status (n.b this doesnt delete any wordpress posts :) )
-function twitpress_db_delete_post( $postID ){
+//Delete a Wassrpress post status (n.b this doesnt delete any wordpress posts :) )
+function Wassrpress_db_delete_post( $postID ){
global $wpdb;
– $table_name = “twitpress";
+ $table_name = “Wassrpress";

$query = “DELETE FROM " . $table_name .
" WHERE id like " . $wpdb->escape( $postID );
return $wpdb->query( $query );
}

-//Process posts currently stored in the twitpress database table
-function twitpress_process_posts() {
+//Process posts currently stored in the Wassrpress database table
+function Wassrpress_process_posts() {
global $wpdb;
– $table_name = “twitpress";
+ $table_name = “Wassrpress";

$query = “SELECT * FROM " . $table_name;

//get the currently stored posts
$tp_rows = $wpdb->get_results( $query, ARRAY_A );

– //for each entry in the twitpress table
+ //for each entry in the Wassrpress table
foreach( $tp_rows as $row ){
$tp_wp_status = $row[“status"];
//if the entry has been published, we can publish to twitter and mark
//that this post has been twittered
if ( $tp_wp_status == “publish" ){
– twitpress_publish( $row[“id"] );
– twitpress_db_update_post( $row[“id"], 'twittered’ );
+ Wassrpress_publish( $row[“id"] );
+ Wassrpress_db_update_post( $row[“id"], 'wassrered’ );
}
}
}

-//Add the twitpress menu to the management page
-function twitpress_add_menu() {
– add_management_page( 'Twitpress’, 'Twitpress’, 8, 'twitpressoptions’, 'twitpress_options_page’ );
+//Add the Wassrpress menu to the management page
+function Wassrpress_add_menu() {
+ add_management_page( 'Wassrpress’, 'Wassrpress’, 8, 'Wassrpressoptions’, 'Wassrpress_options_page’ );
}

//Options page code and html
-function twitpress_options_page() {
+function Wassrpress_options_page() {
global $wpdb;
– $table_name = “twitpress";
– $username = get_option( 'twitpress_uid’ );
– $password = get_option( 'twitpress_password’ );
– $message = get_option( 'twitpress_message’ );
– $submitFieldID = 'twitpress_submit_hidden’;
+ $table_name = “Wassrpress";
+ $username = get_option( 'Wassrpress_uid’ );
+ $password = get_option( 'Wassrpress_password’ );
+ $message = get_option( 'Wassrpress_message’ );
+ $submitFieldID = 'Wassrpress_submit_hidden’;
if ( $_POST[ $submitFieldID ] == 'Y’ ) {
– update_option( 'twitpress_uid’, $_POST[ 'twitpress_form_username’ ] );
– update_option( 'twitpress_password’, str_rot13($_POST[ 'twitpress_form_password’ ]) );
– update_option( 'twitpress_message’, $_POST[ 'twitpress_form_message’ ] );
+ update_option( 'Wassrpress_uid’, $_POST[ 'Wassrpress_form_username’ ] );
+ update_option( 'Wassrpress_password’, str_rot13($_POST[ 'Wassrpress_form_password’ ]) );
+ update_option( 'Wassrpress_message’, $_POST[ 'Wassrpress_form_message’ ] );
?>

';
– echo “

" . __( 'Twitpress Plugin Options’, 'mt_trans_domain’ ) . “

“; ?>
+ echo “

" . __( 'Wassrpress Plugin Options’, 'mt_trans_domain’ ) . “

“; ?>


+

Username

+name="Wassrpress_form_message" />

">

@@ -229,21 +229,21 @@ } //Publishes a tweet given only the postID -function twitpress_publish( $postID ){ +function Wassrpress_publish( $postID ){ //get the post $post = get_post( $postID ); //Now redundant check to make sure the post has been published if ( $post->post_status == "publish" ){ - $message = twitpress_get_message( $postID ); - twitpress_postMessage( get_option( 'twitpress_uid' ), get_option( 'twitpress_password' ), $message ); + $message = Wassrpress_get_message( $postID ); + Wassrpress_postMessage( get_option( 'Wassrpress_uid' ), get_option( 'Wassrpress_password' ), $message ); } } //Converts a wordpress post into a string with user formatting for twitter posting -function twitpress_get_message( $postID ){ - $proto = get_option( 'twitpress_message' ); +function Wassrpress_get_message( $postID ){ + $proto = get_option( 'Wassrpress_message' ); $post = get_post( $postID ); $proto = str_replace( "[title]", $post->post_title, $proto ); $proto = str_replace( "[permalink]", $post->guid, $proto ); @@ -252,15 +252,15 @@ } //Standard curl function, handles actual submission of message to twitter -function twitpress_postMessage( $twitter_username, $twitter_password, $message ){ - $url = 'http://twitter.com/statuses/update.xml'; +function Wassrpress_postMessage( $wassr_username, $wassr_password, $message ){ + $url = 'http://api.wassr.jp/statuses/update.json'; $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=$message&source=twitpress" ); - curl_setopt( $curl_handle, CURLOPT_USERPWD, "$twitter_username:".str_rot13( $twitter_password ) ); + curl_setopt( $curl_handle, CURLOPT_POSTFIELDS, "status=$message&source=Wassrpress" ); + curl_setopt( $curl_handle, CURLOPT_USERPWD, "$wassr_username:".str_rot13( $wassr_password ) ); $buffer = curl_exec( $curl_handle ); curl_close( $curl_handle ); } [/code]