Пример #1
0
 /**
  * Add a message to the log.
  *
  * @static
  * @param  string  $message    message to add to the log
  * @param  array   $args       arguments to pass to the writer
  * @param  string  $context    context of the log message
  * @param  bool    $backtrace  show the backtrace
  * @return void
  */
 public function write($message, array $args = null, $context = null, $backtrace = false)
 {
     if (!Social::option('debug') and !in_array($context, apply_filters('social_log_contexts', array()))) {
         return;
     }
     if ($args !== null) {
         foreach ($args as $key => $value) {
             $message = str_replace(':' . $key, $value, $message);
         }
     }
     if ($context !== null) {
         $context = '[' . strtoupper(str_replace('-', ' ', $context)) . '] ';
     }
     $error_str = $context . '[SOCIAL - ' . current_time('mysql', 1) . ' - ' . $_SERVER['REMOTE_ADDR'] . '] ' . $message;
     if ($backtrace) {
         ob_start();
         debug_print_backtrace();
         $trace = ob_get_contents();
         ob_end_clean();
         $error_str .= "\n\n" . $trace . "\n\n";
     }
     if (is_writable($this->_file)) {
         error_log($error_str . "\n", 3, $this->_file);
     } else {
         error_log($error_str);
     }
 }
Пример #2
0
 /**
  * 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;
                 }
             }
         }
     }
 }
 /**
  * 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');
     }
 }
Пример #4
0
 /**
  * Check for auth against Social's api key
  *
  * @return book
  */
 static function social_key_auth()
 {
     return (bool) (!empty($_GET['social_api_key']) && stripslashes($_GET['social_api_key']) == Social::option('system_cron_api_key'));
 }
Пример #5
0
 /**
  * Disconnects an account from the user's account.
  *
  * @param  int  $id
  * @return void
  */
 public function disconnect($id)
 {
     if (!is_admin() or defined('IS_PROFILE_PAGE')) {
         $accounts = get_user_meta(get_current_user_id(), 'social_accounts', true);
         if (isset($accounts[$this->_key][$id])) {
             if (defined('IS_PROFILE_PAGE')) {
                 unset($accounts[$this->_key][$id]);
             } else {
                 unset($accounts[$this->_key][$id]->user);
             }
             if (!count($accounts[$this->_key])) {
                 unset($accounts[$this->_key]);
             }
             update_user_meta(get_current_user_id(), 'social_accounts', $accounts);
         }
     } else {
         $accounts = Social::option('accounts');
         if (isset($accounts[$this->_key][$id])) {
             unset($accounts[$this->_key][$id]);
             if (!count($accounts[$this->_key])) {
                 unset($accounts[$this->_key]);
             }
             Social::option('accounts', $accounts);
         }
     }
     do_action('social_account_disconnected', $this->_key, $id);
 }
Пример #6
0
?>
" id="import_from_url" class="button"><?php 
_e('Import Tweet', 'social');
?>
</a>
		</span>
		<img src="<?php 
echo esc_url(admin_url('images/wpspin_light.gif'));
?>
" style="position:relative;top:4px;left:0;display:none" id="import_from_url_loader" />
		<span id="social-import-error"></span>
	</p>
</div><!-- .social-meta-box-block -->

