public_html/wp-content/plugins/simple-facebook-og-image/simple-facebook-ogimage.php
/**
	 * The main plugin logic. Determines the image based on these criterias:
	 * - Featured image
	 * - Images in post content
	 * - Default image
	 *
	 * @return String Image to be used as Open Graph image. 
	 *		   Null if no image is suitable.	
	 */
The main plugin logic. Determines the image based on these criterias:
- Featured image
- Images in post content
- Default image

@return String Image to be used as Open Graph image.
Null if no image is suitable.
The main plugin logic . Determines the image based on these criterias : - Featured image - Images in post content - Default image @return String Image to be used as Open Graph image . Null if no image is suitable .
function sfogi_get() {
		$og_image 	= array();
		$post_id 	= get_the_ID();
		$cache_key	= md5( 'sfogi_' . $post_id );
		$cache_group= 'sfogi';
		$cached_image = wp_cache_get($cache_key, $cache_group);
		if($cached_image !== false) {
			$og_image[] = $cached_image;
		}
		if( empty( $og_image ) ) {
			$image 		= wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), 'single-post-thumbnail' );
		 	if($image !== false) {
				$og_image[] = $image[0];
			}
		} 
		if( empty( $og_image ) ) {
			$post = get_post( $post_id );
			preg_match_all('/<img(.*?)src="(?P<src>.*?)"([^>]+)>/', $post->post_content, $matches);
			if(isset($matches['src'][0])) {
				foreach($matches['src'] as $match) {
					$og_image[] = $match;
				}
			}
		}
		if( empty( $og_image ) ) {
			$option = get_option('sfogi_default_image');
			if(!empty($option)) {
				$og_image[] = $option;
			}
		}
		if( empty( $og_image ) ) {
			if(!$post) {
				$post = get_post( $post_id );
			}
			$getty_domain = "embed.gettyimages.com/embed/";
			preg_match_all('#<(.*?)src="//'.$getty_domain.'(?P<src>.*?)"([^>]+)>#', $post->post_content, $matches);
			if(isset($matches['src'][0])) {
				foreach($matches['src'] as $match) {
					$frame_content = wp_remote_get("https://".$getty_domain.$match);
					if( is_array($frame_content) && isset($frame_content['body']) ) {
						$frame_content = $frame_content['body'];
						preg_match('#<meta property="og:image" content="(?P<url>.*)" />#i', $frame_content, $frame_matches);
						if(isset($frame_matches['url']))
							$og_image[] = $frame_matches['url'];
					}
				}
			}
		}
		if( empty($og_image) && is_plugin_active('embedly/embedly.php') ) {
			global $WP_Embedly;
			if($WP_Embedly->valid_key()) {
				if(!isset($post)) {
					$post = get_post($post_id);
				}
				$post_content = apply_filters('the_content', $post->post_content);
				preg_match('{<blockquote class="embedly-card"(.*?)data-card-key="(?P<key>.*?)"(.*?)>(.*?)<a href="(?P<href>.*?)">(.*?)</a>(.*?)</blockquote>}is', $post_content, $embedly_matches);
				if(!empty($embedly_matches['key']) && !empty($embedly_matches['href'])) {
					$scheme = strpos($embedly_matches['href'], 'https') === false ? 'http' : 'https';
					$embedly_remote_url = EMBEDLY_BASE_URI . 'card=1&key=' . $embedly_matches['key'] . '&native=true&scheme=' . $scheme . '&urls=' . rawurlencode($embedly_matches['href']) . '&v=2&youtube_showinfo=0';
					$args = array('timeout' => 5);
					$embedly_remote_response = wp_remote_get($embedly_remote_url, $args);
					if(!is_wp_error($embedly_remote_response)) {
						$embedly_json = json_decode($embedly_remote_response['body']);
						if(is_array($embedly_json) && isset($embedly_json[0]->thumbnail_url)) {
							$og_image[] = $embedly_json[0]->thumbnail_url;
						}
					} 
				}
			}
		}
		if( ! empty( $og_image ) ) {
			if($cached_image === false) {
				$result = wp_cache_set($cache_key, $og_image, $cache_group);
			}
		}
		return $og_image;
	}
