-
Notifications
You must be signed in to change notification settings - Fork 799
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
Add caching for VideoPress functions with meta queries #14803
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9608b4b
Add caching for VideoPress functions that call WP_Query
kienstra f91ef7c
Change the since tag to 8.4.0
kienstra 96a565a
Revert changes to videopress_get_attachment_id_by_url()
kienstra 67df908
Deprecate videopress_get_attachment_id_by_url()
kienstra 85b14a0
Store the videopress ID in a transient if no persistent object caching
kienstra 21dc2cd
Remove needless helper method
kienstra 073e096
Add a new function, using Leo's suggestion
kienstra 69447b8
Move the new function's test lower in the file
kienstra 331f265
Set the transient expiration to an hour
kienstra 5c05e33
Add a see tag, correct DocBlock summary
kienstra 64918b1
Change prefix of new function from video_ to videopress_
kienstra e630175
Add since tags to new functions
kienstra 1e163aa
Remove needless comment
kienstra c13c629
Rename function to keep consistency
leogermani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,13 +73,16 @@ function videopress_get_video_details( $guid ) { | |
* | ||
* Modified from https://wpscholar.com/blog/get-attachment-id-from-wp-image-url/ | ||
* | ||
* @todo: Add some caching in here. | ||
* @deprecated since 8.4.0 | ||
leogermani marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @see videopress_get_post_id_by_guid() | ||
* | ||
* @param string $url | ||
* | ||
* @return int|bool Attachment ID on success, false on failure | ||
*/ | ||
function videopress_get_attachment_id_by_url( $url ) { | ||
_deprecated_function( __FUNCTION__, 'jetpack-8.4' ); | ||
|
||
$wp_upload_dir = wp_upload_dir(); | ||
// Strip out protocols, so it doesn't fail because searching for http: in https: dir. | ||
$dir = set_url_scheme( trailingslashit( $wp_upload_dir['baseurl'] ), 'relative' ); | ||
|
@@ -619,7 +622,7 @@ function video_format_done( $info, $format ) { | |
*/ | ||
function video_image_url_by_guid( $guid, $format ) { | ||
|
||
$post = video_get_post_by_guid( $guid ); | ||
$post = videopress_get_post_by_guid( $guid ); | ||
|
||
if ( is_wp_error( $post ) ) { | ||
return null; | ||
|
@@ -636,14 +639,67 @@ function video_image_url_by_guid( $guid, $format ) { | |
/** | ||
* Using a GUID, find a post. | ||
* | ||
* @param string $guid | ||
* @return WP_Post | ||
* @param string $guid The post guid. | ||
* @return WP_Post|false The post for that guid, or false if none is found. | ||
*/ | ||
function videopress_get_post_by_guid( $guid ) { | ||
$cache_key = 'get_post_by_guid_' . $guid; | ||
$cache_group = 'videopress'; | ||
$cached_post = wp_cache_get( $cache_key, $cache_group ); | ||
|
||
if ( is_object( $cached_post ) && 'WP_Post' === get_class( $cached_post ) ) { | ||
return $cached_post; | ||
} | ||
|
||
$post_id = videopress_get_post_id_by_guid( $guid ); | ||
|
||
if ( is_int( $post_id ) ) { | ||
$post = get_post( $post_id ); | ||
wp_cache_set( $cache_key, $post, $cache_group, HOUR_IN_SECONDS ); | ||
|
||
return $post; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Using a GUID, find a post. | ||
* | ||
* Kept for backward compatibility. Use videopress_get_post_by_guid() instead. | ||
* | ||
* @deprecated since 8.4.0 | ||
* @see videopress_get_post_by_guid() | ||
* | ||
* @param string $guid The post guid. | ||
* @return WP_Post|false The post for that guid, or false if none is found. | ||
*/ | ||
function video_get_post_by_guid( $guid ) { | ||
_deprecated_function( __FUNCTION__, 'jetpack-8.4' ); | ||
return videopress_get_post_by_guid( $guid ); | ||
} | ||
|
||
/** | ||
* Using a GUID, find the associated post ID. | ||
* | ||
* @since 8.4.0 | ||
* @param string $guid The guid to look for the post ID of. | ||
* @return int|false The post ID for that guid, or false if none is found. | ||
*/ | ||
function videopress_get_post_id_by_guid( $guid ) { | ||
$cache_key = 'videopress_get_post_id_by_guid_' . $guid; | ||
$cached_id = get_transient( $cache_key ); | ||
|
||
if ( is_int( $cached_id ) ) { | ||
return $cached_id; | ||
} | ||
|
||
$args = array( | ||
'post_type' => 'attachment', | ||
'post_mime_type' => 'video/videopress', | ||
'post_status' => 'inherit', | ||
'no_found_rows' => true, | ||
'fields' => 'ids', | ||
'meta_query' => array( | ||
leogermani marked this conversation as resolved.
Show resolved
Hide resolved
|
||
array( | ||
'key' => 'videopress_guid', | ||
|
@@ -655,9 +711,14 @@ function video_get_post_by_guid( $guid ) { | |
|
||
$query = new WP_Query( $args ); | ||
|
||
$post = $query->next_post(); | ||
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. There can be an error here if the query didn't find any posts. |
||
if ( $query->have_posts() ) { | ||
$post_id = $query->next_post(); | ||
set_transient( $cache_key, $post_id, HOUR_IN_SECONDS ); | ||
|
||
return $post; | ||
return $post_id; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
With
unset( $video );
above, it looks likeisset( $video->expires )
will never be trueThere 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.
Nice catch.