Skip to content

Commit 8e40311

Browse files
authored
Merge pull request #155 from ryanwelcher/fix/only-cache-when-control-is-enabled
Fix and improve caching controls
2 parents 7cb0a37 + 51c5672 commit 8e40311

File tree

7 files changed

+433
-7
lines changed

7 files changed

+433
-7
lines changed

includes/query-loop.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ function ( $null_return, $query ) {
179179

180180
if ( ! $query->is_admin &&
181181
isset( $query->query['is_aql'] ) &&
182+
isset( $query->query['enable_caching'] ) &&
183+
true === $query->query['enable_caching'] &&
182184
! isset( $_GET['context'] ) && // phpcs:ignore
183185
! isset( $_GET['canvas'] ) // phpcs:ignore
184186
) {
@@ -204,6 +206,8 @@ function ( $null_return, $query ) {
204206
function ( $posts, $query ) {
205207
if ( ! $query->is_admin &&
206208
isset( $query->query['is_aql'] ) &&
209+
isset( $query->query['enable_caching'] ) &&
210+
true === $query->query['enable_caching'] &&
207211
! isset( $_GET['context'] ) && // phpcs:ignore
208212
! isset( $_GET['canvas'] ) // phpcs:ignore
209213
) {

readme.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ Sort in ascending or descending order by:
6565

6666
#### Disable Pagination
6767

68-
Improve the performance of the query by disabling pagination. This is done automatically when there is now Pagination block in teh Post Template.
68+
Improve the performance of the query by disabling pagination.
69+
70+
#### Enable Caching
71+
72+
Store query results in a transient for one hour to reduce database load on subsequent page loads. The caching toggle is unavailable when the order is set to Random, and switching to Random order will clear any existing caching setting. Found in the **AQL: Performance Controls** panel.
6973

7074
## Filtering the available controls
7175

@@ -103,6 +107,7 @@ add_filter(
103107
- `'date_query_dynamic_range'`
104108
- `'date_query_relationship'`
105109
- `'pagination'`
110+
- `'enable_caching'`
106111

107112
## Extending AQL
108113

readme.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ Sort your content exactly how you want:
8686

8787
==== ⚡ Performance Optimization ====
8888

89-
* **Smart pagination**: Automatically disable pagination when not needed
89+
* **Disable pagination**: Reduce query overhead by turning off pagination when it is not needed
90+
* **Enable caching**: Store query results in a transient for one hour to reduce database load on subsequent page loads. The caching toggle is unavailable when the order is set to Random, and switching to Random order will clear any existing caching setting
9091
* **Efficient queries**: Optimized database queries for better performance
91-
* **Caching friendly**: Works seamlessly with popular caching plugins
9292

9393
=== Customization & Extensibility ===
9494

@@ -116,11 +116,13 @@ add_filter(
116116
* `'post_meta_query'` - Meta field queries
117117
* `'post_order'` - Sorting options
118118
* `'exclude_current_post'` - Current post exclusion
119+
* `'exclude_posts'` - Exclude a curated list of posts
119120
* `'include_posts'` - Manual post inclusion
120121
* `'child_items_only'` - Child post filtering
121122
* `'date_query_dynamic_range'` - Date range queries
122123
* `'date_query_relationship'` - Date query logic
123124
* `'pagination'` - Pagination controls
125+
* `'enable_caching'` - Query result caching
124126

125127
==== Developer-Friendly ====
126128

src/components/performance-controls.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,33 @@ import {
1111

1212
import { __ } from '@wordpress/i18n';
1313

14-
export const PerformanceControls = ( { attributes, setAttributes } ) => {
14+
export const PerformanceControls = ( {
15+
attributes,
16+
setAttributes,
17+
allowedControls,
18+
} ) => {
1519
const { query: { enable_caching: enableCaching, orderBy } = {} } =
1620
attributes;
21+
22+
if ( ! allowedControls.includes( 'enable_caching' ) ) {
23+
return null;
24+
}
25+
1726
return (
1827
<ToolsPanel
19-
label={ __( 'AQL: Performance Controls' ) }
20-
resetAll={ () => {} }
28+
label={ __( 'AQL: Performance Controls', 'advanced-query-loop' ) }
29+
resetAll={ () =>
30+
setAttributes( {
31+
query: {
32+
...attributes.query,
33+
enable_caching: false,
34+
},
35+
} )
36+
}
2137
>
2238
<ToolsPanelItem
2339
hasValue={ () => !! enableCaching }
24-
label={ __( 'Caching' ) }
40+
label={ __( 'Caching', 'advanced-query-loop' ) }
2541
onDeselect={ () =>
2642
setAttributes( {
2743
query: {

src/components/post-order-controls.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ export const PostOrderControls = ( {
9494
query: {
9595
...attributes.query,
9696
orderBy: newOrderBy,
97+
...( newOrderBy === 'rand' && {
98+
enable_caching: false,
99+
} ),
97100
},
98101
} );
99102
} }

0 commit comments

Comments
 (0)