ID, 'twipub_oauth_token', $_GET['twipub_oauth_token']); update_usermeta($profileuser->ID, 'twipub_oauth_token_secret', $_GET['twipub_oauth_token_secret']); update_usermeta($profileuser->ID, 'twipub_screen_name', $_GET['twipub_screen_name']); update_usermeta($profileuser->ID, 'twitter_username', $_GET['twipub_screen_name']); header('Location: /wp-admin/profile.php?updated=true'); exit; } echo '

'.__('Twitter credentials and sharing', 'twitter_publisher').'

'.(!empty($profileuser->twipub_screen_name) ? $profileuser->twipub_screen_name : @$_GET['screen_name']).'
twipub_share_on_twitter_too == 1 ? 'checked="checked"': '') .'" /> '.__('Tweet the same Tweet as the main Twitter account under my account too.', 'twitter_publisher').'
'; } /** * Tweet about a new blog post on publish */ function twitter_publisher_post_tweet($post_id = 0, $tweet_manual = false) { $options = get_option('twitter_publisher_options'); //get blog post object $post = get_post($post_id); //always tweet when manual if($tweet_manual !== true) { //are we on auto tweet mode? if( $options['no_auto_tweeting'] == 1) return; //only Tweet once about a post $twipub_tweeted = get_post_meta($post_id, 'twipub_tweeted', true); if( !empty($twipub_tweeted) ) { return; } //do not Tweet when a post is edited if ( strtotime($post->post_date) < strtotime($post->post_modified) ) { return; } } //get blog post title $title = $post->post_title; //get author $author = get_userdata($post->post_author); //get permalink $permalink = get_permalink($post_id); //get main Twitter account credentials $twitter_username = $options['twipub_screen_name']; //only try to Tweet to main account when both twitter username and password have been filled in if(!empty($twitter_username)) { //check if twitter name has been filled in if(!empty($author->twitter_username)) { $author_name = '@'.$author->twitter_username; } else { //use display name instead $author_name = $author->display_name; } //generate short url $post_url = get_post_meta($post_id, 'twipub_short_url_main', true); if(empty($post_url)) { $post_url = twitter_publisher_create_short_url($permalink, 'twitter', 'twitter-publisher-main'); add_post_meta($post_id, 'twipub_short_url_main', $post_url, true); } if($options['debug']) add_post_meta($post_id, 'twipub_debug', 'Short url: '. $post_url, false); //concat the meta data of the tweet (= tweet without the title) when option is checked to add author's name if($options['twitter_incl_author'] == 1) { $tweet_meta = ' '. $post_url .' by '. $author_name; } else { //just concat the URL instead $tweet_meta = ' '. $post_url; } //get the Tweet prefix from the settings (if there is one) $tweet_prefix = $options['title_prefix']; if(!empty($tweet_prefix)) { $tweet_prefix .= ' '; } //get the Tweet suffix from the settings (if there is one) $tweet_suffix = $options['title_suffix']; if(!empty($tweet_suffix)) { $tweet_suffix = ' '. $tweet_suffix; } //if title + meta > 140 chars, shorten the title if(strlen($title) + strlen($tweet_meta) + strlen($tweet_prefix) + strlen($tweet_suffix) > 140) { if($options['drop_suffix'] == 1) { $tweet_suffix = ''; } $title = substr($title, 0, 140-(strlen($tweet_prefix) + strlen($tweet_meta) + strlen($tweet_suffix) + 3)). '...'; } //now concatinate the tweet $tweet = $tweet_prefix . $title . $tweet_meta . $tweet_suffix; //now let's Tweet about it! $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $options['twipub_oauth_token'], $options['twipub_oauth_token_secret']); $parameters['status'] = $tweet; $status = $connection->post('statuses/update', $parameters); /* $snoop = new Snoopy(); $snoop->agent = 'Twitter Publisher'; $snoop->user = $twitter_username; $snoop->pass = $twitter_password; $snoop->submit( 'http://twitter.com/statuses/update.json', array( 'status' => $tweet, 'source' => 'Twitter-Publisher' ) ); $twitter_result = $snoop->response_code; */ if($options['debug']) add_post_meta($post_id, 'twipub_debug', 'Main twitter: '.$tweet.' - '. print_r($status, true), false); } //when the author wants it, we Tweet the same tweet under his account too (but without the 'by author' part). if($author->twipub_share_on_twitter_too && !$tweet_manual && !empty($author->twitter_username)) { //generate short url $author_url = twitter_publisher_create_short_url($permalink, 'twitter', 'twitter-publisher-author'); //if title + url > 140 chars, shorten the title if(strlen($tweet_prefix) + strlen($title) + strlen(' '.$author_url) + strlen($tweet_suffix) > 140) { if($options['drop_suffix'] == 1) { $tweet_suffix = ''; } $title = substr($title, 0, 140-(strlen($tweet_prefix) + strlen(' '.$author_url) + strlen($tweet_suffix) + 3)). '...'; } //now concatinate the tweet $author_tweet = $tweet_prefix . $title .' '. $author_url . $tweet_suffix; //now let's Tweet about it! $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $author->twipub_oauth_token, $author->twipub_oauth_token_secret); $parameters['status'] = $author_tweet; $status = $connection->post('statuses/update', $parameters); if($options['debug']) add_post_meta($post_id, 'twipub_debug', 'Author twitter: '.$author_tweet.' - '. print_r($status, true), false); } //add a flag to this blog post, so we only Tweet once about it add_post_meta($post_id, 'twipub_tweeted', '1', true); } /** * Build settings page for Twitter Publisher */ function twitter_publisher_configpage() { $twitter_publisher_options = get_option('twitter_publisher_options'); if(isset($_GET['twipub_oauth_token'])) { $twitter_publisher_options['twipub_oauth_token'] = $_GET['twipub_oauth_token']; $twitter_publisher_options['twipub_oauth_token_secret'] = $_GET['twipub_oauth_token_secret']; $twitter_publisher_options['twipub_screen_name'] = $_GET['twipub_screen_name']; update_option('twitter_publisher_options', $twitter_publisher_options); } echo '
'. screen_icon() .'

'.__('Twitter Publisher Settings', 'twitter_publisher').'

'; if(!empty($_POST)) { echo '

'.__('Settings saved', 'twitter_publisher').'

'; } echo '
'; settings_fields( 'twitter_publisher_options' ); echo '
'.__('You can prefix your automated tweets with something like New blog post:', 'twitter_publisher').'
'.__('You can suffix your automated tweets with extra hashtags, like #hashtag', 'twitter_publisher').'
'.__('Drop suffix:', 'twitter_publisher').'
'.__('Short URL service:', 'twitter_publisher').'
'.__('Refer to author:', 'twitter_publisher').'
'.__('Let authors tweet manual:', 'twitter_publisher').'
'.__('No automatic tweeting:', 'twitter_publisher').'
'.__('Enable debugging:', 'twitter_publisher').'

'.__('Main Twitter account credentials', 'twitter_publisher').'

'.__('If you have a Twitter account for your blog, please fill in the username and password below. Author-specific accounts can be provided in their personal profile pages.', 'twitter_publisher').'

'.$twitter_publisher_options['twipub_screen_name'].'

'.__('Awe.sm credentials', 'twitter_publisher').'

'.__('Bit.ly credentials', 'twitter_publisher').'

'.__('Campaign variables:', 'twitter_publisher').'

'.__('Do not forget to have your authors enter their Twitter name in their profiles. Otherwise their Wordpress display name will be mentioned in the tweets.', 'twitter_publisher').'

'; } function twitter_publisher_custom_box() { global $post; $options = get_option('twitter_publisher_options'); //get blog post title $title = $post->post_title; //get author $author = get_userdata($post->post_author); //get permalink $permalink = get_permalink($post->ID); //generate short url $post_url = twitter_publisher_create_short_url($permalink, 'twitter', 'twitter-publisher-main'); //check if twitter name has been filled in if(!empty($author->twitter_username)) { $author_name = '@'.$author->twitter_username; } else { //use display name instead $author_name = $author->display_name; } //concat the meta data of the tweet (= tweet without the title) when option is checked to add author's name if($options['twitter_incl_author'] == 1) { $tweet_meta = ' '. $post_url .' by '. $author_name; } else { //just concat the URL instead $tweet_meta = ' '. $post_url; } //get the Tweet prefix from the settings (if there is one) $tweet_prefix = $options['title_prefix']; if(!empty($tweet_prefix)) { $tweet_prefix .= ' '; } //get the Tweet suffix from the settings (if there is one) $tweet_suffix = $options['title_suffix']; if(!empty($tweet_suffix)) { $tweet_suffix = ' '. $tweet_suffix; } //if title + meta > 140 chars, shorten the title if(strlen($title) + strlen($tweet_meta) + strlen($tweet_prefix) + strlen($tweet_suffix) > 140) { if($options['drop_suffix'] == 1) { $tweet_suffix = ''; } $title = substr($title, 0, 140-(strlen($tweet_prefix) + strlen($tweet_meta) + strlen($tweet_suffix) + 3)). '...'; } //now concatinate the tweet $tweet = $tweet_prefix . $title . $tweet_meta . $tweet_suffix; if( $post->post_status == 'publish') { echo '
Tweet will be send through @'.$options['twitter_username'].':

'.$tweet.'
'; //now call do_action for plugins which are using Post Play do_action('postplay_add_content'); } else { echo '
Post has not yet been published, so no Tweeting yet.
'; } } function twitter_publisher_add_meta_box_wrapper() { $options = get_option('twitter_publisher_options'); if($options['manual_tweeting'] == 1) { add_meta_box('twitter_publisher_box_id', 'Twitter Publisher', 'twitter_publisher_custom_box', 'post', 'side', 'high'); } } add_action('admin_menu', 'twitter_publisher_add_meta_box_wrapper'); function twitter_publisher_js_admin_header() { // use JavaScript SACK library for Ajax //wp_print_scripts( array( 'sack' )); // Define custom JavaScript function ?> 0) $delimiter = '&'; $permalink .= urlencode( $delimiter . 'utm_source='.$sharetype.'&utm_medium='.$createtype.'&utm_campaign='.$sharetype); } //generate bit.ly url $snoop = new Snoopy(); $snoop->agent = 'Twitter Publisher'; $snoop->fetch('http://api.bit.ly/shorten?'. 'version=2.0.1&'. 'longUrl='.$permalink.'&'. 'login='.$options['bitly_apilogin'].'&'. 'apiKey='.$options['bitly_apikey'].'&'. 'format=xml&'. 'history=1' ); if (strpos($snoop->response_code, '200')) { //get shortUrl from XML, without the use of a XML parser $post_url_match = null; preg_match('|(.*?)|is', $snoop->results, $post_url_match); $post_url = trim($post_url_match[1]); return $post_url; } else { return $permalink; } } else { //use awe.sm //get awe.sm API key $awesm_api_key = $options['awesm_apikey']; //only continue when API key is set if(empty($awesm_api_key)) { return $permalink; } // generate main awe.sm url $snoop = new Snoopy(); $snoop->agent = 'Twitter Publisher'; $snoop->submit( 'http://create.awe.sm/url.txt', array( 'target' => $permalink, 'version' => 1, 'share_type' => $sharetype, 'create_type' => $createtype, 'api_key' => $awesm_api_key ) ); if (strpos($snoop->response_code, '200')) { $post_url = trim($snoop->results); return $post_url; } else { return $permalink; } } } /** * returns the short url for this post * * @param int $post_id * @return string */ function twitter_publisher_short_url($post_id) { //get post_url $short_url = get_post_meta($post_id, 'twipub_short_url', true); //is there already a short url? if(empty($short_url)) { //get permalink $permalink = get_permalink($post_id); //generate short url $short_url = twitter_publisher_create_short_url($permalink, 'other', 'twitter-publisher-other'); //save short url add_post_meta($post_id, 'twipub_short_url', $short_url, true); } return $short_url; } /** * Adds a link to the menu in Wordpress' admin */ function twitter_publisher_configpagelink() { add_options_page('Twitter Publisher', 'Twitter Publisher', 8, basename(__FILE__), 'twitter_publisher_configpage'); } add_action('admin_menu', 'twitter_publisher_configpagelink'); /** * Adds a settings link to the plugin description row */ function twitter_publisher_filter_plugin_actions($links, $file) { //Static so we don't call plugin_basename on every plugin row. static $this_plugin; if ( ! $this_plugin ) $this_plugin = plugin_basename(__FILE__); if ( $file == $this_plugin ){ $settings_link = '' . __('Settings', 'twitter_publisher') . ''; array_unshift( $links, $settings_link ); // before other links } return $links; } add_filter( 'plugin_action_links', 'twitter_publisher_filter_plugin_actions', 10, 2 );