-
-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathclass-note.php
More file actions
215 lines (173 loc) · 3.76 KB
/
class-note.php
File metadata and controls
215 lines (173 loc) · 3.76 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
<?php
/**
* Note class
*
* @package WP_Ultimo
* @subpackage Models
* @since 2.0.0
*/
namespace WP_Ultimo\Objects;
// Exit if accessed directly
defined('ABSPATH') || exit;
/**
* Note class
*
* @since 2.0.0
*/
class Note {
/**
* The Note content.
*
* @since 2.0.0
* @var array
*/
protected $attributes = [];
/**
* Initializes the object.
*
* @since 2.0.0
*
* @param array $data Array of key => values note fields.
*/
public function __construct($data = []) {
$this->attributes($data);
}
/**
* Loops through allowed fields and loads them.
*
* @since 2.0.0
*
* @param array $data Array of key => values note fields.
* @return void
*/
public function attributes($data): void {
$allowed_attributes = array_keys(self::fields());
foreach ($data as $key => $value) {
if (in_array($key, $allowed_attributes, true)) {
$this->attributes[ $key ] = $value;
}
}
$this->attributes['date_created'] = wu_get_current_time('mysql', true);
}
/**
* Checks if this note has any content at all.
*
* @since 2.0.0
* @return boolean
*/
public function exists() {
return ! empty(array_filter($this->attributes));
}
/**
* Checks if a parameter exists.
*
* @since 2.0.0
*
* @param string $name The parameter to check.
* @return boolean
*/
public function __isset($name) {
return wu_get_isset($this->attributes, $name, '');
}
/**
* Gets a note field.
*
* @since 2.0.0
*
* @param string $name The parameter to return.
* @return string
*/
public function __get($name) {
$value = wu_get_isset($this->attributes, $name, '');
return apply_filters("wu_note_get_{$name}", $value, $this);
}
/**
* Sets a note field.
*
* @since 2.0.0
*
* @param string $name Field name.
* @param string $value The field value.
*/
public function __set($name, $value) {
$value = apply_filters("wu_note_set_{$name}", $value, $this);
$this->attributes[ $name ] = $value;
}
/**
* Returns the validation rules for new notes.
*
* @since 2.0.0
* @return array
*/
protected function validation_rules() {
return [];
}
/**
* Validates the fields following the validation rules.
*
* @since 2.0.0
* @return true|\WP_Error
*/
public function validate() {
$validator = new \WP_Ultimo\Helpers\Validator();
$validator->validate($this->to_array(), $this->validation_rules());
if ($validator->fails()) {
return $validator->get_errors();
}
return true;
}
/**
* Returns a key => value representation of the notes fields.
*
* @since 2.0.0
*
* @param boolean $labels Wether or not to return labels as keys or the actual keys.
* @return array
*/
public function to_array($labels = false) {
$address_array = [];
$fields = self::fields();
foreach ($fields as $field_key => $field) {
if ( ! empty($this->{$field_key})) {
$key = $labels ? $field['title'] : $field_key;
$address_array[ $key ] = $this->{$field_key};
}
}
return $address_array;
}
/**
* Returns the contents of the note.
*
* @since 2.0.0
*
* @param string $delimiter Delimiter to glue address pieces together.
*/
public function to_string($delimiter = PHP_EOL): string {
return implode($delimiter, $this->to_array());
}
/**
* Note field definitions.
*
* This is used to determine fields allowed on the note.
*
* @since 2.0.0
* @return array
*/
public static function fields() {
$fields = [];
$fields['text'] = [
'type' => 'text',
'title' => __('Text', 'ultimate-multisite'),
];
$fields['author_id'] = [
'type' => 'number',
'title' => __('Author ID', 'ultimate-multisite'),
];
$fields['note_id'] = [
'type' => 'text',
'title' => __('Note ID', 'ultimate-multisite'),
];
uasort($fields, 'wu_sort_by_order');
return $fields;
}
}