-
Notifications
You must be signed in to change notification settings - Fork 1
/
wds-tlc-transients.php
137 lines (118 loc) · 3.37 KB
/
wds-tlc-transients.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
* Plugin Name: WDS TLC Transients
* Plugin URI: http://webdevstudios
* Description: Set up TLC transients for theme.
* Version: 1.0.0
* Author: WebDevStudios
* Author URI: http://webdevstudios.com
* License: GPLv2 or later
*/
wds_tlc_load_dependency();
/**
* Load this plugin's third-party dependency.
*
* Check whether it's relative to this plugin's root, or to the wp-content directory, using default Composer vendor
* directory names.
*
* @author Jeremy Ward <jeremy.ward@webdevstudios.com>
* @since 2019-01-02
* @return void
*/
function wds_tlc_load_dependency() {
$file = '/vendor/markjaquith/wp-tlc-transients/tlc-transients.php';
$paths = array_values( array_filter( [
untrailingslashit( __DIR__ ) . $file,
untrailingslashit( WP_CONTENT_DIR ) . $file,
], function ( $path ) {
return is_readable( $path );
} ) );
if ( count( $paths ) ) {
require_once $paths[0];
}
}
/**
* Use in place of `get_posts()`.
*
* @param array $wp_query_args Array of get_posts arguments
* @param array $tlc_args Array of TLC arguments
*
* @return array Array of post objects
*/
function wds_cache_get_posts( $wp_query_args = array(), $tlc_args = array() ) {
// Parse args
$tlc_args = wp_parse_args( $tlc_args,
array(
'time' => 12 * HOUR_IN_SECONDS,
'callback' => 'get_posts',
'cache_bust' => false
)
);
// Run wp_query
$posts = wds_cache_wp_query( $wp_query_args, $tlc_args );
// Return posts
return ! empty( $posts ) ? $posts : array();
}
/**
* Use in place of `new WP_Query()`.
*
* @param array $wp_query_args Array of get_posts arguments
* @param array $tlc_args Array of TLC arguments
*
* @return WP_Query object
*/
function wds_cache_wp_query( $wp_query_args = array(), $tlc_args = array() ) {
// Set default values for WP_Query
$wp_query_args = wp_parse_args( $wp_query_args,
/**
* Setting default WP_Query values after
* _wds_get_original_wp_query() clears them.
*/
array(
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'ignore_sticky_posts' => true,
'suppress_filters' => true,
)
);
// Set default values for TLC Transients
$tlc_args = wp_parse_args( $tlc_args,
array(
'time' => 12 * HOUR_IN_SECONDS,
'callback' => '_wds_get_original_wp_query',
'cache_bust' => false,
)
);
// Setting transient key
$trans_key = md5( $tlc_args['callback'] . serialize( $wp_query_args ) );
// Cache query
$tlc = tlc_transient( $trans_key )
->updates_with( $tlc_args['callback'], array( $wp_query_args ) )
->expires_in( $tlc_args['time'] );
// Get cache query
$query = $tlc->get();
// Request a new fetch if result is empty or we're busting some cache
if ( ( empty( $query ) || is_a( $query, 'WP_Query' ) && ! $query->have_posts() ) || $tlc_args['cache_bust'] ) {
$query = $tlc->fetch_and_cache();
}
return $query;
}
/**
* Wrapper for WP_Query class.
*
* note: Don't call directly, use wds_cache_wp_query()
*/
function _wds_get_original_wp_query( $args = array() ) {
return new WP_Query( $args );
}