/**
 * 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);
    }
}
 /**
  * Prepare the data for the WP List Table
  * @return void
  */
 public function prepare_items()
 {
     $columns = $this->get_columns();
     $hidden = array();
     $data = array();
     $sortable = false;
     $this->_column_headers = array($columns, $hidden, $sortable);
     $per_page = 25;
     $crons = ppp_get_shceduled_crons();
     $cron_tally = array();
     foreach ($crons as $key => $cron) {
         $ppp_data = $cron;
         $timestamp = $ppp_data['timestamp'];
         $cron_tally[$timestamp] = isset($cron_tally[$timestamp]) ? $cron_tally[$timestamp] + 1 : 1;
         $name_parts = explode('_', $ppp_data['args'][1]);
         $index = $name_parts[1];
         $service = isset($name_parts[3]) ? $name_parts[3] : 'tw';
         $builder = 'ppp_' . $service . '_build_share_message';
         $post_meta = get_post_meta($ppp_data['args'][0], '_ppp_tweets', true);
         $image_url = '';
         if (!empty($post_meta[$index]['attachment_id'])) {
             $image_url = ppp_post_has_media($ppp_data['args'][0], 'tw', true, $post_meta[$index]['attachment_id']);
         } elseif (!empty($post_meta[$index]['image'])) {
             $image_url = $post_meta[$index]['image'];
         }
         $conflict = $cron_tally[$timestamp] > 1 ? true : false;
         $data[$key] = array('post_id' => $ppp_data['args'][0], 'post_title' => get_the_title($ppp_data['args'][0]), 'service' => $service, 'index' => $index, 'date' => $timestamp + get_option('gmt_offset') * 3600, 'content' => $builder($ppp_data['args'][0], $ppp_data['args'][1], false), 'name' => 'sharedate_' . $index . '_' . $ppp_data['args'][0], 'conflict' => $conflict);
         if (!empty($image_url)) {
             $data[$key]['image_url'] = $image_url;
         }
     }
     $total_items = count($data);
     $offset = isset($_GET['paged']) ? $_GET['paged'] : 1;
     $data = array_slice($data, ($offset - 1) * $per_page, $per_page, true);
     $this->items = $data;
     $this->set_pagination_args(array('total_items' => $total_items, 'per_page' => $per_page, 'total_pages' => ceil($total_items / $per_page)));
     $this->items = $data;
 }
/**
 * Share a Facebook post on Publish
 * @param  string $old_status The old post status
 * @param  string $new_status The new post status
 * @param  object $post       The Post object
 * @return void
 */
function ppp_fb_share_on_publish($new_status, $old_status, $post)
{
    global $ppp_options;
    $from_meta = get_post_meta($post->ID, '_ppp_fb_share_on_publish', true);
    $from_post = isset($_POST['_ppp_fb_share_on_publish']);
    if (empty($from_meta) && empty($from_post)) {
        return;
    }
    // Determine if we're seeing the share on publish in meta or $_POST
    if ($from_meta && !$from_post) {
        $ppp_share_on_publish_title = get_post_meta($post->ID, '_ppp_fb_share_on_publish_title', true);
    } else {
        $ppp_share_on_publish_title = isset($_POST['_ppp_fb_share_on_publish_title']) ? $_POST['_ppp_fb_share_on_publish_title'] : '';
    }
    $thumbnail = ppp_post_has_media($post->ID, 'fb', true);
    $name = 'sharedate_0_' . $post->ID . '_fb';
    $default_title = isset($ppp_options['default_text']) ? $ppp_options['default_text'] : '';
    // If an override was found, use it, otherwise try the default text content
    $share_title = isset($ppp_share_on_publish_title) && !empty($ppp_share_on_publish_title) ? $ppp_share_on_publish_title : $default_title;
    // If the content is still empty, just use the post title
    $share_title = isset($share_title) && !empty($share_title) ? $share_title : get_the_title($post->ID);
    $share_title = apply_filters('ppp_share_content', $share_title, array('post_id' => $post->ID));
    $share_link = ppp_generate_link($post->ID, $name, true);
    $status['facebook'] = ppp_fb_share($share_link, $share_title, $thumbnail);
    if (isset($ppp_options['enable_debug']) && $ppp_options['enable_debug'] == '1') {
        update_post_meta($post->ID, '_ppp-' . $name . '-status', $status);
    }
}
/**
 * Sets an array of names and content for Twitter Card Meta
 * for easy filtering by devs
 *
 * @since  2.2
 * @return array The array of keys and values for the Twitter Meta
 */
function ppp_tw_default_meta_elements()
{
    global $post, $ppp_social_settings;
    $elements = array();
    $image_url = ppp_post_has_media($post->ID, 'tw', true);
    if ($image_url) {
        $elements['twitter:card'] = 'summary_large_image';
        $elements['twitter:image:src'] = $image_url;
    } else {
        $elements['twitter:card'] = 'summary';
    }
    $elements['twitter:site'] = '@' . $ppp_social_settings['twitter']['user']->screen_name;
    $elements['twitter:title'] = esc_attr(strip_tags($post->post_title));
    $elements['twitter:description'] = esc_attr(strip_tags(ppp_tw_get_card_description()));
    $author_twitter_handle = get_user_meta($post->post_author, 'twitter', true);
    if (!empty($author_twitter_handle)) {
        if (strpos($author_twitter_handle, '@') === false) {
            $author_twitter_handle = '@' . $author_twitter_handle;
        }
        $elements['twitter:creator'] = esc_attr(strip_tags($author_twitter_handle));
    }
    return apply_filters('ppp_tw_card_elements', $elements);
}