-
Notifications
You must be signed in to change notification settings - Fork 815
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open Graph: make sure transients are used to save image IDs. #6632
Changes from 2 commits
4084c0b
a30c365
b564cee
4c25dd3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -293,17 +293,30 @@ function jetpack_og_get_image( $width = 200, $height = 200, $max_images = 4 ) { | |
if ( empty( $image ) && function_exists( 'blavatar_domain' ) ) { | ||
$blavatar_domain = blavatar_domain( site_url() ); | ||
if ( blavatar_exists( $blavatar_domain ) ) { | ||
$image_url = blavatar_url( $blavatar_domain, 'img', $width, false, true ); | ||
|
||
$img_width = ''; | ||
$img_height = ''; | ||
$cached_image_id = get_transient( 'jp_' . $image_url ); | ||
if ( false !== $cached_image_id ) { | ||
$image_id = $cached_image_id; | ||
} else { | ||
|
||
$image_url = blavatar_url( $blavatar_domain, 'img', $width, false, true ); | ||
|
||
// Build a hash of the Image URL. We'll use it later when building the transient. | ||
if ( $image_url ) { | ||
/** | ||
* Transient names are 45 chars max. | ||
* Let's generate a hash that's 41 chars max: | ||
* We will add 'jp_' in front of it later, so 45 chars minus those 3, and a one-off char just in in case. | ||
*/ | ||
$image_hash = substr( md5( $image_url ), 0, 41 ); | ||
} | ||
|
||
// Look for data in our transient. If nothing, let's get an attachment ID. | ||
$cached_image_id = get_transient( 'jp_' . $image_hash ); | ||
if ( ! is_int( $cached_image_id ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would the returned value from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Since we use that transient in Or do you think I should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Honestly, I'm unsure. If it works now, good enough, we can reevaluate later or third-parties can cast to int before saving. I just wasn't sure if the number would get cast to a string when saved in the db. Just being cautious. |
||
$image_id = attachment_url_to_postid( $image_url ); | ||
set_transient( 'jp_' . $image_url, $image_id ); | ||
set_transient( 'jp_' . $image_hash, $image_id ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call. The conditional wasn't necessary since |
||
} else { | ||
$image_id = $cached_image_id; | ||
} | ||
|
||
$image_size = wp_get_attachment_image_src( $image_id, $width >= 512 | ||
? 'full' | ||
: array( $width, $width ) ); | ||
|
@@ -336,20 +349,12 @@ function jetpack_og_get_image( $width = 200, $height = 200, $max_images = 4 ) { | |
|
||
// Third fall back, Core Site Icon, if valid in size. Added in WP 4.3. | ||
if ( empty( $image ) && ( function_exists( 'has_site_icon') && has_site_icon() ) ) { | ||
$max_side = max( $width, $height ); | ||
$image_url = get_site_icon_url( $max_side ); | ||
|
||
$img_width = ''; | ||
$img_height = ''; | ||
$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 ); | ||
} | ||
|
||
$max_side = max( $width, $height ); | ||
$image_url = get_site_icon_url( $max_side ); | ||
$image_id = get_option( 'site_icon' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add a break or return or something if this returns nothing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we check for |
||
$image_size = wp_get_attachment_image_src( $image_id, $max_side >= 512 | ||
? 'full' | ||
: array( $max_side, $max_side ) ); | ||
|
@@ -387,7 +392,7 @@ function jetpack_og_get_image( $width = 200, $height = 200, $max_images = 4 ) { | |
* @param $width int Width of the image | ||
* @param $height int Height of the image | ||
* @param $req_width int Required width to pass validation | ||
* @param $req_height int Required height to pass validation | ||
* @param $req_height int Required height to pass validation | ||
* @return bool - True if the image passed the required size validation | ||
*/ | ||
function _jetpack_og_get_image_validate_size($width, $height, $req_width, $req_height) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're using
$image_hash
here, but it's only set conditionally above. So could there be a situation where$image_url
evaluates to falsey, so$image_hash
never gets declared?