/** * Handles the remote timeout requests for Social. * * @param string $url url to request * @param string $nonce_key key to use when generating the nonce * @param bool $post set to true to do a wp_remote_post * @return void */ private function request($url, $nonce_key = null, $post = false) { if ($nonce_key !== null) { $url = str_replace('&', '&', Social::wp39_nonce_url($url, $nonce_key)); } $data = array('timeout' => 0.01, 'blocking' => false, 'sslverify' => apply_filters('https_local_ssl_verify', true)); if ($post) { Social::log('POST request to: :url', array('url' => $url)); wp_remote_post($url, $data); } else { Social::log('GET request to: :url', array('url' => $url)); wp_remote_get($url, $data); } }
/** * Pre-processor to the comments. * * @wp-filter social_comments_array * @static * @param array $comments * @param int $post_id * @return array */ public static function comments_array(array $comments, $post_id) { // pre-load the hashes for broadcasted tweets $broadcasted_ids = get_post_meta($post_id, '_social_broadcasted_ids', true); if (empty($broadcasted_ids)) { $broadcasted_ids = array(); } global $wpdb; // we need comments to be keyed by ID, check for Tweet comments $tweet_comments = $_comments = $comment_ids = array(); foreach ($comments as $key => $comment) { if (is_object($comment)) { $_comments['id_' . $comment->comment_ID] = $comment; if (in_array($comment->comment_type, Social_Service_Twitter::comment_types())) { $comment_ids[] = $comment->comment_ID; $tweet_comments['id_' . $comment->comment_ID] = $comment; } } else { // social items $_comments[$key] = $comment; } } // if no tweet comments, get out now if (!count($tweet_comments)) { return $comments; } // use our keyed array $comments = $_comments; unset($_comments); $social_map = array(); // key = social id, value = comment_ID $hash_map = array(); // key = hash, value = comment_ID $broadcasted_social_ids = array(); $broadcast_retweets = array(); // array of comments if (isset($broadcasted_ids['twitter'])) { foreach ($broadcasted_ids['twitter'] as $account_id => $broadcasted) { foreach ($broadcasted as $id => $data) { $broadcasted_social_ids[] = $id; // if we don't have a message saved for a tweet, try to get it so that we can use it next time if (empty($data['message'])) { $url = Social::wp39_nonce_url(home_url('index.php?social_controller=aggregation&social_action=retrieve_twitter_content&broadcasted_id=' . $id . '&post_id=' . $post_id), 'retrieve_twitter_content'); wp_remote_get(str_replace('&', '&', $url), array('timeout' => 0.01, 'blocking' => false)); } else { // create a hash from the broadcast so we can match retweets to it $hash = self::build_hash($data['message']); // This is stored as broadcasted and not the ID so we can easily store broadcasted retweets // instead of attaching retweets to non-existent comments. $hash_map[$hash] = 'broadcasted'; } } } } // Load the comment meta $results = $wpdb->get_results("\n\t\t\tSELECT meta_key, meta_value, comment_id\n\t\t\t FROM {$wpdb->commentmeta}\n\t\t\t WHERE comment_id IN (" . implode(',', $comment_ids) . ")\n\t\t\t AND (\n\t\t\t meta_key = 'social_in_reply_to_status_id'\n\t\t\t OR meta_key = 'social_status_id'\n\t\t\t OR meta_key = 'social_raw_data'\n\t\t\t OR meta_key = 'social_profile_image_url'\n\t\t\t OR meta_key = 'social_comment_type'\n\t\t\t)\n\t\t"); // Set up social data for twitter comments foreach ($tweet_comments as $key => &$comment) { $comment->social_items = array(); // Attach meta foreach ($results as $result) { if ($comment->comment_ID == $result->comment_id) { switch ($result->meta_key) { case 'social_raw_data': $comment->social_raw_data = json_decode(base64_decode($result->meta_value)); break; case 'social_status_id': $social_map[$result->meta_value] = $result->comment_id; default: $comment->{$result->meta_key} = $result->meta_value; } } } // Attach hash if (isset($comment->social_raw_data) and isset($comment->social_raw_data->text)) { $text = trim($comment->social_raw_data->text); } else { $text = trim($comment->comment_content); } $comment->social_hash = self::build_hash($text); if (!isset($hash_map[$comment->social_hash])) { $hash_map[$comment->social_hash] = $comment->comment_ID; } } // merge data so that $comments has the data we've set up $comments = array_merge($comments, $tweet_comments); // set-up replies and retweets foreach ($tweet_comments as $key => &$comment) { if (is_object($comment)) { // set reply/comment parent if (!empty($comment->social_in_reply_to_status_id) and isset($social_map[$comment->social_in_reply_to_status_id])) { $comments[$key]->comment_parent = $social_map[$comment->social_in_reply_to_status_id]; } // set retweets $rt_matched = false; if (isset($comment->social_raw_data) and isset($comment->social_raw_data->retweeted_status)) { // explicit match via API data $rt_id = $comment->social_raw_data->retweeted_status->id_str; if (in_array($rt_id, $broadcasted_social_ids)) { $broadcast_retweets[] = $comment; unset($comments[$key]); $rt_matched = true; } else { if (isset($social_map[$rt_id]) and isset($comments['id_' . $social_map[$rt_id]])) { $comments['id_' . $social_map[$rt_id]]->social_items[$key] = $comment; unset($comments[$key]); $rt_matched = true; } } } if (!$rt_matched) { // best guess via hashes $hash_match = $hash_map[$comment->social_hash]; if ($hash_match != $comment->comment_ID) { // hash match to own tweet is expected, at minimum - set above if ($hash_match == 'broadcasted') { $broadcast_retweets[] = $comment; } else { if (isset($comments['id_' . $hash_match])) { $comments['id_' . $hash_match]->social_items[$key] = $comment; } else { // Loop through the broadcasted retweets and see if this is a retweet of one of those. foreach ($broadcast_retweets as $retweet) { if ($retweet->comment_ID == $hash_match) { $broadcast_retweets[] = $comment; break; } } } } unset($comments[$key]); } } } } if (!isset($comments['social_items'])) { $comments['social_items'] = array(); } if (count($broadcast_retweets)) { $comments['social_items']['twitter'] = $broadcast_retweets; } return $comments; }
?> </p> <?php if (Social::option('cron') === '0') { ?> <div class="social-callout"> <h3 class="social-title"><?php _e('CRON Setup', 'social'); ?> </h3> <dl class="social-kv"> <dt><?php _e('CRON API Key', 'social'); ?> <small>(<a href="<?php echo esc_url(Social::wp39_nonce_url(admin_url('options-general.php?page=social.php&social_controller=settings&social_action=regenerate_api_key'), 'regenerate_api_key')); ?> " rel="social_api_key" id="social_regenerate_api_key"><?php _e('regenerate', 'social'); ?> </a>)</small></dt> <dd> <code class="social_api_key"><?php echo esc_html(Social::option('system_cron_api_key')); ?> </code> </dd> </dl> <p><?php _e('For your system CRON to run correctly, make sure it is pointing towards a URL that looks something like the following:', 'social'); ?>
if (Social::option('aggregate_comments')) { ?> <div class="social-meta-box-block cf-clearfix"> <h4> <?php _e('Manual Refresh', 'social'); ?> <span id="social-next-run">(<?php echo sprintf(__('Next automatic run <span>%s</span>', 'social'), $next_run); ?> )</span> </h4> <p class="submit" style="clear:both;float:none;padding:0;"> <a href="<?php echo esc_url(Social::wp39_nonce_url(admin_url('options-general.php?social_controller=aggregation&social_action=run&post_id=' . $post->ID), 'run')); ?> " id="run_aggregation" class="button" style="float:left;margin-bottom:10px;"><?php _e('Find Social Comments', 'social'); ?> </a> <img src="<?php echo esc_url(admin_url('images/wpspin_light.gif')); ?> " style="float:left;position:relative;top:4px;left:5px;display:none;" id="run_aggregation_loader" /> </p> </div><!-- .social-meta-box-block --> <?php } ?>