Check if a Featured Image is set for this post, and return it in a similar
format to the other images?_from_*() methods.
static public from_thumbnail ( integer $post_id, $width = 200, $height = 200 ) : Array | ||
$post_id | integer | The post ID to check |
return | Array | containing details of the Featured Image, or empty array if none. |
function enhanced_og_has_featured_image($post_id) { $featured = Jetpack_PostImages::from_thumbnail($post_id, 200, 200); if (!empty($featured) && count($featured) > 0) { return true; } return false; }
static function twitter_cards_tags($og_tags) { global $post; if (post_password_required()) { return $og_tags; } if (apply_filters('jetpack_disable_twitter_cards', false)) { return $og_tags; } /* * These tags apply to any page (home, archives, etc) */ $site_tag = apply_filters('jetpack_twitter_cards_site_tag', self::site_tag()); $og_tags['twitter:site'] = self::sanitize_twitter_user($site_tag); if (!is_singular() || !empty($og_tags['twitter:card'])) { return $og_tags; } /* * The following tags only apply to single pages. */ $card_type = 'summary'; // Try to give priority to featured images if (class_exists('Jetpack_PostImages')) { $featured = Jetpack_PostImages::from_thumbnail($post->ID, 240, 240); if (!empty($featured) && count($featured) > 0) { if ((int) $featured[0]['src_width'] >= 280 && (int) $featured[0]['src_height'] >= 150) { $card_type = 'summary_large_image'; $og_tags['twitter:image:src'] = add_query_arg('w', 640, $featured[0]['src']); } else { $og_tags['twitter:image'] = add_query_arg('w', 240, $featured[0]['src']); } } } // Only proceed with media analysis if a featured image has not superseded it already. if (empty($og_tags['twitter:image']) && empty($og_tags['twitter:image:src'])) { if (!class_exists('Jetpack_Media_Summary') && defined('IS_WPCOM') && IS_WPCOM) { include WP_CONTENT_DIR . '/lib/class.wpcom-media-summary.php'; } // Test again, class should already be auto-loaded in Jetpack. // If not, skip extra media analysis and stick with a summary card if (class_exists('Jetpack_Media_Summary')) { $extract = Jetpack_Media_Summary::get($post->ID); if ('gallery' == $extract['type']) { list($og_tags, $card_type) = self::twitter_cards_define_type_based_on_image_count($og_tags, $extract); } else { if ('video' == $extract['type']) { // Leave as summary, but with large pict of poster frame (we know those comply to Twitter's size requirements) $card_type = 'summary_large_image'; $og_tags['twitter:image:src'] = add_query_arg('w', 640, $extract['image']); } else { list($og_tags, $card_type) = self::twitter_cards_define_type_based_on_image_count($og_tags, $extract); } } } } $og_tags['twitter:card'] = $card_type; // If we have information on the author/creator, then include that as well if (!empty($post) && !empty($post->post_author)) { $handle = apply_filters('jetpack_sharing_twitter_via', '', $post->ID); if (!empty($handle) && 'wordpressdotcom' != $handle && 'jetpack' != $handle) { $og_tags['twitter:creator'] = self::sanitize_twitter_user($handle); } } // Make sure we have a description for Twitter, their validator isn't happy without some content (single space not valid). if (!isset($og_tags['og:description']) || '' == trim($og_tags['og:description']) || __('Visit the post for more.', 'jetpack') == $og_tags['og:description']) { // empty( trim( $og_tags['og:description'] ) ) isn't valid php $has_creator = !empty($og_tags['twitter:creator']) && '@wordpressdotcom' != $og_tags['twitter:creator'] ? true : false; if ('photo' == $card_type) { $og_tags['twitter:description'] = $has_creator ? sprintf(__('Photo post by %s.', 'jetpack'), $og_tags['twitter:creator']) : __('Photo post.', 'jetpack'); } else { if (!empty($extract) && 'video' == $extract['type']) { // use $extract['type'] since $card_type is 'summary' for video posts $og_tags['twitter:description'] = $has_creator ? sprintf(__('Video post by %s.', 'jetpack'), $og_tags['twitter:creator']) : __('Video post.', 'jetpack'); } else { if ('gallery' == $card_type) { $og_tags['twitter:description'] = $has_creator ? sprintf(__('Gallery post by %s.', 'jetpack'), $og_tags['twitter:creator']) : __('Gallery post.', 'jetpack'); } else { $og_tags['twitter:description'] = $has_creator ? sprintf(__('Post by %s.', 'jetpack'), $og_tags['twitter:creator']) : __('Visit the post for more.', 'jetpack'); } } } } return $og_tags; }
/** * @param $post A post object * @param $args (array) Optional args, see defaults list for details * @returns array Returns an array of all images meeting the specified criteria in $args * * Uses Jetpack Post Images */ private static function get_image_fields($post, $args = array()) { $defaults = array('width' => 200, 'height' => 200); $args = wp_parse_args($args, $defaults); $image_list = array(); $image_booleans = array(); $image_booleans['gallery'] = 0; $from_featured_image = Jetpack_PostImages::from_thumbnail($post->ID, $args['width'], $args['height']); if (!empty($from_featured_image)) { $srcs = wp_list_pluck($from_featured_image, 'src'); $image_list = array_merge($image_list, $srcs); } $from_slideshow = Jetpack_PostImages::from_slideshow($post->ID, $args['width'], $args['height']); if (!empty($from_slideshow)) { $srcs = wp_list_pluck($from_slideshow, 'src'); $image_list = array_merge($image_list, $srcs); } $from_gallery = Jetpack_PostImages::from_gallery($post->ID); if (!empty($from_gallery)) { $srcs = wp_list_pluck($from_gallery, 'src'); $image_list = array_merge($image_list, $srcs); $image_booleans['gallery']++; // @todo This count isn't correct, will only every count 1 } // @todo Can we check width/height of these efficiently? Could maybe use query args at least, before we strip them out $image_list = Jetpack_Media_Meta_Extractor::get_images_from_html($post->post_content, $image_list); return Jetpack_Media_Meta_Extractor::build_image_struct($image_list); }