From d66cb0f633ca1037d471cadad790f3dfde810e68 Mon Sep 17 00:00:00 2001 From: Jeremy Herve Date: Wed, 8 Feb 2017 21:59:58 +0100 Subject: [PATCH] Add image caching to jetpack_og_get_image() (#6297) * Add image caching to jetpack_og_get_image() This adds a transient to store the value of the $image_id to "speed up" the function to fix #6017 * Added missing semicolons at EOL on a couple lines * Adding some whitespace per coding standards --- functions.opengraph.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/functions.opengraph.php b/functions.opengraph.php index aaddc4767a230..5db74f45b6016 100644 --- a/functions.opengraph.php +++ b/functions.opengraph.php @@ -313,7 +313,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 ) ); @@ -351,7 +357,15 @@ function jetpack_og_get_image( $width = 200, $height = 200, $max_images = 4 ) { $img_width = ''; $img_height = ''; - $image_id = get_option( 'site_icon' ); + $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 ) );