function sfogi_get ( ) { $ og_image = array ( ) ; $ post_id = get_the_ID ( ) ; $ cache_key = md5 ( 'sfogi_' . $ post_id ) ; $ cache_group = 'sfogi' ; $ cached_image = wp_cache_get ( $ cache_key , $ cache_group ) ; if ( $ cached_image !== false ) { $ og_image [ ] = $ cached_image ; } if ( empty ( $ og_image ) ) { $ image = wp_get_attachment_image_src ( get_post_thumbnail_id ( $ post_id ) , 'single-post-thumbnail' ) ; if ( $ image !== false ) { $ og_image [ ] = $ image [ 0 ] ; } } if ( empty ( $ og_image ) ) { $ post = get_post ( $ post_id ) ; preg_match_all ( '/<img(.*?)src="(?P<src>.*?)"([^>]+)>/' , $ post -> post_content , $ matches ) ; if ( isset ( $ matches [ 'src' ] [ 0 ] ) ) { foreach ( $ matches [ 'src' ] as $ match ) { $ og_image [ ] = $ match ; } } } if ( empty ( $ og_image ) ) { $ option = get_option ( 'sfogi_default_image' ) ; if ( ! empty ( $ option ) ) { $ og_image [ ] = $ option ; } } if ( empty ( $ og_image ) ) { if ( ! $ post ) { $ post = get_post ( $ post_id ) ; } $ getty_domain = " embed.gettyimages.com/embed/ " ; preg_match_all ( '#<(.*?)src="//' . $ getty_domain . '(?P<src>.*?)"([^>]+)>#' , $ post -> post_content , $ matches ) ; if ( isset ( $ matches [ 'src' ] [ 0 ] ) ) { foreach ( $ matches [ 'src' ] as $ match ) { $ frame_content = wp_remote_get ( " https:// " . $ getty_domain . $ match ) ; if ( is_array ( $ frame_content ) && isset ( $ frame_content [ 'body' ] ) ) { $ frame_content = $ frame_content [ 'body' ] ; preg_match ( '#<meta property="og:image" content="(?P<url>.*)" />#i' , $ frame_content , $ frame_matches ) ; if ( isset ( $ frame_matches [ 'url' ] ) ) $ og_image [ ] = $ frame_matches [ 'url' ] ; } } } } if ( empty ( $ og_image ) && is_plugin_active ( 'embedly/embedly.php' ) ) { global $ WP_Embedly ; if ( $ WP_Embedly -> valid_key ( ) ) { if ( ! isset ( $ post ) ) { $ post = get_post ( $ post_id ) ; } $ post_content = apply_filters ( 'the_content' , $ post -> post_content ) ; preg_match ( '{<blockquote class="embedly-card"(.*?)data-card-key="(?P<key>.*?)"(.*?)>(.*?)<a href="(?P<href>.*?)">(.*?)</a>(.*?)</blockquote>}is' , $ post_content , $ embedly_matches ) ; if ( ! empty ( $ embedly_matches [ 'key' ] ) && ! empty ( $ embedly_matches [ 'href' ] ) ) { $ scheme = strpos ( $ embedly_matches [ 'href' ] , 'https' ) === false ? 'http' : 'https' ; $ embedly_remote_url = EMBEDLY_BASE_URI . 'card=1&key=' . $ embedly_matches [ 'key' ] . '&native=true&scheme=' . $ scheme . '&urls=' . rawurlencode ( $ embedly_matches [ 'href' ] ) . '&v=2&youtube_showinfo=0' ; $ args = array ( 'timeout' => 5 ) ; $ embedly_remote_response = wp_remote_get ( $ embedly_remote_url , $ args ) ; if ( ! is_wp_error ( $ embedly_remote_response ) ) { $ embedly_json = json_decode ( $ embedly_remote_response [ 'body' ] ) ; if ( is_array ( $ embedly_json ) && isset ( $ embedly_json [ 0 ] -> thumbnail_url ) ) { $ og_image [ ] = $ embedly_json [ 0 ] -> thumbnail_url ; } } } } } if ( ! empty ( $ og_image ) ) { if ( $ cached_image === false ) { $ result = wp_cache_set ( $ cache_key , $ og_image , $ cache_group ) ; } } return $ og_image ; }
The main plugin logic.
The main plugin logic .
// No OG image? Get it from featured image // There is a featured image // No OG image still? Get it from post content // Get all images from within post content // No OG ... still? Well let see if there is something in the default section // Support for getty-images embed // Get all getty images frames from content // Get content of the iframe // Find og:image address inside the iframe // Support for Embedly // Force filters to apply the Embedly logic // Seach for some key Embedly components /**
						 * Use when queriy card-details, not only card
						 */ #if(isset($embedly_json[0]) && !empty($embedly_json[0]->images && isset($embedly_json[0]->images[0]->url))) { #	$og_image[] = $embedly_json[0]->images[0]->url; #} // Found an image? Good. Display it. // Cache the image source but only if the source is not retrieved from cache. No point of overwriting the same source.