-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmeta.php
More file actions
236 lines (216 loc) · 5.71 KB
/
meta.php
File metadata and controls
236 lines (216 loc) · 5.71 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
/**
* @package wp-content-aware-engine
* @author Joachim Jensen <joachim@dev.institute>
* @license GPLv3
* @copyright 2023 by Joachim Jensen
*/
defined('ABSPATH') || exit;
if (!class_exists('WPCAMeta')) {
/**
* Post Meta
*/
class WPCAMeta
{
/**
* Id
* @var string
*/
private $id;
/**
* Title
* @var string
*/
private $title;
/**
* Description
* @var string
*/
private $description;
/**
* Default value
* @var mixed
*/
private $default_value;
/**
* Input type
* @var string
*/
private $input_type;
/**
* Input list
* @var string
*/
private $input_list;
/**
* Callback to sanitize data before save
* @var func
*/
private $sanitizer;
/**
* Constructor
*
* @since 1.0
*/
public function __construct(
$id,
$title,
$default_value = '',
$input_type = 'text',
$input_list = [],
$description = '',
$sanitizer = ''
) {
$this->id = $id;
$this->title = $title;
$this->default_value = $default_value;
$this->input_type = $input_type;
$this->input_list = $input_list;
$this->description = $description;
$this->sanitizer = $sanitizer;
}
/**
* Get meta id
*
* @since 1.0
* @return string
*/
public function get_id()
{
return $this->id;
}
/**
* Get meta title
*
* @since 1.0
* @return string
*/
public function get_title()
{
return $this->title;
}
/**
* Get meta input type
*
* @since 1.0
* @return string
*/
public function get_input_type()
{
return $this->input_type;
}
/**
* Get meta input list
*
* @since 1.0
* @return array
*/
public function get_input_list()
{
return $this->input_list;
}
/**
* Set meta input list
*
* @since 1.0
* @param array $input_list
*/
public function set_input_list($input_list)
{
$this->input_list = $input_list;
}
/**
* Get this meta data for a post
*
* @since 1.0
* @param int $post_id
* @param boolean $default_fallback
* @param boolean $single
* @return mixed
*/
public function get_data($post_id, $default_fallback = false, $single = true)
{
$data = get_post_meta($post_id, WPCACore::PREFIX . $this->id, $single);
if ($data == '' && $default_fallback) {
$data = $this->default_value;
}
return $data;
}
/**
* Update this meta data for a post
*
* @since 1.0
* @param int $post_id
* @param string $value
* @return void
*/
public function update($post_id, $value)
{
if ($this->input_type != 'multi') {
update_post_meta($post_id, WPCACore::PREFIX . $this->id, $value);
} else {
add_post_meta($post_id, WPCACore::PREFIX . $this->id, $value);
}
}
/**
* Delete this meta data for a post
*
* @since 1.0
* @param int $post_id
* @param string $value
* @return void
*/
public function delete($post_id, $value = '')
{
delete_post_meta($post_id, WPCACore::PREFIX . $this->id, $value);
}
/**
* Save data based on POST
*
* @since 4.2
* @param int $post_id
*/
public function save($post_id)
{
$value = isset($_POST[$this->id]) ? $_POST[$this->id] : '';
if ($this->sanitizer && is_callable($this->sanitizer)) {
$value = call_user_func($this->sanitizer, $value);
}
if ($this->input_type != 'multi') {
//value can be 0 and valid
if ($value != '') {
$this->update($post_id, $value);
} elseif ($this->get_data($post_id, false, true) != '') {
$this->delete($post_id);
}
} else {
$old = array_flip($this->get_data($post_id, false, false));
if (is_array($value)) {
foreach ($value as $meta) {
if (isset($old[$meta])) {
unset($old[$meta]);
} else {
$this->update($post_id, $meta);
}
}
}
foreach ($old as $meta => $v) {
$this->delete($post_id, $meta);
}
}
}
/**
* Get this meta data for a post
* represented by entry in input list
*
* @since 1.0
* @param int $post_id
* @return mixed
*/
public function get_list_data($post_id, $default_fallback = true)
{
$data = $this->get_data($post_id, $default_fallback);
return isset($this->input_list[$data]) ? $this->input_list[$data] : null;
}
}
}