1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Helper; |
4
|
|
|
|
5
|
|
|
use Carbon_Fields\Datastore\Datastore; |
6
|
|
|
use Carbon_Fields\Exception\Incorrect_Syntax_Exception; |
7
|
|
|
use WP_Query; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Helper functions and main initialization class. |
11
|
|
|
*/ |
12
|
|
|
class Helper { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Get a field from a specific container type or id |
16
|
|
|
* |
17
|
|
|
* @param string $container_type Container type to search in. Optional if $container_id is supplied |
18
|
|
|
* @param string $container_id Container id to search in. Optional if $container_type is supplied |
19
|
|
|
* @param string $field_name Field name to search for |
20
|
|
|
* @return boolean |
21
|
|
|
*/ |
22
|
|
|
public static function get_field( $container_type, $container_id, $field_name ) { |
23
|
|
|
\Carbon_Fields\Carbon_Fields::verify_fields_registered(); |
24
|
|
|
|
25
|
|
|
$repository = \Carbon_Fields\Carbon_Fields::resolve( 'container_repository' ); |
26
|
|
|
if ( $container_id ) { |
27
|
|
|
return $repository->get_field_in_container( $field_name, $container_id ); |
28
|
|
|
} |
29
|
|
|
return $repository->get_field_in_containers( $field_name, $container_type ); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get a clone of a field with a value loaded. |
34
|
|
|
* WARNING: The datastore is cloned! |
35
|
|
|
* |
36
|
|
|
* @param int $object_id Object id to get value for (e.g. post_id, term_id etc.) |
37
|
|
|
* @param string $container_type Container type to search in. Optional if $container_id is supplied |
38
|
|
|
* @param string $container_id Container id to search in. Optional if $container_type is supplied |
39
|
|
|
* @param string $field_name Field name to search for |
40
|
|
|
* @return mixed |
41
|
|
|
*/ |
42
|
|
|
public static function get_field_clone( $object_id, $container_type, $container_id, $field_name ) { |
43
|
|
|
$field = static::get_field( $container_type, $container_id, $field_name ); |
44
|
|
|
|
45
|
|
|
if ( ! $field ) { |
46
|
|
|
return null; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$clone = clone $field; |
50
|
|
|
if ( $object_id !== null ) { |
51
|
|
|
$clone->set_datastore( clone $clone->get_datastore(), $clone->has_default_datastore() ); |
52
|
|
|
$clone->get_datastore()->set_object_id( $object_id ); |
53
|
|
|
} |
54
|
|
|
return $clone; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Execute an action with a clone of a field with a value loaded. |
59
|
|
|
* WARNING: The datastore reference is kept! |
60
|
|
|
* |
61
|
|
|
* @param int $object_id Object id to get value for (e.g. post_id, term_id etc.) |
62
|
|
|
* @param string $container_type Container type to search in. Optional if $container_id is supplied |
63
|
|
|
* @param string $container_id Container id to search in. Optional if $container_type is supplied |
64
|
|
|
* @param string $field_name Field name to search for |
65
|
|
|
* @param \Closure $action Action to execute |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
|
|
public static function with_field_clone( $object_id, $container_type, $container_id, $field_name, $action ) { |
69
|
|
|
$field = static::get_field( $container_type, $container_id, $field_name ); |
70
|
|
|
|
71
|
|
|
if ( ! $field ) { |
72
|
|
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$clone = clone $field; |
76
|
|
|
$datastore = $clone->get_datastore(); |
77
|
|
|
$datastore_object_id = $datastore->get_object_id(); |
78
|
|
|
|
79
|
|
|
if ( $object_id !== null ) { |
80
|
|
|
$datastore->set_object_id( $object_id ); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$result = $action($clone); |
84
|
|
|
|
85
|
|
|
if ( $object_id !== null ) { |
86
|
|
|
$datastore->set_object_id( $datastore_object_id ); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $result; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get a value formatted for end-users |
94
|
|
|
* |
95
|
|
|
* @param int $object_id Object id to get value for (e.g. post_id, term_id etc.) |
96
|
|
|
* @param string $container_type Container type to search in |
97
|
|
|
* @param string $container_id |
98
|
|
|
* @param string $field_name Field name |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
|
|
public static function get_value( $object_id, $container_type, $container_id, $field_name ) { |
102
|
|
|
return static::with_field_clone( |
103
|
|
|
$object_id, |
104
|
|
|
$container_type, |
105
|
|
|
$container_id, |
106
|
|
|
$field_name, |
107
|
|
|
function( $field ) { |
108
|
|
|
if ( ! $field ) { |
109
|
|
|
return ''; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$field->load(); |
113
|
|
|
return $field->get_formatted_value(); |
114
|
|
|
} |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Set value for a field |
120
|
|
|
* |
121
|
|
|
* @param int $object_id Object id to get value for (e.g. post_id, term_id etc.) |
122
|
|
|
* @param string $container_type Container type to search in |
123
|
|
|
* @param string $container_id |
124
|
|
|
* @param string $field_name Field name |
125
|
|
|
* @param array $value Field expects a `value_set`. Complex_Field expects a `value_tree` - refer to DEVELOPMENT.md |
126
|
|
|
* @return void |
127
|
|
|
*/ |
128
|
|
|
public static function set_value( $object_id, $container_type, $container_id, $field_name, $value ) { |
129
|
|
|
static::with_field_clone( |
130
|
|
|
$object_id, |
131
|
|
|
$container_type, |
132
|
|
|
$container_id, |
133
|
|
|
$field_name, |
134
|
|
|
function( $field ) use ( $container_id, $field_name, $value ) { |
135
|
|
|
if ( ! $field ) { |
136
|
|
|
$container_message = $container_id ? 'in container with id "' . $container_id . '"' : 'in containers of type "' . $container_type . '"'; |
|
|
|
|
137
|
|
|
Incorrect_Syntax_Exception::raise( 'Could not find a field which satisfies the supplied pattern ' . $container_message . ': ' . $field_name ); |
138
|
|
|
return; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$field->set_value( $value ); |
142
|
|
|
$field->save(); |
143
|
|
|
} |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Shorthand for get_post_meta(). |
149
|
|
|
* Uses the ID of the current post in the loop. |
150
|
|
|
* |
151
|
|
|
* @param string $name Field name |
152
|
|
|
* @param string $container_id |
153
|
|
|
* @return mixed |
154
|
|
|
*/ |
155
|
|
|
public static function get_the_post_meta( $name, $container_id = '' ) { |
156
|
|
|
return static::get_post_meta( get_the_ID(), $name, $container_id ); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get post meta field for a post. |
161
|
|
|
* |
162
|
|
|
* @param int $id Post ID |
163
|
|
|
* @param string $name Field name |
164
|
|
|
* @param string $container_id |
165
|
|
|
* @return mixed |
166
|
|
|
*/ |
167
|
|
|
public static function get_post_meta( $id, $name, $container_id = '' ) { |
168
|
|
|
return static::get_value( $id, 'post_meta', $container_id, $name ); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Set post meta field for a post. |
173
|
|
|
* |
174
|
|
|
* @param int $id Post ID |
175
|
|
|
* @param string $name Field name |
176
|
|
|
* @param array $value |
177
|
|
|
* @param string $container_id |
178
|
|
|
*/ |
179
|
|
|
public static function set_post_meta( $id, $name, $value, $container_id = '' ) { |
180
|
|
|
return static::set_value( $id, 'post_meta', $container_id, $name, $value ); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get theme option field value. |
185
|
|
|
* |
186
|
|
|
* @param string $name Field name |
187
|
|
|
* @param string $container_id |
188
|
|
|
* @return mixed |
189
|
|
|
*/ |
190
|
|
|
public static function get_theme_option( $name, $container_id = '' ) { |
191
|
|
|
return static::get_value( null, 'theme_options', $container_id, $name ); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Set theme option field value. |
196
|
|
|
* |
197
|
|
|
* @param string $name Field name |
198
|
|
|
* @param array $value |
199
|
|
|
* @param string $container_id |
200
|
|
|
*/ |
201
|
|
|
public static function set_theme_option( $name, $value, $container_id = '' ) { |
202
|
|
|
return static::set_value( null, 'theme_options', $container_id, $name, $value ); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Get network option field value for the main site. |
207
|
|
|
* |
208
|
|
|
* @param string $name Field name |
209
|
|
|
* @param string $container_id |
210
|
|
|
* @return mixed |
211
|
|
|
*/ |
212
|
|
|
public static function get_the_network_option( $name, $container_id = '' ) { |
213
|
|
|
$id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1; |
214
|
|
|
return static::get_network_option( $id, $name, $container_id ); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Get network option field value for a site. |
219
|
|
|
* |
220
|
|
|
* @param string $id Site ID |
221
|
|
|
* @param string $name Field name |
222
|
|
|
* @param string $container_id |
223
|
|
|
* @return mixed |
224
|
|
|
*/ |
225
|
|
|
public static function get_network_option( $id, $name, $container_id = '' ) { |
226
|
|
|
return static::get_value( $id, 'network', $container_id, $name ); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Set network option field value for a site. |
231
|
|
|
* |
232
|
|
|
* @param string $id Site ID |
233
|
|
|
* @param string $name Field name |
234
|
|
|
* @param string $container_id |
235
|
|
|
* @return mixed |
236
|
|
|
*/ |
237
|
|
|
public static function set_network_option( $id, $name, $value, $container_id = '' ) { |
238
|
|
|
return static::set_value( $id, 'network', $container_id, $name, $value ); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Get term meta field for a term. |
243
|
|
|
* |
244
|
|
|
* @param int $id Term ID |
245
|
|
|
* @param string $name Field name |
246
|
|
|
* @param string $container_id |
247
|
|
|
* @return mixed |
248
|
|
|
*/ |
249
|
|
|
public static function get_term_meta( $id, $name, $container_id = '' ) { |
250
|
|
|
return static::get_value( $id, 'term_meta', $container_id, $name ); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Set term meta field for a term. |
255
|
|
|
* |
256
|
|
|
* @param int $id Term ID |
257
|
|
|
* @param string $name Field name |
258
|
|
|
* @param array $value |
259
|
|
|
* @param string $container_id |
260
|
|
|
*/ |
261
|
|
|
public static function set_term_meta( $id, $name, $value, $container_id = '' ) { |
262
|
|
|
return static::set_value( $id, 'term_meta', $container_id, $name, $value ); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Get user meta field for a user. |
267
|
|
|
* |
268
|
|
|
* @param int $id User ID |
269
|
|
|
* @param string $name Field name |
270
|
|
|
* @param string $container_id |
271
|
|
|
* @return mixed |
272
|
|
|
*/ |
273
|
|
|
public static function get_user_meta( $id, $name, $container_id = '' ) { |
274
|
|
|
return static::get_value( $id, 'user_meta', $container_id, $name ); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Set user meta field for a user. |
279
|
|
|
* |
280
|
|
|
* @param int $id User ID |
281
|
|
|
* @param string $name Field name |
282
|
|
|
* @param array $value |
283
|
|
|
* @param string $container_id |
284
|
|
|
*/ |
285
|
|
|
public static function set_user_meta( $id, $name, $value, $container_id = '' ) { |
286
|
|
|
return static::set_value( $id, 'user_meta', $container_id, $name, $value ); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Get comment meta field for a comment. |
291
|
|
|
* |
292
|
|
|
* @param int $id Comment ID |
293
|
|
|
* @param string $name Field name |
294
|
|
|
* @param string $container_id |
295
|
|
|
* @return mixed |
296
|
|
|
*/ |
297
|
|
|
public static function get_comment_meta( $id, $name, $container_id = '' ) { |
298
|
|
|
return static::get_value( $id, 'comment_meta', $container_id, $name ); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Set comment meta field for a comment. |
303
|
|
|
* |
304
|
|
|
* @param int $id Comment ID |
305
|
|
|
* @param string $name Field name |
306
|
|
|
* @param array $value |
307
|
|
|
* @param string $container_id |
308
|
|
|
*/ |
309
|
|
|
public static function set_comment_meta( $id, $name, $value, $container_id = '' ) { |
310
|
|
|
return static::set_value( $id, 'comment_meta', $container_id, $name, $value ); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Get nav menu item meta field for a nav menu item. |
315
|
|
|
* |
316
|
|
|
* @param int $id Nav menu item ID |
317
|
|
|
* @param string $name Field name |
318
|
|
|
* @param string $container_id |
319
|
|
|
* @return mixed |
320
|
|
|
*/ |
321
|
|
|
public static function get_nav_menu_item_meta( $id, $name, $container_id = '' ) { |
322
|
|
|
return static::get_value( $id, 'nav_menu_item', $container_id, $name ); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Set nav menu item meta field for a nav menu item. |
327
|
|
|
* |
328
|
|
|
* @param int $id Nav menu item ID |
329
|
|
|
* @param string $name Field name |
330
|
|
|
* @param array $value |
331
|
|
|
* @param string $container_id |
332
|
|
|
*/ |
333
|
|
|
public static function set_nav_menu_item_meta( $id, $name, $value, $container_id = '' ) { |
334
|
|
|
return static::set_value( $id, 'nav_menu_item', $container_id, $name, $value ); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Recursive sorting function by array key. |
339
|
|
|
* |
340
|
|
|
* @param array &$array The input array. |
341
|
|
|
* @param int $sort_flags Flags for controlling sorting behavior. |
342
|
|
|
* @return boolean |
343
|
|
|
*/ |
344
|
|
|
public static function ksort_recursive( &$array, $sort_flags = SORT_REGULAR ) { |
345
|
|
|
if ( ! is_array( $array ) ) { |
346
|
|
|
return false; |
347
|
|
|
} |
348
|
|
|
ksort( $array, $sort_flags ); |
349
|
|
|
foreach ( $array as $key => $value ) { |
350
|
|
|
self::ksort_recursive( $array[ $key ], $sort_flags ); |
351
|
|
|
} |
352
|
|
|
return true; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Get the relation type from an array similar to how meta_query works in WP_Query |
357
|
|
|
* |
358
|
|
|
* @param array $array |
359
|
|
|
* @param array<string> $allowed_relations |
360
|
|
|
* @param string $relation_key |
361
|
|
|
* @return string |
362
|
|
|
*/ |
363
|
|
|
public static function get_relation_type_from_array( $array, $allowed_relations = array( 'AND', 'OR' ), $relation_key = 'relation' ) { |
364
|
|
|
$allowed_relations = array_values( $allowed_relations ); |
365
|
|
|
$allowed_relations = array_map( 'strtoupper', $allowed_relations ); |
366
|
|
|
$relation = isset( $allowed_relations[0] ) ? $allowed_relations[0] : ''; |
367
|
|
|
|
368
|
|
|
if ( isset( $array[ $relation_key ] ) ) { |
369
|
|
|
$relation = strtoupper( $array[ $relation_key ] ); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
View Code Duplication |
if ( ! in_array( $relation, $allowed_relations ) ) { |
373
|
|
|
Incorrect_Syntax_Exception::raise( 'Invalid relation type ' . $relation . '. ' . |
374
|
|
|
'The rule should be one of the following: "' . implode( '", "', $allowed_relations ) . '"' ); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
return $relation; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* Normalize a label by updating case, stripping common prefixes etc. |
382
|
|
|
* |
383
|
|
|
* @param string $label |
384
|
|
|
* @return string |
385
|
|
|
*/ |
386
|
|
View Code Duplication |
public static function normalize_label( $label ) { |
387
|
|
|
// remove the leading underscore(if it's there) |
388
|
|
|
$label = preg_replace( '~^_~', '', $label ); |
389
|
|
|
|
390
|
|
|
// remove the leading "crb_"(if it's there) |
391
|
|
|
$label = preg_replace( '~^crb_~', '', $label ); |
392
|
|
|
|
393
|
|
|
// split the name into words and make them capitalized |
394
|
|
|
$label = mb_convert_case( str_replace( '_', ' ', $label ), MB_CASE_TITLE ); |
395
|
|
|
|
396
|
|
|
return $label; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* Normalize a type string representing an object type |
401
|
|
|
* |
402
|
|
|
* @param string $type |
403
|
|
|
* @return string |
404
|
|
|
*/ |
405
|
|
View Code Duplication |
public static function normalize_type( $type ) { |
406
|
|
|
$normalized_type = str_replace( ' ', '_', $type ); |
407
|
|
|
$normalized_type = preg_replace( '/[_\s]+/', '_', $normalized_type ); |
408
|
|
|
$normalized_type = preg_replace( '/^_|_$/', '', $normalized_type ); |
409
|
|
|
$normalized_type = strtolower( $normalized_type ); |
410
|
|
|
return $normalized_type; |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* Convert a string representing an object type to a fully qualified class name |
415
|
|
|
* |
416
|
|
|
* @param string $type |
417
|
|
|
* @param string $namespace |
418
|
|
|
* @param string $class_suffix |
419
|
|
|
* @return string |
420
|
|
|
*/ |
421
|
|
|
public static function type_to_class( $type, $namespace = '', $class_suffix = '' ) { |
422
|
|
|
$classlike_type = static::normalize_type( $type ); |
423
|
|
|
$classlike_type = str_replace( '_', ' ', $classlike_type ); |
424
|
|
|
$classlike_type = ucwords( $classlike_type ); |
425
|
|
|
$classlike_type = str_replace( ' ', '_', $classlike_type ); |
426
|
|
|
|
427
|
|
|
$class = $classlike_type . $class_suffix; |
428
|
|
|
if ( $namespace ) { |
429
|
|
|
$class = $namespace . '\\' . $class; |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
return $class; |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* Convert a string representing an object type to a fully qualified class name |
437
|
|
|
* |
438
|
|
|
* @param string $class |
439
|
|
|
* @param string $class_suffix |
440
|
|
|
* @return string |
441
|
|
|
*/ |
442
|
|
|
public static function class_to_type( $class, $class_suffix = '' ) { |
443
|
|
|
$reflection = new \ReflectionClass( $class ); |
444
|
|
|
$type = $reflection->getShortName(); |
445
|
|
|
|
446
|
|
|
if ( $class_suffix ) { |
447
|
|
|
$type = preg_replace( '/(' . preg_quote( $class_suffix, '/' ) . ')$/i', '', $type ); |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
$type = static::normalize_type( $type ); |
451
|
|
|
|
452
|
|
|
return $type; |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* Get an array of sanitized html classes |
457
|
|
|
* |
458
|
|
|
* @param string|array<string> $classes |
459
|
|
|
* @return array<string> |
460
|
|
|
*/ |
461
|
|
|
public static function sanitize_classes( $classes ) { |
462
|
|
|
if ( ! is_array( $classes ) ) { |
463
|
|
|
$classes = array_values( array_filter( explode( ' ', $classes ) ) ); |
464
|
|
|
} |
465
|
|
|
$classes = array_map( 'sanitize_html_class', $classes ); |
466
|
|
|
return $classes; |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
/** |
470
|
|
|
* Check if an id or name for containers and fields is valid |
471
|
|
|
* |
472
|
|
|
* @param string $id |
473
|
|
|
* @return boolean |
474
|
|
|
*/ |
475
|
|
|
public static function is_valid_entity_id( $id ) { |
476
|
|
|
return ! empty( $id ) && preg_match( '/\A[a-z0-9_\-]+\z/', $id ); |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
/** |
480
|
|
|
* Return a partial regex pettern matching allowed field name characters |
481
|
|
|
* |
482
|
|
|
* @return string |
483
|
|
|
*/ |
484
|
|
|
public static function get_field_name_characters_pattern() { |
485
|
|
|
return 'a-z0-9_\-'; |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
/** |
489
|
|
|
* Get an attachment ID given a file URL |
490
|
|
|
* Modified version of https://wpscholar.com/blog/get-attachment-id-from-wp-image-url/ |
491
|
|
|
* |
492
|
|
|
* @param string $url |
493
|
|
|
* @return integet |
494
|
|
|
*/ |
495
|
|
|
public static function get_attachment_id( $url ) { |
496
|
|
|
$dir = wp_upload_dir(); |
497
|
|
|
$filename = basename( $url ); |
498
|
|
|
|
499
|
|
|
if ( strpos( $url, $dir['baseurl'] . '/' ) === false ) { |
500
|
|
|
return 0; |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
$query_args = array( |
504
|
|
|
'post_type' => 'attachment', |
505
|
|
|
'post_status' => 'inherit', |
506
|
|
|
'fields' => 'ids', |
507
|
|
|
'meta_query' => array( |
508
|
|
|
array( |
509
|
|
|
'value' => $filename, |
510
|
|
|
'compare' => 'LIKE', |
511
|
|
|
'key' => '_wp_attachment_metadata', |
512
|
|
|
), |
513
|
|
|
) |
514
|
|
|
); |
515
|
|
|
$query = new WP_Query( $query_args ); |
516
|
|
|
|
517
|
|
|
if ( $query->have_posts() ) { |
518
|
|
|
foreach ( $query->posts as $post_id ) { |
519
|
|
|
$meta = wp_get_attachment_metadata( $post_id ); |
520
|
|
|
$original_file = basename( $meta['file'] ); |
521
|
|
|
$cropped_image_files = wp_list_pluck( $meta['sizes'], 'file' ); |
522
|
|
|
|
523
|
|
|
if ( $original_file === $filename || in_array( $filename, $cropped_image_files ) ) { |
524
|
|
|
return intval( $post_id ); |
525
|
|
|
} |
526
|
|
|
} |
527
|
|
|
} |
528
|
|
|
|
529
|
|
|
return 0; |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
/** |
533
|
|
|
* Returns attachment metadata from an ID. |
534
|
|
|
* |
535
|
|
|
* @param string $id |
536
|
|
|
* @param string $type Value Type. Can be either id or url |
537
|
|
|
* @return boolean |
538
|
|
|
*/ |
539
|
|
|
public static function get_attachment_metadata( $id, $type ) { |
540
|
|
|
$attachment_meta = array( |
541
|
|
|
'thumb_url' => '', |
542
|
|
|
'default_thumb_url' => '', |
543
|
|
|
'file_ext' => '', |
544
|
|
|
'file_type' => '', |
545
|
|
|
'file_name' => '', |
546
|
|
|
'file_url' => '', |
547
|
|
|
'edit_nonce' => '', |
548
|
|
|
'title' => '', |
549
|
|
|
'caption' => '', |
550
|
|
|
'description' => '', |
551
|
|
|
'alt' => '', |
552
|
|
|
'date' => '', |
553
|
|
|
'filesize' => '', |
554
|
|
|
'width' => '', |
555
|
|
|
'height' => '', |
556
|
|
|
); |
557
|
|
|
|
558
|
|
|
// when value_type is set to "url" the $id will hold the url, not the id |
559
|
|
|
if ( $type === 'url' ) { |
560
|
|
|
$attachment_id = static::get_attachment_id( $id ); |
561
|
|
|
|
562
|
|
|
if ( $attachment_id === 0 ) { |
563
|
|
|
$attachment_meta['thumb_url'] = $id; |
564
|
|
|
$attachment_meta['default_thumb_url'] = $id; |
565
|
|
|
$attachment_meta['file_url'] = $id; |
566
|
|
|
return $attachment_meta; |
567
|
|
|
} |
568
|
|
|
|
569
|
|
|
$id = $attachment_id; |
570
|
|
|
} |
571
|
|
|
|
572
|
|
|
$attachment = get_post( $id ); |
573
|
|
|
|
574
|
|
|
if ( ! $attachment ) { |
575
|
|
|
return $attachment_meta; |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
$meta = wp_get_attachment_metadata( $attachment->ID ); |
579
|
|
|
list( $src, $width, $height ) = wp_get_attachment_image_src( $attachment->ID, 'full' ); |
|
|
|
|
580
|
|
|
|
581
|
|
|
$attachment_meta['edit_nonce'] = wp_create_nonce( 'update-post_' . $id ); |
582
|
|
|
$attachment_meta['title'] = get_the_title( $id ); |
583
|
|
|
$attachment_meta['caption'] = get_post_field( 'post_excerpt', $id ); |
584
|
|
|
$attachment_meta['description'] = get_post_field( 'post_content', $id ); |
585
|
|
|
$attachment_meta['alt'] = get_post_meta( $id, '_wp_attachment_image_alt', true ); |
586
|
|
|
$attachment_meta['date'] = mysql2date( __( 'F j, Y' ), $attachment->post_date ); |
587
|
|
|
$attachment_meta['width'] = $width; |
588
|
|
|
$attachment_meta['height'] = $height; |
589
|
|
|
$attachment_meta['file_url'] = is_numeric( $id ) ? wp_get_attachment_url( $id ) : $id; |
590
|
|
|
$attachment_meta['file_name'] = basename( $attachment_meta['file_url'] ); |
591
|
|
|
$attachment_meta['filetype'] = wp_check_filetype( $attachment_meta['file_url'] ); |
592
|
|
|
$attachment_meta['file_ext'] = $attachment_meta['filetype']['ext']; // png, mp3, etc.. |
593
|
|
|
$attachment_meta['file_type'] = preg_replace( '~\/.+$~', '', $attachment_meta['filetype']['type'] ); // image, video, etc.. |
594
|
|
|
|
595
|
|
|
if ( $attachment_meta['file_type'] === 'audio' ) { |
596
|
|
|
$attachment_meta['artist'] = $meta['artist']; |
597
|
|
|
$attachment_meta['album'] = $meta['album']; |
598
|
|
|
$attachment_meta['length'] = $meta['length_formatted']; |
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
$attachment_meta['default_thumb_url'] = wp_mime_type_icon( $id ); |
602
|
|
|
|
603
|
|
|
if ( $attachment_meta['file_type'] == 'image' ) { |
604
|
|
|
$attachment_meta['thumb_url'] = $attachment_meta['file_url']; |
605
|
|
|
|
606
|
|
|
if ( $type == 'id' ) { |
607
|
|
|
$thumb_src = wp_get_attachment_image_src( $id, 'thumbnail' ); |
608
|
|
|
$attachment_meta['thumb_url'] = $thumb_src[0]; |
609
|
|
|
} |
610
|
|
|
} else { |
611
|
|
|
$attachment_meta['thumb_url'] = $attachment_meta['default_thumb_url']; |
612
|
|
|
} |
613
|
|
|
|
614
|
|
|
$attached_file = get_attached_file( $attachment->ID ); |
615
|
|
|
if ( file_exists( $attached_file ) ) { |
616
|
|
|
$attachment_meta['filesize'] = size_format( filesize( $attached_file ) ); |
617
|
|
|
} |
618
|
|
|
|
619
|
|
|
return $attachment_meta; |
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
/** |
623
|
|
|
* Get the current $_POST or $_GET input array with compacted input values merged in |
624
|
|
|
* |
625
|
|
|
* @return array |
626
|
|
|
*/ |
627
|
|
|
public static function input() { |
628
|
|
|
$input = ( isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] === 'POST' ) ? $_POST : $_GET; |
|
|
|
|
629
|
|
|
$input = stripslashes_deep( $input ); |
630
|
|
|
|
631
|
|
|
if ( \Carbon_Fields\COMPACT_INPUT ) { |
632
|
|
|
$input = static::expand_compacted_input( $input ); |
633
|
|
|
} |
634
|
|
|
|
635
|
|
|
return $input; |
636
|
|
|
} |
637
|
|
|
|
638
|
|
|
/** |
639
|
|
|
* Get a copy of the passed array with compacted input values merged in |
640
|
|
|
* |
641
|
|
|
* @param array $input |
642
|
|
|
* @return array |
643
|
|
|
*/ |
644
|
|
|
public static function expand_compacted_input( $input ) { |
645
|
|
|
if ( isset( $input[ \Carbon_Fields\COMPACT_INPUT_KEY ] ) ) { |
646
|
|
|
$json = json_decode( $input[ \Carbon_Fields\COMPACT_INPUT_KEY ], true ); |
647
|
|
|
$input = array_merge( $input, $json ); |
648
|
|
|
} |
649
|
|
|
return $input; |
650
|
|
|
} |
651
|
|
|
|
652
|
|
|
/** |
653
|
|
|
* Get valid input from an input array compared to predefined options |
654
|
|
|
* |
655
|
|
|
* @param array $input |
656
|
|
|
* @param array $options |
657
|
|
|
* @return array |
658
|
|
|
*/ |
659
|
|
|
public static function get_valid_options( $input, $options ) { |
660
|
|
|
// enfore comparison to be string so we do not get unexpected matches |
661
|
|
|
// for cases such as "string without any numbers" == 0 |
662
|
|
|
// in array_search() |
663
|
|
|
$search_options = array_map( 'strval', $options ); |
664
|
|
|
|
665
|
|
|
$valid_input = array(); |
666
|
|
|
foreach ( $input as $raw_value ) { |
667
|
|
|
$index = array_search( strval( $raw_value ), $search_options, true ); |
668
|
|
|
|
669
|
|
|
if ( $index === false ) { |
670
|
|
|
continue; |
671
|
|
|
} |
672
|
|
|
|
673
|
|
|
$valid_input[] = $options[ $index ]; |
674
|
|
|
} |
675
|
|
|
return $valid_input; |
676
|
|
|
} |
677
|
|
|
|
678
|
|
|
/** |
679
|
|
|
* Get an array of active sidebars |
680
|
|
|
* |
681
|
|
|
* @return array |
682
|
|
|
*/ |
683
|
|
|
public static function get_active_sidebars() { |
684
|
|
|
global $wp_registered_sidebars; |
685
|
|
|
|
686
|
|
|
$sidebars = array(); |
687
|
|
|
|
688
|
|
|
foreach ( $wp_registered_sidebars as $sidebar ) { |
689
|
|
|
// Check if we have inactive sidebars |
690
|
|
|
if ( isset( $sidebar['class'] ) && strpos( $sidebar['class'], 'inactive-sidebar' ) !== false ) { |
691
|
|
|
continue; |
692
|
|
|
} |
693
|
|
|
|
694
|
|
|
$sidebars[] = array( |
695
|
|
|
'id' => $sidebar['id'], |
696
|
|
|
'name' => $sidebar['name'], |
697
|
|
|
); |
698
|
|
|
} |
699
|
|
|
|
700
|
|
|
return $sidebars; |
701
|
|
|
} |
702
|
|
|
} |
703
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.