|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Converters: Cache Post Converter |
|
4
|
|
|
* |
|
5
|
|
|
* The Cached Post Converter looks for cached copies of converter results before |
|
6
|
|
|
* actually calling the underlying {@link Wordlift_Post_Converter} (injected |
|
7
|
|
|
* via the constructor). |
|
8
|
|
|
* |
|
9
|
|
|
* @since 3.16.0 |
|
10
|
|
|
* @package Wordlift |
|
11
|
|
|
* @subpackage Wordlift/includes/cache |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Define the {@link Wordlift_Cached_Post_Converter} class. |
|
16
|
|
|
* |
|
17
|
|
|
* @since 3.16.0 |
|
18
|
|
|
*/ |
|
19
|
|
|
class Wordlift_Cached_Post_Converter implements Wordlift_Post_Converter { |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* A {@link Wordlift_Post_Converter} instance. |
|
23
|
|
|
* |
|
24
|
|
|
* @since 3.16.0 |
|
25
|
|
|
* |
|
26
|
|
|
* @var \Wordlift_Post_Converter $converter A {@link Wordlift_Post_Converter} instance. |
|
27
|
|
|
*/ |
|
28
|
|
|
private $converter; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* A {@link Wordlift_Log_Service} instance. |
|
32
|
|
|
* |
|
33
|
|
|
* @since 3.16.0 |
|
34
|
|
|
* |
|
35
|
|
|
* @var Wordlift_Log_Service \$log A {@link Wordlift_Log_Service} instance. |
|
36
|
|
|
*/ |
|
37
|
|
|
private $log; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var Wordlift_Cache_Service |
|
41
|
|
|
*/ |
|
42
|
|
|
private $cache_service; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Wordlift_Cached_Post_Converter constructor. |
|
46
|
|
|
* |
|
47
|
|
|
* @param \Wordlift_Post_Converter $converter |
|
48
|
|
|
* @param \Wordlift_Cache_Service $cache_service |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct( $converter, $cache_service ) { |
|
51
|
|
|
|
|
52
|
|
|
$this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
53
|
|
|
|
|
54
|
|
|
$this->converter = $converter; |
|
55
|
|
|
$this->cache_service = $cache_service; |
|
56
|
|
|
|
|
57
|
|
|
$this->init_hooks(); |
|
58
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Hooks to catch post/post meta changes in order to invalidate the cache. |
|
63
|
|
|
* |
|
64
|
|
|
* @since 3.16.0 |
|
65
|
|
|
*/ |
|
66
|
|
|
private function init_hooks() { |
|
67
|
|
|
|
|
68
|
|
|
// Hook on post save to flush relevant cache. |
|
69
|
|
|
add_action( 'save_post', array( $this, 'save_post' ) ); |
|
70
|
|
|
|
|
71
|
|
|
add_action( 'added_post_meta', array( |
|
72
|
|
|
$this, |
|
73
|
|
|
'changed_post_meta', |
|
74
|
|
|
), 10, 2 ); |
|
75
|
|
|
add_action( 'updated_post_meta', array( |
|
76
|
|
|
$this, |
|
77
|
|
|
'changed_post_meta', |
|
78
|
|
|
), 10, 2 ); |
|
79
|
|
|
add_action( 'deleted_post_meta', array( |
|
80
|
|
|
$this, |
|
81
|
|
|
'changed_post_meta', |
|
82
|
|
|
), 10, 2 ); |
|
83
|
|
|
|
|
84
|
|
|
// Flush cache when wordlift settings were updated. |
|
85
|
|
|
add_action( 'update_option_wl_general_settings', array( |
|
86
|
|
|
$this, |
|
87
|
|
|
'update_option_wl_general_settings', |
|
88
|
|
|
) ); |
|
89
|
|
|
|
|
90
|
|
|
// Invalid cache on relationship change. |
|
91
|
|
|
add_action( 'wl_relation_added', array( $this, 'relation_changed' ) ); |
|
92
|
|
|
add_action( 'wl_relation_deleted', array( $this, 'relation_changed' ) ); |
|
93
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @inheritdoc |
|
98
|
|
|
*/ |
|
99
|
|
|
public function convert( $post_id, &$references = array(), &$cache = false ) { |
|
100
|
|
|
|
|
101
|
|
|
$this->log->trace( "Converting post $post_id..." ); |
|
102
|
|
|
|
|
103
|
|
|
// Try to get a cached result. |
|
104
|
|
|
$contents = $this->get_cache( $post_id, $references ); |
|
105
|
|
|
|
|
106
|
|
|
// Return the cached contents if any. |
|
107
|
|
|
if ( false !== $contents ) { |
|
108
|
|
|
$this->log->debug( "Cached contents found for post $post_id." ); |
|
109
|
|
|
|
|
110
|
|
|
// Inform the caller that this is cached result. |
|
111
|
|
|
$cache = true; |
|
112
|
|
|
|
|
113
|
|
|
// Return the contents. |
|
114
|
|
|
return $contents; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$cache = false; |
|
118
|
|
|
|
|
119
|
|
|
// Convert the the post. |
|
120
|
|
|
$jsonld = $this->converter->convert( $post_id, $references ); |
|
121
|
|
|
|
|
122
|
|
|
// Cache the results. |
|
123
|
|
|
$this->set_cache( $post_id, $references, $jsonld ); |
|
124
|
|
|
|
|
125
|
|
|
// Finally return the JSON-LD. |
|
126
|
|
|
return $jsonld; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Try to get the cached contents. |
|
131
|
|
|
* |
|
132
|
|
|
* @since 3.16.0 |
|
133
|
|
|
* |
|
134
|
|
|
* @param int $post_id The {@link WP_Post} id. |
|
135
|
|
|
* @param array $references The referenced posts. |
|
136
|
|
|
* |
|
137
|
|
|
* @return mixed|bool The cached contents or false if the cached isn't found. |
|
138
|
|
|
*/ |
|
139
|
|
|
private function get_cache( $post_id, &$references = array() ) { |
|
140
|
|
|
|
|
141
|
|
|
$this->log->trace( "Getting cached contents for post $post_id..." ); |
|
142
|
|
|
|
|
143
|
|
|
// Get the cache. |
|
144
|
|
|
$contents = $this->cache_service->get_cache( $post_id ); |
|
145
|
|
|
|
|
146
|
|
|
// Bail out if we don't have cached contents or the cached contents are |
|
147
|
|
|
// invalid. |
|
148
|
|
|
if ( false === $contents || ! isset( $contents['jsonld'] ) || ! isset( $contents['references'] ) ) { |
|
149
|
|
|
$this->log->debug( "Cached contents for post $post_id not found." ); |
|
150
|
|
|
|
|
151
|
|
|
return false; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
// Remap the cache. |
|
155
|
|
|
$references = $contents['references']; |
|
156
|
|
|
|
|
157
|
|
|
return $contents['jsonld']; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Set the cache with the provided results. |
|
162
|
|
|
* |
|
163
|
|
|
* The function will prepare the provided results and will ask the {@link Wordlift_Cache_Service} |
|
164
|
|
|
* to cache them. |
|
165
|
|
|
* |
|
166
|
|
|
* @since 3.16.0 |
|
167
|
|
|
* |
|
168
|
|
|
* @param int $post_id The {@link WP_Post} id. |
|
169
|
|
|
* @param array $references An array of references. |
|
170
|
|
|
* @param array $jsonld A JSON-LD structure. |
|
171
|
|
|
*/ |
|
172
|
|
|
private function set_cache( $post_id, $references, $jsonld ) { |
|
173
|
|
|
|
|
174
|
|
|
$this->log->trace( "Caching result for post $post_id..." ); |
|
175
|
|
|
|
|
176
|
|
|
$this->cache_service->set_cache( $post_id, array( |
|
177
|
|
|
'references' => $references, |
|
178
|
|
|
'jsonld' => $jsonld, |
|
179
|
|
|
) ); |
|
180
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Hook to 'save_post', will invalidate the cache for that post. |
|
185
|
|
|
* |
|
186
|
|
|
* @since 3.16.0 |
|
187
|
|
|
* |
|
188
|
|
|
* @param int $post_id The {@link WP_Post} id. |
|
189
|
|
|
*/ |
|
190
|
|
|
public function save_post( $post_id ) { |
|
191
|
|
|
$this->log->trace( "Post $post_id saved, invalidating cache..." ); |
|
192
|
|
|
|
|
193
|
|
|
$this->cache_service->delete_cache( $post_id ); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Hook to meta changed for a {@link WP_Post}, will cause the cause to |
|
198
|
|
|
* invalidate. |
|
199
|
|
|
* |
|
200
|
|
|
* @since 3.16.0 |
|
201
|
|
|
* |
|
202
|
|
|
* @param int $id The {@link WP_Post} meta id. |
|
203
|
|
|
* @param int $post_id The {@link WP_Post} id. |
|
204
|
|
|
*/ |
|
205
|
|
|
public function changed_post_meta( $id, $post_id ) { |
|
206
|
|
|
$this->log->trace( "Post $post_id meta changed, invalidating cache..." ); |
|
207
|
|
|
|
|
208
|
|
|
$this->cache_service->delete_cache( $post_id ); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Hook to WordLift's options changes, will flush the cache. |
|
213
|
|
|
* |
|
214
|
|
|
* @since 3.16.0 |
|
215
|
|
|
*/ |
|
216
|
|
|
public function update_option_wl_general_settings() { |
|
217
|
|
|
$this->log->trace( "WordLift options changed, flushing cache..." ); |
|
218
|
|
|
|
|
219
|
|
|
$this->cache_service->flush(); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Hook to WordLift's post/entity relation changes, will invalidate the cache. |
|
224
|
|
|
* |
|
225
|
|
|
* @since 3.16.0 |
|
226
|
|
|
* |
|
227
|
|
|
* @param int $post_id The {@link WP_Post} id. |
|
228
|
|
|
*/ |
|
229
|
|
|
public function relation_changed( $post_id ) { |
|
230
|
|
|
$this->log->trace( "Post $post_id relations changed, invalidating cache..." ); |
|
231
|
|
|
|
|
232
|
|
|
$this->cache_service->delete_cache( $post_id ); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
} |
|
236
|
|
|
|