Example #1
0
 public function test_get_timestamps()
 {
     $timestamps = ppp_get_timestamps($this->_post_id);
     $this->assertInternalType('array', $timestamps);
     $this->assertEmpty($timestamps);
     // Verify a past date doesn't save
     $tweet_data[1] = array('date' => '1/1/2015', 'time' => '12:00pm');
     update_post_meta($this->_post_id, '_ppp_tweets', $tweet_data);
     $timestamps = ppp_get_timestamps($this->_post_id);
     $this->assertEmpty($timestamps);
     $tweet_data[1] = array('date' => date('m/d/Y', time() + 86400), 'time' => '12:00pm');
     update_post_meta($this->_post_id, '_ppp_tweets', $tweet_data);
     $timestamps = ppp_get_timestamps($this->_post_id);
     $this->assertNotEmpty($timestamps);
     $found_timestamp = strtotime($tweet_data[1]['date'] . ' ' . $tweet_data[1]['time']);
     $timestamp = key($timestamps);
     $this->assertEquals($timestamp, $found_timestamp);
     $this->assertEquals('sharedate_1_' . $this->_post_id, $timestamps[$found_timestamp]);
     $tweet_data[1] = array('date' => date('m/d/Y', time() + 86400), 'time' => '12:00pm');
     $tweet_data[2] = array('date' => '1/1/2015', 'time' => '12:00pm');
     update_post_meta($this->_post_id, '_ppp_tweets', $tweet_data);
     $timestamps = ppp_get_timestamps($this->_post_id);
     $this->assertEquals(1, count($timestamps));
     $tweet_data[1] = array('date' => date('m/d/Y', time() + 86400), 'time' => '12:00pm');
     $tweet_data[2] = array('date' => date('m/d/Y', time() + 86400 + 86400), 'time' => '12:00pm');
     update_post_meta($this->_post_id, '_ppp_tweets', $tweet_data);
     $timestamps = ppp_get_timestamps($this->_post_id);
     $this->assertEquals(2, count($timestamps));
     $found_timestamp = strtotime($tweet_data[2]['date'] . ' ' . $tweet_data[2]['time']);
     $this->assertEquals('sharedate_2_' . $this->_post_id, $timestamps[$found_timestamp]);
 }
/**
 * Schedule social media posts with wp_schedule_single_event
 * @param  id $post_id
 * @param  object $post
 * @return void
 */
function ppp_schedule_share($post_id, $post)
{
    global $ppp_options;
    $allowed_post_types = isset($ppp_options['post_types']) ? $ppp_options['post_types'] : array();
    $allowed_post_types = apply_filters('ppp_schedule_share_post_types', $allowed_post_types);
    if (!isset($_POST['post_status']) || !array_key_exists($post->post_type, $allowed_post_types)) {
        return;
    }
    ppp_remove_scheduled_shares($post_id);
    if ($_POST['post_status'] == 'publish' && $_POST['original_post_status'] == 'publish' || $_POST['post_status'] == 'future' && $_POST['original_post_status'] == 'future') {
        // Be sure to clear any currently scheduled tweets so we aren't creating multiple instances
        // This will stop something from moving between draft and post and continuing to schedule tweets
        ppp_remove_scheduled_shares($post_id);
    }
    if ($_POST['post_status'] == 'publish' && $_POST['original_post_status'] != 'publish' || $_POST['post_status'] == 'future' && $_POST['original_post_status'] == 'future' || $_POST['post_status'] == 'publish' && $_POST['original_post_status'] == 'publish') {
        // Updating an already published post
        global $ppp_options, $ppp_social_settings;
        $timestamps = ppp_get_timestamps($post_id);
        foreach ($timestamps as $timestamp => $name) {
            wp_schedule_single_event($timestamp, 'ppp_share_post_event', array($post_id, $name));
        }
    }
}