Add Transient cache to taxonomy submenu

This commit is contained in:
Tetrakern 2024-09-18 11:55:48 +02:00
parent 0154155d83
commit 4daee5bc0a
2 changed files with 27 additions and 1 deletions

View File

@ -1681,6 +1681,16 @@ Filters the built HTML of the taxonomy submenu before it is rendered.
---
### `apply_filters( 'fictioneer_filter_cached_taxonomy_submenu_html', $html, $type, $hide_empty )`
Filters the Transient HTML of the taxonomy submenu before it is rendered. The difference to the non-cached filter is that the terms are not queried in this case.
**Parameters:**
* $html (string) The current submenu HTML to be rendered.
* $type (string) The taxonomy type.
* $hide_empty (boolean) Whether to hide empty terms. Default `true`.
---
### `apply_filters( 'fictioneer_filter_taxonomy_submenu_note', $note, $taxonomy )`
Filters the string of the note rendered above the links in the taxonomy submenu. By default, that is "Choose a {taxonomy} to browse", with the dynamic part taken from the singular name of the taxonomy object.

View File

@ -1022,12 +1022,22 @@ if ( FICTIONEER_MU_REGISTRATION ) {
* Note: The ID of the template element is "term-submenu-{$type}".
*
* @since 5.22.1
* @since 5.24.1 - Added Transient.
*
* @param string $type Optional. The taxonomy type to build the menu for. Default 'fcn_genre'.
* @param bool $hide_empty Optional. Whether to include empty taxonomies. Default true.
*/
function fictioneer_render_taxonomy_submenu( $type = 'fcn_genre', $hide_empty = true ) {
// Transient cache
$key = "fictioneer_taxonomy_submenu_{$type}" . ( $hide_empty ? '_1' : '' );
$transient = get_transient( $key );
if ( $transient ) {
echo apply_filters( 'fictioneer_filter_cached_taxonomy_submenu_html', $transient, $type, $hide_empty );
return;
}
// Query terms
$terms = get_terms(
apply_filters(
@ -1066,8 +1076,14 @@ function fictioneer_render_taxonomy_submenu( $type = 'fcn_genre', $hide_empty =
$html .= '</div></div></template>';
// Apply filters
$html = apply_filters( 'fictioneer_filter_taxonomy_submenu_html', $html, $terms, $type, $hide_empty );
// Cache as Transient
set_transient( $key, $html, HOUR_IN_SECONDS );
// Render
echo apply_filters( 'fictioneer_filter_taxonomy_submenu_html', $html, $terms, $type, $hide_empty );
echo $html;
}
/**