<?php 
if (in_array(Social::option('fetch_comments'), array('1', '2'))) {
    ?>
<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(wp_nonce_url(admin_url('index.php?social_controller=aggregation&social_action=run&post_id=' . $post->ID), 'run'));
Пример #7
0
?>
" id="import_from_url" class="button"><?php 
_e('Import Tweet', 'social');
?>
</a>
		</span>
		<img src="<?php 
echo esc_url(admin_url('images/wpspin_light.gif'));
?>
" style="position:relative;top:4px;left:0;display:none" id="import_from_url_loader" />
		<span id="social-import-error"></span>
	</p>
</div><!-- .social-meta-box-block -->

<?php 
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(wp_nonce_url(admin_url('options-general.php?social_controller=aggregation&social_action=run&post_id=' . $post->ID), 'run'));
Пример #8
0
 /**
  * Runs the aggregation for the requested post ID.
  *
  * @return void
  */
 public function action_run()
 {
     $this->verify_nonce();
     $fetch = Social::option('fetch_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();
 }
Пример #9
0
 /**
  * Enqueues the @Anywhere script.
  *
  * @static
  * @return void
  */
 public static function enqueue_assets()
 {
     $api_key = Social::option('twitter_anywhere_api_key');
     if (!empty($api_key)) {
         wp_enqueue_script('twitter_anywhere', 'http://platform.twitter.com/anywhere.js?id=' . $api_key, array('social_js'), Social::$version, true);
     }
 }
Пример #10
0
 /**
  * Regenerates the API key.
  *
  * @return void
  */
 public function action_regenerate_api_key()
 {
     $this->verify_nonce();
     if (!$this->request->is_ajax()) {
         wp_die('Oops, this method can only be accessed via an AJAX request.');
     }
     $key = wp_generate_password(16, false);
     Social::option('system_cron_api_key', $key, true);
     echo $key;
     exit;
 }
Пример #11
0
 /**
  * Saves the queue.
  *
  * @return void
  */
 public function save()
 {
     Social::option('aggregation_queue', $this->_queue);
 }
Пример #12
0
							<ul>
								<li>
									<label for="debug_mode_no">
										<input type="radio" name="social_debug" id="debug_mode_no" value="0"<?php 
echo Social::option('debug') != '1' ? ' checked="checked"' : '';
?>
 />
										<?php 
_e('Off <span class="description">(recommended)</span>', 'social');
?>
									</label>
								</li>
								<li>
									<label for="debug_mode_yes">
										<input type="radio" name="social_debug" id="debug_mode_yes" value="1"<?php 
echo Social::option('debug') == '1' ? ' checked="checked"' : '';
?>
 />
										<?php 
_e('On <span class="description">(for troubleshooting)</span>', 'social');
?>
									</label>
								</li>
							</ul>

							<strong><?php 
_e('Debug log location:', 'social');
?>
</strong> <code><?php 
echo Social::$plugins_path . 'debug_log.txt';
?>
Пример #13
0
 /**
  * Adds the account ID to the rel for the author link.
  *
  * @static
  * @param  string  $url
  * @return string
  */
 public static function get_comment_author_link($url)
 {
     if (Social::option('use_standard_comments') == '1') {
         return $url;
     }
     global $comment;
     if (in_array($comment->comment_type, self::comment_types())) {
         $status_id = get_comment_meta($comment->comment_ID, 'social_status_id', true);
         $output = str_replace("rel='", "rel='" . $status_id . " ", $url);
         $api_key = Social::option('twitter_anywhere_api_key');
         if (!empty($api_key)) {
             $output = str_replace("'>", "' style='display:none'>@", $output);
             $output .= '@' . get_comment_author($comment->comment_ID);
         } else {
             $output = str_replace("'>", "'>@", $output);
         }
         return $output;
     }
     return $url;
 }
 /**
  * Broadcasts the message to the specified account. Returns the broadcasted ID.
  *
  * @param  Social_Service_Facebook_Account|object  $account     account to broadcast to
  * @param  string                                  $message     message to broadcast
  * @param  array                                   $args        extra arguments to pass to the request
  * @param  int                                     $post_id     post ID being broadcasted
  * @param  int                                     $comment_id  comment ID being broadcasted
  *
  * @return Social_Response
  */
 public function broadcast($account, $message, array $args = array(), $post_id = null, $comment_id = null)
 {
     global $post;
     // if post ID is set, this is a broadcast of a post,
     // if the comment ID is set it is a broadcast of a comment
     // TODO - add wrapper functions that abstract these actions out to separate methods
     // check comment being replied to, if it is a facebook comment on a post then
     // send the comment as a reply on the same post.
     // If that fails, then send as posting a link with a comment.
     $args = $args + array('message' => $message);
     // first try to send comment to an existing Fb post
     if (!is_null($comment_id)) {
         $comment = get_comment($comment_id);
         if (!empty($comment->comment_parent)) {
             $parent_comment = get_comment($comment->comment_parent);
             if (!is_null($parent_comment) && in_array($parent_comment->comment_type, self::comment_types())) {
                 $status_id = get_comment_meta($parent_comment->comment_ID, 'social_status_id', true);
                 if (!empty($status_id)) {
                     // we have a Facebook post to reply to
                     $parts = explode('_', $status_id);
                     if (count($parts) == 3) {
                         $status_id = $parts[0] . '_' . $parts[1];
                     }
                     $args = apply_filters($this->key() . '_broadcast_args', $args, $post_id, $comment_id);
                     $response = $this->request($account, $status_id . '/comments', $args, 'POST');
                     if ($response !== false && $response->id() !== '0') {
                         // post succeeded, return response
                         return $response;
                     }
                     // ...broadcast failed, continue and send as post to feed
                 }
             }
         }
         // posting with a link, do not include URL in comment.
         $format = trim(str_replace('{url}', '', Social::option('comment_broadcast_format')));
         $message = $this->format_comment_content($comment, $format);
         $args['message'] = $message;
         // prep data
         $post = get_post($comment->comment_post_ID);
         setup_postdata($post);
         $link_args = array('link' => get_post_permalink($post->ID), 'title' => get_the_title($post->ID), 'description' => get_the_excerpt());
         if (function_exists('has_post_thumbnail') and has_post_thumbnail($post->ID)) {
             $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'single-post-thumbnail');
             $link_args = $link_args + array('picture' => $image[0]);
         }
         wp_reset_postdata();
         $args = $args + $link_args;
     }
     // Set access token?
     $broadcast_account = $account->broadcast_page();
     if ($broadcast_account !== null) {
         $args = $args + array('access_token' => $broadcast_account->access_token, 'page_id' => $broadcast_account->id);
     }
     $args = apply_filters($this->key() . '_broadcast_args', $args, $post_id, $comment_id);
     return $this->request($account, 'me/feed', $args, 'POST');
 }
Пример #15
0
                             }
                         }
                     } 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();
 }
 // Fix comment author urls for Facebook comments...
 $results = $wpdb->get_results("\n\t\tSELECT comment_ID, comment_author_url\n\t\tFROM {$wpdb->comments}\n\t\tWHERE comment_type = 'social-facebook'\n\t\tAND comment_author_url LIKE 'http://graph.facebook.com/%'\n\t");
 foreach ($results as $result) {
     $url = explode('http://graph.facebook.com/', $result->comment_author_url);
Пример #16
0
 /**
  * Loads the services.
  *
  * @return array
  */
 private function load_services()
 {
     if (isset($_GET['page']) and $_GET['page'] == basename(SOCIAL_FILE) or defined('IS_PROFILE_PAGE')) {
         $services = false;
     } else {
         $services = wp_cache_get('services', 'social');
     }
     if ($services === false) {
         $services = array();
         // Register services
         $registered_services = apply_filters('social_register_service', array());
         if (is_array($registered_services) and count($registered_services)) {
             $accounts = Social::option('accounts');
             foreach ($registered_services as $service) {
                 if (!isset($services[$service])) {
                     $service_accounts = array();
                     if (isset($accounts[$service]) && is_array($accounts[$service]) && !empty($accounts[$service])) {
                         // Flag social as enabled, we have at least one account.
                         if ($this->_enabled === null) {
                             $this->_enabled = true;
                         }
                         foreach ($accounts[$service] as $account_id => $account) {
                             // TODO Shouldn't have to do this. Fix later.
                             $account->personal = '0';
                             $service_accounts[$account_id] = $account;
                         }
                     }
                     $class = 'Social_Service_' . $service;
                     $services[$service] = new $class($service_accounts);
                 }
             }
             wp_cache_set('services', $services, 'social');
         }
     } else {
         if ($this->_enabled === null and is_array($services)) {
             foreach ($services as $service) {
                 if (count($service->accounts())) {
                     $this->_enabled = true;
                     break;
                 }
             }
         }
     }
     // don't return global services for commenters
     $commenter = get_user_meta(get_current_user_id(), 'social_commenter', true);
     if ($commenter == 'true' && !current_user_can('publish_posts')) {
         foreach ($services as $key => $accounts) {
             $services[$key]->clear_accounts();
         }
     }
     $personal_accounts = get_user_meta(get_current_user_id(), 'social_accounts', true);
     if (is_array($personal_accounts)) {
         foreach ($personal_accounts as $key => $_accounts) {
             if (count($_accounts) and isset($services[$key])) {
                 $class = 'Social_Service_' . $key . '_Account';
                 foreach ($_accounts as $account_id => $account) {
                     // TODO Shouldn't have to do this. Fix later.
                     $account->universal = '0';
                     if ($services[$key]->account_exists($account_id) and !defined('IS_PROFILE_PAGE')) {
                         $account = $this->merge_accounts($services[$key]->account($account_id)->as_object(), $account, $key);
                     }
                     $account = new $class((object) $account);
                     $services[$key]->account($account);
                     // Flag social as enabled, we have at least one account.
                     if ($this->_enabled === null) {
                         $this->_enabled = true;
                     }
                 }
             }
         }
     }
     return $services;
 }
Пример #17
0
 /**
  * Adds the account ID to the rel for the author link.
  *
  * @static
  * @param  string  $url
  * @return string
  */
 public static function get_comment_author_link($url)
 {
     if (Social::option('use_standard_comments') == '1') {
         return $url;
     }
     global $comment;
     $status_id = get_comment_meta($comment->comment_ID, 'social_status_id', true);
     if (in_array($comment->comment_type, self::comment_types()) && is_string($status_id) && $status_id) {
         $output = str_replace("rel='", "rel='" . $status_id . " ", $url);
         $output = str_replace("'>", "'>@", $output);
         return $output;
     }
     return $url;
 }
Пример #18
0
 /**
  * Broadcasts the message to the specified account. Returns the broadcasted ID.
  *
  * @param  Social_Service_Facebook_Account|object  $account     account to broadcast to
  * @param  string                                  $message     message to broadcast
  * @param  array                                   $args        extra arguments to pass to the request
  * @param  int                                     $post_id     post ID being broadcasted
  * @param  int                                     $comment_id  comment ID being broadcasted
  *
  * @return Social_Response
  */
 public function broadcast($account, $message, array $args = array(), $post_id = null, $comment_id = null)
 {
     global $post;
     // if post ID is set, this is a broadcast of a post,
     // if the comment ID is set it is a broadcast of a comment
     // TODO - add wrapper functions that abstract these actions out to separate methods
     // check comment being replied to, if it is a facebook comment on a post then
     // send the comment as a reply on the same post.
     // If that fails, then send as posting a link with a comment.
     $args = $args + array('message' => $message);
     if ($comment_id && ($comment = get_comment($comment_id))) {
         // Check for facebook comment reply
         if ($comment->comment_parent && ($parent_comment = get_comment($comment->comment_parent)) && in_array($parent_comment->comment_type, self::comment_types())) {
             if ($status_id = get_comment_meta($parent_comment->comment_ID, 'social_reply_to_id', true)) {
                 $parent_status_id = get_comment_meta($comment->comment_parent, 'social_status_id', true);
                 $args = apply_filters($this->key() . '_broadcast_args', $args, $post_id, $comment_id);
                 $response = $this->request($account, $status_id . '/comments', $args, 'POST');
                 if ($response !== false && $response->body()->result == 'success') {
                     // post succeeded, return response
                     update_comment_meta($comment->comment_ID, 'social_reply_to_id', addslashes_deep($status_id));
                     update_comment_meta($comment->comment_ID, 'social_status_id', addslashes_deep($parent_status_id));
                     update_comment_meta($comment->comment_ID, 'social_broadcast_id', addslashes_deep($response->body()->response->id));
                     return $response;
                 }
             }
         }
         $broadcasted_ids = get_post_meta($comment->comment_post_ID, '_social_broadcasted_ids', true);
         // If only 1 account has been posted to
         if (count($broadcasted_ids[$this->key()]) === 1) {
             $broadcast_account = array_shift($broadcasted_ids[$this->key()]);
             // And only one broadcast has been made from that account
             if (count($broadcast_account) === 1) {
                 reset($broadcast_account);
                 $status_id = key($broadcast_account);
                 $args = apply_filters($this->key() . '_broadcast_args', $args, $post_id, $comment_id);
                 $response = $this->request($account, $status_id . '/comments', $args, 'POST');
                 if ($response !== false && $response->body()->result == 'success') {
                     // post succeeded, return response
                     $broadcasted_id = $response->body()->response->id;
                     $response = $this->request($account, $broadcasted_id, array('fields' => 'can_comment'));
                     if ($response !== false && $response->body()->result == 'success' && $response->body()->response->can_comment) {
                         update_comment_meta($comment->comment_ID, 'social_reply_to_id', addslashes_deep($response->body()->response->id));
                     } else {
                         update_comment_meta($comment->comment_ID, 'social_reply_to_id', addslashes_deep($status_id));
                     }
                     update_comment_meta($comment->comment_ID, 'social_broadcast_id', addslashes_deep($broadcasted_id));
                     update_comment_meta($comment->comment_ID, 'social_status_id', addslashes_deep($status_id));
                     return $response;
                 }
             }
         }
         // Continuing on to simply post on user's wall
         // posting with a link, do not include URL in comment.
         $format = trim(str_replace('{url}', '', Social::option('comment_broadcast_format')));
         $message = $this->format_comment_content($comment, $format);
         $args['message'] = $message;
         // prep data
         $post = get_post($comment->comment_post_ID);
         setup_postdata($post);
         $link_args = array('link' => social_get_shortlink($post->ID), 'title' => get_the_title($post->ID), 'description' => get_the_excerpt());
         if (function_exists('has_post_thumbnail') and has_post_thumbnail($post->ID)) {
             $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'single-post-thumbnail');
             $link_args = $link_args + array('picture' => $image[0]);
         }
         wp_reset_postdata();
         $args = $args + $link_args;
     }
     // Set access token?
     $broadcast_account = $account->broadcast_page();
     if ($broadcast_account !== null) {
         $args = $args + array('access_token' => $broadcast_account->access_token, 'page_id' => $broadcast_account->id);
     }
     $args = apply_filters($this->key() . '_broadcast_args', $args, $post_id, $comment_id);
     $request = apply_filters($this->key() . '_broadcast_request', array('url' => 'me/feed', 'args' => $args, 'post_id' => $post_id, 'comment_id' => $comment_id));
     $response = $this->request($account, $request['url'], $request['args'], 'POST');
     if ($response !== false && $response->body()->result == 'success') {
         // post succeeded, return response
         update_comment_meta($comment->comment_ID, 'social_reply_to_id', addslashes_deep($response->body()->response->id));
         update_comment_meta($comment->comment_ID, 'social_broadcast_id', addslashes_deep($response->body()->response->id));
     }
     return $response;
 }
