This repository was archived by the owner on Jul 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathAcf.php
More file actions
131 lines (112 loc) · 2.7 KB
/
Acf.php
File metadata and controls
131 lines (112 loc) · 2.7 KB
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
<?php
namespace Sober\Controller\Module;
use Sober\Controller\Utils;
class Acf
{
// Config
protected $data = [];
private $returnArrayFormat = false;
/**
* Construct
*
* Initialise the Loader methods
*/
public function __construct()
{
$this->setReturnFilter();
}
/**
* Set Return Filter
*
* Return filter sober/controller/acf-array
*/
private function setReturnFilter()
{
$this->returnArrayFormat =
(has_filter('sober/controller/acf/array')
? apply_filters('sober/controller/acf/array', $this->returnArrayFormat)
: false);
}
/**
* Iterates over array and adds a new snake cased key, with orignial value, for each kebab cased key
*
* Return void
*/
private function recursiveSnakeCase(&$data) {
if(!is_array($data))
return;
foreach ($data as $key => $val) {
if (is_array($val)) {
$this->recursiveSnakeCase($val);
} else {
$data[Utils::convertKebabCaseToSnakeCase($key)] = $val;
}
}
}
/**
* Set Data Return Format
*
* Return object from array if acf/array filter is not set to true
*/
public function setDataReturnFormat()
{
if ($this->returnArrayFormat) {
return;
}
if ($this->data) {
foreach ($this->data as $key => $item) {
$this->data[$key] = json_decode(json_encode($item));
}
}
}
/**
* Set Data Options Page
*
* Set data from the options page
*/
public function setDataOptionsPage()
{
if (!function_exists('acf_add_options_page')) {
return [];
}
if (get_fields('options')) {
$this->data['acf_options'] = get_fields('options');
} else {
return [];
}
}
/**
* Set Data
*
* Set data from passed in field keys
*/
public function setData($acf)
{
$query = get_queried_object();
if (!acf_get_valid_post_id($query)) {
return;
}
if (is_bool($acf)) {
$this->data = get_fields($query);
}
if (is_string($acf)) {
$this->data = [$acf => get_field($acf, $query)];
}
if (is_array($acf)) {
foreach ($acf as $item) {
$this->data[$item] = get_field($item, $query);
}
}
$this->recursiveSnakeCase($this->data);
}
/**
* Get Data
*
* Return the data
* @return array
*/
public function getData()
{
return is_array($this->data) ? $this->data : [];
}
}