protected function verify_nonce() { $nonce = $this->request->query('_wpnonce'); if (!Social::wp39_verify_nonce($nonce, $this->request->action())) { Social::log('NONCE Failure', array(), null, true); wp_die('Oops, please try again.'); } }
/** * Imports a tweet by URL. * * @return void */ public function action_from_url() { $this->verify_nonce(); Social::log('Import tweet by URL started.'); $service = $this->social->service('twitter'); if ($service !== false) { $response = $service->import_tweet_by_url($this->request->query('post_id'), $this->request->query('url')); if ($response !== true) { echo $response; exit; } Social::log('Import tweet by URL finished.'); } else { Social::log('Import tweet by URL failed, Twitter class not found.'); } echo Social_Aggregation_Log::instance($this->request->query('post_id')); exit; }
/** * Makes sure Social CRONs are not scheduled more than once. * * @return void */ public function action_check_crons() { // this is an internal only call, so manually calling URL decode if (urldecode($this->request->query('social_api_key')) != Social::option('system_cron_api_key')) { Social::log('API key failed'); wp_die('Oops, invalid API key.'); } $crons = _get_cron_array(); $social_crons = array('15' => false); foreach ($crons as $timestamp => $_crons) { foreach ($_crons as $key => $cron) { foreach ($social_crons as $cron_key => $status) { $event_key = 'social_cron_' . $cron_key . '_core'; if ($key == $event_key and $social_crons[$cron_key]) { wp_unschedule_event($timestamp, $event_key); Social::log('Unscheduled extra event: ' . $event_key); } else { $social_crons[$cron_key] = true; } } } } }
/** * Runs the aggregation for the requested post ID. * * @return void */ public function action_run() { $this->verify_nonce(); $fetch = Social::option('aggregate_comments'); if (empty($fetch)) { Social::log('Aggregation has been disabled, exiting.'); return; } $post = get_post($this->request->query('post_id')); if ($post === null) { return; } Social::log('Begin aggregation for post #:post_id.', array('post_id' => $post->ID)); // Get URLs to query $default_urls = array(home_url('?p=' . $post->ID)); $url = wp_get_shortlink($post->ID); if (strpos($url, '?p=') === false) { $default_urls[] = $url; } // Add the permalink? $permalink = get_permalink($post->ID); if ($default_urls[0] != $permalink) { $default_urls[] = $permalink; } $broadcasted_ids = get_post_meta($post->ID, '_social_broadcasted_ids', true); if (empty($broadcasted_ids)) { $broadcasted_ids = array(); } $aggregated_ids = get_post_meta($post->ID, '_social_aggregated_ids', true); if (empty($aggregated_ids)) { $aggregated_ids = array(); } $post->broadcasted_ids = $broadcasted_ids; $post->aggregated_ids = $aggregated_ids; $post->results = array(); foreach ($this->social->services() as $key => $service) { $urls = $default_urls; $post->results[$key] = array(); if (!isset($post->aggregated_ids[$key])) { $post->aggregated_ids[$key] = array(); } if (isset($broadcasted_ids[$key]) and count($broadcasted_ids[$key])) { $service->aggregate_by_api($post); foreach ($broadcasted_ids[$key] as $broadcasted) { foreach ($broadcasted as $data) { if (isset($data['urls']) and is_array($data['urls'])) { foreach ($data['urls'] as $url) { $urls[] = $url; } } } } } // URL Search $urls = apply_filters('social_search_urls', $urls, $key); $urls = array_unique($urls); if (count($urls)) { foreach ($urls as $key => $url) { $urls[$key] = urlencode($url); } $service->aggregate_by_url($post, $urls); } } if (count($post->results)) { foreach ($post->results as $key => $results) { if (count($results)) { $this->social->service($key)->save_aggregated_comments($post); } } update_post_meta($post->ID, '_social_aggregated_ids', $post->aggregated_ids); } Social::log('Aggregation for post #:post_id complete.', array('post_id' => $post->ID)); // Some cleanup... unset($post->broadcasted_ids); unset($post->aggregated_ids); unset($post->results); if ($this->request->is_ajax()) { // Re-add to the queue? $queue = Social_Aggregation_Queue::factory(); if (!$queue->find($post->ID)) { $queue->add($post->ID, '24hr')->save(); } $log = Social_Aggregation_Log::instance($post->ID); $log->save(true); if (isset($_GET['render']) and $_GET['render'] == 'false') { $total = 0; $log = $log->current(); if (isset($log->items)) { foreach ($log->items as $service => $items) { foreach ($items as $item) { if (!$item->ignored) { ++$total; } } } } $awaiting_mod = wp_count_comments(); $awaiting_mod = $awaiting_mod->moderated; $link = esc_url(admin_url('edit-comments.php?p=' . $post->ID)); $html = ''; if (!isset($_GET['hide_li']) or $_GET['hide_li'] == 'false') { $html = '<li id="wp-adminbar-comments-social">'; } $html .= '<a href="' . $link . '"><span class="social-aggregation-results">' . sprintf(__('(%s New)', 'social'), $total) . '</span></a>'; if (!isset($_GET['hide_li']) or $_GET['hide_li'] == 'false') { $html .= '</li>'; } $response = array('total' => number_format_i18n($awaiting_mod), 'link' => $link, 'html' => $html); echo json_encode($response); } else { $queue = $queue->find($post->ID); $next_run = 0; if ($queue !== false) { $next_run = Social_Aggregation_Queue::next_run($queue->next_run); } echo json_encode(array('html' => $log->render(), 'next_run' => $next_run)); } exit; } else { Social_Aggregation_Log::instance($post->ID)->save(); } // Decrement the semaphore Social_Semaphore::factory()->decrement(); }
/** * Renders the new comment form. * * @return void */ public function action_reload_form() { if (!$this->request->is_ajax()) { exit; } if (isset($_COOKIE['social_auth_nonce'])) { $cookie_nonce = stripslashes($_COOKIE['social_auth_nonce']); // Find the user by NONCE. global $wpdb; $user_id = $wpdb->get_var($wpdb->prepare("\n\t\t\t\tSELECT user_id\n\t\t\t\t FROM {$wpdb->usermeta}\n\t\t\t\t WHERE meta_key = %s\n\t\t\t", 'social_auth_nonce_' . $cookie_nonce)); if ($user_id !== null) { Social::log('Found user #:id using nonce :nonce.', array('id' => $user_id, 'nonce' => $cookie_nonce)); // Log the user in wp_set_current_user($user_id); add_filter('auth_cookie_expiration', array($this->social, 'auth_cookie_expiration')); wp_set_auth_cookie($user_id, true); remove_filter('auth_cookie_expiration', array($this->social, 'auth_cookie_expiration')); delete_user_meta($user_id, 'social_auth_nonce_' . $cookie_nonce); setcookie('social_auth_nonce', '', -3600, '/'); $post_id = $this->request->query('post_id'); $form = trim(Social_Comment_Form::instance($post_id)->render()); echo json_encode(array('result' => 'success', 'html' => $form, 'disconnect_url' => wp_loginout('', false))); } else { Social::log('Failed to find the user using nonce :nonce.', array('nonce' => $_COOKIE['social_auth_nonce'])); echo json_encode(array('result' => 'error', 'html' => 'not logged in')); } } else { echo json_encode(array('result' => 'error', 'html' => 'not logged in')); } exit; }
/** * Broadcasts a post to the services. * * @param int|WP_Post $post_id post id or post object * @return void */ public function action_run($post_id = null) { if ($post_id === null) { $post_id = intval($this->request->query('post_ID')); } $post = $post_id; if (is_int($post_id)) { $post = get_post($post_id); } if ($post === null) { Social::log('Failed to broadcast post :post_id.', array('post_id' => $post_id)); return; } // Load content to broadcast (accounts, broadcast message, etc) $personal_accounts = null; $errored_accounts = false; $broadcast_accounts = get_post_meta($post->ID, '_social_broadcast_accounts', true); $broadcasted_ids = get_post_meta($post->ID, '_social_broadcasted_ids', true); if (empty($broadcasted_ids)) { $broadcasted_ids = array(); } $account_content = get_post_meta($post->ID, '_social_broadcast_content', true); if (empty($account_content)) { $account_content = array(); } $account_meta = get_post_meta($post->ID, '_social_broadcast_meta', true); if (empty($account_meta)) { $account_meta = array(); } Social::log('About to start broadcasting.'); foreach ($broadcast_accounts as $key => $accounts) { Social::log('Loading service :service', array('service' => $key)); $service = $this->social->service($key); if ($service) { Social::log('Found service :service', array('service' => $key)); foreach ($accounts as $_account) { if ($_account->universal != '1') { if ($personal_accounts === null) { $personal_accounts = get_user_meta($post->post_author, 'social_accounts', true); } if (isset($personal_accounts[$key][$_account->id])) { $class = 'Social_Service_' . $key . '_Account'; $account = new $class($personal_accounts[$key][$_account->id]); } else { $account = false; } } else { $account = $service->account($_account->id); } if ($account == false) { $account = apply_filters('social_get_broadcast_account', $_account, $post, $service); } if ($account !== false) { // Load the message $message = ''; if (isset($account_content[$key][$_account->id])) { $message = $account_content[$key][$_account->id]; } $args = array(); if (isset($account_meta[$key][$_account->id])) { $args = $account_meta[$key][$_account->id]; } if (!empty($message)) { Social::log('Broadcasting to :username, account #:id. (:service)', array('id' => $account->id(), 'username' => $account->name(), 'service' => $service->title())); $response = $service->broadcast($account, $message, $args, $post->ID); if ($response !== false) { if ($response->limit_reached()) { if (!isset($errored_accounts[$key])) { $errored_accounts[$key] = array(); } $reason = __('Rate limit reached', 'social'); $errored_accounts[$key][] = (object) array('account' => $account, 'reason' => $reason, 'type' => 'limit_reached'); Social::log('Broadcasting to :username, account #:id FAILED. Reason: :reason', array('id' => $account->id(), 'username' => $account->name(), 'reason' => $reason)); } else { if ($response->duplicate_status()) { if (!isset($errored_accounts[$key])) { $errored_accounts[$key] = array(); } $reason = __('Duplicate status', 'social'); $errored_accounts[$key][] = (object) array('account' => $account, 'reason' => $reason, 'type' => 'duplicate_status'); Social::log('Broadcasting to :username, account #:id FAILED. Reason: :reason', array('id' => $account->id(), 'username' => $account->name(), 'reason' => $reason)); } else { if ($response->deauthorized() or $response->deauthorized(true)) { if (!isset($errored_accounts[$key])) { $errored_accounts[$key] = array(); } $reason = __('Account deauthorized', 'social'); $errored_accounts[$key][] = (object) array('account' => $account, 'reason' => $reason, 'deauthed' => true); Social::log('Broadcasting to :username, account #:id FAILED. Reason: :reason', array('id' => $account->id(), 'username' => $account->name(), 'reason' => $reason)); } else { if ($response->general_error()) { if (!isset($errored_accounts[$key])) { $errored_accounts[$key] = array(); } $reason = $response->body()->response; $errored_accounts[$key][] = (object) array('account' => $account, 'reason' => $reason, 'type' => 'general'); Social::log('Broadcasting to :username, account #:id FAILED. Reason: :reason' . "\n\n" . 'Response:' . "\n\n" . ':response', array('id' => $account->id(), 'username' => $account->name(), 'reason' => $reason, 'response' => print_r($response, true))); } else { if (!isset($broadcasted_ids[$key])) { $broadcasted_ids[$key] = array(); } $this->social->add_broadcasted_id($post->ID, $key, $response->id(), $response->message($message), $account, $response); do_action('social_broadcast_response', $response, $key, $post); Social::log('Broadcasting to :username, account #:id COMPLETE. (:service)', array('id' => $account->id(), 'username' => $account->name(), 'service' => $service->title())); } } } } } } } } } } // Errored accounts? if ($errored_accounts !== false) { $deauthed_accounts = false; $_broadcast_accounts = array(); foreach ($errored_accounts as $key => $accounts) { foreach ($accounts as $account) { if (isset($account->deauthed)) { if (!isset($deauthed_accounts[$key])) { $deauthed_accounts[$key] = array(); } $deauthed_accounts[$key][] = $account; } if (isset($broadcasted_ids[$key]) and isset($broadcast_accounts[$key][$account->id])) { if (!isset($_broadcast_accounts[$key])) { $_broadcast_accounts[$key] = array(); } $service = $this->social->service($key); if ($service !== false) { $account = $service->account($account->id); if ($account !== false) { $_broadcast_accounts[$key][$account->id] = (object) array('id' => $account->id, 'universal' => $account->universal()); } } } } } update_post_meta($post->ID, '_social_broadcast_error', $errored_accounts); if (count($_broadcast_accounts)) { update_post_meta($post->ID, '_social_broadcast_accounts', $_broadcast_accounts); } else { delete_post_meta($post->ID, '_social_broadcast_accounts'); } // Retry? $retry = Social::option('retry_broadcast'); if (is_array($retry) and !in_array($post->ID, $retry)) { $retry[] = $post->ID; Social::option('retry_broadcast', $retry); } // Deauthed accounts? if ($deauthed_accounts !== false or defined('XMLRPC_REQUEST')) { if (defined('XMLRPC_REQUEST')) { $deauthed_accounts = $errored_accounts; } $this->send_publish_error($post, $deauthed_accounts); } } else { delete_post_meta($post->ID, '_social_broadcast_accounts'); } }
/** * Outputs checkboxes for cross-posting * * @uses Social::to_tag() * @return string */ public function get_also_post_to_controls() { if ($this->is_logged_in and $this->post->post_status != 'private') { $id = 'post_to_service'; $label_base = array('for' => $id, 'id' => 'post_to'); $checkbox = $this->to_tag('input', false, array('type' => 'checkbox', 'name' => $id, 'id' => $id, 'value' => 1)); if (current_user_can('manage_options')) { $text = sprintf(__('Also post to %s', 'social'), '<span></span>'); $post_to = $this->to_tag('label', $checkbox . ' ' . $text, $label_base, array('style' => 'display:none;')); } else { $post_to = ''; foreach (Social::instance()->services() as $key => $service) { if (count($service->accounts())) { Social::log(print_r($service->accounts(), true)); foreach ($service->accounts() as $account) { if ($account->personal()) { $text = sprintf(__('Also post to %s', 'social'), $service->title()); $post_to .= $this->to_tag('label', $checkbox . ' ' . $text, $label_base); break; } } } } } return $post_to; } return ''; }
/** * Retrieves missing Twitter content. * * @return void */ public function action_retrieve_twitter_content() { $this->verify_nonce(); $broadcasted_id = $this->request->query('broadcasted_id'); if ($broadcasted_id === null) { exit; } $post_id = $this->request->query('post_id'); if ($post_id !== null) { $recovered = false; $run = get_post_meta('_social_run_twitter_retrieval', true); if (empty($run) or (int) $run <= current_time('timestamp', 1)) { Social::log('Running Twitter retrieval on post #:post_id for broadcasted #:broadcasted_id.', array('post_id' => $post_id, 'broadcasted_id' => $broadcasted_id)); // Do we have accounts to use? $service = Social::instance()->service('twitter'); if ($service !== false) { $accounts = $service->accounts(); if (count($accounts)) { foreach ($accounts as $account) { Social::log('Using account #:account_id.', array('account_id' => $account->id())); // Run the request to the find Tweet $response = $service->request($account, 'statuses/show/' . $broadcasted_id); if ($response !== false and $response->body()->result != 'error') { $recovered = $service->recovered_meta($post_id, $broadcasted_id, $response->body()->response); } Social::log('Response: :response', array('response' => print_r($response, true))); } } else { Social::log('Using public API, no accounts found.'); $response = wp_remote_get('http://api.twitter.com/1/statuses/show/' . $broadcasted_id . '.json'); if (!is_wp_error($response) and !isset($response->error)) { $recovered = $service->recovered_meta($post_id, $broadcasted_id, $response); } Social::log('Response: :response', array('response' => print_r($response, true))); } } else { Social::log('Failed, Twitter class not found.'); } } if (!$recovered) { Social::log('Failed to recover the data, try again in 15 minutes.'); // Something went wrong, retry again in 15 minutes. update_post_meta($post_id, '_social_run_twitter_retrieval', current_time('timestamp', 1) + 54000); } else { if (!empty($run)) { Social::log('Content found, cya!'); delete_post_meta($post_id, '_social_run_twitter_retrieval'); } } } }
/** * Attempts to jiggle the stuck lock loose. * * @return bool */ private function stuck_check() { global $wpdb; // Check to see if we already broke the lock. if ($this->lock_broke) { return true; } $current_time = current_time('mysql', 1); $unlock_time = gmdate('Y-m-d H:i:s', time() - 30 * 60); $affected = $wpdb->query($wpdb->prepare("\n\t\t\tUPDATE {$wpdb->options}\n\t\t\t SET option_value = %s\n\t\t\t WHERE option_name = 'social_last_lock_time'\n\t\t\t AND option_value <= %s\n\t\t", $current_time, $unlock_time)); if ($affected == '1') { Social::log('Semaphore was stuck, set lock time to ' . $current_time); $this->lock_broke = true; return true; } return false; }
/** * Imports a Tweet by URL. * * @param int $post_id * @param string $url * @return bool|string */ public function import_tweet_by_url($post_id, $url) { if (!($account = $this->api_account())) { return; } $post = get_post($post_id); $post->broadcasted_ids = get_post_meta($post->ID, '_social_broadcasted_ids', true); if (empty($post->broadcasted_ids)) { $post->broadcasted_ids = array(); } $invalid = false; $id = $this->tweet_url_to_id($url); if (!empty($id) and !$this->is_original_broadcast($post, $id)) { Social::log('Importing tweet. -- ID: :id -- URL: :url', array("id" => $id, "url" => $url)); $social_response = $this->request($account, '1.1/statuses/show/' . $id, array('include_entities' => 'true')); error_log(print_r($social_response, true)); if ($social_response !== false and is_object($social_response->body()->response)) { $response = $social_response->body()->response; if ($response !== null and !isset($response->error)) { $logger = Social_Aggregation_Log::instance($post->ID); $post->aggregated_ids = get_post_meta($post->ID, '_social_aggregated_ids', true); if (empty($post->aggregated_ids)) { $post->aggregated_ids = array(); } if (!isset($post->aggregated_ids[$this->_key])) { $post->aggregated_ids[$this->_key] = array(); } if (in_array($id, $post->aggregated_ids[$this->_key])) { $logger->add($this->_key, $response->id, 'Imported', true, array('username' => $response->user->screen_name)); } else { $logger->add($this->_key, $response->id, 'Imported', false, array('username' => $response->user->screen_name)); $post->aggregated_ids[$this->_key][] = $response->id; $post->results = array($this->_key => array($response->id => (object) array('id' => $response->id, 'from_user_id' => $response->user->id, 'from_user' => $response->user->screen_name, 'text' => $response->text, 'created_at' => $response->created_at, 'profile_image_url' => $response->user->profile_image_url, 'profile_image_url_https' => $response->user->profile_image_url_https, 'in_reply_to_status_id' => $response->in_reply_to_status_id, 'raw' => $response, 'comment_type' => 'social-twitter'))); $this->save_aggregated_comments($post, true); // Some cleanup... unset($post->aggregated_ids); unset($post->results); } $logger->save(true); } else { Social::log('Something went wrong... -- :response', array('response' => print_r($response, true))); if (isset($response->error)) { if ($response->error == 'Sorry, you are not authorized to see this status.') { return 'protected'; } else { $invalid = true; } } } } else { $invalid = true; } } else { Social::log('Something went wrong... -- ID: :id -- URL: :url', array('id' => $id, 'url' => implode('/', $url))); $invalid = true; } unset($post->broadcasted_ids); if ($invalid) { return 'invalid'; } return true; }
/** * Removes a post from the queue completely, or by timestamp. * * @param int $post_id post id * @param int $timestamp (optional) timestamp to remove by * @return Social_Aggregation_Queue */ public function remove($post_id, $timestamp = null) { $unset = false; if ($timestamp === null) { $queue = array(); foreach ($this->_queue as $timestamp => $posts) { foreach ($posts as $id => $interval) { if ($id !== $post_id) { if (!isset($queue[$timestamp])) { $queue[$timestamp] = array(); } $queue[$timestamp][$id] = $interval; } else { $unset = true; } } } $this->_queue = $queue; } else { if (isset($this->_queue[$timestamp]) and isset($this->_queue[$timestamp][$post_id])) { $unset = true; unset($this->_queue[$timestamp][$post_id]); if (empty($this->_queue[$timestamp])) { unset($this->_queue[$timestamp]); } } } if ($unset) { delete_post_meta($post_id, '_social_aggregation_next_run'); $this->save(); Social::log('Post #:post_id removed from the aggregation queue.', array('post_id' => $post_id)); } return $this; }
/** * Imports a Tweet by URL. * * @param int $post_id * @param string $url * @return bool|string */ public function import_tweet_by_url($post_id, $url) { $post = get_post($post_id); $post->broadcasted_ids = get_post_meta($post->ID, '_social_broadcasted_ids', true); if (empty($post->broadcasted_ids)) { $post->broadcasted_ids = array(); } $invalid = false; $id = $this->tweet_url_to_id($url); if (!empty($id) and !$this->is_original_broadcast($post, $id)) { Social::log('Importing tweet. -- ID: :id -- URL: :url'); $url = 'http://api.twitter.com/1/statuses/show.json?id=' . $id; $request = wp_remote_get($url); if (!is_wp_error($request)) { $response = apply_filters('social_response_body', $request['body'], $this->_key); if ($response !== null and !isset($response->error)) { $logger = Social_Aggregation_Log::instance($post->ID); $post->aggregated_ids = get_post_meta($post->ID, '_social_aggregated_ids', true); if (empty($post->aggregated_ids)) { $post->aggregated_ids = array(); } if (!isset($post->aggregated_ids[$this->_key])) { $post->aggregated_ids[$this->_key] = array(); } if (in_array($id, $post->aggregated_ids[$this->_key])) { $logger->add($this->_key, $response->id, 'Imported', true, array('username' => $response->user->screen_name)); } else { $logger->add($this->_key, $response->id, 'Imported', false, array('username' => $response->user->screen_name)); $post->aggregated_ids[$this->_key][] = $response->id; $post->results[$this->_key][$response->id] = (object) array('id' => $response->id, 'from_user_id' => $response->user->id, 'from_user' => $response->user->screen_name, 'text' => $response->text, 'created_at' => $response->created_at, 'profile_image_url' => $response->user->profile_image_url, 'in_reply_to_status_id' => $response->in_reply_to_status_id, 'raw' => $response, 'comment_type' => 'social-twitter'); $this->save_aggregated_comments($post, true); // Some cleanup... unset($post->aggregated_ids); unset($post->results); } $logger->save(true); } else { Social::log('Something went wrong... -- :response', array('response' => print_r($response, true))); if (isset($response->error)) { if ($response->error == 'Sorry, you are not authorized to see this status.') { return 'protected'; } else { $invalid = true; } } } } else { Social::log('Something went wrong... -- :response', array('response' => print_r($request, true))); $invalid = true; } } else { Social::log('Something went wrong... -- ID: :id -- URL: :url', array('id' => $id, 'url' => implode('/', $url))); $invalid = true; } unset($post->broadcasted_ids); if ($invalid) { return 'invalid'; } return true; }
/** * Saves the aggregated comments. * * @param object $post * @return void */ public function save_aggregated_comments(&$post) { if (isset($post->results[$this->_key])) { global $wpdb; foreach ($post->results[$this->_key] as $result) { $commentdata = array('comment_post_ID' => $post->ID, 'comment_author_email' => $wpdb->escape($this->_key . '.' . $result->id . '@example.com'), 'comment_author_IP' => $_SERVER['SERVER_ADDR'], 'comment_agent' => 'Social Aggregator'); if (!isset($result->like)) { $url = 'http://graph.facebook.com/' . $result->from->id; $request = wp_remote_get($url); if (!is_wp_error($request)) { $response = json_decode($request['body']); $account = (object) array('user' => $response); $class = 'Social_Service_' . $this->_key . '_Account'; $account = new $class($account); $commentdata = array_merge($commentdata, array('comment_type' => 'social-facebook', 'comment_author' => $wpdb->escape($result->from->name), 'comment_author_url' => $account->url(), 'comment_content' => $wpdb->escape($result->message), 'comment_date' => date('Y-m-d H:i:s', strtotime($result->created_time) + get_option('gmt_offset') * 3600), 'comment_date_gmt' => gmdate('Y-m-d H:i:s', strtotime($result->created_time)))); } } else { $url = 'http://facebook.com/profile.php?id=' . $result->id; $commentdata = array_merge($commentdata, array('comment_type' => 'social-facebook-like', 'comment_author' => $wpdb->escape($result->name), 'comment_author_url' => $url, 'comment_content' => $wpdb->escape('<a href="' . $url . '" target="_blank">' . $result->name . '</a> liked this on Facebook.'), 'comment_date' => current_time('mysql'), 'comment_date_gmt' => current_time('mysql', 1))); } $user_id = isset($result->like) ? $result->id : $result->from->id; $commentdata = array_merge($commentdata, array('comment_post_ID' => $post->ID, 'comment_author_email' => $this->_key . '.' . $user_id . '@example.com')); $result_id = isset($result->status_id) ? $result->status_id : $result->id; if (apply_filters('social_approve_likes_and_retweets', true) && isset($result->like)) { $commentdata['comment_approved'] = 1; } else { if (($commentdata = $this->allow_comment($commentdata, $result_id, $post)) === false) { continue; } } // sanity check to make sure this comment is not a duplicate if ($this->is_duplicate_comment($post, $result->id)) { Social::log('Result #:result_id already exists, skipping.', array('result_id' => $result->id), 'duplicate-comment'); continue; } Social::log('Saving #:result_id.', array('result_id' => isset($result->status_id) ? $result->status_id : $result->id)); $comment_id = 0; try { Social::Log('Attempting to save commentdata: :commentdata', array('commentdata' => print_r($commentdata, true))); $comment_id = wp_insert_comment($commentdata); update_comment_meta($comment_id, 'social_account_id', addslashes_deep($user_id)); update_comment_meta($comment_id, 'social_profile_image_url', addslashes_deep('http://graph.facebook.com/' . $user_id . '/picture')); update_comment_meta($comment_id, 'social_status_id', addslashes_deep(isset($result->status_id) ? $result->status_id : $result->id)); if (!isset($result->raw)) { $result = (object) array_merge((array) $result, array('raw' => $result)); } update_comment_meta($comment_id, 'social_raw_data', addslashes_deep(base64_encode(json_encode($result->raw)))); if ($commentdata['comment_approved'] !== 'spam') { if ($commentdata['comment_approved'] == '0') { wp_notify_moderator($comment_id); } if (get_option('comments_notify') and $commentdata['comment_approved'] and (!isset($commentdata['user_id']) or $post->post_author != $commentdata['user_id'])) { wp_notify_postauthor($comment_id, 'comment'); } } } catch (Exception $e) { // Something went wrong, remove the aggregated ID. if (($key = array_search(isset($result->status_id) ? $result->status_id : $result->id, $post->aggregated_ids['facebook'])) !== false) { unset($post->aggregated_ids['facebook'][$key]); } if ((int) $comment_id) { // Delete the comment in case it wasn't the insert that failed. wp_delete_comment($comment_id); } } } } }
Social::log('Current Meta Value: :meta_value', array('meta_value' => print_r($_meta_value, true))); if (is_scalar($data)) { $_meta_value[$service_key][$account_id][$data] = array('message' => ''); } else { $_meta_value[$service_key][$account_id][$id] = $data; } } } else { $_meta_value[$service_key][$account_id][$broadcasted] = array('message' => ''); } } } if (!empty($_meta_value)) { update_post_meta($result->post_id, '_social_broadcasted_ids', $_meta_value); } Social::log('New meta value for post #:post_id: :meta_value', array('post_id' => $result->post_id, 'meta_value' => print_r($_meta_value, true))); } } } // Add broadcast by default Social::option('broadcast_by_default', '0'); // Reschedule posts for aggregation $results = $wpdb->get_results("\n\t\tSELECT post_id\n\t\tFROM {$wpdb->postmeta}\n\t\tWHERE meta_key = '_social_broadcasted_ids'\n\t\tORDER BY post_id DESC\n\t\tLIMIT 50\n\t"); if ($results !== null) { $queue = Social_Aggregation_Queue::factory(); foreach ($results as $result) { if (!$queue->find($result->post_id)) { $queue->add($result->post_id); } } $queue->save();
/** * Checks to see if the broadcasting account has been deauthorized. * * @param bool $check_invalid_key * @return bool */ public function deauthorized($check_invalid_key = FALSE) { $body = $this->body(); if (isset($body->result) and $body->result == 'error' and isset($body->response) and $this->_service->deauthorized($body->response, $check_invalid_key)) { if ($this->_account->personal()) { $url = Social::settings_url(array(), true); } else { $url = Social::settings_url(); } $deauthorized = get_option('social_deauthorized', array()); if (!isset($deauthorized[$this->_service->key()])) { $deauthorized[$this->_service->key()] = array(); } $deauthorized[$this->_service->key()][$this->_account->id()] = sprintf(__('Unable to publish to %s with account %s. Please <a href="%">re-authorize</a> this account.', 'social'), esc_html($this->_service->title()), esc_html($this->_account->name()), esc_url($url)); update_option('social_deauthorized', $deauthorized); Social::log('Removing deauthorized account: :account', array('account' => print_r($this->_account))); $this->_service->remove_account($this->_account)->save(); return true; } return false; }
/** * 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); } }
/** * Checks to see if the result ID is the original broadcasted ID. * * @param WP_Post|int $post * @param int $result_id * @return bool */ public function is_original_broadcast($post, $result_id) { if (!is_object($post)) { $broadcasted_ids = get_post_meta($post, '_social_broadcasted_ids', true); if (empty($broadcasted_ids)) { $broadcasted_ids = array(); } $post = (object) array('broadcasted_ids' => $broadcasted_ids); } if (isset($post->broadcasted_ids[$this->_key])) { foreach ($post->broadcasted_ids[$this->_key] as $account_id => $broadcasted) { if (isset($broadcasted[$result_id])) { Social::log('This is the original broadcast. (:result_id)', array('result_id' => $result_id)); return true; } } } return false; }
/** * Saves the aggregated comments. * * @param object $post * @return void */ public function save_aggregated_comments(&$post) { if (isset($post->results[$this->_key])) { global $wpdb; foreach ($post->results[$this->_key] as $result) { $commentdata = array('comment_post_ID' => $post->ID, 'comment_author_email' => $wpdb->escape($this->_key . '.' . $result->id . '@example.com'), 'comment_author_IP' => $_SERVER['SERVER_ADDR'], 'comment_agent' => 'Social Aggregator'); if (isset($result->parent)) { if ($wp_parent = $this->get_comment_from_fb_id($result->parent->id)) { $commentdata['comment_parent'] = $wp_parent->comment_id; } } if (!isset($result->like)) { $commentdata = array_merge($commentdata, array('comment_type' => 'social-facebook', 'comment_author' => $wpdb->escape($result->from->name), 'comment_author_url' => $result->from->link, 'comment_content' => $wpdb->escape($result->message), 'comment_date' => date('Y-m-d H:i:s', strtotime($result->created_time) + get_option('gmt_offset') * 3600), 'comment_date_gmt' => gmdate('Y-m-d H:i:s', strtotime($result->created_time)))); } else { // v2.0+ returns app scoped ids, both app scoped ids and real ids redirect to the profile with // https://www.facebook.com/{user-id}|{app-scoped-id} $url = 'https://www.facebook.com/' . $result->id . '/'; $commentdata = array_merge($commentdata, array('comment_type' => 'social-facebook-like', 'comment_author' => $wpdb->escape($result->name), 'comment_author_url' => $url, 'comment_content' => $wpdb->escape('<a href="' . $url . '" target="_blank">' . $result->name . '</a> liked this on Facebook.'), 'comment_date' => current_time('mysql'), 'comment_date_gmt' => current_time('mysql', 1))); } $user_id = isset($result->like) ? $result->from_id : $result->from->id; $commentdata = array_merge($commentdata, array('comment_post_ID' => $post->ID, 'comment_author_email' => $this->_key . '.' . $user_id . '@example.com')); if (apply_filters('social_approve_likes_and_retweets', false) && isset($result->like)) { $commentdata['comment_approved'] = 1; } else { if (($commentdata = $this->allow_comment($commentdata, $result->id, $post)) === false) { continue; } } Social::log('Saving #:result_id.', array('result_id' => $result->id)); $comment_id = 0; try { Social::Log('Attempting to save commentdata: :commentdata', array('commentdata' => print_r($commentdata, true))); $comment_id = wp_insert_comment($commentdata); update_comment_meta($comment_id, 'social_account_id', addslashes_deep($user_id)); update_comment_meta($comment_id, 'social_profile_image_url', addslashes_deep('https://graph.facebook.com/' . $user_id . '/picture')); update_comment_meta($comment_id, 'social_status_id', addslashes_deep($result->status_id)); update_comment_meta($comment_id, 'social_broadcast_id', addslashes_deep($result->id)); if ($result->reply_to_id) { update_comment_meta($comment_id, 'social_reply_to_id', addslashes_deep($result->reply_to_id)); } if (!isset($result->raw)) { $result = (object) array_merge((array) $result, array('raw' => $result)); } update_comment_meta($comment_id, 'social_raw_data', addslashes_deep(base64_encode(json_encode($result->raw)))); if ($commentdata['comment_approved'] !== 'spam') { if ($commentdata['comment_approved'] == '0') { wp_notify_moderator($comment_id); } if (get_option('comments_notify') and $commentdata['comment_approved'] and (!isset($commentdata['user_id']) or $post->post_author != $commentdata['user_id'])) { wp_notify_postauthor($comment_id, 'comment'); } } } catch (Exception $e) { // Something went wrong, remove the aggregated ID. if (($key = array_search($result->id, $post->aggregated_ids['facebook'])) !== false) { unset($post->aggregated_ids['facebook'][$key]); } if ((int) $comment_id) { // Delete the comment in case it wasn't the insert that failed. wp_delete_comment($comment_id); } } } } }
/** * Executes the request. * * @return Social_Request */ public function execute() { require 'controller.php'; $controller = apply_filters('social_controller', Social::$plugins_path . 'lib/social/controller/' . $this->controller()); if (file_exists($controller . '.php')) { require $controller . '.php'; $controller = 'Social_Controller_' . $this->controller(); $controller = new $controller($this); $action = 'action_' . $this->action(); if (method_exists($controller, $action)) { Social::log('Running request: :controller/:action', array('controller' => str_replace('Social_Controller_', '', get_class($controller)), 'action' => $action)); $controller->{$action}(); } else { Social::log('Invalid action :action called on controller :controller.', array('action' => $this->action(), 'controller' => $this->controller())); die; } } else { Social::log('Controller :controller does not exist.', array('controller' => $this->controller())); die; } return $this; }