Пример #19
0
 function link_entities($defer_to_anywhere = true)
 {
     $entities = array();
     // mentions
     $anywhere = Social::option('twitter_anywhere_api_key');
     if (!$defer_to_anywhere || empty($anywhere) || is_feed()) {
         foreach ($this->mentions() as $entity) {
             $entities['start_' . str_pad($entity->indices[0], 5, '0', STR_PAD_LEFT)] = array('find' => $entity->screen_name, 'replace' => AKTT::profile_link($entity->screen_name), 'start' => $entity->indices[0], 'end' => $entity->indices[1]);
         }
     }
     // hashtags
     foreach ($this->hashtags() as $entity) {
         $entities['start_' . str_pad($entity->indices[0], 5, '0', STR_PAD_LEFT)] = array('find' => $entity->text, 'replace' => AKTT::hashtag_link($entity->text), 'start' => $entity->indices[0], 'end' => $entity->indices[1]);
     }
     // URLs
     foreach ($this->urls() as $entity) {
         $entities['start_' . str_pad($entity->indices[0], 5, '0', STR_PAD_LEFT)] = array('find' => $entity->url, 'replace' => '<a href="' . esc_url($entity->expanded_url) . '">' . esc_html($entity->display_url) . '</a>', 'start' => $entity->indices[0], 'end' => $entity->indices[1]);
     }
     ksort($entities);
     $str = $this->content();
     $diff = 0;
     foreach ($entities as $entity) {
         $start = $entity['start'] + $diff;
         $end = $entity['end'] + $diff;
         // $log = array();
         // $log[] = 'diff: '.$diff;
         // $log[] = 'entity start: '.$entity['start'];
         // $log[] = 'entity start chars: '.substr($this->content(), $entity['start'], 3);
         // $log[] = 'diff start: '.$start;
         // $log[] = 'diff start chars: '.substr($str, $start, 3);
         // $log[] = 'entity end: '.$entity['end'];
         // $log[] = 'diff end: '.$end;
         // $log[] = 'find len: '.strlen($entity['find']);
         // $log[] = 'find: '.htmlspecialchars($entity['find']);
         // $log[] = 'replace len: '.strlen($entity['replace']);
         // $log[] = 'replace: '.htmlspecialchars($entity['replace']);
         // echo '<p>'.implode('<br>', $log).'</p>';
         $str = substr_replace($str, $entity['replace'], $start, $end - $start);
         $diff += strlen($entity['replace']) - ($end - $start);
     }
     return $str;
 }