/**
 * Hook for the crons to fire and send tweets
 * @param  id $post_id
 * @param  string $name
 * @return void
 */
function ppp_share_post($post_id, $name)
{
    global $ppp_options, $ppp_social_settings, $ppp_share_settings, $ppp_twitter_oauth;
    // If we've already started to share this, don't share it again.
    // Compensates for wp-cron's race conditions
    if (get_transient('ppp_sharing' . $name) === 'true') {
        return;
    }
    // For 60 seconds, don't allow another share to go for this post
    set_transient('ppp_sharing' . $name, 'true', 60);
    $share_message = ppp_tw_build_share_message($post_id, $name);
    $name_parts = explode('_', $name);
    $post_meta = get_post_meta($post_id, '_ppp_tweets', true);
    $this_share = $post_meta[$name_parts[1]];
    $attachment_id = isset($this_share['attachment_id']) ? $this_share['attachment_id'] : false;
    if (empty($attachment_id) && !empty($this_share['image'])) {
        $media = $this_share['image'];
    } else {
        $media = ppp_post_has_media($post_id, 'tw', ppp_tw_use_media($post_id, $name_parts[1]), $attachment_id);
    }
    $status['twitter'] = ppp_send_tweet($share_message, $post_id, $media);
    if (!empty($status['twitter']->id_str)) {
        $post = get_post($post_id);
        $author_id = $post->post_author;
        $author_rt = get_user_meta($author_id, '_ppp_share_scheduled', true);
        if ($author_rt) {
            $twitter_user = new PPP_Twitter_User($author_id);
            $twitter_user->retweet($status['twitter']->id_str);
        }
    }
    if (isset($ppp_options['enable_debug']) && $ppp_options['enable_debug'] == '1') {
        update_post_meta($post_id, '_ppp-' . $name . '-status', $status);
    }
}
/**
 * Adds in the Post Promoter Pro Preferences Profile Section
 * @param  object $user The User object being viewed
 * @return void         Displays HTML
 */
function ppp_tw_profile_settings($user)
{
    if ($user->ID == get_current_user_id() && !current_user_can('edit_posts')) {
        return;
    }
    if ($user->ID !== get_current_user_id() && !current_user_can('manage_options')) {
        return;
    }
    $connected = false;
    ?>
	<h3><?php 
    _e('Post Promoter Pro', 'ppp-txt');
    ?>
</h3>
	<table class="form-table">
		<tr>
			<th><?php 
    _e('Connect to Twitter', 'ppp-txt');
    ?>
</th>
			<td>
			<?php 
    $twitter = new PPP_Twitter_User(get_current_user_id());
    $tw_user = get_user_meta($user->ID, '_ppp_twitter_data', true);
    if (empty($tw_user)) {
        $tw_authurl = $twitter->get_auth_url(admin_url('user-edit.php?user_id=' . $user->ID));
        echo '<a href="' . $tw_authurl . '"><img src="' . PPP_URL . '/includes/images/sign-in-with-twitter-gray.png" /></a>';
    } else {
        $connected = true;
        ?>
				<p><strong><?php 
        _e('Signed in as', 'ppp-txt');
        ?>
: </strong><?php 
        echo $tw_user['user']->screen_name;
        ?>
</p>
				<p>
					<a class="button-primary" href="<?php 
        echo admin_url('user-edit.php?user_id=' . $user->ID . '&ppp_social_disconnect=true&ppp_network=twitter&user_id=' . $user->ID);
        ?>
" ><?php 
        _e('Disconnect from Twitter', 'ppp-txt');
        ?>
</a>&nbsp;
					<a class="button-secondary" href="https://twitter.com/settings/applications" target="blank"><?php 
        _e('Revoke Access via Twitter', 'ppp-txt');
        ?>
</a>
				</p>
				<?php 
    }
    ?>
			</td>
		</tr>

		<?php 
    if ($connected) {
        ?>
		<?php 
        $share_on_publish = get_user_meta($user->ID, '_ppp_share_on_publish', true);
        $share_scheduled = get_user_meta($user->ID, '_ppp_share_scheduled', true);
        ?>
		<tr>
			<th><?php 
        _e('Sharing Options', 'ppp-txt');
        ?>
</th>
			<td>
				<input type="checkbox" <?php 
        checked(true, $share_on_publish, true);
        ?>
name="share_on_publish" value="1" id="share-on-publish" /> <label for="share-on-publish"><?php 
        _e('Retweet my posts when they are published', 'ppp-txt');
        ?>
</label>
				<p class="description"><?php 
        printf(__('Retweet the primary account as %s when it Tweets on publishing my posts.', 'ppp-txt'), $tw_user['user']->screen_name);
        ?>
</p>
			</td>
		</tr>
		<tr>
			<th></th>
			<td>
				<input type="checkbox" <?php 
        checked(true, $share_scheduled, true);
        ?>
 name="share_scheduled" value="1" id="share-scheduled" /> <label for="share-scheduled"><?php 
        _e('Retweet scheduled shares of my posts', 'ppp-txt');
        ?>
</label>
				<p class="description"><?php 
        printf(__('When the primary account schedules a Tweet for one of my posts, Retweet it as %s.', 'ppp-txt'), $tw_user['user']->screen_name);
        ?>
</p>
			</td>
		</tr>

		<?php 
    }
    ?>
	</table>
	<?php 
}