/**
  * Publish a post to a Facebook User Timeline.
  *
  * @since 1.0
  *
  * @global \Facebook_Loader $facebook_loader Access Facebook application credentials
  * @param int $post_id WordPress post identifier
  * @param stdClass|WP_Post $post WordPress post object
  * @return void
  */
 public static function publish_to_facebook_profile($post_id, $post)
 {
     global $facebook_loader;
     $post_id = absint($post_id);
     if (!(isset($facebook_loader) && $facebook_loader->app_access_token_exists() && $post && $post_id)) {
         return;
     }
     // does the current post have an existing Facebook post id stored? no need to publish again
     if (get_post_meta($post_id, 'fb_author_post_id', true)) {
         return;
     }
     $meta_box_present = true;
     if (defined('XMLRPC_REQUEST') && XMLRPC_REQUEST) {
         $meta_box_present = false;
     }
     if (!class_exists('Facebook_Social_Publisher_Meta_Box_Profile')) {
         require_once dirname(__FILE__) . '/publish-box-profile.php';
     }
     if ($meta_box_present && get_post_meta($post_id, Facebook_Social_Publisher_Meta_Box_Profile::POST_META_KEY_FEATURE_ENABLED, true) === '0') {
         return;
     }
     setup_postdata($post);
     $post_type = get_post_type($post);
     if (!(self::post_type_is_public($post_type) && post_type_supports($post_type, 'author') && isset($post->post_author))) {
         return;
     }
     $post_author = (int) $post->post_author;
     if (!$post_author) {
         return;
     }
     // test the author, not the current actor
     if (!self::user_can_publish_to_facebook($post_author)) {
         return;
     }
     if (!class_exists('Facebook_User')) {
         require_once $facebook_loader->plugin_directory . 'facebook-user.php';
     }
     $author_facebook_id = Facebook_User::get_facebook_profile_id($post_author);
     if (!$author_facebook_id) {
         return;
     }
     // check our assumptions about a valid link in place
     // fail if a piece of the filter process killed our response
     $link = apply_filters('facebook_rel_canonical', get_permalink($post_id));
     if (!$link) {
         return;
     }
     $og_action = false;
     if (!class_exists('Facebook_Social_Publisher_Settings')) {
         require_once $facebook_loader->plugin_directory . 'admin/settings-social-publisher.php';
     }
     if (get_option(Facebook_Social_Publisher_Settings::OPTION_OG_ACTION)) {
         $og_action = true;
     }
     if (!class_exists('Facebook_Open_Graph_Protocol')) {
         require_once $facebook_loader->plugin_directory . 'open-graph-protocol.php';
     }
     $path = $author_facebook_id . '/';
     if ($og_action && Facebook_Open_Graph_Protocol::get_post_og_type($post) === 'article') {
         $story = array('article' => $link);
         $path .= 'news.publishes';
         if ($meta_box_present) {
             $story['fb:explicitly_shared'] = 'true';
         }
     } else {
         $story = array('link' => $link);
         $path .= 'feed';
     }
     $message = get_post_meta($post_id, Facebook_Social_Publisher_Meta_Box_Profile::POST_META_KEY_MESSAGE, true);
     if (is_string($message) && $message) {
         $story['message'] = trim($message);
     }
     if (!class_exists('Facebook_WP_Extend')) {
         require_once $facebook_loader->plugin_directory . 'includes/facebook-php-sdk/class-facebook-wp.php';
     }
     $status_messages = array();
     try {
         $publish_result = Facebook_WP_Extend::graph_api_with_app_access_token($path, 'POST', $story);
         if (isset($publish_result['id'])) {
             update_post_meta($post_id, 'fb_author_post_id', sanitize_text_field($publish_result['id']));
             delete_post_meta($post_id, Facebook_Social_Publisher_Meta_Box_Profile::POST_META_KEY_MESSAGE);
             delete_post_meta($post_id, Facebook_Social_Publisher_Meta_Box_Profile::POST_META_KEY_FEATURE_ENABLED);
         }
     } catch (WP_FacebookApiException $e) {
         $error_result = $e->getResult();
         $status_messages[] = array('message' => esc_html(__('Failed posting to your Facebook Timeline.', 'facebook')) . ' ' . esc_html(__('Error', 'facebook')) . ': ' . esc_html(json_encode($error_result['error'])), 'error' => true);
     }
     if (isset($publish_result) && isset($publish_result['id'])) {
         $link = '<a href="' . esc_url('https://www.facebook.com/' . $publish_result['id'], array('http', 'https')) . '" target="_blank">' . esc_html(__('Facebook Timeline', 'facebook')) . '</a>';
         if (empty($message)) {
             $message = sprintf(esc_html(__('Posted to %s', 'facebook')), $link);
         } else {
             $message = sprintf(esc_html(__('Posted to %1$s with message "%2$s"', 'facebook')), $link, esc_html($message));
         }
         $status_messages[] = array('message' => $message, 'error' => false);
     }
     // add new status messages
     if (!empty($status_messages)) {
         $existing_status_messages = get_post_meta($post_id, 'fb_status_messages', true);
         if (is_array($existing_status_messages) && !empty($existing_status_messages)) {
             $status_messages = array_merge($existing_status_messages, $status_messages);
         }
         update_post_meta($post_id, 'facebook_status_messages', $status_messages);
         add_filter('redirect_post_location', array('Facebook_Social_Publisher', 'add_new_post_location'));
     }
 }
