Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion web/app/themes/juniper-theme/class-startersite.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public function __construct() {
* @param string $context context['this'] Being the Twig's {{ this }}.
*/
public function add_to_context( $context ) {
$context['site'] = $this;
$context['site'] = $this;
$context['options'] = juniper_get_cached_options();
Comment thread
rainkom marked this conversation as resolved.
return $context;
}

Expand Down
1 change: 1 addition & 0 deletions web/app/themes/juniper-theme/inc/include.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

require_once 'optimization.php';
require_once 'gutenberg-styles.php';
require_once 'pattern-categories.php';

Expand Down
41 changes: 41 additions & 0 deletions web/app/themes/juniper-theme/inc/optimization.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* Get cached ACF options page fields
*
* This function caches the ACF options to avoid multiple database queries
* on each page load. Cache is invalidated when ACF options are saved.
*
* @param bool $use_cache Optional. Whether to use cache. Default false.
* @return array The cached options or empty array if ACF is not available
*/
function juniper_get_cached_options( bool $use_cache = false ): array {
Comment thread
rainkom marked this conversation as resolved.
if ( ! class_exists( 'ACF' ) ) {
return array();
}

$use_cache = apply_filters( 'juniper_use_options_cache', $use_cache );

if ( ! $use_cache ) {
return get_fields( 'options' ) ?: array();
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
}

$cache_key = 'juniper_acf_options';
$options = get_transient( $cache_key );

if ( false === $options ) {
$options = get_fields( 'options' ) ?: array();
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
set_transient( $cache_key, $options, WEEK_IN_SECONDS );
}

return $options;
}

/**
* Clear ACF options cache when options page is saved
*/
function juniper_clear_options_cache(): void {
delete_transient( 'juniper_acf_options' );
}

add_action( 'acf/options_page/save', 'juniper_clear_options_cache', 20 );
Loading