Skip to content

Commit

Permalink
Change prefix of new function from video_ to videopress_
Browse files Browse the repository at this point in the history
Also change the transient cache key.
Also, simply return the post, instead of
accessing the ->ID property.
  • Loading branch information
kienstra committed Mar 19, 2020
1 parent 5c05e33 commit 64918b1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
15 changes: 8 additions & 7 deletions modules/videopress/utility-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function videopress_get_video_details( $guid ) {
* Modified from https://wpscholar.com/blog/get-attachment-id-from-wp-image-url/
*
* @deprecated since 8.4.0
* @see video_get_post_id_by_guid()
* @see videopress_get_post_id_by_guid()
*
* @param string $url
*
Expand Down Expand Up @@ -651,7 +651,7 @@ function video_get_post_by_guid( $guid ) {
return $cached_post;
}

$post_id = video_get_post_id_by_guid( $guid );
$post_id = videopress_get_post_id_by_guid( $guid );

if ( is_int( $post_id ) ) {
$post = get_post( $post_id );
Expand All @@ -669,8 +669,8 @@ function video_get_post_by_guid( $guid ) {
* @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 video_get_post_id_by_guid( $guid ) {
$cache_key = 'video_get_post_id_by_guid_' . $guid;
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 ) ) {
Expand All @@ -682,6 +682,7 @@ function video_get_post_id_by_guid( $guid ) {
'post_mime_type' => 'video/videopress',
'post_status' => 'inherit',
'no_found_rows' => true,
'fields' => 'ids',
'meta_query' => array(
array(
'key' => 'videopress_guid',
Expand All @@ -694,11 +695,11 @@ function video_get_post_id_by_guid( $guid ) {
$query = new WP_Query( $args );

if ( $query->have_posts() ) {
$post = $query->next_post();
$post_id = $query->next_post();
// Only store the ID, to prevent filling the database.
set_transient( $cache_key, $post->ID, HOUR_IN_SECONDS );
set_transient( $cache_key, $post_id, HOUR_IN_SECONDS );

return $post->ID;
return $post_id;
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function get_data_test_video_non_cached() {
'external_object_cache_is_not_enabled' => array(
'get_transient',
false,
'video_get_post_id_by_guid_',
'videopress_get_post_id_by_guid_',
),
);
}
Expand Down Expand Up @@ -170,20 +170,20 @@ public function test_cached_invalid_video_get_post_by_guid( $invalid_cached_valu
/**
* Tests a helper function to get the post id by guid.
*
* @covers ::video_get_post_id_by_guid
* @covers ::videopress_get_post_id_by_guid
* @since 8.4.0
*/
public function test_non_cached_video_get_post_id_by_guid() {
public function test_non_cached_videopress_get_post_id_by_guid() {
$guid = wp_generate_uuid4();
$expected_id = videopress_create_new_media_item( 'Example', $guid );
$actual_post_id = video_get_post_id_by_guid( $guid );
$actual_post_id = videopress_get_post_id_by_guid( $guid );

$this->assertEquals( $expected_id, $actual_post_id );

// The function should have cached the value.
$this->assertEquals(
$expected_id,
get_transient( 'video_get_post_id_by_guid_' . $guid )
get_transient( 'videopress_get_post_id_by_guid_' . $guid )
);
}

Expand Down

0 comments on commit 64918b1

Please sign in to comment.