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 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 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; }
function jd_twit_comment($comment_id, $approved) { $_t = get_comment($comment_id); $post_ID = $_t->comment_post_ID; $commenter = $_t->comment_author; $jd_tweet_this = get_post_meta($post_ID, '_jd_tweet_this', TRUE); if ($jd_tweet_this != 'no' && $_t->comment_approved == 1) { // comments only tweeted on posts which are tweeted $post_info = jd_post_info($post_ID); $sentence = ''; $sentence = stripcslashes(get_option('comment-published-text')); if ($post_info['shortUrl'] != '') { $shrink = $post_info['shortUrl']; } else { $shrink = jd_shorten_link($post_info['postLink'], $post_info['postTitle'], $post_ID); store_url($post_ID, $shrink); } $sentence = jd_truncate_tweet($sentence, $post_info, $shrink, $post_ID); $sentence = str_replace("#commenter#", $commenter, $sentence); if ($sentence != '') { $sendToTwitter = jd_doTwitterAPIPost($sentence); } } return $post_ID; }
/** * 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); } } } }