query_vars ) ) {
switch( $wp->query_vars['wordtwit'] ) {
case 'create':
include( 'ajax/create-url.php' );
break;
case 'news':
include( 'ajax/news.php' );
break;
}
exit;
}
}
function wordtwit_avatar( $avatar ) {
global $comment;
if ( $comment->comment_type == 'wordtwit' ) {
echo "
comment_agent . "\" alt=\"\" />";
} else return $avatar;
}
function wordtwit_head() {
echo '';
}
$wordtwit_defaults = array(
'username' => '',
'password' => '',
'alternate_domain' => '',
'tags' => array(),
'reverse' => false,
'activation_time' => 0,
'message' => 'New blog posting, [title] - [link]',
'url_type' => 'tinyurl',
'enable_banner' => true,
'enable_content_conversion' => false
);
//update_option( 'wordtwit_settings', '' );
function wordtwit_get_settings() {
global $wordtwit_defaults;
$settings = $wordtwit_defaults;
$wordpress_settings = get_option( 'wordtwit_settings' );
if ( $wordpress_settings ) {
foreach( $wordpress_settings as $key => $value ) {
$settings[ $key ] = $value;
}
}
return $settings;
}
function wordtwit_save_post( $post_id ) {
if ( !wp_verify_nonce( $_POST['wordtwit_nonce'], 'WordTwit' ) ) {
return $post_id;
}
delete_post_meta( $post_id, 'wordtwit_hashtags' );
}
function wordtwit_delete_post( $id ) {
global $wpdb;
global $table_prefix;
$sql = $wpdb->prepare( 'DELETE FROM ' . $table_prefix . 'tweet_urls WHERE post_id = %d', $id );
$wpdb->query( $sql );
}
function wordtwit_post_box() {
echo '';
echo '
';
echo '';
echo '';
echo '';
}
function wordtwit_admin_menu() {
if( function_exists( 'add_meta_box' ) ) {
add_meta_box( 'wordtwit-box', __( 'WordTwit Post Management', 'wordtwit' ), 'wordtwit_post_box', 'post', 'side' );
}
}
function wordtwit_init() {
global $twit_plugin_prefix;
global $wordtwit_cache_time;
global $wpdb;
global $table_prefix;
$settings = wordtwit_get_settings();
if ( $settings['enable_content_conversion'] ) {
add_action( 'the_content', 'wordtwit_content' );
}
if ( $settings['activation_time'] == 0 ) {
$settings['activation_time'] = time();
wordtwit_save_settings( $settings );
}
if ( strpos( $_SERVER["REQUEST_URI"], "wp-admin" ) !== false ) {
wp_enqueue_script( 'fancybox', compat_get_plugin_url('wordtwit') . '/js/fancybox1.2.1.js', array( 'jquery' ) );
}
wordtwit_check_table();
// check TinyURl
$url = $_SERVER["REQUEST_URI"];
if ( strlen( $url ) < 6 ) {
// might be a tiny URL
$params = explode( '/', $url );
if ( count( $params ) == 2 ) {
$tiny_url = $params[1];
$sql = $wpdb->prepare( "SELECT original FROM " . $table_prefix . "tweet_urls WHERE url = %s", $tiny_url );
$result = $wpdb->get_row( $sql );
if ( $result ) {
$sql = $wpdb->prepare( "UPDATE " . $table_prefix . "tweet_urls SET views = views + 1 WHERE url = %s", $tiny_url );
$wpdb->query( $sql );
$this_site = strtolower( wordtwit_short_home( get_bloginfo('home') ) );
$short_url = strtolower( str_replace( 'www.', '', $result->original ) );
if ( strpos( $short_url, $this_site ) === false && $settings['enable_banner'] ) {
// external site
header( "Content-type: text/html" );
$profile = $settings['profile'];
echo "
\n";
echo "\t
\n";
echo "\t" . $result->original . " - Courtesy of " . $profile['user']['name'] . "\n";
echo "\t\n";
echo "\t\n";
echo "\t\n";
echo "\n";
echo "\n";
echo "\t\n";
echo "\t\n";
echo "\n";
echo "\n";
die;
} else {
// internal site
header( "HTTP/1.1 301 Moved Permanently" );
header( "Location: " . $result->original );
}
die;
}
}
}
}
function twit_hit_server( $location, $username, $password, &$output, $post = false, $post_fields = '' ) {
global $wordtwit_version;
$output = '';
$snoopy = new Snoopy;
$snoopy->agent = 'WordTwit ' . $wordtwit_version;
if ( $username ) {
$snoopy->user = $username;
if ( $password ) {
$snoopy->pass = $password;
}
}
if ( $post ) {
// need to do the actual post
$result = $snoopy->submit( $location, $post_fields );
if ( $result ) {
return $true;
}
} else {
$result = $snoopy->fetch( $location );
if ( $result ) {
$output = $snoopy->results;
}
$code = explode( ' ', $snoopy->response_code );
if ( $code[1] == 200) {
return true;
} else {
return false;
}
}
}
function twit_update_status( $username, $password, $new_status ) {
$output = '';
return twit_hit_server( 'http://twitter.com/statuses/update.xml', $username, $password, $output, true, array( 'status' => $new_status, 'source' => 'wordtwit' ) );
}
function twit_verify_credentials( $username, $password, &$cred ) {
global $twit_plugin_prefix;
$output = '';
$result = twit_hit_server( 'http://twitter.com/account/verify_credentials.xml', $username, $password, $output );
if ( $result ) {
$cred = wordtwit_parsexml( $output );
$settings = wordtwit_get_settings();
$settings['profile'] = $cred;
wordtwit_save_settings( $settings );
}
return $result;
}
function twit_get_tiny_url( $link ) {
$output = '';
$result = twit_hit_server( 'http://tinyurl.com/api-create.php?url=' . $link, '', '', $output );
return $output;
}
function twit_get_bitly_url( $link ) {
$settings = wordtwit_get_settings();
$output = false;
$result = twit_hit_server( 'http://api.bit.ly/shorten?version=2.0.1&longUrl=' . urlencode( $link ) . '&format=xml&login=' . $settings['bitly-user-name'] . '&apiKey=' . $settings['bitly-api-key'], '', '', $output );
preg_match( '#(.*)#iUs', $output, $url );
if ( isset( $url[1] ) ) {
return $url[1];
} else {
return false;
}
}
function wordtwit_make_tinyurl( $link, $update = true ) {
if ( strpos( $link, 'http://' ) === false) {
return $link;
}
$settings = wordtwit_get_settings();
if ( $settings['url_type'] == 'tinyurl' ) {
return twit_get_tiny_url( $link );
} else if ( $settings['url_type'] == 'local' ) {
return wordtwit_tinyurl( $link, $update );
} else if ( $settings['url_type'] == 'bitly' ) {
return twit_get_bitly_url( $link );
}
}
function post_now_published( $post_id ) {
global $twit_plugin_prefix;
$settings = wordtwit_get_settings();
$wt_tags = $settings['tags'];
$wt_reverse = $settings['reverse'];
$activation_time = $settings['activation_time'];
$cur_time = time();
query_posts( 'p=' . $post_id );
if ( have_posts() ) {
the_post();
global $post;
$cur_time = strtotime( $post->post_date_gmt );
if ( $cur_time < $activation_time ) {
//echo $cur_time . " " . $activation_time . " " . time() . " " ;
//echo 'leaving'; die;
// don't publish old posts
return;
}
$can_tweet = true;
// check tags
if ( count( $wt_tags ) ) {
// we have a tag or a category
$new_taxonomy = array();
$post_tags = get_the_tags();
if ( $post_tags ) {
foreach ( $post_tags as $some_tag ) {
$new_taxonomy[] = strtolower( $some_tag->name );
}
}
$post_categories = get_the_category();
if ( $post_categories ) {
foreach ( $post_categories as $some_category ) {
$new_taxonomy[] = strtolower( $some_category->name );
}
}
$category_hits = array_intersect( $wt_tags, $new_taxonomy );
if ( $wt_reverse ) {
$can_tweet = ( count( $category_hits ) == 0);
} else {
$can_tweet = ( count( $category_hits ) > 0);
}
}
$has_been_twittered = get_post_meta( $post_id, 'has_been_twittered', true );
if ( $has_been_twittered !== 'yes' && $can_tweet ) {
$i = 'New blog entry \'' . the_title('','',false) . '\' - ' . get_permalink();
$message = $settings['message'];
$message = str_replace( '[title]', get_the_title(), $message );
$message = str_replace( '[link]', wordtwit_make_tinyurl( get_permalink(), true ), $message );
$twit_username = $settings['username'];
$twit_password = $settings['password'];
twit_update_status( $twit_username, $twit_password, $message );
add_post_meta( $post_id, 'has_been_twittered', 'yes' );
}
}
}
function wordtwit_admin_css() {
$url = get_bloginfo('wpurl');
echo '';
echo '';
}
function bnc_stripslashes_deep( $value ) {
$value = is_array($value) ?
array_map('bnc_stripslashes_deep', $value) :
stripslashes($value);
return $value;
}
function wordtwit_options_subpanel() {
if (get_magic_quotes_gpc()) {
$_POST = array_map( 'bnc_stripslashes_deep', $_POST );
$_GET = array_map( 'bnc_stripslashes_deep', $_GET );
$_COOKIE = array_map( 'bnc_stripslashes_deep', $_COOKIE );
$_REQUEST = array_map( 'bnc_stripslashes_deep', $_REQUEST );
}
global $twit_plugin_name;
global $twit_plugin_prefix;
$settings = wordtwit_get_settings();
if ( isset($_POST['info_update']) ) {
if ( isset($_POST['username'] ) ) {
$username = $_POST['username'];
} else {
$username = '';
}
if ( isset( $_POST['enable_content_conversion'] ) ) {
$settings['enable_content_conversion'] = true;
} else {
$settings['enable_content_conversion'] = false;
}
if ( isset($_POST['password']) ) {
$password = $_POST['password'];
} else {
$password = '';
}
if ( isset($_POST['message']) ) {
$message = $_POST['message'];
} else {
$message = '';
}
if ( isset( $_POST['wordtwit-url-type'] ) ) {
$settings['url_type'] = $_POST['wordtwit-url-type'];
}
if ( isset( $_POST['enable_banner'] ) ) {
$settings['enable_banner'] = true;
} else {
$settings['enable_banner'] = false;
}
if ( isset( $_POST['bitly-api-key'] ) ) {
$settings['bitly-api-key'] = $_POST['bitly-api-key'];
} else {
$settings['bitly-api-key'] = '';
}
if ( isset( $_POST['bitly-user-name'] ) ) {
$settings['bitly-user-name'] = $_POST['bitly-user-name'];
} else {
$settings['bitly-user-name'] = '';
}
if ( isset( $_POST['alternate-domain'] ) ) {
if ( strlen( $_POST['alternate-domain'] ) == 0 ) {
$settings['alternate_domain'] = '';
} else {
$settings['alternate_domain'] = $_POST['alternate-domain'];
$settings['alternate_domain'] = strtolower( rtrim( $settings['alternate_domain'], '/' ) );
if ( strpos( $settings['alternate_domain'], 'http://' ) === false ) {
$settings['alternate_domain'] = 'http://' . $settings['alternate_domain'];
}
}
}
if ( isset( $_POST['wordtwit-reverse'] ) ) {
$reverse = true;
} else {
$reverse = false;
}
$wt_tags = explode( ",", $_POST['wordtwit-tags'] );
$settings['username'] = $username;
$settings['password'] = $password;
$new_tags = array();
foreach ( $wt_tags as $tags ) {
$clean_tag = strtolower( rtrim( ltrim( $tags ) ) );
if ( strlen( $clean_tag ) ) {
$new_tags[] = $clean_tag;
}
}
$settings['tags'] = $new_tags;
$settings['message'] = stripslashes( $message );
$settings['reverse'] = $reverse;
wordtwit_save_settings( $settings );
}
include( 'html/options.php' );
}
function wordtwit_save_settings( $settings ) {
update_option( 'wordtwit_settings', $settings );
}
function wordtwit_check_table() {
global $table_prefix;
global $wpdb;
$table_name = $table_prefix . 'tweet_urls';
$sql = "describe $table_name";
$result = $wpdb->get_results( $sql );
if ( !$result ) {
// create table
$sql = "CREATE TABLE `" . $table_name . "` (" .
"`id` int(10) unsigned NOT NULL auto_increment," .
"`original` varchar(128) collate utf8_bin default NULL," .
"`url` varchar(7) collate utf8_bin NOT NULL," .
"`views` int(11) NOT NULL default '0'," .
"`post_id` int(11) NOT NULL default '0'," .
"PRIMARY KEY (`id`)," .
"UNIQUE KEY `url_index` (`url`)," .
"KEY `original` (`original`)," .
"KEY `url_index_2` (`url`)" .
") ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;";
$result = $wpdb->query( $sql );
}
}
function wordtwit_cache_tweets() {
global $twit_plugin_prefix;
global $table_prefix;
global $wpdb;
$table_name = $table_prefix . 'tweetcache';
wordtwit_check_table();
$username = get_option( $twit_plugin_prefix . 'username' );
$password = get_option( $twit_plugin_prefix . 'password' );
if ( $username && $password ) {
$content = '';
$result = twit_hit_server( 'http://twitter.com/statuses/replies.xml', $username, $password, $content );
//$result = twit_hit_server( 'http://duanestorey.com/test.xml', $username, $password, $content );
if ( $result ) {
$xml = wordtwit_parsexml( $content );
foreach( $xml['statuses']['status'] as $status ) {
$sql = "select * from $table_name where from_status_id = '" . $status["id"] . "'";
$result = $wpdb->get_row( $sql );
if ( !$result ) {
$from_name = $status["user"]["name"];
$from_screen_name = $status["user"]["screen_name"];
$profile_pic = $status["user"]["profile_image_url"];
$from_date_time = strtotime( $status["created_at"] );
$id = $status["id"];
$reply_to = $status["in_reply_to_status_id"];
$tweet = substr( $status["text"], strlen( $username ) + 2, strlen( $status["text"] ) - strlen( $username ) - 2);
$home_url = $status["user"]["url"];
if ( is_array( $status["user"]["url"] ) && count( $status["user"]["url"] ) == 0 ) {
$home_url = '';
} else {
$home_url = (string)$status["user"]["url"];
}
if ( is_array( $status["user"]["location"] ) && count( $status["user"]["location"] ) == 0 ) {
$from_location = '';
} else {
$from_location = (string)$status["user"]["location"];
}
$sql = $wpdb->prepare( "INSERT INTO $table_name ( from_name, from_profile_pic, from_location, from_date_time, from_status_id, from_in_reply_to, from_tweet, from_home_url, from_screen_name, imported ) VALUES ( %s, %s, %s, %d, %d, %d, %s, %s, %s, %d )", $from_name, $profile_pic, $from_location, $from_date_time, $id, $reply_to, $tweet, $home_url, $from_screen_name, 0 );
$result = $wpdb->query( $sql );
} else {
// we already have this in the database. probably safe to assume that we have everything else after it since the feed
// is hopefully ordered by date
break;
}
}
}
$result = twit_hit_server( 'http://twitter.com/statuses/user_timeline/' . $username . '.xml', $username, $password, $content );
if ( $result ) {
$table_name = $table_name . "_local";
$xml = wordtwit_parsexml( $content );
$profile_url = '';
foreach( $xml['statuses']['status'] as $status ) {
$sql = "select * from $table_name where status_id = '" . $status["id"] . "'";
$result = $wpdb->get_row( $sql );
if ( !$result ) {
$tweet = $status["text"];
$date_time = strtotime( $status["created_at"] );
$id = $status["id"];
if ( !$profile_url ) {
$profile_url = $status["user"]["profile_image_url"];
}
$sql = $wpdb->prepare( "INSERT INTO $table_name ( status_id, tweet, date_time ) VALUES ( %d, %s, %d )", $id, $tweet, $date_time );
$result = $wpdb->query( $sql );
} else {
break;
}
}
if ( $profile_url ) {
update_option( $twit_plugin_prefix . 'profile_pic', $profile_url );
}
}
}
}
function wordtwit_add_plugin_option() {
global $twit_plugin_name;
if (function_exists('add_options_page')) {
add_options_page($twit_plugin_name, $twit_plugin_name, 9, basename(__FILE__), 'wordtwit_options_subpanel');
}
}
function wordtwit_shortcode( $data ) {
global $twit_plugin_prefix;
global $table_prefix;
global $wpdb;
$table_name = $table_prefix . 'tweetcache';
$profile_pic = get_option( $twit_plugin_prefix . 'profile_pic' );
$username = get_option( $twit_plugin_prefix . 'username');
$content = '';
if ( isset( $data['status'] ) ) {
$content .= "Thoughts From Twitter
\n";
if ( is_single() ) print_r( $comments );
}
//return $content;
return '';
}
function wordtwit_content( $content ) {
preg_match_all( "'href=\"?(.[^\"\>]*)\"?(.[^\>]*)>(.[^\<]*)'im",$content,$matches );
if ( $matches ) {
foreach ( $matches[1] as $url ) {
$update_post_num = false;
if ( strtolower( $url ) == strtolower( get_permalink() ) ) {
$update_post_num = true;
}
$tinyurl = wordtwit_make_tinyurl( $url, $update_post_num );
$content = str_replace( $url, $tinyurl, $content );
}
}
return $content;
}
add_action( 'admin_menu', 'wordtwit_add_plugin_option' );
//add_shortcode( 'wordtwit', 'wordtwit_shortcode');
?>