web/wp-content/plugins/social/lib/social/service/facebook.php
changeset 196 5e8dcbe22c24
equal deleted inserted replaced
195:c7c0fbc09788 196:5e8dcbe22c24
       
     1 <?php
       
     2 /**
       
     3  * Facebook implementation for the service.
       
     4  *
       
     5  * @package    Social
       
     6  * @subpackage services
       
     7  */
       
     8 final class Social_Service_Facebook extends Social_Service implements Social_Interface_Service {
       
     9 
       
    10 	/**
       
    11 	 * @var  string  service key
       
    12 	 */
       
    13 	protected $_key = 'facebook';
       
    14 
       
    15 	/**
       
    16 	 * The max length a post can be when broadcasted.
       
    17 	 *
       
    18 	 * @return int
       
    19 	 */
       
    20 	public function max_broadcast_length() {
       
    21 		return 420;
       
    22 	}
       
    23 
       
    24 	/**
       
    25 	 * Handles the requests to the proxy.
       
    26 	 *
       
    27 	 * @param  Social_Service_Account|int  $account
       
    28 	 * @param  string                      $api
       
    29 	 * @param  array                       $args
       
    30 	 * @param  string                      $method
       
    31 	 * @return Social_Response|bool
       
    32 	 */
       
    33 	public function request($account, $api, array $args = array(), $method = 'GET') {
       
    34 		$api = urlencode($api);
       
    35 		return parent::request($account, $api, $args, $method);
       
    36 	}
       
    37 
       
    38 	/**
       
    39 	 * Any additional parameters that should be passed with a broadcast.
       
    40 	 *
       
    41 	 * @static
       
    42 	 * @return array
       
    43 	 */
       
    44 	public function get_broadcast_extras($account_id, $post, $args = array()) {
       
    45 		if (get_post_format($post->ID) !== 'status') {
       
    46 			setup_postdata($post);
       
    47 			$link_args = array(
       
    48 				'link' => get_post_permalink($post->ID),
       
    49 				'title' => get_the_title($post->ID),
       
    50 				'description' => get_the_excerpt(),
       
    51 			);
       
    52 			if (function_exists('has_post_thumbnail') and has_post_thumbnail($post->ID)) {
       
    53 				$image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'single-post-thumbnail');
       
    54 				$link_args = $link_args + array(
       
    55 					'picture' => $image[0],
       
    56 				);
       
    57 			}
       
    58 			wp_reset_postdata();
       
    59 			$args = $args + $link_args;
       
    60 		}
       
    61 		return parent::get_broadcast_extras($account_id, $post, $args);
       
    62 	}
       
    63 
       
    64 	/**
       
    65 	 * Broadcasts the message to the specified account. Returns the broadcasted ID.
       
    66 	 *
       
    67 	 * @param  Social_Service_Facebook_Account|object  $account     account to broadcast to
       
    68 	 * @param  string                                  $message     message to broadcast
       
    69 	 * @param  array                                   $args        extra arguments to pass to the request
       
    70 	 * @param  int                                     $post_id     post ID being broadcasted
       
    71 	 * @param  int                                     $comment_id  comment ID being broadcasted
       
    72 	 *
       
    73 	 * @return Social_Response
       
    74 	 */
       
    75 	public function broadcast($account, $message, array $args = array(), $post_id = null, $comment_id = null) {
       
    76 		global $post;
       
    77 		// if post ID is set, this is a broadcast of a post,
       
    78 		// if the comment ID is set it is a broadcast of a comment
       
    79 		// TODO - add wrapper functions that abstract these actions out to separate methods
       
    80 
       
    81 		// check comment being replied to, if it is a facebook comment on a post then
       
    82 		// send the comment as a reply on the same post.
       
    83 		// If that fails, then send as posting a link with a comment.
       
    84 
       
    85 		$args = $args + array(
       
    86 			'message' => $message,
       
    87 		);
       
    88 
       
    89 		// first try to send comment to an existing Fb post
       
    90 		if (!is_null($comment_id)) {
       
    91 			$comment = get_comment($comment_id);
       
    92 			if (!empty($comment->comment_parent)) {
       
    93 				$parent_comment = get_comment($comment->comment_parent);
       
    94 				if (!is_null($parent_comment) && in_array($parent_comment->comment_type, self::comment_types())) {
       
    95 					$status_id = get_comment_meta($parent_comment->comment_ID, 'social_status_id', true);
       
    96 					if (!empty($status_id)) {
       
    97 						// we have a Facebook post to reply to
       
    98 						$parts = explode('_', $status_id);
       
    99 						if (count($parts) == 3) {
       
   100 							$status_id = $parts[0].'_'.$parts[1];
       
   101 						}
       
   102 						$args = apply_filters($this->key().'_broadcast_args', $args, $post_id, $comment_id);
       
   103 						$response = $this->request($account, $status_id.'/comments', $args, 'POST');
       
   104 						if ($response !== false && $response->id() !== '0') {
       
   105 							// post succeeded, return response
       
   106 							return $response;
       
   107 						}
       
   108 						// ...broadcast failed, continue and send as post to feed
       
   109 					}
       
   110 				}
       
   111 			}
       
   112 
       
   113 			// posting with a link, do not include URL in comment.
       
   114 			$format = trim(str_replace('{url}', '', Social::option('comment_broadcast_format')));
       
   115 			$message = $this->format_comment_content($comment, $format);
       
   116 			$args['message'] = $message;
       
   117 
       
   118 			// prep data
       
   119 			$post = get_post($comment->comment_post_ID);
       
   120 			setup_postdata($post);
       
   121 			$link_args = array(
       
   122 				'link' => get_post_permalink($post->ID),
       
   123 				'title' => get_the_title($post->ID),
       
   124 				'description' => get_the_excerpt(),
       
   125 			);
       
   126 			if (function_exists('has_post_thumbnail') and has_post_thumbnail($post->ID)) {
       
   127 				$image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'single-post-thumbnail');
       
   128 				$link_args = $link_args + array(
       
   129 					'picture' => $image[0],
       
   130 				);
       
   131 			}
       
   132 			wp_reset_postdata();
       
   133 			$args = $args + $link_args;
       
   134 		}
       
   135 
       
   136 		// Set access token?
       
   137 		$broadcast_account = $account->broadcast_page();
       
   138 		if ($broadcast_account !== null) {
       
   139 			$args = $args + array(
       
   140 				'access_token' => $broadcast_account->access_token,
       
   141 				'page_id' => $broadcast_account->id,
       
   142 			);
       
   143 		}
       
   144 
       
   145 		$args = apply_filters($this->key().'_broadcast_args', $args, $post_id, $comment_id);
       
   146 		$endpoint = isset($args['link']) ? 'links' : 'feed';
       
   147 		return $this->request($account, 'me/'.$endpoint, $args, 'POST');
       
   148 	}
       
   149 
       
   150 	/**
       
   151 	 * Aggregates comments by URL.
       
   152 	 *
       
   153 	 * @param  object  $post
       
   154 	 * @param  array   $urls
       
   155 	 *
       
   156 	 * @return void
       
   157 	 */
       
   158 	public function aggregate_by_url(&$post, array $urls) {
       
   159 		foreach ($urls as $url) {
       
   160 			if (!empty($url)) {
       
   161 				$url = 'https://graph.facebook.com/search?type=post&q='.$url;
       
   162 				Social::log('Searching by URL(s) for post #:post_id. (Query: :url)', array(
       
   163 					'post_id' => $post->ID,
       
   164 					'url' => $url,
       
   165 				));
       
   166 				$response = wp_remote_get($url);
       
   167 				if (!is_wp_error($response)) {
       
   168 					$response = json_decode($response['body']);
       
   169 
       
   170 					if (isset($response->data) and is_array($response->data) and count($response->data)) {
       
   171 						foreach ($response->data as $result) {
       
   172 							if (in_array($result->id, $post->aggregated_ids[$this->_key])) {
       
   173 								Social_Aggregation_Log::instance($post->ID)->add($this->_key, $result->id, 'url', true);
       
   174 								continue;
       
   175 							}
       
   176 							else {
       
   177 								if ($this->is_original_broadcast($post, $result->id)) {
       
   178 									continue;
       
   179 								}
       
   180 							}
       
   181 
       
   182 							Social_Aggregation_Log::instance($post->ID)->add($this->_key, $result->id, 'url');
       
   183 							$post->aggregated_ids[$this->_key][] = $result->id;
       
   184 							$post->results[$this->_key][$result->id] = $result;
       
   185 						}
       
   186 					}
       
   187 				}
       
   188 				else {
       
   189 					Social::log('URL search failed for post #:post_id.', array(
       
   190 						'post_id' => $post->ID,
       
   191 					));
       
   192 				}
       
   193 			}
       
   194 		}
       
   195 	}
       
   196 
       
   197 	/**
       
   198 	 * Aggregates comments by the service's API.
       
   199 	 *
       
   200 	 * @param  object  $post
       
   201 	 *
       
   202 	 * @return array
       
   203 	 */
       
   204 	public function aggregate_by_api(&$post) {
       
   205 		// find broadcasts for service
       
   206 		$accounts = $this->get_aggregation_accounts($post);
       
   207 
       
   208 		if (isset($accounts[$this->_key]) and count($accounts[$this->_key])) {
       
   209 			$like_count = 0;
       
   210 			foreach ($accounts[$this->_key] as $account) {
       
   211 				if (isset($post->broadcasted_ids[$this->_key][$account->id()])) {
       
   212 					foreach ($post->broadcasted_ids[$this->_key][$account->id()] as $broadcasted_id => $data) {
       
   213 						$id = explode('_', $broadcasted_id);
       
   214 						$request = $this->request($account, $broadcasted_id.'/comments');
       
   215 						if ($request !== false && isset($request->body()->response)) {
       
   216 							$response = $request->body()->response;
       
   217 							if (isset($response->data) and is_array($response->data) and count($response->data)) {
       
   218 								foreach ($response->data as $result) {
       
   219 									$data = array(
       
   220 										'parent_id' => $broadcasted_id,
       
   221 									);
       
   222 									if (in_array($result->id, $post->aggregated_ids[$this->_key])) {
       
   223 										Social_Aggregation_Log::instance($post->ID)->add($this->_key, $result->id, 'reply', true, $data);
       
   224 										continue;
       
   225 									}
       
   226 									else {
       
   227 										if ($this->is_original_broadcast($post, $result->id)) {
       
   228 											continue;
       
   229 										}
       
   230 									}
       
   231 
       
   232 									Social_Aggregation_Log::instance($post->ID)->add($this->_key, $result->id, 'reply', false, $data);
       
   233 									$result->status_id = $broadcasted_id;
       
   234 									$post->results[$this->_key][$result->id] = $result;
       
   235 								}
       
   236 							}
       
   237 						}
       
   238 
       
   239 						$this->search_for_likes($account, $broadcasted_id, $id[0], $post, $like_count);
       
   240 					}
       
   241 				}
       
   242 			}
       
   243 
       
   244 			if (count($like_count)) {
       
   245 				Social_Aggregation_Log::instance($post->ID)->add($this->_key, $post->ID.time(), 'like', !$like_count, array('total' => $like_count));
       
   246 			}
       
   247 		}
       
   248 	}
       
   249 
       
   250 	/**
       
   251 	 * Searches for likes on the post.
       
   252 	 *
       
   253 	 * @param  object       $account
       
   254 	 * @param  string       $id
       
   255 	 * @param  int          $parent_id
       
   256 	 * @param  WP_Post      $post
       
   257 	 * @param  int          $like_count
       
   258 	 * @param  bool|string  $next
       
   259 	 * @return void
       
   260 	 */
       
   261 	private function search_for_likes(&$account, $id, $parent_id, &$post, &$like_count, $next = false) {
       
   262 		$url = $id.'/likes';
       
   263 		$args = array(
       
   264 			'limit' => '100'
       
   265 		);
       
   266 		if ($next !== false) {
       
   267 			$args['offset'] = $next;
       
   268 		}
       
   269 
       
   270 		$request = $this->request($account, $url, $args);
       
   271 		if ($request !== false && isset($request->body()->response)) {
       
   272 			$response = $request->body()->response;
       
   273 			if (isset($response->data) && is_array($response->data) && count($response->data)) {
       
   274 				foreach ($response->data as $result) {
       
   275 					if ((isset($post->results) && isset($post->results[$this->_key]) && isset($post->results[$this->_key][$result->id])) ||
       
   276 						(in_array($result->id, $post->aggregated_ids[$this->_key]))
       
   277 					) {
       
   278 						continue;
       
   279 					}
       
   280 					$post->aggregated_ids[$this->_key][] = $result->id;
       
   281 					$post->results[$this->_key][$result->id] = (object) array_merge(array(
       
   282 						'like' => true,
       
   283 						'status_id' => $id,
       
   284 						'raw' => $result,
       
   285 					), (array) $result);
       
   286 					++$like_count;
       
   287 				}
       
   288 			}
       
   289 
       
   290 			if (isset($response->paging) && isset($response->paging->next)) {
       
   291 				$url = parse_url($response->paging->next);
       
   292 				if (!empty($url['query'])) {
       
   293 					parse_str($url['query'], $query);
       
   294 					if (!empty($query['offset'])) {
       
   295 						$this->search_for_likes($account, $id, $parent_id, $post, $like_count, $query['offset']);
       
   296 					}
       
   297 				}
       
   298 			}
       
   299 		}
       
   300 	}
       
   301 
       
   302 	/**
       
   303 	 * Saves the aggregated comments.
       
   304 	 *
       
   305 	 * @param  object  $post
       
   306 	 * @return void
       
   307 	 */
       
   308 	public function save_aggregated_comments(&$post) {
       
   309 		if (isset($post->results[$this->_key])) {
       
   310 			global $wpdb;
       
   311 
       
   312 			foreach ($post->results[$this->_key] as $result) {
       
   313 				$commentdata = array(
       
   314 					'comment_post_ID' => $post->ID,
       
   315 					'comment_author_email' => $wpdb->escape($this->_key.'.'.$result->id.'@example.com'),
       
   316 					'comment_author_IP' => $_SERVER['SERVER_ADDR'],
       
   317 					'comment_agent' => 'Social Aggregator'
       
   318 				);
       
   319 				if (!isset($result->like)) {
       
   320 					$url = 'http://graph.facebook.com/'.$result->from->id;
       
   321 					$request = wp_remote_get($url);
       
   322 					if (!is_wp_error($request)) {
       
   323 						$response = json_decode($request['body']);
       
   324 
       
   325 						$account = (object) array(
       
   326 							'user' => $response
       
   327 						);
       
   328 						$class = 'Social_Service_'.$this->_key.'_Account';
       
   329 						$account = new $class($account);
       
   330 
       
   331 						$commentdata = array_merge($commentdata, array(
       
   332 							'comment_type' => 'social-facebook',
       
   333 							'comment_author' => $wpdb->escape($result->from->name),
       
   334 							'comment_author_url' => $account->url(),
       
   335 							'comment_content' => $wpdb->escape($result->message),
       
   336 							'comment_date' => date('Y-m-d H:i:s', strtotime($result->created_time) + (get_option('gmt_offset') * 3600)),
       
   337 							'comment_date_gmt' => gmdate('Y-m-d H:i:s', strtotime($result->created_time)),
       
   338 						));
       
   339 
       
   340 					}
       
   341 				}
       
   342 				else {
       
   343 					$url = 'http://facebook.com/profile.php?id='.$result->id;
       
   344 					$commentdata = array_merge($commentdata, array(
       
   345 						'comment_type' => 'social-facebook-like',
       
   346 						'comment_author' => $wpdb->escape($result->name),
       
   347 						'comment_author_url' => $url,
       
   348 						'comment_content' => $wpdb->escape('<a href="'.$url.'" target="_blank">'.$result->name.'</a> liked this on Facebook.'),
       
   349 						'comment_date' => current_time('mysql'),
       
   350 						'comment_date_gmt' => current_time('mysql', 1),
       
   351 					));
       
   352 				}
       
   353 
       
   354 				$user_id = (isset($result->like) ? $result->id : $result->from->id);
       
   355 				$commentdata = array_merge($commentdata, array(
       
   356 					'comment_post_ID' => $post->ID,
       
   357 					'comment_author_email' => $this->_key.'.'.$user_id.'@example.com',
       
   358 				));
       
   359 
       
   360 				$result_id = (isset($result->status_id) ? $result->status_id : $result->id);
       
   361 				if (apply_filters('social_approve_likes_and_retweets', true) && isset($result->like)) {
       
   362 					$commentdata['comment_approved'] = 1;
       
   363 				}
       
   364 				else if (($commentdata = $this->allow_comment($commentdata, $result_id, $post)) === false) {
       
   365 					continue;
       
   366 				}
       
   367 
       
   368 				// sanity check to make sure this comment is not a duplicate
       
   369 				if ($this->is_duplicate_comment($post, $result->id)) {
       
   370 					Social::log('Result #:result_id already exists, skipping.', array(
       
   371 						'result_id' => $result->id
       
   372 					), 'duplicate-comment');
       
   373 					continue;
       
   374 				}
       
   375 
       
   376 				Social::log('Saving #:result_id.', array(
       
   377 					'result_id' => (isset($result->status_id) ? $result->status_id : $result->id)
       
   378 				));
       
   379 
       
   380 				$comment_id = 0;
       
   381 				try
       
   382 				{
       
   383 					Social::Log('Attempting to save commentdata: :commentdata', array(
       
   384 						'commentdata' => print_r($commentdata, true)
       
   385 					));
       
   386 					$comment_id = wp_insert_comment($commentdata);
       
   387 
       
   388 					update_comment_meta($comment_id, 'social_account_id', addslashes_deep($user_id));
       
   389 					update_comment_meta($comment_id, 'social_profile_image_url', addslashes_deep('http://graph.facebook.com/'.$user_id.'/picture'));
       
   390 					update_comment_meta($comment_id, 'social_status_id', addslashes_deep((isset($result->status_id) ? $result->status_id : $result->id)));
       
   391 
       
   392 					if (!isset($result->raw)) {
       
   393 						$result = (object) array_merge((array) $result, array('raw' => $result));
       
   394 					}
       
   395 					update_comment_meta($comment_id, 'social_raw_data', addslashes_deep(base64_encode(json_encode($result->raw))));
       
   396 
       
   397 					if ($commentdata['comment_approved'] !== 'spam') {
       
   398 						if ($commentdata['comment_approved'] == '0') {
       
   399 							wp_notify_moderator($comment_id);
       
   400 						}
       
   401 
       
   402 						if (get_option('comments_notify') and $commentdata['comment_approved'] and (!isset($commentdata['user_id']) or $post->post_author != $commentdata['user_id'])) {
       
   403 							wp_notify_postauthor($comment_id, 'comment');
       
   404 						}
       
   405 					}
       
   406 				}
       
   407 				catch (Exception $e) {
       
   408 					// Something went wrong, remove the aggregated ID.
       
   409 					if (($key = array_search((isset($result->status_id) ? $result->status_id : $result->id), $post->aggregated_ids['facebook'])) !== false) {
       
   410 						unset($post->aggregated_ids['facebook'][$key]);
       
   411 					}
       
   412 
       
   413 					if ((int) $comment_id) {
       
   414 						// Delete the comment in case it wasn't the insert that failed.
       
   415 						wp_delete_comment($comment_id);
       
   416 					}
       
   417 				}
       
   418 			}
       
   419 		}
       
   420 	}
       
   421 
       
   422 	/**
       
   423 	 * Hook to allow services to define their aggregation row items based on the passed in type.
       
   424 	 *
       
   425 	 * @param  string  $type
       
   426 	 * @param  object  $item
       
   427 	 * @param  string  $username
       
   428 	 * @param  int     $id
       
   429 	 * @return string
       
   430 	 */
       
   431 	public function aggregation_row($type, $item, $username, $id) {
       
   432 		if ($type == 'like') {
       
   433 			return sprintf(__('Found %s additional likes.', 'social'), $item->data['total']);
       
   434 		}
       
   435 		return '';
       
   436 	}
       
   437 
       
   438 	/**
       
   439 	 * Checks the response to see if the broadcast limit has been reached.
       
   440 	 *
       
   441 	 * @param  string  $response
       
   442 	 * @return bool
       
   443 	 */
       
   444 	public function limit_reached($response) {
       
   445 		return ($response == '(#341) Feed action request limit reached');
       
   446 	}
       
   447 
       
   448 	/**
       
   449 	 * Checks the response to see if the broadcast is a duplicate.
       
   450 	 *
       
   451 	 * @param  string  $response
       
   452 	 * @return bool
       
   453 	 */
       
   454 	public function duplicate_status($response) {
       
   455 		return ($response == '(#506) Duplicate status message');
       
   456 	}
       
   457 
       
   458 	/**
       
   459 	 * Checks the response to see if the account has been deauthorized.
       
   460 	 *
       
   461 	 * @param  string  $response
       
   462 	 * @param  bool    $check_invalid_key
       
   463 	 * @return bool
       
   464 	 */
       
   465 	public function deauthorized($response, $check_invalid_key = false) {
       
   466 		if (($check_invalid_key and $response == 'invalid key') or $response == 'Error validating access token') {
       
   467 			return true;
       
   468 		}
       
   469 		return false;
       
   470 	}
       
   471 
       
   472 	/**
       
   473 	 * Returns the key to use on the request response to pull the ID.
       
   474 	 *
       
   475 	 * @return string
       
   476 	 */
       
   477 	public function response_id_key() {
       
   478 		return 'id';
       
   479 	}
       
   480 
       
   481 	/**
       
   482 	 * Returns the response message.
       
   483 	 *
       
   484 	 * @param  object  $body
       
   485 	 * @param  string  $default
       
   486 	 *
       
   487 	 * @return mixed
       
   488 	 */
       
   489 	public function response_message($body, $default) {
       
   490 		if (isset($body->response) and isset($body->response->message)) {
       
   491 			return $body->response->message;
       
   492 		}
       
   493 
       
   494 		return $default;
       
   495 	}
       
   496 
       
   497 	/**
       
   498 	 * Returns the status URL to a broadcasted item.
       
   499 	 *
       
   500 	 * @param  string      $username
       
   501 	 * @param  string|int  $id
       
   502 	 * @return string|null
       
   503 	 */
       
   504 	public function status_url($username, $id) {
       
   505 		if (strpos($id, '_') === false) {
       
   506 			return null;
       
   507 		}
       
   508 
       
   509 		$ids = explode('_', $id);
       
   510 		return 'http://facebook.com/permalink.php?story_fbid='.$ids[1].'&id='.$ids[0];
       
   511 	}
       
   512 
       
   513 	/**
       
   514 	 * Loads the pages for the account.
       
   515 	 *
       
   516 	 * @param  Social_Service_Account  $account
       
   517 	 * @param  bool                    $is_profile
       
   518 	 * @param  bool                    $save
       
   519 	 * @return array
       
   520 	 */
       
   521 	public function get_pages(Social_Service_Account $account, $is_profile = false, $save = true) {
       
   522 		$pages = array();
       
   523 		if ($account->use_pages() or $account->use_pages(true)) {
       
   524 			$response = $this->request($account, $account->id().'/accounts');
       
   525 			if ($response !== false and isset($response->body()->response)) {
       
   526 				if (isset($response->body()->response->data)) {
       
   527 					foreach ($response->body()->response->data as $item) {
       
   528 						if ($item->category != 'Application') {
       
   529 							$pages[$item->id] = $item;
       
   530 						}
       
   531 					}
       
   532 				}
       
   533 			    else if ($response->body()->response == 'incorrect method') {
       
   534 					// Account no longer has page permissions.
       
   535 					$service = Social::instance()->service('facebook');
       
   536 					$accounts = $service->accounts();
       
   537 					foreach ($accounts as $account_id => $_account) {
       
   538 						if ($account_id == $account->id()) {
       
   539 							$_account->use_pages(false, false);
       
   540 							$_account->use_pages(true, false);
       
   541 							$_account->pages(array(), $is_profile);
       
   542 						}
       
   543 
       
   544 						$accounts[$account_id] = $account->as_object();
       
   545 					}
       
   546 
       
   547 					if ($save) {
       
   548 						$service->accounts($accounts)->save($is_profile);
       
   549 					}
       
   550 				}
       
   551 			}
       
   552 		}
       
   553 		return $pages;
       
   554 	}
       
   555 
       
   556 	/**
       
   557 	 * Builds the page's image URL.
       
   558 	 *
       
   559 	 * @param  object  $account
       
   560 	 * @return string
       
   561 	 */
       
   562 	public function page_image_url($account) {
       
   563 		return apply_filters('social_facebook_page_image_url', 'http://graph.facebook.com/'.$account->id.'/picture', $account);
       
   564 	}
       
   565 
       
   566 	/**
       
   567 	 * Comment types for this service.
       
   568 	 *
       
   569 	 * @static
       
   570 	 * @return array
       
   571 	 */
       
   572 	public static function comment_types() {
       
   573 		return array(
       
   574 			'social-facebook',
       
   575 			'social-facebook-like',
       
   576 		);
       
   577 	}
       
   578 
       
   579 	/**
       
   580 	 * Comment types that are "meta". In this case, Likes (and perhaps Shares in the future).
       
   581 	 *
       
   582 	 * @static
       
   583 	 * @return array
       
   584 	 */
       
   585 	public static function comment_types_meta() {
       
   586 		return array(
       
   587 			'social-facebook-like',
       
   588 		);
       
   589 	}
       
   590 
       
   591 	public static function social_settings_save($controller) {
       
   592 		// Save Facebook pages
       
   593 		$is_profile = ($controller->request()->post('social_profile') == 'true');
       
   594 		if ($is_profile and !defined('IS_PROFILE_PAGE')) {
       
   595 			define('IS_PROFILE_PAGE', true);
       
   596 		}
       
   597 
       
   598 		$enabled_child_accounts = $controller->request()->post('social_enabled_child_accounts');
       
   599 		if (!is_array($enabled_child_accounts)) {
       
   600 			$enabled_child_accounts = array();
       
   601 		}
       
   602 		$service = $controller->social()->service('facebook');
       
   603 		if ($service !== false) {
       
   604 			foreach ($service->accounts() as $account) {
       
   605 				$updated_accounts = array();
       
   606 				foreach ($service->accounts() as $account) {
       
   607 					//default service to empty array in case it is not set
       
   608 					$enabled_child_accounts[$service->key()] = isset($enabled_child_accounts[$service->key()]) ? $enabled_child_accounts[$service->key()] : array();
       
   609 
       
   610 					$account->update_enabled_child_accounts($enabled_child_accounts[$service->key()]);
       
   611 					$updated_accounts[$account->id()] = $account->as_object();
       
   612 				}
       
   613 				$service->accounts($updated_accounts)->save($is_profile);
       
   614 			}
       
   615 		}
       
   616 	}
       
   617 
       
   618 	public static function social_settings_default_accounts($accounts, $controller) {
       
   619 		if (is_array($controller->request()->post('social_default_pages'))) {
       
   620 			if (!isset($accounts['facebook'])) {
       
   621 				$accounts['facebook'] = array(
       
   622 					'pages' => array()
       
   623 				);
       
   624 			}
       
   625 			$accounts['facebook']['pages'] = $controller->request()->post('social_default_pages');
       
   626 		}
       
   627 		else {
       
   628 			$accounts['facebook']['pages'] = array();
       
   629 		}
       
   630 		return $accounts;
       
   631 	}
       
   632 
       
   633 } // End Social_Service_Facebook