function wpt_auto_schedule()
{
    $notify_user = get_option('wpt_autopost_notification');
    // select post from criteria:
    $template = get_option('wpt_schedule_template') != '' ? get_option('wpt_schedule_template') : '#title# #url#';
    $post = wpt_select_post();
    // $post = $post_ID
    if (!$post) {
        if ($notify_user && is_email($notify_user)) {
            wp_mail($notify_user, __('Failed to select any post for Tweeting', 'wp-tweets-pro'), __('WP Tweets PRO did not find a valid post to repost. This may mean that there are no posts between the selected minimum and maximum ages.', 'wp-tweets-pro'));
        }
        die;
    }
    /**
     * If a custom template is set on this post & the option to use them is enabled, use the custom template instead of the setting.
     */
    $custom_template = get_post_meta($post_ID, '_jd_twitter', true);
    $template = get_option('wpt_schedule_custom') == 'true' && $custom_template != '' ? $custom_template : $template;
    $post_info = wpt_post_info($post);
    $sentence = jd_truncate_tweet($template, $post_info, $post);
    $media = get_option('wpt_media') == 1 && (has_post_thumbnail($post) || wpt_post_attachment($post)) ? true : false;
    $media = apply_filters('wpt_upload_media', $media, $post);
    // filter based on post ID
    $tweet = jd_doTwitterAPIPost($sentence, false, $post, $media);
    do_action('wpt_autopost', $tweet, $post);
    // if Tweeted successfully, add to post meta so this will not be Tweeted again until all old posts have been Tweeted.
    if ($tweet) {
        if ($notify_user && is_email($notify_user)) {
            wp_mail($notify_user, __('Autoposted Tweet succeeded', 'wp-tweets-pro'), sprintf(__("Tweet autoposted: %s", 'wp-tweets-pro'), $sentence));
        }
        update_post_meta($post, '_wpt_autoposted', '1');
    } else {
        if ($notify_user && is_email($notify_user)) {
            $log = wpt_log('wpt_status_message', $post);
            wp_mail($notify_user, __('Autoposted Tweet failed', 'wp-tweets-pro'), sprintf(__("Site failed to automatically post. Tweet attempted: %s", 'wp-tweets-pro') . "\n\n" . __('Error message from Twitter:', 'wp-tweets-pro') . ' ' . $log, $sentence));
        }
    }
}
function wpt_set_comment_tweet($comment_id, $approved)
{
    $_t = get_comment($comment_id);
    $post_ID = $_t->comment_post_ID;
    $commenter = $_t->comment_author;
    $comment = $_t->comment_content;
    $user_id = $_t->user_id;
    $excerpt_length = get_option('jd_post_excerpt');
    $comment_excerpt = @mb_substr(strip_tags(strip_shortcodes($comment)), 0, $excerpt_length);
    $comment_date = $_t->comment_date;
    $dateformat = get_option('jd_date_format') == '' ? get_option('date_format') : get_option('jd_date_format');
    $comment_date = mysql2date($dateformat, $comment_date);
    $jd_tweet_this = get_post_meta($post_ID, '_jd_tweet_this', TRUE);
    $post_info = wpt_post_info($post_ID);
    $sentence = '';
    $comment_url = apply_filters('wptt_shorten_link', get_comment_link($comment_id), $post_info['postTitle'], $post_ID, false);
    $sentence = stripcslashes(get_option('comment-published-text'));
    $sentence = jd_truncate_tweet($sentence, $post_info, $post_ID);
    $sentence = str_replace("#commenter#", $commenter, $sentence);
    $sentence = str_replace("#comment#", $comment_excerpt, $sentence);
    $sentence = str_replace("#comment_date#", $comment_date, $sentence);
    $sentence = str_replace("#comment_url#", $comment_url, $sentence);
    if ($sentence != '') {
        $comment_tweet = apply_filters('wpt_filter_comment_tweet', $sentence, $_t);
        add_comment_meta($comment_id, 'wpt_comment_tweet', $comment_tweet, true);
    }
    if (user_can($user_id, 'moderate_comments') || $approved == 1) {
        wpt_twit_comment($_t);
    }
    return $post_ID;
}
示例#3
0
/**
 * Handle Tweets sent via Ajax Tweet Now/Schedule Tweet buttons.
 *
 * @return string Confirmation message indicating success or failure of Tweeting.
 */
