query_vars ) ) { switch( $wp->query_vars['wordtwit'] ) { case 'create': include( 'ajax/create-url.php' ); break; case 'news': include( 'ajax/news.php' ); break; case 'tweets': include( 'ajax/tweets.php' ); break; } exit; } } function wordtwit_avatar( $avatar ) { global $comment; if ( $comment->comment_type == 'wordtwit' ) { echo "comment_agent . "\" alt=\"\" />"; } else { return $avatar; } } function wordtwit_get_hash() { return md5( WORDTWIT_VERSION ); } function wordtwit_head() { echo ''; } //Add a link to settings on the plugin listings page function wordtwit_settings_link( $links, $file ) { if ( $file == 'wordtwit/wordtwit.php' && function_exists( "admin_url" ) ) { $settings_link = '' . __('Settings') . ''; array_unshift( $links, $settings_link ); // before other links } return $links; } function wordtwit_get_auth_url() { global $wordtwit_oauth; $settings = wordtwit_get_settings(); $token = $wordtwit_oauth->get_request_token(); if ( $token ) { $settings['oauth_request_token'] = $token['oauth_token']; $settings['oauth_request_token_secret'] = $token['oauth_token_secret']; wordtwit_save_settings( $settings ); return $wordtwit_oauth->get_auth_url( $token['oauth_token'] ); } } $wordtwit_defaults = array( '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, 'oauth_request_token' => false, 'oauth_request_token_secret' => false, 'oauth_access_token' => false, 'oauth_access_token_secret' => false, 'enable-utm' => false, 'tweet_queue' => array(), 'last_tweet_time' => 0, 'user_id' => 0, 'profile_image_url' => '', 'screen_name' => '', 'location' => false, 'enabled_tweet_queue' => false, 'disable_curl' => false ); function wordtwit_is_bitly() { $settings = wordtwit_get_settings(); return ( $settings['url_type'] == 'bitly' ); } function wordtwit_get_username() { $settings = wordtwit_get_settings(); return $settings['screen_name']; } 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() { include( 'html/post-box.php' ); } function wordtwit_admin_menu() { if ( function_exists( 'add_meta_box' ) ) { add_meta_box( 'wordtwit-box', __( 'WordTwit Management', 'wordtwit' ), 'wordtwit_post_box', 'post', 'side' ); } } function wordtwit_friendly_date( $datetime ) { $datetime = strtotime( str_replace( 'T', ' ', $datetime ) ); $server_time = current_time( 'timestamp', 1 ); $tweet_time = $server_time - $datetime; $minutes = floor( $tweet_time / (60) ); $hours = $tweet_time / (60*60); $days = $tweet_time / (60*60*24); if ( $minutes == 0 ) { return __( "Just now", "wordtwit" ); } else if ( $minutes < 120 && $hours < 2 ) { return sprintf( __( "About %d minutes ago", "wordtwit" ), $minutes ); } else if ( $hours < 48 ) { return sprintf( __( "About %d hours ago", "wordtwit" ), $hours ); } else { return sprintf( __( "About %d days ago", "wordtwit" ), $days ); } } function wordtwit_service_queue() { $settings = wordtwit_get_settings(); // Service the pending Tweet queue if ( isset( $settings['tweet_queue'] ) && count( $settings['tweet_queue'] ) ) { if ( time() > ( $settings['last_tweet_time'] + WORDTWIT_QUEUE_RETRY_TIME ) ) { $f = @fopen( WP_PLUGIN_DIR . '/wordtwit/wordtwit.lock', 'r+' ); if ( $f ) { // Use a lock file to prevent race condition with the queue if ( flock( $f, LOCK_EX | LOCK_NB ) ) { foreach( $settings['tweet_queue'] as $post_id => $tweet_data ) { // output tweet if ( wordtwit_do_tweet( $post_id ) ) { // delete it from the queue and update status unset( $settings['tweet_queue'][ $post_id ] ); update_post_meta( $post_id, 'has_been_twittered', 'yes' ); wordtwit_save_settings( $settings ); } else { global $wordtwit_oauth; if ( $wordtwit_oauth->was_duplicate_tweet() ) { unset( $settings['tweet_queue'][ $post_id ] ); update_post_meta( $post_id, 'has_been_twittered', 'previously' ); wordtwit_save_settings( $settings ); } } } } fclose( $f ); } $settings['last_tweet_time'] = time(); wordtwit_save_settings( $settings ); } } } function wordtwit_is_tweet_queue_enabled() { $settings = wordtwit_get_settings(); if ( isset( $settings['enable-tweet-queue'] ) ) { return $settings['enable-tweet-queue']; } return false; } function wordtwit_do_tweet( $post_id ) { $settings = wordtwit_get_settings(); $message = wordtwit_get_message( $post_id ); // If we have a valid message, Tweet it // this will fail if the Tiny URL service is done if ( $message ) { // If we successfully posted this to Twitter, then we can remove it from the queue eventually if ( twit_update_status( $message ) ) { return true; } } return false; } function wordtwit_init() { global $wordtwit_cache_time; global $wpdb; global $table_prefix; if ( isset( $_GET['wordtwit_oauth'] ) ) { global $wordtwit_oauth; $settings = wordtwit_get_settings(); $result = $wordtwit_oauth->get_access_token( $settings['oauth_request_token'], $settings['oauth_request_token_secret'], $_GET['oauth_verifier'] ); if ( $result ) { $settings['oauth_access_token'] = $result['oauth_token']; $settings['oauth_access_token_secret'] = $result['oauth_token_secret']; $settings['user_id'] = $result['user_id']; $result = $wordtwit_oauth->get_user_info( $result['user_id'] ); if ( $result ) { $settings['profile_image_url'] = $result['user']['profile_image_url']; $settings['screen_name'] = $result['user']['screen_name']; if ( isset( $result['user']['location'] ) ) { $settings['location'] = $result['user']['location']; } else { $settings['location'] = false; } } wordtwit_save_settings( $settings ); header( 'Location: ' . get_bloginfo('wpurl') . '/wp-admin/options-general.php?page=wordtwit.php' ); die; } } else if ( isset( $_GET['wordtwit'] ) && $_GET['wordtwit'] == 'deauthorize' ) { $settings = wordtwit_get_settings(); $settings['oauth_access_token'] = ''; $settings['oauth_access_token_secret'] = ''; $settings['user_id'] = ''; $settings['tweet_queue'] = array(); wordtwit_save_settings( $settings ); header( 'Location: ' . get_bloginfo('wpurl') . '/wp-admin/options-general.php?page=wordtwit.php' ); die; } $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 ) { if ( isset( $_GET['page'] ) && $_GET['page'] == 'wordtwit.php' ) { wp_enqueue_script( 'fancybox', compat_get_plugin_url('wordtwit') . '/js/fancybox_1.2.5.js', array( 'jquery' ) ); wp_enqueue_script( 'wordtwitadmin', compat_get_plugin_url('wordtwit') . '/js/wordtwit_admin_2.1.js', array( 'jquery' ) ); } } if ( isset( $_GET['wordtwit_retweet'] ) ) { $post_id = (int)$_GET['wordtwit_retweet']; delete_post_meta( $post_id, 'has_been_twittered' ); wordtwit_post_now_published( $post_id, true ); header( 'Location: ' . $_GET['wordtwit_redirect'] ); die; } wordtwit_check_table(); // Only service the queue if the Tweet queue is enabled if ( wordtwit_is_tweet_queue_enabled() ) { wordtwit_service_queue(); } // check TinyURL $location = explode( '/', ltrim( $_SERVER['REQUEST_URI'], '/' ) ); if ( count( $location ) ) { $possible_url = $location[ count( $location ) - 1 ]; if ( $possible_url && strlen( $possible_url ) < 6 ) { // might be a tiny URL $tiny_url = $possible_url; $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
\"\"Follow " . $profile['user']['name'] . " On Twitter

" . str_replace( 'www.', '', str_replace('http://', '', strtolower( get_bloginfo('home') ) ) ) . "

\n"; echo "

"; echo "

Latest posts from '" . get_bloginfo('name') . "'

\n"; echo "\n"; echo "

\n"; echo "
\"\"original . "\">View Original
"; echo "
\n"; echo "\t