'.__('Spread.us settings', 'spreadus').'
';
}
/**
* Tweet about a new blog post on publish
*/
function spreadus_post($new_status, $old_status = '', $post = null, $tweet_manual = false)
{
// Action called, write a log
error_log('Spread action called for published post:' . PHP_EOL, 3, dirname(__FILE__) . DIRECTORY_SEPARATOR . 'debug.log' );
// If this is not a publish action, get out of here.
if($new_status != 'publish')
{
error_log(' - We have no business with the [' . $new_status . '] status, exiting.' . PHP_EOL . PHP_EOL, 3, dirname(__FILE__) . DIRECTORY_SEPARATOR . 'debug.log' );
return;
}
// Require simple_html_dom
require_once('simple_html_dom.php');
// Require debug
require_once('debug.php');
// Import Wordpress Core
global $wpdb;
global $blog_id;
// Get the post ID
$post_id = isset($post->ID) ? $post->ID : 0;
// Only spread once
$spread_already = get_post_meta($post_id, 'spreadus_spread', true);
if( !empty($spread_already) )
{
error_log(' - Post was spread already, exiting.' . PHP_EOL . PHP_EOL, 3, dirname(__FILE__) . DIRECTORY_SEPARATOR . 'debug.log' );
return;
}
error_log(' - Unspread post, check the date.' . PHP_EOL, 3, dirname(__FILE__) . DIRECTORY_SEPARATOR . 'debug.log' );
// Make sure the post is not older than 4 hours
if(strtotime($post->post_date) < strtotime("-4 hours", current_time('timestamp')))
{
update_post_meta($post_id, 'spreadus_spread', '1');
update_post_meta($post_id, 'spreadus_spread_date', 'too_old');
error_log(' - Post ' . $post_id . ' too old, marked as already spread & exiting.' . PHP_EOL . PHP_EOL, 3, dirname(__FILE__) . DIRECTORY_SEPARATOR . 'debug.log' );
return;
}
error_log(' - New post, passed time checks, allowed to make an API call.' . PHP_EOL, 3, dirname(__FILE__) . DIRECTORY_SEPARATOR . 'debug.log' );
// Get blog post title
$title = $post->post_title;
$body_html = str_get_html($post->post_content);
$first_image = "";
foreach($body_html->find('img') as $e){
$first_image = $e->src;
add_post_meta($post_id, 'spreadus_images', $first_image, true);
break;
}
//if($first_image)
$plain_text = $body_html->plaintext;
//get permalink
$post_url = get_permalink($post_id);
$post_categories = array();
foreach((get_the_category()) as $category) {
if($category->cat_name != "Uncategorized"){
$post_categories[]=$category->cat_name;
}
}
//get author
$author = get_userdata($post->post_author);
/**
* Get Spread.us site options
* These options are stored for the whole mu site
*/
$spreadus = get_site_option('spreadus.settings');
$spreadus['account_name'] = get_site_option('spreadus.account_name');
$spreadus['secret'] = get_site_option('spreadus.secret');
$spreadus['authenticated'] = get_site_option('spreadus.authenticated');
// Debugging
//add_post_meta($post_id, 'spreadus_debug', json_encode($spreadus), true);
// Try to Spread only when authenticated
if($spreadus['authenticated'])
{
// use default or user spreadus email adddress for publish information
$spreader_email = !empty($author->spreader_email) ? $author->spreader_email : isset($spreadus['default_publish_address']) ? $spreadus['default_publish_address'] : '';
// get author twitter account credentials
$author_twitter_username = isset($author->twitter_username) ? $author->twitter_username : '';
// get sub-blog twitter account credentials
//$twitter_publisher_options = get_option('twitter_publisher_options');
//$blog_twitter_username = isset($twitter_publisher_options['twipub_screen_name']) ? $twitter_publisher_options['twipub_screen_name'] : '';
$blog_twitter_username = '';
$author_name = isset($author->display_name) ? $author->display_name : isset($author->first_name) && isset($author->last_name) ? $author->first_name . ' ' . $author->last_name : '';
//if($options['debug']){
// add_post_meta($post_id, 'twipub_debug', 'Short url: '. $post_url, false);
//}
//var_dump($author);
//var_dump($spreadus_email);
//exit;
$hash = sha1(trim($spreadus['account_name']) . '_' . trim($spreader_email) . '_' . trim($spreadus['secret']) . '_' . trim($title) . '_' . trim($author_twitter_username) . '_' . trim($blog_twitter_username));
$request_args = array(
'hash' => $hash,
'account_name' => $spreadus['account_name'],
'email' => $spreader_email,
'title' => $title,
'text' => $plain_text,
'url' => $post_url,
'img_url' => $first_image,
'caption' => $spreadus['message_caption'],
'categories' => implode(',', $post_categories),
'author_twitter_username' => $author_twitter_username,
'blog_twitter_username' => $blog_twitter_username,
'author' => $author_name
);
//error_log(__FILE__." : ". __LINE__."\n", 3, "/var/log/spreadus/plugin.log" );
//error_log(print_r($request_args, true)."\n", 3, "/var/log/spreadus/plugin.log" );
// Spread.us testing overwrite
if(strpos($_SERVER['HTTP_HOST'], 'spread.us') !== false)
{
$spreadus_url = 'http://preproduction.spread.us';
}
else
{
$spreadus_url = 'http://spread.us';
}
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $spreadus_url . '/actions/spread_post.json' );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_TIMEOUT, 10 );
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, array( 'data' => json_encode($request_args) ) );
$response = curl_exec( $ch );
$http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
curl_close( $ch );
error_log(' - Response:' . PHP_EOL . ' - ' . print_r($response, true) . PHP_EOL . PHP_EOL, 3, dirname(__FILE__) . DIRECTORY_SEPARATOR . 'debug.log' );
//$request->post('http://spread.us/action/create', $request_args);
//add a flag to this blog post, so we only Tweet once about it
add_post_meta($post_id, 'spreadus_spread', '1', true);
add_post_meta($post_id, 'spreadus_spread_date', date('Y-m-d H:i:s'), true);
}
// cleanly return
return;
}
/**
* Add settings link to the plugin description row
*/
function spreadus_plugins_overview_link($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 )
{
$links[] = '' . __('Settings', 'spreadus') . '';
//array_unshift( $links, $settings_link ); // before other links
}
return $links;
}
/**
* Render queued notices
*/
function spreadus_admin_notices()
{
$notices = get_site_option('spreadus.notices');
if(is_array($notices))
{
foreach($notices as $notice)
{
echo $notice;
}
}
update_site_option('spreadus.notices', false);
}
/**
* Add link to the admin menu
*/
function spreadus_admin_menu_link() {
// Add a menu item to the settings menu
add_options_page('Spread.Us', 'Spread.us', 8, 'spreadus', 'spreadus_configpage');
}
/**
* Build settings page for Spread.us
*/
function spreadus_configpage()
{
require('settings.php');
$spreadus_settings->init();
}