示例#1
0
function jd_twit($post_ID, $type = 'instant')
{
    if (WPT_DEBUG && function_exists('wpt_pro_exists')) {
        wpt_mail("0: jd_twit running #{$post_ID}", "Post ID: {$post_ID} / {$type}");
        // DEBUG
    }
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || wp_is_post_revision($post_ID)) {
        return $post_ID;
    }
    wpt_check_version();
    $jd_tweet_this = get_post_meta($post_ID, '_jd_tweet_this', true);
    $newpost = $oldpost = $is_inline_edit = false;
    $sentence = $template = '';
    if (get_option('wpt_inline_edits') != 1) {
        if (isset($_POST['_inline_edit']) || isset($_REQUEST['bulk_edit'])) {
            return;
        }
    } else {
        if (isset($_POST['_inline_edit']) || isset($_REQUEST['bulk_edit'])) {
            $is_inline_edit = true;
        }
    }
    if (WPT_DEBUG && function_exists('wpt_pro_exists')) {
        wpt_mail("1: JD Tweet This Value: #{$post_ID}", "Tweet this: {$jd_tweet_this} /" . get_option('jd_tweet_default') . " / {$type}");
        // DEBUG
    }
    if (get_option('jd_tweet_default') == 0) {
        $test = $jd_tweet_this != 'no' ? true : false;
    } else {
        $test = $jd_tweet_this == 'yes' ? true : false;
    }
    if ($test) {
        // test switch: depend on default settings.
        $post_info = jd_post_info($post_ID);
        $media = wpt_post_with_media($post_ID, $post_info);
        if (function_exists('wpt_pro_exists') && wpt_pro_exists() == true) {
            $auth = get_option('wpt_cotweet_lock') == 'false' || !get_option('wpt_cotweet_lock') ? $post_info['authId'] : get_option('wpt_cotweet_lock');
        } else {
            $auth = $post_info['authId'];
        }
        /* debug data */
        if (WPT_DEBUG && function_exists('wpt_pro_exists')) {
            wpt_mail("2: POST Debug Data #{$post_ID}", "Post_Info: " . print_r($post_info, 1) . "\n\nPOST: " . print_r($_POST, 1) . " / {$type}");
        }
        if (function_exists('wpt_pro_exists') && wpt_pro_exists() == true && function_exists('wpt_filter_post_info')) {
            $filter = wpt_filter_post_info($post_info);
            if ($filter == true) {
                if (WPT_DEBUG && function_exists('wpt_pro_exists')) {
                    wpt_mail("3a: Post filtered: #{$post_ID}", print_r($post_info, 1) . " / {$type}");
                }
                return false;
            }
        }
        /* Filter Tweet based on POST data -- allows custom filtering of unknown plug-ins, etc. */
        $filter = apply_filters('wpt_filter_post_data', false, $_POST);
        if ($filter) {
            return false;
        }
        $post_type = $post_info['postType'];
        if ($type == 'future' || get_post_meta($post_ID, 'wpt_publishing') == 'future') {
            $new = 1;
            // if this is a future action, then it should be published regardless of relationship
            if (WPT_DEBUG && function_exists('wpt_pro_exists')) {
                wpt_mail("4: Future post: #{$post_ID}", print_r($post_info, 1) . " / {$type}");
            }
            delete_post_meta($post_ID, 'wpt_publishing');
        } else {
            // if the post modified date and the post date are the same, this is new.
            // true if first date before or equal to last date
            $new = wpt_date_compare($post_info['_postModified'], $post_info['_postDate']);
        }
        // post is not previously published but has been backdated:
        // (post date is edited, but save option is 'publish')
        if ($new == 0 && (isset($_POST['edit_date']) && $_POST['edit_date'] == 1 && !isset($_POST['save']))) {
            $new = 1;
        }
        // can't catch posts that were set to a past date as a draft, then published.
        $post_type_settings = get_option('wpt_post_types');
        $post_types = array_keys($post_type_settings);
        if (in_array($post_type, $post_types)) {
            // identify whether limited by category/taxonomy
            $continue = wpt_category_limit($post_type, $post_info, $post_ID);
            if ($continue == false) {
                return;
            }
            // create Tweet and ID whether current action is edit or new
            $cT = get_post_meta($post_ID, '_jd_twitter', true);
            if (isset($_POST['_jd_twitter']) && $_POST['_jd_twitter'] != '') {
                $cT = $_POST['_jd_twitter'];
            }
            $customTweet = $cT != '' ? stripcslashes(trim($cT)) : '';
            // if ops is set and equals 'publish', this is being edited. Otherwise, it's a new post.
            if ($new == 0 || $is_inline_edit == true) {
                // if this is an old post and editing updates are enabled
                if (get_option('jd_tweet_default_edit') == 1) {
                    $jd_tweet_this = apply_filters('wpt_tweet_this_edit', $jd_tweet_this, $_POST);
                    if ($jd_tweet_this != 'yes') {
                        if (WPT_DEBUG && function_exists('wpt_pro_exists')) {
                            wpt_mail("3c: Tweet this: not equal to yes", "Exit // Post ID: {$post_ID}");
                            // DEBUG
                        }
                        return;
                    }
                }
                if (WPT_DEBUG && function_exists('wpt_pro_exists')) {
                    wpt_mail("4a: Edited post #{$post_ID}", "Tweet this: " . print_r($post_info, 1) . " / {$type}");
                    // DEBUG
                }
                if ($post_type_settings[$post_type]['post-edited-update'] == '1') {
                    $nptext = stripcslashes($post_type_settings[$post_type]['post-edited-text']);
                    $oldpost = true;
                }
            } else {
                if (WPT_DEBUG && function_exists('wpt_pro_exists')) {
                    wpt_mail("4b: New Post #{$post_ID}", "Tweet this: " . print_r($post_info, 1) . " / {$type}");
                    // DEBUG
                }
                if ($post_type_settings[$post_type]['post-published-update'] == '1') {
                    $nptext = stripcslashes($post_type_settings[$post_type]['post-published-text']);
                    $newpost = true;
                }
            }
            if ($newpost || $oldpost) {
                $template = $customTweet != "" ? $customTweet : $nptext;
                $sentence = jd_truncate_tweet($template, $post_info, $post_ID);
                if (WPT_DEBUG && function_exists('wpt_pro_exists')) {
                    wpt_mail("5: Tweet Truncated #{$post_ID}", "Truncated Tweet: {$sentence} / {$template} / {$type}");
                    // DEBUG
                }
                if (function_exists('wpt_pro_exists') && wpt_pro_exists() == true) {
                    $sentence2 = jd_truncate_tweet($template, $post_info, $post_ID, false, $auth);
                }
            }
            if ($sentence != '') {
                // WPT PRO //
                if (function_exists('wpt_pro_exists') && wpt_pro_exists() == true) {
                    $wpt_selected_users = $post_info['wpt_authorized_users'];
                    /* set up basic author/main account values */
                    $auth_verified = wtt_oauth_test($auth, 'verify');
                    if (empty($wpt_selected_users) && get_option('jd_individual_twitter_users') == 1) {
                        $wpt_selected_users = $auth_verified ? array($auth) : array(false);
                    }
                    if ($post_info['wpt_cotweet'] == 1 || get_option('jd_individual_twitter_users') != 1) {
                        $wpt_selected_users['main'] = false;
                    }
                    // filter selected users before using
                    $wpt_selected_users = apply_filters('wpt_filter_users', $wpt_selected_users, $post_info);
                    if ($post_info['wpt_delay_tweet'] == 0 || $post_info['wpt_delay_tweet'] == '' || $post_info['wpt_no_delay'] == 'on') {
                        foreach ($wpt_selected_users as $acct) {
                            if (wtt_oauth_test($acct, 'verify')) {
                                $tweet = jd_doTwitterAPIPost($sentence2, $acct, $post_ID, $media);
                            }
                        }
                    } else {
                        foreach ($wpt_selected_users as $acct) {
                            if ($auth != $acct) {
                                $offset = rand(60, 480);
                                // offset by 1-8 minutes for additional users
                            } else {
                                $offset = 0;
                            }
                            if (wtt_oauth_test($acct, 'verify')) {
                                $time = apply_filters('wpt_schedule_delay', (int) $post_info['wpt_delay_tweet'] * 60, $acct);
                                $scheduled = wp_schedule_single_event(time() + $time + $offset, 'wpt_schedule_tweet_action', array('id' => $acct, 'sentence' => $sentence, 'rt' => 0, 'post_id' => $post_ID));
                                $tweet = true;
                                // if scheduled, return true.
                                if (WPT_DEBUG && function_exists('wpt_pro_exists')) {
                                    if ($acct) {
                                        $author_id = "#{$acct}";
                                    } else {
                                        $author_id = 'Main';
                                    }
                                    wpt_mail("7a: Tweet Scheduled for Auth ID {$author_id} #{$post_ID}", print_r(array('id' => $acct, 'sentence' => $sentence, 'rt' => 0, 'post_id' => $post_ID, 'timestamp' => time() + $time + $offset, 'current_time' => time(), 'timezone' => get_option('gmt_offset'), 'scheduled' => $scheduled, 'timestamp_string' => date('Y-m-d H:i:s', time() + $time + $offset), 'current_time_string' => date('Y-m-d H:i:s', time())), 1));
                                    // DEBUG
                                }
                            }
                        }
                    }
                    /* This cycle handles scheduling the automatic retweets */
                    if ($post_info['wpt_retweet_after'] != 0 && $post_info['wpt_no_repost'] != 'on') {
                        $repeat = $post_info['wpt_retweet_repeat'];
                        $first = true;
                        foreach ($wpt_selected_users as $acct) {
                            if (wtt_oauth_test($acct, 'verify')) {
                                for ($i = 1; $i <= $repeat; $i++) {
                                    switch ($i) {
                                        case 1:
                                            $prepend = get_option('wpt_prepend') == 1 ? '' : get_option('wpt_prepend_rt');
                                            $append = get_option('wpt_prepend') != 1 ? '' : get_option('wpt_prepend_rt');
                                            break;
                                        case 2:
                                            $prepend = get_option('wpt_prepend') == 1 ? '' : get_option('wpt_prepend_rt2');
                                            $append = get_option('wpt_prepend') != 1 ? '' : get_option('wpt_prepend_rt2');
                                            break;
                                        case 3:
                                            $prepend = get_option('wpt_prepend') == 1 ? '' : get_option('wpt_prepend_rt3');
                                            $append = get_option('wpt_prepend') != 1 ? '' : get_option('wpt_prepend_rt3');
                                            break;
                                    }
                                    if (get_option('wpt_custom_type') == 'template') {
                                        $retweet = jd_truncate_tweet(trim($prepend . $append), $post_info, $post_ID, true, $acct);
                                    } else {
                                        $retweet = jd_truncate_tweet(trim($prepend . $template . $append), $post_info, $post_ID, true, $acct);
                                    }
                                    // add original delay to schedule
                                    $delay = isset($post_info['wpt_delay_tweet']) ? (int) $post_info['wpt_delay_tweet'] * 60 : 0;
                                    /* Don't delay the first Tweet of the group */
                                    $offset = $first == true ? 0 : rand(60, 240);
                                    // delay each co-tweet by 1-4 minutes
                                    $time = apply_filters('wpt_schedule_retweet', $post_info['wpt_retweet_after'] * (60 * 60) * $i, $acct, $i, $post_info);
                                    wp_schedule_single_event(time() + $time + $offset + $delay, 'wpt_schedule_tweet_action', array('id' => $acct, 'sentence' => $retweet, 'rt' => $i, 'post_id' => $post_ID));
                                    $tweet = true;
                                    if ($i == 4) {
                                        break;
                                    }
                                }
                            }
                            $first = false;
                        }
                    }
                } else {
                    $tweet = jd_doTwitterAPIPost($sentence, false, $post_ID, $media);
                }
                // END WPT PRO //
                if ($tweet == false) {
                    update_option('wp_twitter_failure', '1');
                }
            }
        } else {
            if (WPT_DEBUG && function_exists('wpt_pro_exists')) {
                wpt_mail("3c: Not a Tweeted post type #{$post_ID}", "Post_Info: " . print_r($post_info, 1) . " / {$type}");
            }
            return $post_ID;
        }
    }
    return $post_ID;
}
示例#2
0
function jd_twit_xmlrpc($post_ID)
{
    wpt_check_version();
    $post_info = jd_post_info($post_ID);
    $post_type = $post_info['postType'];
    $settings = get_option('wpt_post_types');
    $post_types = array_keys($settings);
    // if the post modified date and the post date are the same, this is new.
    $new = wpt_date_compare($post_info['_postModified'], $post_info['_postDate']);
    if (in_array($post_type, $post_types)) {
        $sentence = '';
        if (get_option('jd_tweet_default') != '1' && get_option('jd_twit_remote') == '1') {
            $poststatus = $post_info['postStatus'];
            if ($poststatus == 'publish') {
                if ($new === 1 && $settings[$post_type]['post-published-update'] == '1') {
                    $sentence = stripcslashes($settings[$post_type]['post-published-text']);
                } else {
                    if ($new === 0 && $settings[$post_type]['post-edited-update'] == '1' && get_option('jd_tweet_default_edit') != '1') {
                        $sentence = stripcslashes($settings[$post_type]['post-edited-text']);
                    } else {
                        return;
                    }
                }
            } else {
                return;
            }
            $shrink = jd_shorten_link($post_info['postLink'], $post_info['postTitle'], $post_ID);
            // Stores the short URL in a custom field for later use as needed.
            store_url($post_ID, $shrink);
            // Check the length of the Tweet and truncate parts as necessary.
            $sentence = jd_truncate_tweet($sentence, $post_info, $shrink, $post_ID);
            if ($sentence != '') {
                if (get_option('jd_twit_cats') == '1') {
                    $continue = !in_allowed_category($post_info['categoryIds']) ? true : false;
                } else {
                    $continue = in_allowed_category($post_info['categoryIds']) ? true : false;
                }
                if (get_option('limit_categories') == '0') {
                    $continue = true;
                }
                if ($continue) {
                    // WPT PRO //
                    if (function_exists('wpt_pro_exists')) {
                        $auth = $post_info['authId'];
                        $user = get_userdata($auth);
                        //if ( user_can( $user, 'update_core' ) ) { $auth = false; } // This is the super admin. No, it's not.
                        if ($post_info['wpt_delay_tweet'] == 0 || $post_info['wpt_no_delay'] == 'on') {
                            $sendToTwitter = jd_doTwitterAPIPost($sentence, $auth);
                        } else {
                            $time = (int) $post_info['wpt_delay_tweet'] * 60;
                            wp_schedule_single_event(time() + $time, 'wpt_schedule_tweet_action', array('id' => $auth, 'sentence' => $sentence, 'rt' => 0));
                            $sendToTwitter = true;
                        }
                        if ($post_info['wpt_retweet_after'] != 0 && $post_info['wpt_no_repost'] != 'on') {
                            $repeat = $post_info['wpt_retweet_repeat'];
                            for ($i = 1; $i <= $repeat; $i++) {
                                if ($i == 1) {
                                    $prepend = get_option('wpt_prepend') == 1 ? '' : get_option('wpt_prepend_rt');
                                    $append = get_option('wpt_prepend') != 1 ? '' : get_option('wpt_prepend_rt');
                                    $retweet = jd_truncate_tweet(trim($prepend . $sentence . $append), $post_info, $shrink, $post_ID, true);
                                }
                                if ($i == 2) {
                                    $prepend = get_option('wpt_prepend') == 1 ? '' : get_option('wpt_prepend_rt2');
                                    $append = get_option('wpt_prepend') != 1 ? '' : get_option('wpt_prepend_rt2');
                                    $retweet = jd_truncate_tweet(trim($prepend . $sentence . $append), $post_info, $shrink, $post_ID, true);
                                }
                                if ($i == 3) {
                                    $retweet = $sentence;
                                }
                                if ($i == 4) {
                                    $prepend = get_option('wpt_prepend') == 1 ? '' : get_option('wpt_prepend_rt');
                                    $append = get_option('wpt_prepend') != 1 ? '' : get_option('wpt_prepend_rt');
                                    $retweet = jd_truncate_tweet(trim($prepend . $sentence . $append), $post_info, $shrink, $post_ID, true);
                                }
                                $time = $post_info['wpt_retweet_after'] * (60 * 60) * $i;
                                wp_schedule_single_event(time() + $time, 'wpt_schedule_tweet_action', array('id' => $auth, 'sentence' => $retweet, 'rt' => $i));
                                $sendToTwitter = true;
                                if ($i == 4) {
                                    break;
                                }
                            }
                        }
                    } else {
                        $sendToTwitter = jd_doTwitterAPIPost($sentence);
                    }
                    // END WPT PRO //
                    $sendToTwitter = jd_doTwitterAPIPost($sentence);
                    $jwt = get_post_meta($post_ID, '_jd_wp_twitter', true);
                    if (!is_array($jwt)) {
                        $jwt = array();
                    }
                    $jwt[] = urldecode($sentence);
                    update_post_meta($post_ID, '_jd_wp_twitter', $jwt);
                    if ($sendToTwitter == false) {
                        update_option('wp_twitter_failure', '1');
                    }
                }
            }
        }
        return $post_ID;
    }
}