-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.php
99 lines (82 loc) · 2.34 KB
/
index.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
<?php
/*
Plugin Name: ACF Get All Objects
Plugin URI: #
Description: get_field has cost in front end! it means when you perform this function it calls multiple time queries as you store data in your backend. in simple words if you had 1000 text box in your acf option back-end and you call acf get_field in front-end it will call mysql query 1000 times and if you want use acf and care about your performance you will need to use this plugin or other approciate approche.
Version: 1.0.0
Author: dariushvesal
Author URI: http://www.devlife.ir
License: No License
License URI: -
*/
class ACFAllObjects
{
public function existACFAllObjects($key)
{
$re = get_option($key, []);
if (empty($re)) {
return false;
} else {
return true;
}
}
}
class ACFAllObjectsBackend extends ACFAllObjects
{
public function __construct()
{
add_action('acf/save_post', [$this, 'my_acf_save_post'], 9999);
}
public function my_acf_save_post($post_id)
{
if (! function_exists('get_fields')) {
return;
}
if (empty($_POST['acf'])) {
return;
}
$re = get_fields($post_id);
$this->setACFAllObjects($post_id, $re);
}
public function setACFAllObjects($post_id, $value)
{
$jsonValue = json_encode($value);
$newKey = "acfAllObjects_$post_id";
//acfAllObjects_options
if ($this->existACFAllObjects($newKey)) {
update_option($newKey, $jsonValue, 'no');
} else {
add_option($newKey, $jsonValue, '', 'no');
}
}
}
class ACFAllObj {
public static function all($post_id = '')
{
if ( !$post_id ) {
$post_id = get_the_id();
}
$newKey = "acfAllObjects_$post_id";
$re = get_option($newKey, []);
return json_decode($re);
}
public static function get($key = '', $post_id = '')
{
if ( !$post_id ) {
$post_id = get_the_id();
}
if($post_id === 'option') {
$post_id = 'options';
}
$newKey = "acfAllObjects_$post_id";
$re = get_option($newKey, []);
$reArray = json_decode($re, true);
if (! empty($reArray) ) {
if (isset($reArray[$key])) {
return $reArray[$key];
}
}
return [];
}
}
new ACFAllObjectsBackend();