Created
January 15, 2014 16:48
-
-
Save stiucsib86/8439847 to your computer and use it in GitHub Desktop.
Facebook Wordpress plugin temporary fix. This fixes issue where https://www.facebook.com/sharer/sharer.php throws "The message could not be posted to this Wall." when there are multiple "og:image" meta tag.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Identify image attachments related to the post. | |
* | |
* Request the full size of the attachment, not necessarily the same as the size used in the post. | |
* | |
* @since 1.5 | |
* | |
* @param stdClass|WP_Post $post WordPress post of interest | |
* @return array { | |
* Open Graph protocol image structured data | |
* | |
* @type string URL of the image | |
* @type array associative array of image data | |
* } | |
*/ | |
public static function get_og_images( $post ) { | |
$og_images = array(); | |
if ( ! $post || ! isset( $post->ID ) ) | |
return $og_images; | |
// does current post type and the current theme support post thumbnails? | |
if ( post_type_supports( get_post_type( $post ), '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' ); | |
$image = self::attachment_to_og_image( get_post_thumbnail_id( $post->ID ) ); | |
if ( isset( $image['url'] ) && ! isset( $og_images[ $image['url'] ] ) ) | |
$og_images[ $image['url'] ] = $image; | |
unset( $image ); | |
} | |
// get image attachments | |
if ( function_exists( 'get_attached_media' ) ) { | |
$images = get_attached_media( 'image', $post->ID ); | |
if ( ! empty( $images ) ) { | |
if (count( $og_images ) === 1) { | |
return $og_images; // Force only 1 image. | |
} | |
foreach( $images as $i ) { | |
if ( ! isset( $i->ID ) ) | |
continue; | |
$image = self::attachment_to_og_image( $i->ID ); | |
if ( ! isset( $image['url'] ) || isset( $og_images[ $image['url'] ] ) ) | |
continue; | |
$og_images[ $image['url'] ] = $image; | |
if ( count( $og_images ) === self::MAX_IMAGE_COUNT ) | |
return $og_images; | |
} | |
} | |
unset( $images ); | |
} | |
// test for WP_Embed handled images | |
if ( ! empty( $post->post_content ) ) { | |
preg_match_all( '#\s*http://instagr(\.am|am\.com)/p/(.*)/\s*#i', $post->post_content, $matches ); | |
if ( isset( $matches[2] ) ) { | |
foreach( $matches[2] as $instagram_id ) { | |
$instagram_url = esc_url_raw( 'http://instagram.com/p/' . $instagram_id . '/media/?size=l', array( 'http' ) ); | |
if ( ! $instagram_url || isset( $og_images[$instagram_url] ) ) | |
continue; | |
$og_images[$instagram_url] = array( 'url' => $instagram_url ); | |
unset( $instagram_url ); | |
} | |
} | |
unset( $matches ); | |
} | |
// add gallery content | |
if ( function_exists( 'get_post_galleries' ) ) { | |
// use get_post_galleries function from WP 3.6+ | |
$galleries = get_post_galleries( $post, false ); | |
foreach( $galleries as $gallery ) { | |
// use ids to request full-size image URI | |
if ( ! ( isset( $gallery['ids'] ) && $gallery['ids'] ) ) | |
continue; | |
$gallery['ids'] = explode( ',', $gallery['ids'] ); | |
foreach( $gallery['ids'] as $attachment_id ) { | |
$image = self::attachment_to_og_image( $attachment_id ); | |
if ( ! isset( $image['url'] ) || isset( $og_images[ $image['url'] ] ) ) | |
continue; | |
$og_images[ $image['url'] ] = $image; | |
if ( count( $og_images ) === self::MAX_IMAGE_COUNT ) | |
return $og_images; | |
unset( $image ); | |
} | |
} | |
unset( $galleries ); | |
} else { | |
// extract full-size images from gallery shortcode | |
$og_images = self::gallery_images( $post, $og_images ); | |
} | |
return $og_images; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment