PluginProbe
Advanced Custom Fields: Extended / 0.9.2.5
Advanced Custom Fields: Extended v0.9.2.5
0.9.2.7 0.9.2.6 0.9.2.5 0.8.6 0.8.6.1 0.8.6.3 0.8.6.5 0.8.6.6 0.8.6.7 0.8.6.8 0.8.6.9 0.8.7 0.8.7.1 0.8.7.2 0.8.7.3 0.8.7.4 0.8.7.5 0.8.7.6 0.8.8 0.8.8.1 0.8.8.10 0.8.8.11 0.8.8.2 0.8.8.3 0.8.8.4 All 92 releases
acf-extended / includes / modules / performance / module-performance.php
module-performance.php
455 lines 9.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if(!defined('ABSPATH')){
4 exit;
5 }
6
7 if(!class_exists('acfe_performance')):
8
9 class acfe_performance{
10
11 // vars
12 public $name = '';
13 public $meta_key = '';
14 public $option_key = '';
15 public $bypass = false;
16 public $compile = false;
17
18 /**
19 * construct
20 */
21 function __construct(){
22
23 // setup
24 $this->initialize();
25
26 // register store
27 acf_register_store("acfe/performance_meta/{$this->name}")->prop('multisite', true);
28
29 // check setting
30 if(!acfe_is_performance_enabled()){
31 return;
32 }
33
34 // hooks
35 $this->add_filter('acf/pre_load_meta', array($this, 'pre_load_meta'), 999, 2);
36 $this->add_filter('acf/pre_load_metadata', array($this, 'pre_load_metadata'), 999, 4);
37 $this->add_filter('acf/pre_update_metadata', array($this, 'pre_update_metadata'), 999, 5);
38 $this->add_filter('acf/pre_delete_metadata', array($this, 'pre_delete_metadata'), 999, 4);
39 $this->add_filter('acf/update_value', array($this, 'update_value'), 999, 3);
40 $this->add_action('acf/save_post', array($this, 'pre_save_post'), 1);
41 $this->add_action('acf/save_post', array($this, 'save_post'), 999);
42
43 }
44
45
46 /**
47 * initialize
48 */
49 function initialize(){
50 // ...
51 }
52
53
54 /**
55 * add_action
56 *
57 * @param $tag
58 * @param $function_to_add
59 * @param $priority
60 * @param $accepted_args
61 */
62 function add_action($tag = '', $function_to_add = '', $priority = 10, $accepted_args = 1){
63
64 if(is_callable($function_to_add)){
65 add_action($tag, $function_to_add, $priority, $accepted_args);
66 }
67
68 }
69
70
71 /**
72 * add_filter
73 *
74 * @param $tag
75 * @param $function_to_add
76 * @param $priority
77 * @param $accepted_args
78 */
79 function add_filter($tag = '', $function_to_add = '', $priority = 10, $accepted_args = 1){
80
81 if(is_callable($function_to_add)){
82 add_filter($tag, $function_to_add, $priority, $accepted_args);
83 }
84
85 }
86
87
88 /**
89 * pre_save_post
90 *
91 * the 'compile' logic allows to gather all meta into the $store
92 * and udpate the 'acf' array only one time to avoid multiple db update calls
93 *
94 * @param $post_id
95 *
96 * @hook acf/save_post:1
97 *
98 * @function acf_save_post()
99 */
100 function pre_save_post($post_id = 0){
101
102 // start compile
103 if($this->is_enabled($post_id)){
104 $this->compile = $post_id;
105 }
106
107 }
108
109
110 /**
111 * save_post
112 *
113 * @param $post_id
114 *
115 * @hook acf/save_post:999
116 *
117 * @function acf_save_post()
118 */
119 function save_post($post_id = 0){
120
121 // compile enabled
122 if($this->compile === $post_id){
123
124 // get compiled store
125 $acf = $this->get_store($post_id);
126
127 // update with compiled data
128 $this->update_meta($acf, $post_id);
129
130 // end of compile
131 // free compile for an eventual another acf_save_post() call
132 $this->compile = false;
133
134 // convert meta
135 // has to be after compile reset to actually update meta
136 acfe_do_performance_convert($post_id);
137
138 // rollback
139 if($this->get_config('mode') === 'rollback'){
140 acfe_do_performance_rollback($post_id);
141 }
142
143 }
144
145 }
146
147
148 /**
149 * get_store
150 *
151 * @param $post_id
152 *
153 * @return array|mixed|null
154 */
155 function get_store($post_id){
156
157 // check store
158 $store = acf_get_store("acfe/performance_meta/{$this->name}");
159
160 // store found
161 if(!$store->has($post_id)){
162
163 // get meta
164 $acf = $this->get_meta($post_id);
165 $acf = acf_get_array($acf);
166
167 // set store: acf meta
168 $store->set($post_id, $acf);
169
170 }
171
172 return $store->get($post_id);
173
174 }
175
176
177 /**
178 * update_store
179 *
180 * @param $post_id
181 * @param $value
182 */
183 function update_store($post_id, $value){
184
185 // get store
186 $store = acf_get_store("acfe/performance_meta/{$this->name}");
187
188 // update store
189 $store->set($post_id, $value);
190
191 }
192
193
194 /**
195 * get_meta_key
196 *
197 * @param $post_id
198 *
199 * @return array|mixed|string|string[]
200 */
201 function get_meta_key($post_id){
202
203 /**
204 * @var $type
205 * @var $id
206 */
207 extract(acf_decode_post_id($post_id));
208
209 // get option key
210 if($type === 'option'){
211
212 $option_key = $this->option_key;
213 $option_key = str_replace('%id%', $id, $option_key);
214
215 return $option_key;
216
217 // get meta key
218 }else{
219 return $this->meta_key;
220 }
221
222 }
223
224
225 /**
226 * get_meta
227 *
228 * @param $post_id
229 *
230 * @return false|mixed|null
231 */
232 function get_meta($post_id){
233
234 /**
235 * @var $type
236 * @var $id
237 */
238 extract(acf_decode_post_id($post_id));
239
240 // meta key
241 $meta_key = $this->get_meta_key($post_id);
242
243 // get option
244 if($type === 'option'){
245 $value = get_option($meta_key, null);
246
247 // get meta
248 }else{
249 $value = acf_get_metadata($post_id, $meta_key);
250 }
251
252 return $value;
253
254 }
255
256
257 /**
258 * update_meta
259 *
260 * @param $value
261 * @param $post_id
262 *
263 * @return bool|int
264 */
265 function update_meta($value, $post_id){
266
267 /**
268 * @var $type
269 * @var $id
270 */
271 extract(acf_decode_post_id($post_id));
272
273 // meta key
274 $meta_key = $this->get_meta_key($post_id);
275
276 // update option
277 if($type === 'option'){
278
279 $value = wp_unslash($value);
280 $autoload = (bool) acf_get_setting('autoload');
281
282 return update_option($meta_key, $value, $autoload);
283
284 // update meta
285 }else{
286 return acf_update_metadata($post_id, $meta_key, $value);
287 }
288
289 }
290
291
292 /**
293 * delete_meta
294 *
295 * @param $post_id
296 *
297 * @return bool
298 */
299 function delete_meta($post_id){
300
301 /**
302 * @var $type
303 * @var $id
304 */
305 extract(acf_decode_post_id($post_id));
306
307 // meta key
308 $meta_key = $this->get_meta_key($post_id);
309
310 // delete option
311 if($type === 'option'){
312 return delete_option($meta_key);
313
314 // delete meta
315 }else{
316 return delete_metadata($type, $id, $meta_key);
317 }
318
319 }
320
321
322 /**
323 * do_bypass
324 *
325 * executes a callback within a bypass scope
326 * allowing to get/update/delete 'real' meta
327 *
328 * @param $func
329 * @param $args
330 */
331 function do_bypass($func, $args = array()){
332
333 if(is_callable($func)){
334
335 $this->bypass = true;
336
337 $return = call_user_func_array($func, $args);
338
339 $this->bypass = false;
340
341 return $return;
342
343 }
344
345 return false;
346
347 }
348
349
350 /**
351 * is_enabled
352 *
353 * @param $post_id
354 *
355 * @return bool
356 */
357 function is_enabled($post_id = 0){
358 return acfe_get_object_performance_engine_name($post_id) === $this->name;
359 }
360
361
362 /**
363 * get_config
364 *
365 * @param $key
366 *
367 * @return mixed|null
368 */
369 function get_config($key = ''){
370 return acfe_get_performance_config($key);
371 }
372
373 }
374
375 endif;
376
377 // register store
378 acf_register_store('acfe-performance');
379
380
381 /**
382 * acfe_register_performance_engine
383 *
384 * @param $class
385 *
386 * @return bool
387 */
388 function acfe_register_performance_engine($class){
389
390 // instantiate
391 $engine = new $class();
392
393 // add to store
394 acf_get_store('acfe-performance')->set($engine->name, $engine);
395
396 // return
397 return true;
398
399 }
400
401
402 /**
403 * acfe_get_performance_engines
404 *
405 * @return array|mixed|null
406 */
407 function acfe_get_performance_engines(){
408 return acf_get_store('acfe-performance')->get();
409 }
410
411
412 /**
413 * acfe_get_performance_engine
414 *
415 * @param $engine
416 *
417 * @return acfe_performance|array|mixed|null
418 */
419 function acfe_get_performance_engine($engine){
420
421 if($engine instanceof acfe_performance){
422 return $engine;
423 }
424
425 return acf_get_store('acfe-performance')->get($engine);
426 }
427
428
429 /**
430 * acfe_query_performance_engine
431 *
432 * @param $query
433 * @param $operator
434 *
435 * @return false|mixed
436 */
437 function acfe_query_performance_engine($query = array(), $operator = 'AND'){
438
439 $engines = acfe_query_performance_engines($query, $operator);
440 return current($engines);
441
442 }
443
444
445 /**
446 * acfe_query_performance_engines
447 *
448 * @param $query
449 * @param $operator
450 *
451 * @return false|mixed
452 */
453 function acfe_query_performance_engines($query = array(), $operator = 'AND'){
454 return acf_get_store('acfe-performance')->query($query, $operator);
455 }