Skip to content

add ACF options caching to Timber context - #26

Merged
bartnovak merged 2 commits into
masterfrom
feature/options_cache
Mar 12, 2026
Merged

add ACF options caching to Timber context#26
bartnovak merged 2 commits into
masterfrom
feature/options_cache

Conversation

@rainkom

@rainkom rainkom commented Mar 11, 2026

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • New Features

    • Site templates now receive centralized site options for consistent rendering.
  • Performance

    • Implemented caching for site options to reduce load times.
    • Cache is automatically cleared when site options are updated to keep content fresh.

@coderabbitai

coderabbitai Bot commented Mar 11, 2026

Copy link
Copy Markdown

Walkthrough

Adds an optimization module that provides transient-cached retrieval and clearing of ACF options, loads that module from the theme includes, and injects the cached options into the Twig template context as context['options'].

Changes

Cohort / File(s) Summary
Optimization module
web/app/themes/juniper-theme/inc/optimization.php
Adds juniper_get_cached_options(bool $use_cache = false): array (transient-backed ACF options retrieval) and juniper_clear_options_cache(): void (deletes transient). Registers add_action('acf/options_page/save', 'juniper_clear_options_cache', 20).
Includes loader
web/app/themes/juniper-theme/inc/include.php
Adds require_once 'optimization.php'; to load the new optimization module.
Template context integration
web/app/themes/juniper-theme/class-startersite.php
Adds context['options'] = juniper_get_cached_options() inside add_to_context to expose cached ACF options to Twig/templates.
🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title 'add ACF options caching to Timber context' directly and accurately summarizes the main change: adding caching functionality for ACF options to the Timber context.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feature/options_cache

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (1)
web/app/themes/juniper-theme/inc/optimization.php (1)

23-23: Consider extracting the cache key to a constant.

The cache key 'juniper_acf_options' is hardcoded in two places. A constant would reduce duplication.

Proposed refactor
+const JUNIPER_ACF_OPTIONS_CACHE_KEY = 'juniper_acf_options';
+
 function juniper_get_cached_options( bool $use_cache = false ): array {
 	// ...
-	$cache_key = 'juniper_acf_options';
+	$cache_key = JUNIPER_ACF_OPTIONS_CACHE_KEY;
 	// ...
 }

 function juniper_clear_options_cache(): void {
-	delete_transient( 'juniper_acf_options' );
+	delete_transient( JUNIPER_ACF_OPTIONS_CACHE_KEY );
 }

Also applies to: 37-38

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@web/app/themes/juniper-theme/inc/optimization.php` at line 23, Extract the
hardcoded cache key 'juniper_acf_options' into a constant (e.g.
JUNIPER_ACF_OPTIONS_CACHE_KEY) and replace all occurrences where $cache_key is
assigned or the literal is used (the instances around the $cache_key assignment
and the other occurrence at lines referenced in the review) with that constant;
update any functions or checks that reference the string to use the constant so
duplication is removed and future changes only require a single constant update.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@web/app/themes/juniper-theme/class-startersite.php`:
- Around line 22-23: The options are being fetched without enabling caching
because juniper_get_cached_options() defaults to $use_cache = false; change the
call that sets $context['options'] to pass true (i.e., call
juniper_get_cached_options(true)) so caching is used; update the assignment
where $context['options'] is set in class-startersite.php to call
juniper_get_cached_options(true) and confirm behavior remains correct for
templates that consume $context['options'].

In `@web/app/themes/juniper-theme/inc/optimization.php`:
- Line 27: The short ternary usage in the assignment to $options using
get_fields('options') (in optimization.php) violates WP coding standards;
replace the short ternary with an explicit full ternary or null-coalescing
equivalent so that $options is set to the result of get_fields('options') when
truthy and to an empty array otherwise (avoid calling get_fields twice by
capturing its return in a temporary variable if needed).
- Line 20: Replace the short ternary expression "get_fields( 'options' ) ?:
array()" with an explicit ternary to satisfy WP coding standards: capture the
result of get_fields('options') into a local variable (e.g., $fields =
get_fields('options')), then return $fields ? $fields : array(); update the
return in the function where this expression appears (the get_fields('options')
return site options code) so you don't call get_fields() twice and the short
ternary operator is removed.

