forked from szepeviktor/tiny-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiny-cache.php
159 lines (133 loc) · 5.12 KB
/
tiny-cache.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/*
Plugin name: Tiny cache (MU)
Description: Cache post content in persistent object cache.
Version: 0.5.2
Plugin URI: https://developer.wordpress.org/reference/functions/the_content/
*/
/**
* Display content from the object cache.
*/
function the_content_cached( $more_link_text = null, $strip_teaser = false ) {
$post_id = get_the_ID();
// Learned from W3TC Page Cache rules and WP Super Cache rules
if ( ! wp_using_ext_object_cache() /* Object cache is unavailable */
|| is_user_logged_in() /* User is logged in */
|| ! ( isset( $_SERVER['REQUEST_METHOD'] ) && 'GET' === $_SERVER['REQUEST_METHOD'] ) /* Not a GET request */ // WPCS: input var ok.
|| ! $post_id /* Not possible to tie content to post ID */
|| ( defined( 'DONOTCACHEPAGE' ) && DONOTCACHEPAGE ) /* DO-NOT-CACHE tag present */
|| ! ( null === $more_link_text && false === $strip_teaser ) /* Pull requests are welcome! */
) {
the_content( $more_link_text, $strip_teaser );
return;
}
$found = null;
$cached = wp_cache_get( $post_id, 'the_content', false, $found );
// Cache hit
if ( $found ) {
print $cached; // WPCS: XSS ok.
return;
}
// Cache miss
$save_to_cache = false;
$post = get_post( $post_id );
// Public post
if ( true === is_object( $post ) ) {
if ( 'publish' === $post->post_status && empty( $post->post_password ) ) {
$save_to_cache = true;
}
}
// Print and save the content
if ( true === $save_to_cache ) {
add_filter( 'the_content', 'tiny_cache_save_the_content', PHP_INT_MAX );
}
the_content( $more_link_text, $strip_teaser );
if ( true === $save_to_cache ) {
remove_filter( 'the_content', 'tiny_cache_save_the_content', PHP_INT_MAX );
}
}
/**
* Retrieve content from the object cache.
*/
function get_the_content_cached( $more_link_text = null, $strip_teaser = false ) {
$post_id = get_the_ID();
// Learned from W3TC Page Cache rules and WP Super Cache rules
if ( ! wp_using_ext_object_cache() /* Object cache is unavailable */
|| is_user_logged_in() /* User is logged in */
|| ! ( isset( $_SERVER['REQUEST_METHOD'] ) && 'GET' === $_SERVER['REQUEST_METHOD'] ) /* Not a GET request */ // WPCS: input var ok.
|| ! $post_id /* Not possible to tie content to post ID */
|| ! ( defined( 'DONOTCACHEPAGE' ) && DONOTCACHEPAGE ) /* DO-NOT-CACHE tag present */
|| ! ( null === $more_link_text && false === $strip_teaser ) /* Pull requests are welcome! */
) {
return get_the_content( $more_link_text, $strip_teaser );
}
$found = null;
$cached = wp_cache_get( $post_id, 'get_the_content', false, $found );
// Cache hit
if ( $found ) {
return $cached;
}
// Cache miss
$save_to_cache = false;
$post = get_post( $post_id );
// Public post
if ( true === is_object( $post ) ) {
if ( 'publish' === $post->post_status && empty( $post->post_password ) ) {
$save_to_cache = true;
}
}
$content = get_the_content( $more_link_text, $strip_teaser );
if ( true === $save_to_cache ) {
$message_tpl = '<!-- Cached content generated by Tiny cache on %s -->';
$timestamp = gmdate( 'c' );
$message = sprintf( $message_tpl, esc_html( $timestamp ) );
wp_cache_set( $post_id, $content . $message, 'get_the_content', DAY_IN_SECONDS );
}
return $content;
}
/**
* Save the content to the object cache.
*/
function tiny_cache_save_the_content( $content ) {
$post_id = get_the_ID();
// Tie content to post ID
if ( $post_id ) {
$message_tpl = '<!-- Cached content generated by Tiny cache on %s -->';
$timestamp = gmdate( 'c' );
$message = sprintf( $message_tpl, esc_html( $timestamp ) );
wp_cache_set( $post_id, $content . $message, 'the_content', DAY_IN_SECONDS );
}
return $content;
}
/**
* Delete cached content by ID.
*/
function tiny_cache_delete_the_content( $post_id ) {
wp_cache_delete( $post_id, 'the_content' );
}
/**
* Delete cached content on transition_post_status.
*/
function tiny_cache_post_transition( $new_status, $old_status, $post ) {
// Post unpublished or published
if ( ( 'publish' === $old_status && 'publish' !== $new_status )
|| ( 'publish' !== $old_status && 'publish' === $new_status )
) {
tiny_cache_delete_the_content( $post->ID );
}
}
/**
* Hook cache delete actions.
*/
function tiny_cache_actions() {
// Post ID is received
add_action( 'publish_post', 'tiny_cache_delete_the_content', 0 );
add_action( 'publish_phone', 'tiny_cache_delete_the_content', 0 );
add_action( 'edit_post', 'tiny_cache_delete_the_content', 0 );
add_action( 'delete_post', 'tiny_cache_delete_the_content', 0 );
add_action( 'wp_trash_post', 'tiny_cache_delete_the_content', 0 );
add_action( 'clean_post_cache', 'tiny_cache_delete_the_content', 0 );
// Post as third argument
add_action( 'transition_post_status', 'tiny_cache_post_transition', 10, 3 );
};
add_action( 'init', 'tiny_cache_actions' );