/** * 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; }
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; }
function jd_truncate_tweet($tweet, $post, $post_ID, $retweet = false, $ref = false) { // media file occupies 22 characters, need to account for in shortening. $maxlength = apply_filters('wpt_max_length', array('with_media' => 116, 'without_media' => 139)); $length = wpt_post_with_media($post_ID, $post) ? $maxlength['with_media'] : $maxlength['without_media']; $tweet = apply_filters('wpt_tweet_sentence', $tweet, $post_ID); $tweet = trim(wpt_custom_shortcodes($tweet, $post_ID)); $encoding = get_option('blog_charset') != 'UTF-8' && get_option('blog_charset') != '' ? get_option('blog_charset') : 'UTF-8'; $diff = 0; // Add custom append/prepend fields to Tweet text if (get_option('jd_twit_prepend') != "" && $tweet != '') { $tweet = stripslashes(get_option('jd_twit_prepend')) . " " . $tweet; } if (get_option('jd_twit_append') != "" && $tweet != '') { $tweet = $tweet . " " . stripslashes(get_option('jd_twit_append')); } // there are no tags in this Tweet. Truncate and return. if (!wpt_has_tags($tweet)) { $post_tweet = mb_substr($tweet, 0, $length, $encoding); return apply_filters('wpt_custom_truncate', $post_tweet, $tweet, $post_ID, $retweet, 1); } // create full unconditional post tweet - prior to truncation // order matters; arrays have to be ordered the same way. $tags = array_map('wpt_make_tag', wpt_tags()); $values = wpt_create_values($post, $post_ID, $ref); $post_tweet = str_ireplace($tags, $values, $tweet); // check total length $str_length = mb_strlen(urldecode(wpt_normalize($post_tweet)), $encoding); /** * Check whether completed replacement is still within allowed length. * * If so, post as is. */ if ($str_length < $length + 1) { if (mb_strlen(wpt_normalize($post_tweet)) > $length + 1) { $post_tweet = mb_substr($post_tweet, 0, $length, $encoding); } return apply_filters('wpt_custom_truncate', $post_tweet, $tweet, $post_ID, $retweet, 2); // return early if all is well without replacements. } else { $has_excerpt_tag = wpt_has($tweet, '#post#'); $has_title_tag = wpt_has($tweet, '#title#'); $has_short_url = wpt_has($tweet, '#url#'); $has_long_url = wpt_has($tweet, '#longurl#'); $url_strlen = mb_strlen(urldecode(wpt_normalize($values['url'])), $encoding); $longurl_strlen = mb_strlen(urldecode(wpt_normalize($values['longurl'])), $encoding); /** * Tweet is too long, so we'll have to truncate that sucker. */ $length_array = wpt_length_array($values, $encoding); // Twitter's t.co shortener is mandatory. All URLS are max-character value set by Twitter. $tco = wpt_is_ssl($values['url']) ? 23 : 22; $order = get_option('wpt_truncation_order'); if (is_array($order)) { asort($order); $preferred = array(); foreach ($order as $k => $v) { if ($k == 'excerpt') { $k = 'post'; $value = $length_array['post']; } else { if ($k == 'blogname') { $k = 'blog'; $value = $length_array['blog']; } else { $value = $length_array[$k]; } } $preferred[$k] = $value; } } else { $preferred = $length_array; } if ($has_short_url) { $diff = $url_strlen - $tco > 0 ? $url_strlen - $tco : 0; } else { if ($has_long_url) { $diff = $longurl_strlen - $tco > 0 ? $longurl_strlen - $tco : 0; } } if ($str_length > $length + 1 + $diff) { foreach ($preferred as $key => $value) { // don't truncate content of post excerpt or title if those tags not in use if (!($key == 'excerpt' && !$has_excerpt_tag) && !($key == 'title' && !$has_title_tag)) { $str_length = mb_strlen(urldecode(wpt_normalize(trim($post_tweet))), $encoding); if ($str_length > $length + 1 + $diff) { $trim = $str_length - ($length + 1 + $diff); $old_value = $values[$key]; // prevent URL from being modified $post_tweet = str_ireplace(array($values['url'], $values['longurl']), array('#url#', '#longurl#'), $post_tweet); /** * These tag fields should be removed completely, rather than truncated. */ if (wpt_remove_tag($key)) { $new_value = ''; /** * These tag fields should have stray characters removed on word boundaries */ } else { if ($key == 'tags') { // remove any stray hash characters due to string truncation if (mb_strlen($old_value) - $trim <= 2) { $new_value = ''; } else { $new_value = $old_value; while (mb_strlen($old_value) - $trim < mb_strlen($new_value)) { $new_value = trim(mb_substr($new_value, 0, mb_strrpos($new_value, '#', $encoding) - 1)); } } /** * Just flat out truncate everything else cold. */ } else { // trim letters $new_value = mb_substr($old_value, 0, -$trim, $encoding); // trim rest of last word $last_space = strrpos($new_value, ' '); $new_value = mb_substr($new_value, 0, $last_space, $encoding); /** * If you want to add something like an ellipsis after truncation, use this filter. */ $new_value = apply_filters('wpt_filter_truncated_value', $new_value, $key, $old_value); } } $post_tweet = str_ireplace($old_value, $new_value, $post_tweet); // put URL back before checking length $post_tweet = str_ireplace(array('#url#', '#longurl#'), array($values['url'], $values['longurl']), $post_tweet); } else { if (mb_strlen(wpt_normalize($post_tweet), $encoding) > $length + 1 + $diff) { $post_tweet = mb_substr($post_tweet, 0, $length + $diff, $encoding); } } } } } // this is needed in case a tweet needs to be truncated outright and the truncation values aren't in the above. // 1) removes URL 2) checks length of remainder 3) Replaces URL if (mb_strlen(wpt_normalize($post_tweet)) > $length + 1) { $tweet = false; if ($has_short_url) { $url = $values['url']; $tag = '#url#'; } else { if ($has_long_url) { $url = $values['longurl']; $tag = '#longurl#'; } else { $post_tweet = mb_substr($post_tweet, 0, $length + $diff, $encoding); $tweet = true; } } if (!$tweet) { $temp = str_ireplace($url, $tag, $post_tweet); if (mb_strlen(wpt_normalize($temp)) > $length + 1 - ($tco - strlen($tag)) && $temp != $post_tweet) { if (stripos($temp, '#url#') === false && stripos($temp, '#longurl#') === false) { $post_tweet = trim(mb_substr($temp, 0, $length, $encoding)); } else { $post_tweet = trim(mb_substr($temp, 0, $length - $tco - 1, $encoding)); } // it's possible to trim off the #url# part in this process. If that happens, put it back. $sub_sentence = !wpt_has($post_tweet, $tag) && ($has_short_url || $has_long_url) ? $post_tweet . ' ' . $tag : $post_tweet; $post_tweet = str_ireplace($tag, $url, $sub_sentence); } } } } return apply_filters('wpt_custom_truncate', $post_tweet, $tweet, $post_ID, $retweet, 3); }