diff options
Diffstat (limited to 'plugins/jetpack/functions.opengraph.php')
-rw-r--r-- | plugins/jetpack/functions.opengraph.php | 106 |
1 files changed, 35 insertions, 71 deletions
diff --git a/plugins/jetpack/functions.opengraph.php b/plugins/jetpack/functions.opengraph.php index 4f608ff8..6d830d9f 100644 --- a/plugins/jetpack/functions.opengraph.php +++ b/plugins/jetpack/functions.opengraph.php @@ -261,22 +261,18 @@ function jetpack_og_tags() { } function jetpack_og_get_image( $width = 200, $height = 200, $max_images = 4 ) { // Facebook requires thumbnails to be a minimum of 200x200 - $image = ''; + $image = array(); if ( is_singular() && ! is_home() ) { - global $post; - $image = ''; - - // Grab obvious image if $post is an attachment page for an image - if ( is_attachment( $post->ID ) && 'image' == substr( $post->post_mime_type, 0, 5 ) ) { - $image = wp_get_attachment_url( $post->ID ); + // Grab obvious image if post is an attachment page for an image + if ( is_attachment( get_the_ID() ) && 'image' == substr( get_post_mime_type(), 0, 5 ) ) { + $image['src'] = wp_get_attachment_url( get_the_ID() ); } // Attempt to find something good for this post using our generalized PostImages code - if ( ! $image && class_exists( 'Jetpack_PostImages' ) ) { - $post_images = Jetpack_PostImages::get_images( $post->ID, array( 'width' => $width, 'height' => $height ) ); + if ( empty( $image ) && class_exists( 'Jetpack_PostImages' ) ) { + $post_images = Jetpack_PostImages::get_images( get_the_ID(), array( 'width' => $width, 'height' => $height ) ); if ( $post_images && ! is_wp_error( $post_images ) ) { - $image = array(); foreach ( (array) $post_images as $post_image ) { $image['src'] = $post_image['src']; if ( isset( $post_image['src_width'], $post_image['src_height'] ) ) { @@ -286,35 +282,11 @@ function jetpack_og_get_image( $width = 200, $height = 200, $max_images = 4 ) { } } } - } else if ( is_author() ) { + } elseif ( is_author() ) { $author = get_queried_object(); - if ( function_exists( 'get_avatar_url' ) ) { - // Prefer the core function get_avatar_url() if available, WP 4.2+ - $image['src'] = get_avatar_url( $author->user_email, array( 'size' => $width ) ); - } - else { - $has_filter = has_filter( 'pre_option_show_avatars', '__return_true' ); - if ( ! $has_filter ) { - add_filter( 'pre_option_show_avatars', '__return_true' ); - } - $avatar = get_avatar( $author->user_email, $width ); - if ( ! $has_filter ) { - remove_filter( 'pre_option_show_avatars', '__return_true' ); - } - - if ( ! empty( $avatar ) && ! is_wp_error( $avatar ) ) { - if ( preg_match( '/src=["\']([^"\']+)["\']/', $avatar, $matches ) ); - $image['src'] = wp_specialchars_decode( $matches[1], ENT_QUOTES ); - } - } - } - - if ( empty( $image ) ) { - $image = array(); - } else if ( ! is_array( $image ) ) { - $image = array( - 'src' => $image - ); + $image['src'] = get_avatar_url( $author->user_email, array( + 'size' => $width, + ) ); } // First fall back, blavatar @@ -325,7 +297,13 @@ function jetpack_og_get_image( $width = 200, $height = 200, $max_images = 4 ) { $img_width = ''; $img_height = ''; - $image_id = attachment_url_to_postid( $image_url ); + $cached_image_id = get_transient( 'jp_' . $image_url ); + if ( false !== $cached_image_id ) { + $image_id = $cached_image_id; + } else { + $image_id = attachment_url_to_postid( $image_url ); + set_transient( 'jp_' . $image_url, $image_id ); + } $image_size = wp_get_attachment_image_src( $image_id, $width >= 512 ? 'full' : array( $width, $width ) ); @@ -363,7 +341,15 @@ function jetpack_og_get_image( $width = 200, $height = 200, $max_images = 4 ) { $img_width = ''; $img_height = ''; - $image_id = attachment_url_to_postid( $image_url ); + $cached_image_id = get_transient( 'jp_' . $image_url ); + + if ( false !== $cached_image_id ) { + $image_id = $cached_image_id; + } else { + $image_id = attachment_url_to_postid( $image_url ); + set_transient( 'jp_' . $image_url, $image_id ); + } + $image_size = wp_get_attachment_image_src( $image_id, $max_side >= 512 ? 'full' : array( $max_side, $max_side ) ); @@ -416,36 +402,14 @@ function _jetpack_og_get_image_validate_size($width, $height, $req_width, $req_h } /** -* @param $email -* @param $width -* @return array|bool|mixed|string -*/ + * Gets a gravatar URL of the specified size. + * + * @param string $email E-mail address to get gravatar for. + * @param int $width Size of returned gravatar. + * @return array|bool|mixed|string + */ function jetpack_og_get_image_gravatar( $email, $width ) { - $image = ''; - if ( function_exists( 'get_avatar_url' ) ) { - $avatar = get_avatar_url( $email, $width ); - if ( ! empty( $avatar ) ) { - if ( is_array( $avatar ) ) - $image = $avatar[0]; - else - $image = $avatar; - } - } else { - $has_filter = has_filter( 'pre_option_show_avatars', '__return_true' ); - if ( !$has_filter ) { - add_filter( 'pre_option_show_avatars', '__return_true' ); - } - $avatar = get_avatar( $email, $width ); - - if ( !$has_filter ) { - remove_filter( 'pre_option_show_avatars', '__return_true' ); - } - - if ( !empty( $avatar ) && !is_wp_error( $avatar ) ) { - if ( preg_match( '/src=["\']([^"\']+)["\']/', $avatar, $matches ) ) - $image = wp_specialchars_decode($matches[1], ENT_QUOTES); - } - } - - return $image; + return get_avatar_url( $email, array( + 'size' => $width, + ) ); } |