---

Nitpick comments:
In `@web/app/themes/juniper-theme/inc/optimization.php`:
- Line 23: Extract the hardcoded cache key 'juniper_acf_options' into a constant
(e.g. JUNIPER_ACF_OPTIONS_CACHE_KEY) and replace all occurrences where
$cache_key is assigned or the literal is used (the instances around the
$cache_key assignment and the other occurrence at lines referenced in the
review) with that constant; update any functions or checks that reference the
string to use the constant so duplication is removed and future changes only
require a single constant update.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 46d2a269-30b4-4437-ad4c-6fdc08d6010d

📥 Commits

Reviewing files that changed from the base of the PR and between 1ddc03c and 071518f.

📒 Files selected for processing (3)
  • web/app/themes/juniper-theme/class-startersite.php
  • web/app/themes/juniper-theme/inc/include.php
  • web/app/themes/juniper-theme/inc/optimization.php

Comment thread web/app/themes/juniper-theme/class-startersite.php
Comment thread web/app/themes/juniper-theme/inc/optimization.php Outdated
Comment thread web/app/themes/juniper-theme/inc/optimization.php Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
web/app/themes/juniper-theme/inc/optimization.php (1)

24-24: Consider extracting the cache key to a constant.

'juniper_acf_options' appears in both juniper_get_cached_options and juniper_clear_options_cache. Centralizing it prevents accidental mismatches.

Proposed refactor
+const JUNIPER_OPTIONS_CACHE_KEY = 'juniper_acf_options';
+
 function juniper_get_cached_options( bool $use_cache = false ): array {
     // ...
-    $cache_key = 'juniper_acf_options';
+    $cache_key = JUNIPER_OPTIONS_CACHE_KEY;
     // ...
 }

 function juniper_clear_options_cache(): void {
-    delete_transient( 'juniper_acf_options' );
+    delete_transient( JUNIPER_OPTIONS_CACHE_KEY );
 }

Also applies to: 39-41

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@web/app/themes/juniper-theme/inc/optimization.php` at line 24, The literal
cache key 'juniper_acf_options' is duplicated in juniper_get_cached_options and
juniper_clear_options_cache; extract it to a single constant (e.g.,
JUNIPER_ACF_OPTIONS_CACHE_KEY) and replace the string occurrences in both
functions with that constant to avoid mismatches and centralize configuration.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@web/app/themes/juniper-theme/inc/optimization.php`:
- Line 12: The default should enable caching: change the function signature of
juniper_get_cached_options to default the $use_cache parameter to true (bool
$use_cache = true) so calls that omit the argument (e.g., from StarterSite
class) will use the cache; alternatively, if you prefer explicitness, update the
caller that invokes juniper_get_cached_options() with no args to pass true
instead (juniper_get_cached_options(true)).

---

Nitpick comments:
In `@web/app/themes/juniper-theme/inc/optimization.php`:
- Line 24: The literal cache key 'juniper_acf_options' is duplicated in
juniper_get_cached_options and juniper_clear_options_cache; extract it to a
single constant (e.g., JUNIPER_ACF_OPTIONS_CACHE_KEY) and replace the string
occurrences in both functions with that constant to avoid mismatches and
centralize configuration.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: eb441aa7-1043-4af6-854d-a92c0111d609

📥 Commits

Reviewing files that changed from the base of the PR and between 071518f and 2cb888d.

📒 Files selected for processing (1)
  • web/app/themes/juniper-theme/inc/optimization.php

Comment thread web/app/themes/juniper-theme/inc/optimization.php
@bartnovak
bartnovak merged commit a42244a into master Mar 12, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants