Add static cache to Splide breakpoints functions

This commit is contained in:
Tetrakern 2024-10-07 13:16:46 +02:00
parent c88e122d99
commit d36465ace5
2 changed files with 17 additions and 5 deletions

View File

@ -1721,7 +1721,7 @@ Filters the intermediate output array of the `fictioneer_filter_media_buttons( $
Filters the associative array of Splide breakpoints returned by the `fictioneer_extract_splide_breakpoints()` function. These breakpoints are used to generate dynamic placeholder styles for a specific slider. Modifying the breakpoints at this stage is generally inadvisable, the filter exists primarily for completeness and edge cases.
**Parameters:**
* $breakpoints (array) Breakpoint data or empty if Splide JSON is invalid.
* $breakpoints (array) Breakpoint data or empty.
* $json_string (string) Stringified Splide JSON.
* $uid (string|null) Optional. Unique ID of the target element (only for reference).
@ -1731,9 +1731,9 @@ Filters the associative array of Splide breakpoints returned by the `fictioneer_
Filters the dynamically generated placeholder style for a specific Splide slider before it is minified, wrapped in a `<style>` tag, and returned by the `fictioneer_get_splide_breakpoint_style()` function. The style is rendered within the `<body>` and is removed once the slider is initialized.
**Parameters:**
* $style (string) The placeholder style.
* $style (string) The placeholder style or empty.
* $uid (string) Unique ID of the target element (used as CSS class).
* $breakpoints (array) Breakpoint data or empty if Splide JSON is invalid.
* $breakpoints (array) Breakpoint data or empty.
* $json_string (string) Stringified Splide JSON.
**Example:**

View File

@ -3616,6 +3616,15 @@ function fictioneer_get_post_type_label( $type ) {
*/
function fictioneer_extract_splide_breakpoints( $json_string, $uid = null ) {
// Static variable cache
static $cache = [];
$cache_key = wp_hash( $json_string );
if ( isset( $cache[ $cache_key] ) ) {
return apply_filters( 'fictioneer_filter_splide_breakpoints', $cache[ $cache_key ], $json_string, $uid );
}
// Valid JSON?
if ( ! fictioneer_is_valid_json( $json_string ) ) {
return apply_filters( 'fictioneer_filter_splide_breakpoints', [], $json_string, $uid );
@ -3672,7 +3681,10 @@ function fictioneer_extract_splide_breakpoints( $json_string, $uid = null ) {
}
}
// Apply filters and return result
// Apply filters and cache
$cache[ $cache_key ] = $breakpoints;
// Return
return apply_filters( 'fictioneer_filter_splide_breakpoints', $breakpoints, $json_string, $uid );
}
@ -3692,7 +3704,7 @@ function fictioneer_get_splide_breakpoint_style( $json_string, $uid ) {
$breakpoints = fictioneer_extract_splide_breakpoints( $json_string, $uid );
if ( empty( $breakpoints ) ) {
return '';
apply_filters( 'fictioneer_filter_splide_placeholder_style', '', $uid, $breakpoints, $json_string );
}
$base = $breakpoints['base'];