示例#2
0
 /**
  * Post to the timeline of mentioned Facebook pages
  *
  * @since 1.1
  * @param stdClass $post post object
  * @return array status messages with message and error. used to update publisher of performed action on admin_notices
  */
 public static function post_to_mentioned_pages_timelines($post)
 {
     global $facebook;
     $post_id = $post->ID;
     $post_type = get_post_type($post);
     if (!$post_type) {
         return;
     }
     $mentioned_pages = get_post_meta($post_id, 'fb_mentioned_pages', true);
     if (!is_array($mentioned_pages) || empty($mentioned_pages)) {
         return;
     }
     // check our assumptions about a valid link in place
     // fail if a piece of the filter process killed our response
     $link = apply_filters('facebook_rel_canonical', get_permalink($post_id));
     if (!$link) {
         return;
     }
     $story = array('link' => $link, 'ref' => 'fbwpp');
     // either message or link is required
     // confident we have link, making message optional
     $mentioned_pages_message = get_post_meta($post_id, 'fb_mentioned_pages_message', true);
     if (!empty($mentioned_pages_message)) {
         $story['message'] = $mentioned_pages_message;
     }
     unset($mentioned_pages_message);
     // does current post type and the current theme support post thumbnails?
     if (post_type_supports($post_type, 'thumbnail') && function_exists('has_post_thumbnail') && has_post_thumbnail()) {
         list($post_thumbnail_url, $post_thumbnail_width, $post_thumbnail_height) = wp_get_attachment_image_src(get_post_thumbnail_id($post_id), 'full');
         if (!empty($post_thumbnail_url)) {
             $story['picture'] = $post_thumbnail_url;
         }
     }
     if (post_type_supports($post_type, 'title')) {
         $title = trim(html_entity_decode(get_the_title($post_id), ENT_COMPAT, 'UTF-8'));
         if ($title) {
             $story['name'] = $title;
         }
         unset($title);
     }
     if (!class_exists('Facebook_Open_Graph_Protocol')) {
         require_once dirname(dirname(dirname(__FILE__))) . '/open-graph-protocol.php';
     }
     if (post_type_supports($post_type, 'excerpt') && !empty($post->post_excerpt)) {
         $excerpt = trim(apply_filters('get_the_excerpt', $post->post_excerpt));
         if ($excerpt) {
             $excerpt = Facebook_Open_Graph_Protocol::clean_description($excerpt);
             if ($excerpt) {
                 $story['caption'] = $excerpt;
             }
         }
         unset($excerpt);
     }
     $post_content = Facebook_Open_Graph_Protocol::clean_description($post->post_content, false);
     if ($post_content) {
         $story['description'] = $post_content;
     }
     unset($post_content);
     // define a photo URL template once, with SSL goodness. sprintf later inside the loop
     $photo_params = array('width' => 15, 'height' => 15);
     if (is_ssl()) {
         $photo_params['return_ssl_resources'] = 1;
     }
     $photo_url = 'http' . (is_ssl() ? 's' : '') . '://graph.facebook.com/%s/picture?' . http_build_query($photo_params);
     unset($photo_params);
     $pages_posts = array();
     $publish_ids_pages = array();
     foreach ($mentioned_pages as $page) {
         try {
             $publish_result = $facebook->api('/' . $page['id'] . '/feed', 'POST', $story);
             $published_id = sanitize_text_field($publish_result['id']);
             $publish_ids_pages[] = $published_id;
             $pages_posts[] = '<a href="' . esc_url(self::get_permalink_from_feed_publish_id($published_id), array('http', 'https')) . '" target="_blank"><img src="' . esc_url(sprintf($photo_url, $page['id']), array('http', 'https')) . '" alt="' . (isset($page['name']) ? esc_attr($page['name']) : '') . '" width="15" height="15" /></a>';
         } catch (WP_FacebookApiException $e) {
             $error_result = $e->getResult();
             if ($e->getCode() == 210) {
                 $status_messages[] = array('message' => esc_html(__('Failed posting to mentioned page\'s Facebook Timeline.', 'facebook')) . ' <img src="' . esc_url(sprintf($photo_url, $page['id']), array('http', 'https')) . '" alt="' . (isset($page['name']) ? esc_attr($page['name']) : '') . '" width="15" height="15" /> ' . esc_html(__('Error: Page doesn\'t allow posts from other Facebook users. Full error:', 'facebook')) . ' ' . esc_html(json_encode($error_result['error'])), 'error' => true);
             } else {
                 $status_messages[] = array('message' => __('Failed posting to mentioned page\'s Facebook Timeline.', 'facebook') . ' <img src="' . esc_url(sprintf($photo_url, $page['id']), array('http', 'https')) . '" alt="' . (isset($page['name']) ? esc_attr($page['name']) : '') . '" width="15" height="15" /> ' . esc_html(__('Error', 'facebook')) . ': ' . esc_html(json_encode($error_result['error'])), 'error' => true);
             }
         }
     }
     if (!empty($publish_ids_pages)) {
         update_post_meta($post_id, 'fb_mentioned_pages_post_ids', $publish_ids_pages);
     }
     if (!empty($publish_ids_pages)) {
         $status_messages[] = array('message' => esc_html(__('Posted to mentioned pages\' Facebook Timelines.', 'facebook')) . ' ' . implode(' ', $pages_posts), 'error' => false);
     }
     return $status_messages;
 }