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)); } } }
/** * Handles a status update that includes an image. * @param type $url * @param type $args * @return boolean */ function handleMediaRequest($url, $args = array()) { /* Load tmhOAuth for Media uploads only when needed: https://github.com/themattharris/tmhOAuth */ if (!class_exists('tmhOAuth')) { require_once 'tmhOAuth/tmhOAuth.php'; require_once 'tmhOAuth/tmhUtilities.php'; } $auth = $args['auth']; if (!$auth) { $ack = get_option('app_consumer_key'); $acs = get_option('app_consumer_secret'); $ot = get_option('oauth_token'); $ots = get_option('oauth_token_secret'); } else { $ack = get_user_meta($auth, 'app_consumer_key', true); $acs = get_user_meta($auth, 'app_consumer_secret', true); $ot = get_user_meta($auth, 'oauth_token', true); $ots = get_user_meta($auth, 'oauth_token_secret', true); } // when performing as a scheduled action, need to include file.php if (!function_exists('get_home_path')) { require_once ABSPATH . 'wp-admin/includes/file.php'; } $connect = array('consumer_key' => $ack, 'consumer_secret' => $acs, 'user_token' => $ot, 'user_secret' => $ots); $tmhOAuth = new tmhOAuth($connect); $attachment = wpt_post_attachment($args['id']); // if install is at root, can query src path. Otherwise, need to take full image. $at_root = wp_make_link_relative(home_url()) == home_url() || wp_make_link_relative(home_url()) == '/' ? true : false; if ($at_root) { $image_sizes = get_intermediate_image_sizes(); if (in_array('large', $image_sizes)) { $size = 'large'; } else { $size = array_pop($image_sizes); } $upload = wp_get_attachment_image_src($attachment, apply_filters('wpt_upload_image_size', $size)); $path = get_home_path() . wp_make_link_relative($upload[0]); $image = str_replace('//', '/', $path); } else { $image = get_attached_file($attachment); } $image = apply_filters('wpt_image_path', $image, $args); $mime_type = get_post_mime_type($attachment); if (!$mime_type) { $mime_type = 'image/jpeg'; } $code = $tmhOAuth->request('POST', $url, array('media[]' => "@{$image};type={$mime_type};filename={$image}", 'status' => $args['status']), true, true); if (WPT_DEBUG && function_exists('wpt_pro_exists')) { $debug = array('media[]' => "@{$image};type={$mime_type};filename={$image}", 'status' => $args['status']); wpt_mail("Media Submitted - Post ID #{$args['id']}", print_r($debug, 1)); } $response = $tmhOAuth->response['response']; if (is_wp_error($response)) { return false; } $this->http_code = $code; $this->last_api_call = $url; $this->format = 'json'; $this->http_header = $response; return $response; }
function wpt_post_with_media($post_ID, $post_info = array()) { $return = false; if (isset($post_info['wpt_image']) && $post_info['wpt_image'] == 1) { return $return; } if (!function_exists('wpt_pro_exists') || get_option('wpt_media') != '1') { $return = false; } else { if (has_post_thumbnail($post_ID) || wpt_post_attachment($post_ID)) { $return = true; } } return apply_filters('wpt_upload_media', $return, $post_ID); }
function wpt_schedule_tweet($auth, $sentence, $rt, $post_ID) { wpt_mail("Scheduled Action Happening: #{$auth}", "{$sentence}, {$rt}, {$post_ID}"); // DEBUG $media = get_option('wpt_media') == '1' && (has_post_thumbnail($post_ID) || wpt_post_attachment($post_ID)) ? true : false; $media = apply_filters('wpt_scheduled_media', $media, $post_ID, $rt); // filter based on post ID // generate hash of this Tweet's data $attachment = wpt_post_attachment($post_ID); $hash = md5("{$sentence}, {$auth}, {$post_ID}, {$media}, {$attachment}"); // check whether this exact Tweet has already occurred $action = wpt_check_action($hash); // if action has already happened, don't perform again if (!$action) { $post = jd_doTwitterAPIPost($sentence, $auth, $post_ID, $media); wpt_register_action($hash); } }
function wpt_schedule_tweet($id, $sentence, $rt, $post_ID) { if (WPT_DEBUG && function_exists('wpt_pro_exists')) { wpt_mail("Scheduled Action Happening: #{$id}", "{$sentence}, {$rt}, {$post_ID}"); // DEBUG } // only upload media the first time... $this_rt = apply_filters('wpt_upload_media_count', 0); $media = get_option('wpt_media') == '1' && $rt == $this_rt && (has_post_thumbnail($post_ID) || wpt_post_attachment($post_ID)) ? true : false; if (get_post_meta($post_ID, '_wpt_image', true) == 1) { $media = false; } $media = apply_filters('wpt_upload_media', $media, $post_ID); // filter based on post ID $post = jd_doTwitterAPIPost($sentence, $id, $post_ID, $media); }
/** * Get and display list of scheduled Tweets */ function wpt_get_scheduled_tweets() { $schedule = wpt_schedule_custom_tweet($_POST); $deletions = isset($_POST['delete-tweets']) && isset($_POST['delete-list']) ? $_POST['delete-list'] : array(); $cron = _get_cron_array(); $schedules = wp_get_schedules(); $date_format = _x('M j, Y @ G:i', 'Publish box date format', 'wp-tweets-pro'); $clear_queue = wp_nonce_url(admin_url("admin.php?page=wp-to-twitter-schedule&wpt=clear")); $cur_sched = ''; if (isset($schedule['message'])) { echo $schedule['message']; } ?> <div class="wrap" id="wp-to-twitter" > <?php $elem = version_compare('4.3', get_option('version'), '>=') ? 'h1' : 'h2'; ?> <<?php echo $elem; ?> ><?php _e('Scheduled Tweets from WP Tweets PRO', 'wp-tweets-pro'); ?> </<?php echo $elem; ?> > <div class="postbox-container jcd-wide"> <div class="metabox-holder"> <div class="ui-sortable meta-box-sortables"> <div class="postbox"> <h3><?php _e('Your Scheduled Tweets', 'wp-tweets-pro'); ?> </h3> <div class="inside"> <form method="post" action="<?php echo admin_url('admin.php?page=wp-to-twitter-schedule&action=delete'); ?> "> <table class="widefat fixed"> <thead> <tr> <th scope="col"><?php _e('Scheduled', 'wp-tweets-pro'); ?> </th> <th scope="col" style="width:60%;"><?php _e('Tweet', 'wp-tweets-pro'); ?> </th> <th scope="col"><?php _e('Account', 'wp-tweets-pro'); ?> </th> <th scope="col"><?php _e('Delete', 'wp-tweets-pro'); ?> </th> </tr> </thead> <tbody> <?php $offset = 60 * 60 * get_option('gmt_offset'); $class = ''; foreach ($cron as $ts => $cronhooks) { foreach ((array) $cronhooks as $hook => $events) { $i = 0; foreach ((array) $events as $event) { if ($hook == 'wpt_schedule_tweet_action' || $hook == 'wpt_recurring_tweets') { $i++; if ($hook == 'wpt_recurring_tweets') { $class = 'is_recurring'; $cur_sched = ', ' . $event['schedule']; } if (count($event['args'])) { $auth = $event['args']['id']; $sentence = $event['args']['sentence']; $rt = $event['args']['rt']; $post_ID = $event['args']['post_id']; } $id = md5($ts . $auth . $rt . $post_ID . $sentence); if (isset($_GET['wpt']) && $_GET['wpt'] == 'clear' && (isset($_GET['_wpnonce']) && wp_verify_nonce($_GET['_wpnonce']))) { wp_unschedule_event($ts, $hook, array('id' => $auth, 'sentence' => $sentence, 'rt' => $rt, 'post_id' => $post_ID)); echo "<div id='message' class='updated'><p>" . sprintf(__('Tweet for %1$s has been deleted.', 'wp-tweets-pro'), date($date_format, $ts + $offset)) . "</p></div>"; } else { if (in_array($id, $deletions)) { wp_unschedule_event($ts, $hook, array('id' => $auth, 'sentence' => $sentence, 'rt' => $rt, 'post_id' => $post_ID)); echo "<div id='message' class='updated'><p>" . __('Scheduled Tweet has been deleted.', 'wp-tweets-pro') . "</p></div>"; } else { $time_diff = human_time_diff($ts + $offset, time() + $offset); $image = ''; if (get_option('wpt_media') == 1) { if (get_post_meta($post_ID, '_wpt_image', true) != 1) { $tweet_this_image = wpt_filter_scheduled_media(true, $post_ID, $rt); if ($tweet_this_image) { $img = wpt_post_attachment($post_ID); if ($img) { $img_url = wp_get_attachment_image_src($img, apply_filters('wpt_upload_image_size', 'medium')); $image = "<a href='{$img_url['0']}' class='wpt_image'>" . __('Includes Image', 'wp-tweets-pro') . "</a>"; } } } } if (!$auth || $auth == 'main') { $account = '@' . get_option('wtt_twitter_username'); $link = 'https://twitter.com/' . get_option('wtt_twitter_username'); } else { $account = '@' . get_user_meta($auth, 'wtt_twitter_username', true); $link = 'https://twitter.com/' . get_user_meta($auth, 'wtt_twitter_username', true); } ?> <tr class='<?php echo $class; ?> '> <th scope="row"><?php echo date_i18n($date_format, $ts + $offset); ?> <br /><small>(~<?php echo $time_diff . $cur_sched; ?> )</small></th> <td id='sentence-<?php echo $id; ?> '><strong><?php echo "{$sentence} {$image}"; ?> </td> <td><a href='<?php echo $link; ?> '><?php echo $account; ?> </a></td> <td><input type='checkbox' id='checkbox-<?php echo $id; ?> ' value='<?php echo $id; ?> ' name='delete-list[]' aria-describedby='sentence-<?php echo $id; ?> ' /> <label for='checkbox-<?php echo $id; ?> '><?php _e('Delete', 'wp-tweets-pro'); ?> </label></td> </tr><?php } } } } } } ?> </tbody> </table> <p><input type='submit' class='button-primary' name='delete-tweets' value='<?php _e('Delete checked Tweets', 'wp-tweets-pro'); ?> ' /></p> </form> <p><a href="<?php echo $clear_queue; ?> "><?php _e('Clear Tweets Queue', 'wp-tweets-pro'); ?> </a></p> </div> </div> </div> <div class="ui-sortable meta-box-sortables"> <div class="postbox"> <h3><?php _e('Schedule a Tweet', 'wp-tweets-pro'); ?> </h3> <div class="inside schedule" id="wp2t"> <?php $admin_url = admin_url('admin.php?page=wp-to-twitter-schedule'); ?> <form method="post" action="<?php echo $admin_url; ?> "> <div><input type="hidden" name="submit-type" value="schedule-tweet" /><input type="hidden" name='author' id='author' value='<?php echo get_current_user_id(); ?> ' /></div> <?php $nonce = wp_nonce_field('wp-to-twitter-nonce', '_wpnonce', true, false); echo "<div>{$nonce}</div>"; $tweet = isset($schedule['tweet']) ? stripslashes($schedule['tweet']) : ''; $tweet = isset($_GET['tweet']) ? stripslashes(urldecode($_GET['tweet'])) : $tweet; ?> <p style='position: relative'> <label for='jtw'><?php _e('Tweet Text', 'wp-tweets-pro'); ?> </label> <input type="checkbox" value='on' id='filter' name='filter' checked='checked' /><label for='filter'><?php _e('Run WP to Twitter filters on this Tweet', 'wp-tweets-pro'); ?> </label><br /> <textarea id='jtw' name='tweet' rows='3' cols='70'><?php echo strip_tags($tweet); ?> </textarea> </p> <div class="datetime"> <div class='date'> <label for='wpt_date'><?php _e('Date', 'wp-tweets-pro'); ?> </label><br /> <?php $date = date_i18n('Y-m-d', current_time('timestamp') + 300); ?> <input type='text' name='date' id='wpt_date' size="20" value='' data-value='<?php echo $date; ?> ' /> </div> <div class='time'> <label for='wpt_time'><?php _e('Time', 'wp-tweets-pro'); ?> </label><br /> <input type='text' name='time' id='wpt_time' size="20" value='<?php echo date_i18n('h:i a', current_time('timestamp') + 300); ?> ' /> </div> <div class='recurrence'> <label for='wpt_recurrence'><?php _e('Frequency', 'wp-tweets-pro'); ?> </label> <select name='wpt_recurrence' id='wpt_recurrence'> <option value=''><?php _e('Once', 'wp-tweets-pro'); ?> </option> <?php $schedules = wp_get_schedules(); $frequency = isset($_GET['schedule']) ? '' : ''; foreach ($schedules as $key => $schedule) { if ($key != 'four-hours' && $key != 'eight-hours' && $key != 'sixteen-hours') { echo "<option value='{$key}'" . selected($frequency, $key) . ">{$schedule['display']}</option>"; } } ?> </select> </div> <div class='autoschedule'> <p> <input type='checkbox' name='autoschedule' id='wpt_autoschedule' size="20" value='true' /> <label for='wpt_autoschedule'><?php _e('Auto-schedule', 'wp-tweets-pro'); ?> </label> </p> </div> </div> <?php $last = wp_get_recent_posts(array('numberposts' => 1, 'post_type' => 'post', 'post_status' => 'publish')); $last_id = $last['0']['ID']; ?> <p> <?php if (isset($_GET['post'])) { $post_id = intval($_GET['post']); $post_title = get_the_title($post_id); $edit_link = get_edit_post_link($post_id); ?> <input type='hidden' name='post' value='<?php echo $post_id; ?> ' /> <p> <?php printf(__('Scheduling Tweet for “<a href="%s">%s</a>”', 'wp-tweets-pro'), $edit_link, $post_title); ?> </p> <?php } else { $post_title = isset($schedule['post']) ? get_the_title($schedule['post']) : get_the_title($last_id); ?> <label for='post'><?php _e('Associate with Post:', 'wp-tweets-pro'); ?> </label> <input type="text" name="post" class="suggest" id="post" aria-describedby="post_title" value="<?php echo isset($schedule['post']) ? $schedule['post'] : $last_id; ?> " /> <span class="new" aria-live="assertive"></span><span id="post_title">(<?php echo $post_title; ?> )</span><?php } ?> </p> <?php if (get_option('jd_individual_twitter_users') == '1') { ?> <p> <?php print ' <label for="alt_author">' . __('Post to author', 'wp-tweets-pro') . '</label> <select name="alt_author" id="alt_author"> <option value="main">' . __('Main site account', 'wp-tweets-pro') . '</option> <option value="false">' . __('Current User\'s account', 'wp-tweets-pro') . '</option>'; $user_query = get_users(array('role' => 'subscriber')); // This gets the array of ids of the subscribers $subscribers = wp_list_pluck($user_query, 'ID'); // Now use the exclude parameter to exclude the subscribers $users = get_users(array('exclude' => $subscribers)); if (count($users) < 1000) { foreach ($users as $this_user) { if (get_user_meta($this_user->ID, 'wtt_twitter_username', true) != '') { print '<option value="' . $this_user->ID . '">' . $this_user->display_name . '</option>'; } } } print ' </select>'; ?> </p> <?php } ?> <p><input type="submit" name="submit" value="<?php _e("Schedule a Tweet", 'wp-tweets-pro'); ?> " class="button-primary" /></p> </form> </div> </div> </div> </div> </div> <?php if (function_exists('wpt_sidebar')) { wpt_sidebar(); } else { _e('Please Activate WP to Twitter!', 'wp-tweets-pro'); } ?> </div> <?php }