function sfc_media_handler($og, $post) { // only add this sort of meta to single post pages if (!is_singular()) { return $og; } $options = get_option('sfc_options'); // first we apply the filters to the content, just in case they're using shortcodes or oembed to display stuff $content = apply_filters('the_content', $post->post_content); // video handling $vids = sfc_media_find_video($post, $content); $og = array_merge($og, $vids); // image handling $images = sfc_media_find_images($post, $content); if (!empty($images)) { foreach ($images as $image) { $og['og:image'][] = $image; } } else { if (!empty($options['default_image'])) { $og['og:image'][] = $options['default_image']; } } // audio handling $auds = sfc_media_find_audio($post, $content); $og = array_merge($og, $auds); if (isset($og['og:audio'])) { // audio files on posts sometimes get misidentified as video, clear those out unset($og['og:video']); unset($og['og:video:height']); unset($og['og:video:width']); unset($og['og:video:type']); } return $og; }
function sfc_publish_automatic($id, $post) { // check to make sure post is published if ($post->post_status !== 'publish') { return; } // check options to see if we need to send to FB at all $options = get_option('sfc_options'); if (!$options['autopublish_app'] && !$options['autopublish_profile']) { return; } // build the post to send to FB // look for the images/video to add with image_src $images = sfc_media_find_images($post); $video = sfc_media_find_video($post); if (!empty($video['og:image'])) { array_unshift($images, $video['og:image'][0]); } // build the attachment $permalink = apply_filters('sfc_publish_permalink', wp_get_shortlink($post->ID), $post->ID); $title = get_the_title($post->ID); $title = strip_tags($title); $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8'); $title = htmlspecialchars_decode($title); $attachment['name'] = $title; $attachment['message'] = $title; $attachment['link'] = $permalink; $attachment['scrape'] = 'true'; $attachment['description'] = sfc_base_make_excerpt($post); $attachment['caption'] = ' '; if (!empty($images)) { $attachment['picture'] = $images[0]; } if (!empty($video)) { $attachment['source'] = $video['og:video']; } // Actions $actions[0]['name'] = 'Share'; $actions[0]['link'] = 'http://www.facebook.com/share.php?u=' . urlencode($permalink); $attachment['actions'] = json_encode($actions); $attachment = apply_filters('sfc_publish_automatic', $attachment, $post); // publish to page if (!empty($options['fanpage']) && $options['autopublish_app'] && !get_post_meta($id, '_fb_post_id_app', true)) { $url = "https://graph.facebook.com/{$options['fanpage']}/" . SFC_PUBLISH_ENDPOINT; $attachment['access_token'] = $options['page_access_token']; $data = wp_remote_post($url, array('body' => $attachment)); if (!is_wp_error($data)) { $resp = json_decode($data['body'], true); if ($resp['id']) { update_post_meta($id, '_fb_post_id_app', $resp['id']); } } } // publish to profile if ($options['autopublish_profile'] && !get_post_meta($id, '_fb_post_id_profile', true)) { $url = "https://graph.facebook.com/{$options['user']}/" . SFC_PUBLISH_ENDPOINT; // check the cookie for an access token. If not found, try to use the stored one. $cookie = sfc_cookie_parse(); if ($cookie['access_token']) { $attachment['access_token'] = $cookie['access_token']; } else { $attachment['access_token'] = $options['access_token']; } $data = wp_remote_post($url, array('body' => $attachment)); if (!is_wp_error($data)) { $resp = json_decode($data['body'], true); if ($resp['id']) { update_post_meta($id, '_fb_post_id_profile', $resp['id']); } } } }