function wpt_ajax_tweet()
{
    if (!check_ajax_referer('wpt-tweet-nonce', 'security', false)) {
        echo "Invalid Security Check";
        die;
    }
    $action = $_REQUEST['tweet_action'] == 'tweet' ? 'tweet' : 'schedule';
    // This isn't used right now, because of time.
    $authors = isset($_REQUEST['tweet_auth']) && $_REQUEST['tweet_auth'] != null ? $_REQUEST['tweet_auth'] : false;
    $current_user = wp_get_current_user();
    if (function_exists('wpt_pro_exists') && wpt_pro_exists()) {
        if (wtt_oauth_test($current_user->ID, 'verify')) {
            $auth = $user_ID = $current_user->ID;
        } else {
            $auth = false;
            $user_ID = $current_user->ID;
        }
    } else {
        $auth = false;
        $user_ID = $current_user->ID;
    }
    if (current_user_can('wpt_can_tweet')) {
        $options = get_option('wpt_post_types');
        $post_ID = intval($_REQUEST['tweet_post_id']);
        $type = get_post_type($post_ID);
        $default = isset($options[$type]['post-edited-text']) ? $options[$type]['post-edited-text'] : '';
        $sentence = isset($_REQUEST['tweet_text']) && trim($_REQUEST['tweet_text']) != '' ? $_REQUEST['tweet_text'] : $default;
        $sentence = stripcslashes(trim($sentence));
        $post_info = wpt_post_info($post_ID);
        $sentence = jd_truncate_tweet($sentence, $post_info, $post_ID, false, $user_ID);
        $schedule = isset($_REQUEST['tweet_schedule']) ? strtotime($_REQUEST['tweet_schedule']) : rand(60, 240);
        $print_schedule = date_i18n(get_option('date_format') . ' @ ' . get_option('time_format'), $schedule);
        $offset = 60 * 60 * get_option('gmt_offset');
        $schedule = $schedule - $offset;
        $media = wpt_post_with_media($post_ID, $post_info);
        switch ($action) {
            case 'tweet':
                jd_doTwitterAPIPost($sentence, $auth, $post_ID, $media);
                break;
            case 'schedule':
                wp_schedule_single_event($schedule, 'wpt_schedule_tweet_action', array('id' => $auth, 'sentence' => $sentence, 'rt' => 0, 'post_id' => $post_ID));
                break;
        }
        $return = $action == 'tweet' ? wpt_log('wpt_status_message', $post_ID) : "Tweet scheduled: '{$sentence}' for {$print_schedule}";
        echo $return;
    } else {
        echo __('You are not authorized to perform this action', 'wp-to-twitter');
    }
    die;
}
/**
 * Schedule a custom Tweet
 */
function wpt_schedule_custom_tweet($post)
{
    $offset = 60 * 60 * get_option('gmt_offset');
    if (isset($post['submit-type']) && $post['submit-type'] == 'schedule-tweet') {
        if (isset($post['alt_author'])) {
            $auth = isset($post['author']) && $post['author'] != '' ? (int) $post['author'] : false;
            $auth = isset($post['alt_author']) && $post['alt_author'] == 'false' ? $auth : (int) $post['alt_author'];
            $auth = isset($post['alt_author']) && $post['alt_author'] == 'main' ? false : $auth;
            if ($auth && get_user_meta($auth, 'wtt_twitter_username', true) == '') {
                $auth = false;
            }
        } else {
            $auth = false;
        }
        $encoding = get_option('blog_charset');
        if ($encoding == '') {
            $encoding = 'UTF-8';
        }
        $sentence = isset($post['tweet']) ? html_entity_decode(stripcslashes($post['tweet']), ENT_COMPAT, $encoding) : '';
        $orig_sentence = $sentence;
        $post_id = isset($post['post']) ? (int) $post['post'] : '';
        if (isset($post['filter']) && $post['filter'] == 'on') {
            $post_info = wpt_post_info($post_id);
            $sentence = jd_truncate_tweet($sentence, $post_info, $post_id, false, false);
        }
        if (isset($post['autoschedule']) && $post['autoschedule'] == 'true') {
            $max = DAY_IN_SECONDS;
            $time = mt_rand(300, $max);
            $blackout = get_option('wpt_blackout');
            $from = $blackout['from'];
            $to = $blackout['to'];
            if ($from == $to) {
                $time = current_time('timestamp') + mt_rand(300, $max);
            } else {
                $time = current_time('timestamp') + mt_rand(300, $max);
                $time = wpt_test_time($time, $from, $to);
            }
        } else {
            $time = isset($post['time']) && isset($post['date']) ? strtotime($post['date'] . ' ' . $post['time']) : '';
            $time = $time > current_time('timestamp') ? $time : false;
            $time = $time ? $time - $offset : $time;
        }
        if (!$sentence || !$post) {
            return array('message' => "<div class='error'><p>" . __('You must include a custom tweet text and a post ID to associate the tweet with.', 'wp-tweets-pro') . "</p></div>", 'tweet' => $sentence, 'post' => $post_id);
        } else {
            if (!$time) {
                return array('message' => "<div class='error'><p>" . __('The time provided was either invalid or in the past.', 'wp-tweets-pro') . "</p></div>", 'tweet' => $sentence, 'post' => $post_id);
            } else {
                if (!isset($_POST['wpt_recurrence']) || $_POST['wpt_recurrence'] == '') {
                    wp_schedule_single_event($time, 'wpt_schedule_tweet_action', array('id' => $auth, 'sentence' => $sentence, 'rt' => 0, 'post_id' => $post_id));
                } else {
                    $recurrence = sanitize_text_field($_POST['wpt_recurrence']);
                    wp_schedule_event($time, $recurrence, 'wpt_recurring_tweets', array('id' => $auth, 'sentence' => $orig_sentence, 'rt' => 0, 'post_id' => $post_id));
                }
                return array('message' => "<div class='updated'><p>" . __('Your custom Tweet has been scheduled.', 'wp-tweets-pro') . "</p></div>", 'tweet' => $sentence, 'post' => $post_id);
            }
        }
    }
}