Skip to content
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

Merged
merged 4 commits into from
Mar 28, 2017
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions functions.opengraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Copy link
Member

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?

if ( ! is_int( $cached_image_id ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would the returned value from get_transient() be an int type, or a string type? I know is_numeric() would include integers stored as strings, but unless we're casting it, I'm not sure if is_int() would.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

attachment_url_to_postid() casts its result as an integer so get_transient() should return an integer as well. If it doesn't, it means the value was modified and we probably shouldn't use it.

Since we use that transient in wp_get_attachment_image_src() later, and since wp_get_attachment_image_src() expects an integer, I think it's best to check for is_int() before to use the value.

Or do you think I should use is_numeric() and allow for third-parties to overwrite the transients saved by Jetpack and save strings instead?

Copy link
Member

Choose a reason for hiding this comment

The 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 );
Copy link
Member

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?

Copy link
Member Author

@jeherve jeherve Mar 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. The conditional wasn't necessary since $image_url will always be set. I removed it.

} else {
$image_id = $cached_image_id;
}

$image_size = wp_get_attachment_image_src( $image_id, $width >= 512
? 'full'
: array( $width, $width ) );
Expand Down Expand Up @@ -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' );
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we check for has_site_icon() before to start the whole process, I don't think that's necessary. For has_site_icon() to return true, get_option( 'site_icon' ) has to return an ID.

$image_size = wp_get_attachment_image_src( $image_id, $max_side >= 512
? 'full'
: array( $max_side, $max_side ) );
Expand Down Expand Up @@ -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) {
Expand Down