Skip to content

Commit 13b127c

Browse files
committed
fix: memoization for get_sponsors_for_post
1 parent b073bbb commit 13b127c

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

includes/theme-helpers.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ function get_sponsors_for_post( $post_id = null, $scope = null, $logo_options =
118118
return false;
119119
}
120120

121+
// Memoization.
122+
static $cache = [];
123+
$hash = md5( (string) $post_id . (string) $scope . wp_json_encode( $logo_options ) );
124+
if ( isset( $cache[ $hash ] ) ) {
125+
return $cache[ $hash ];
126+
}
127+
121128
$sponsors = [];
122129
$direct_sponsors = get_the_terms( $post_id, Core::NEWSPACK_SPONSORS_TAX );
123130
$categories = get_the_category( $post_id );
@@ -185,9 +192,11 @@ function get_sponsors_for_post( $post_id = null, $scope = null, $logo_options =
185192
}
186193

187194
if ( 0 === count( $sponsors ) ) {
188-
return false;
195+
$sponsors = false;
189196
}
190197

198+
$cache[ $hash ] = $sponsors;
199+
191200
return $sponsors;
192201
}
193202

0 commit comments

Comments
 (0)