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_type, $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
|
|
|
$id = apply_filters( 'carbon_get_post_meta_post_id', $id, $name, $container_id ); |
169
|
|
|
return static::get_value( $id, 'post_meta', $container_id, $name ); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Set post meta field for a post. |
174
|
|
|
* |
175
|
|
|
* @param int $id Post ID |
176
|
|
|
* @param string $name Field name |
177
|
|
|
* @param array $value |
178
|
|
|
* @param string $container_id |
179
|
|
|
*/ |
180
|
|
|
public static function set_post_meta( $id, $name, $value, $container_id = '' ) { |
181
|
|
|
return static::set_value( $id, 'post_meta', $container_id, $name, $value ); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Get theme option field value. |
186
|
|
|
* |
187
|
|
|
* @param string $name Field name |
188
|
|
|
* @param string $container_id |
189
|
|
|
* @return mixed |
190
|
|
|
*/ |
191
|
|
|
public static function get_theme_option( $name, $container_id = '' ) { |
192
|
|
|
return static::get_value( null, 'theme_options', $container_id, $name ); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Set theme option field value. |
197
|
|
|
* |
198
|
|
|
* @param string $name Field name |
199
|
|
|
* @param array $value |
200
|
|
|
* @param string $container_id |
201
|
|
|
*/ |
202
|
|
|
public static function set_theme_option( $name, $value, $container_id = '' ) { |
203
|
|
|
return static::set_value( null, 'theme_options', $container_id, $name, $value ); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Get network option field value for the main site. |
208
|
|
|
* |
209
|
|
|
* @param string $name Field name |
210
|
|
|
* @param string $container_id |
211
|
|
|
* @return mixed |
212
|
|
|
*/ |
213
|
|
|
public static function get_the_network_option( $name, $container_id = '' ) { |
214
|
|
|
$id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1; |
215
|
|
|
return static::get_network_option( $id, $name, $container_id ); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Get network option field value for a site. |
220
|
|
|
* |
221
|
|
|
* @param string $id Site ID |
222
|
|
|
* @param string $name Field name |
223
|
|
|
* @param string $container_id |
224
|
|
|
* @return mixed |
225
|
|
|
*/ |
226
|
|
|
public static function get_network_option( $id, $name, $container_id = '' ) { |
227
|
|
|
return static::get_value( $id, 'network', $container_id, $name ); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Set network option field value for a site. |
232
|
|
|
* |
233
|
|
|
* @param string $id Site ID |
234
|
|
|
* @param string $name Field name |
235
|
|
|
* @param string $container_id |
236
|
|
|
* @return mixed |
237
|
|
|
*/ |
238
|
|
|
public static function set_network_option( $id, $name, $value, $container_id = '' ) { |
239
|
|
|
return static::set_value( $id, 'network', $container_id, $name, $value ); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Get term meta field for a term. |
244
|
|
|
* |
245
|
|
|
* @param int $id Term ID |
246
|
|
|
* @param string $name Field name |
247
|
|
|
* @param string $container_id |
248
|
|
|
* @return mixed |
249
|
|
|
*/ |
250
|
|
|
public static function get_term_meta( $id, $name, $container_id = '' ) { |
251
|
|
|
return static::get_value( $id, 'term_meta', $container_id, $name ); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Set term meta field for a term. |
256
|
|
|
* |
257
|
|
|
* @param int $id Term ID |
258
|
|
|
* @param string $name Field name |
259
|
|
|
* @param array $value |
260
|
|
|
* @param string $container_id |
261
|
|
|
*/ |
262
|
|
|
public static function set_term_meta( $id, $name, $value, $container_id = '' ) { |
263
|
|
|
return static::set_value( $id, 'term_meta', $container_id, $name, $value ); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Get user meta field for a user. |
268
|
|
|
* |
269
|
|
|
* @param int $id User ID |
270
|
|
|
* @param string $name Field name |
271
|
|
|
* @param string $container_id |
272
|
|
|
* @return mixed |
273
|
|
|
*/ |
274
|
|
|
public static function get_user_meta( $id, $name, $container_id = '' ) { |
275
|
|
|
return static::get_value( $id, 'user_meta', $container_id, $name ); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Set user meta field for a user. |
280
|
|
|
* |
281
|
|
|
* @param int $id User ID |
282
|
|
|
* @param string $name Field name |
283
|
|
|
* @param array $value |
284
|
|
|
* @param string $container_id |
285
|
|
|
*/ |
286
|
|
|
public static function set_user_meta( $id, $name, $value, $container_id = '' ) { |
287
|
|
|
return static::set_value( $id, 'user_meta', $container_id, $name, $value ); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Get comment meta field for a comment. |
292
|
|
|
* |
293
|
|
|
* @param int $id Comment ID |
294
|
|
|
* @param string $name Field name |
295
|
|
|
* @param string $container_id |
296
|
|
|
* @return mixed |
297
|
|
|
*/ |
298
|
|
|
public static function get_comment_meta( $id, $name, $container_id = '' ) { |
299
|
|
|
return static::get_value( $id, 'comment_meta', $container_id, $name ); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Set comment meta field for a comment. |
304
|
|
|
* |
305
|
|
|
* @param int $id Comment ID |
306
|
|
|
* @param string $name Field name |
307
|
|
|
* @param array $value |
308
|
|
|
* @param string $container_id |
309
|
|
|
*/ |
310
|
|
|
public static function set_comment_meta( $id, $name, $value, $container_id = '' ) { |
311
|
|
|
return static::set_value( $id, 'comment_meta', $container_id, $name, $value ); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Get nav menu item meta field for a nav menu item. |
316
|
|
|
* |
317
|
|
|
* @param int $id Nav menu item ID |
318
|
|
|
* @param string $name Field name |
319
|
|
|
* @param string $container_id |
320
|
|
|
* @return mixed |
321
|
|
|
*/ |
322
|
|
|
public static function get_nav_menu_item_meta( $id, $name, $container_id = '' ) { |
323
|
|
|
return static::get_value( $id, 'nav_menu_item', $container_id, $name ); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Set nav menu item meta field for a nav menu item. |
328
|
|
|
* |
329
|
|
|
* @param int $id Nav menu item ID |
330
|
|
|
* @param string $name Field name |
331
|
|
|
* @param array $value |
332
|
|
|
* @param string $container_id |
333
|
|
|
*/ |
334
|
|
|
public static function set_nav_menu_item_meta( $id, $name, $value, $container_id = '' ) { |
335
|
|
|
return static::set_value( $id, 'nav_menu_item', $container_id, $name, $value ); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Recursive sorting function by array key. |
340
|
|
|
* |
341
|
|
|
* @param array &$array The input array. |
342
|
|
|
* @param int $sort_flags Flags for controlling sorting behavior. |
343
|
|
|
* @return boolean |
344
|
|
|
*/ |
345
|
|
|
public static function ksort_recursive( &$array, $sort_flags = SORT_REGULAR ) { |
346
|
|
|
if ( ! is_array( $array ) ) { |
347
|
|
|
return false; |
348
|
|
|
} |
349
|
|
|
ksort( $array, $sort_flags ); |
350
|
|
|
foreach ( $array as $key => $value ) { |
351
|
|
|
self::ksort_recursive( $array[ $key ], $sort_flags ); |
352
|
|
|
} |
353
|
|
|
return true; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Get the relation type from an array similar to how meta_query works in WP_Query |
358
|
|
|
* |
359
|
|
|
* @param array $array |
360
|
|
|
* @param array<string> $allowed_relations |
361
|
|
|
* @param string $relation_key |
362
|
|
|
* @return string |
363
|
|
|
*/ |
364
|
|
|
public static function get_relation_type_from_array( $array, $allowed_relations = array( 'AND', 'OR' ), $relation_key = 'relation' ) { |
365
|
|
|
$allowed_relations = array_values( $allowed_relations ); |
366
|
|
|
$allowed_relations = array_map( 'strtoupper', $allowed_relations ); |
367
|
|
|
$relation = isset( $allowed_relations[0] ) ? $allowed_relations[0] : ''; |
368
|
|
|
|
369
|
|
|
if ( isset( $array[ $relation_key ] ) ) { |
370
|
|
|
$relation = strtoupper( $array[ $relation_key ] ); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
View Code Duplication |
if ( ! in_array( $relation, $allowed_relations ) ) { |
374
|
|
|
Incorrect_Syntax_Exception::raise( 'Invalid relation type ' . $relation . '. ' . |
375
|
|
|
'The rule should be one of the following: "' . implode( '", "', $allowed_relations ) . '"' ); |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
return $relation; |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* Normalize a label by updating case, stripping common prefixes etc. |
383
|
|
|
* |
384
|
|
|
* @param string $label |
385
|
|
|
* @return string |
386
|
|
|
*/ |
387
|
|
View Code Duplication |
public static function normalize_label( $label ) { |
388
|
|
|
// remove the leading underscore(if it's there) |
389
|
|
|
$label = preg_replace( '~^_~', '', $label ); |
390
|
|
|
|
391
|
|
|
// remove the leading "crb_"(if it's there) |
392
|
|
|
$label = preg_replace( '~^crb_~', '', $label ); |
393
|
|
|
|
394
|
|
|
// split the name into words and make them capitalized |
395
|
|
|
$label = mb_convert_case( str_replace( '_', ' ', $label ), MB_CASE_TITLE ); |
396
|
|
|
|
397
|
|
|
return $label; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Normalize a type string representing an object type |
402
|
|
|
* |
403
|
|
|
* @param string $type |
404
|
|
|
* @return string |
405
|
|
|
*/ |
406
|
|
View Code Duplication |
public static function normalize_type( $type ) { |
407
|
|
|
$normalized_type = str_replace( ' ', '_', $type ); |
408
|
|
|
$normalized_type = preg_replace( '/[_\s]+/', '_', $normalized_type ); |
409
|
|
|
$normalized_type = preg_replace( '/^_|_$/', '', $normalized_type ); |
410
|
|
|
$normalized_type = strtolower( $normalized_type ); |
411
|
|
|
return $normalized_type; |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* Convert a string representing an object type to a fully qualified class name |
416
|
|
|
* |
417
|
|
|
* @param string $type |
418
|
|
|
* @param string $namespace |
419
|
|
|
* @param string $class_suffix |
420
|
|
|
* @return string |
421
|
|
|
*/ |
422
|
|
|
public static function type_to_class( $type, $namespace = '', $class_suffix = '' ) { |
423
|
|
|
$classlike_type = static::normalize_type( $type ); |
424
|
|
|
$classlike_type = str_replace( '_', ' ', $classlike_type ); |
425
|
|
|
$classlike_type = ucwords( $classlike_type ); |
426
|
|
|
$classlike_type = str_replace( ' ', '_', $classlike_type ); |
427
|
|
|
|
428
|
|
|
$class = $classlike_type . $class_suffix; |
429
|
|
|
if ( $namespace ) { |
430
|
|
|
$class = $namespace . '\\' . $class; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
return $class; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* Convert a string representing an object type to a fully qualified class name |
438
|
|
|
* |
439
|
|
|
* @param string $class |
440
|
|
|
* @param string $class_suffix |
441
|
|
|
* @return string |
442
|
|
|
*/ |
443
|
|
|
public static function class_to_type( $class, $class_suffix = '' ) { |
444
|
|
|
$reflection = new \ReflectionClass( $class ); |
445
|
|
|
$type = $reflection->getShortName(); |
446
|
|
|
|
447
|
|
|
if ( $class_suffix ) { |
448
|
|
|
$type = preg_replace( '/(' . preg_quote( $class_suffix, '/' ) . ')$/i', '', $type ); |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
$type = static::normalize_type( $type ); |
452
|
|
|
|
453
|
|
|
return $type; |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
/** |
457
|
|
|
* Get an array of sanitized html classes |
458
|
|
|
* |
459
|
|
|
* @param string|array<string> $classes |
460
|
|
|
* @return array<string> |
461
|
|
|
*/ |
462
|
|
|
public static function sanitize_classes( $classes ) { |
463
|
|
|
if ( ! is_array( $classes ) ) { |
464
|
|
|
$classes = array_values( array_filter( explode( ' ', $classes ) ) ); |
465
|
|
|
} |
466
|
|
|
$classes = array_map( 'sanitize_html_class', $classes ); |
467
|
|
|
return $classes; |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* Check if an id or name for containers and fields is valid |
472
|
|
|
* |
473
|
|
|
* @param string $id |
474
|
|
|
* @return boolean |
475
|
|
|
*/ |
476
|
|
|
public static function is_valid_entity_id( $id ) { |
477
|
|
|
return ! empty( $id ) && preg_match( '/\A[a-z0-9_\-]+\z/', $id ); |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* Return a partial regex pettern matching allowed field name characters |
482
|
|
|
* |
483
|
|
|
* @return string |
484
|
|
|
*/ |
485
|
|
|
public static function get_field_name_characters_pattern() { |
486
|
|
|
return 'a-z0-9_\-'; |
487
|
|
|
} |
488
|
|
|
|
489
|
|
|
/** |
490
|
|
|
* Get an attachment ID given a file URL |
491
|
|
|
* Modified version of https://wpscholar.com/blog/get-attachment-id-from-wp-image-url/ |
492
|
|
|
* |
493
|
|
|
* @static |
494
|
|
|
* @access public |
495
|
|
|
* |
496
|
|
|
* @param string $url |
497
|
|
|
* @return integer |
498
|
|
|
*/ |
499
|
|
|
public static function get_attachment_id( $url ) { |
500
|
|
|
$attachment_id = 0; |
501
|
|
|
$dir = wp_upload_dir(); |
502
|
|
|
|
503
|
|
|
/** |
504
|
|
|
* Filters the attachment URL from which the attachment ID is being determined. |
505
|
|
|
* |
506
|
|
|
* @since 3.0.0 |
507
|
|
|
* |
508
|
|
|
* @param string $url |
509
|
|
|
*/ |
510
|
|
|
$url = apply_filters( 'carbon_fields_attachment_id_base_url', $url ); |
511
|
|
|
|
512
|
|
|
$filename = basename( $url ); |
513
|
|
|
|
514
|
|
|
if ( strpos( $url, $dir['baseurl'] . '/' ) !== false ) { |
515
|
|
|
$query_args = array( |
516
|
|
|
'post_type' => 'attachment', |
517
|
|
|
'post_status' => 'inherit', |
518
|
|
|
'fields' => 'ids', |
519
|
|
|
'meta_query' => array( |
520
|
|
|
array( |
521
|
|
|
'value' => $filename, |
522
|
|
|
'compare' => 'LIKE', |
523
|
|
|
'key' => '_wp_attachment_metadata', |
524
|
|
|
), |
525
|
|
|
) |
526
|
|
|
); |
527
|
|
|
|
528
|
|
|
$query = new WP_Query( $query_args ); |
529
|
|
|
|
530
|
|
|
if ( $query->have_posts() ) { |
531
|
|
|
foreach ( $query->posts as $post_id ) { |
532
|
|
|
$meta = wp_get_attachment_metadata( $post_id ); |
533
|
|
|
$original_file = basename( $meta['file'] ); |
534
|
|
|
$cropped_image_files = wp_list_pluck( $meta['sizes'], 'file' ); |
535
|
|
|
|
536
|
|
|
if ( $original_file === $filename || in_array( $filename, $cropped_image_files ) ) { |
537
|
|
|
$attachment_id = intval( $post_id ); |
538
|
|
|
|
539
|
|
|
break; |
540
|
|
|
} |
541
|
|
|
} |
542
|
|
|
} |
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
/** |
546
|
|
|
* Filters the attachment id found from the passed attachment URL. |
547
|
|
|
* |
548
|
|
|
* @since 3.0.0 |
549
|
|
|
* |
550
|
|
|
* @param integer $attachment_id |
551
|
|
|
* @param string $url |
552
|
|
|
*/ |
553
|
|
|
return apply_filters( 'carbon_fields_attachment_id_from_url', $attachment_id, $url ); |
554
|
|
|
} |
555
|
|
|
|
556
|
|
|
/** |
557
|
|
|
* Returns attachment metadata from an ID. |
558
|
|
|
* |
559
|
|
|
* @static |
560
|
|
|
* @access public |
561
|
|
|
* |
562
|
|
|
* @param string $id |
563
|
|
|
* @param string $type Value Type. Can be either id or url. |
564
|
|
|
* @return boolean |
565
|
|
|
*/ |
566
|
|
|
public static function get_attachment_metadata( $id, $type ) { |
567
|
|
|
$attachment_metadata = array( |
568
|
|
|
'id' => 0, |
569
|
|
|
'thumb_url' => '', |
570
|
|
|
'file_type' => '', |
571
|
|
|
'file_name' => '', |
572
|
|
|
); |
573
|
|
|
|
574
|
|
|
// when `$type` is set to 'url' the `$id` will hold the url, not the id |
575
|
|
|
if ( $type === 'url' ) { |
576
|
|
|
$attachment_id = static::get_attachment_id( $id ); |
577
|
|
|
|
578
|
|
|
if ( $attachment_id === 0 ) { |
579
|
|
|
$attachment_metadata['thumb_url'] = $id; |
580
|
|
|
} |
581
|
|
|
|
582
|
|
|
$id = $attachment_id; |
583
|
|
|
} |
584
|
|
|
|
585
|
|
|
$attachment = get_post( $id ); |
586
|
|
|
|
587
|
|
|
if ( ! $attachment ) { |
588
|
|
|
/** |
589
|
|
|
* Filter the metadata for the attachment in case the attachment post is not found. |
590
|
|
|
* |
591
|
|
|
* @since 3.0.0 |
592
|
|
|
* |
593
|
|
|
* @param array $attachment_metadata The attachment metadata. |
594
|
|
|
* @param integer|string $id The attachment ID. Either attachment post ID or attachment url. |
595
|
|
|
* @param string $type The type of `$id` passed. Either 'id' or 'url'. |
596
|
|
|
*/ |
597
|
|
|
return apply_filters( 'carbon_fields_attachment_not_found_metadata', $attachment_metadata, $id, $type ); |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
$attachment_metadata['id'] = intval( $id ); |
601
|
|
|
$attachment_metadata['file_url'] = is_numeric( $id ) ? wp_get_attachment_url( $id ) : $id; |
602
|
|
|
$attachment_metadata['file_name'] = basename( $attachment_metadata['file_url'] ); |
603
|
|
|
$attachment_metadata['filetype'] = wp_check_filetype( $attachment_metadata['file_url'] ); |
604
|
|
|
$attachment_metadata['file_type'] = preg_replace( '~\/.+$~', '', $attachment_metadata['filetype']['type'] ); // image, video, etc.. |
605
|
|
|
|
606
|
|
|
if ( $attachment_metadata['file_type'] == 'image' ) { |
607
|
|
|
$attachment_metadata['thumb_url'] = $attachment_metadata['file_url']; |
608
|
|
|
|
609
|
|
|
if ( $type == 'id' ) { |
610
|
|
|
$attachment_metadata['thumb_url'] = wp_get_attachment_thumb_url( $id ); |
611
|
|
|
} |
612
|
|
|
} else { |
613
|
|
|
$attachment_metadata['thumb_url'] = wp_mime_type_icon( $id ); |
614
|
|
|
} |
615
|
|
|
|
616
|
|
|
/** |
617
|
|
|
* Filter the metadata for the attachment. |
618
|
|
|
* |
619
|
|
|
* @since 3.0.0 |
620
|
|
|
* |
621
|
|
|
* @param array $attachment_metadata The attachment metadata. |
622
|
|
|
* @param integer|string $id The attachment ID. Either attachment post ID or attachment url. |
623
|
|
|
* @param string $type The type of `$id` passed. Either 'id' or 'url'. |
624
|
|
|
*/ |
625
|
|
|
return apply_filters( 'carbon_fields_attachment_metadata', $attachment_metadata, $id, $type ); |
626
|
|
|
} |
627
|
|
|
|
628
|
|
|
/** |
629
|
|
|
* Get the current $_POST or $_GET input array with compacted input values merged in |
630
|
|
|
* |
631
|
|
|
* @return array |
632
|
|
|
*/ |
633
|
|
|
public static function input() { |
634
|
|
|
$input = ( isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] === 'POST' ) ? $_POST : $_GET; |
|
|
|
|
635
|
|
|
$input = stripslashes_deep( $input ); |
636
|
|
|
|
637
|
|
|
if ( \Carbon_Fields\COMPACT_INPUT ) { |
638
|
|
|
$input = static::expand_compacted_input( $input ); |
639
|
|
|
} |
640
|
|
|
|
641
|
|
|
return $input; |
642
|
|
|
} |
643
|
|
|
|
644
|
|
|
/** |
645
|
|
|
* Get a copy of the passed array with compacted input values merged in |
646
|
|
|
* |
647
|
|
|
* @param array $input |
648
|
|
|
* @return array |
649
|
|
|
*/ |
650
|
|
|
public static function expand_compacted_input( $input ) { |
651
|
|
|
if ( isset( $input[ \Carbon_Fields\COMPACT_INPUT_KEY ] ) ) { |
652
|
|
|
$inputs = $input[ \Carbon_Fields\COMPACT_INPUT_KEY ]; |
653
|
|
|
$input = array_merge( $input, $inputs ); |
654
|
|
|
} |
655
|
|
|
return $input; |
656
|
|
|
} |
657
|
|
|
|
658
|
|
|
/** |
659
|
|
|
* Get valid input from an input array compared to predefined options |
660
|
|
|
* |
661
|
|
|
* @param array $input |
662
|
|
|
* @param array $options |
663
|
|
|
* @return array |
664
|
|
|
*/ |
665
|
|
|
public static function get_valid_options( $input, $options ) { |
666
|
|
|
// enfore comparison to be string so we do not get unexpected matches |
667
|
|
|
// for cases such as "string without any numbers" == 0 |
668
|
|
|
// in array_search() |
669
|
|
|
$search_options = array_map( 'strval', $options ); |
670
|
|
|
|
671
|
|
|
$valid_input = array(); |
672
|
|
|
foreach ( $input as $raw_value ) { |
673
|
|
|
$index = array_search( strval( $raw_value ), $search_options, true ); |
674
|
|
|
|
675
|
|
|
if ( $index === false ) { |
676
|
|
|
continue; |
677
|
|
|
} |
678
|
|
|
|
679
|
|
|
$valid_input[] = $options[ $index ]; |
680
|
|
|
} |
681
|
|
|
return $valid_input; |
682
|
|
|
} |
683
|
|
|
|
684
|
|
|
/** |
685
|
|
|
* Get an array of active sidebars |
686
|
|
|
* |
687
|
|
|
* @return array |
688
|
|
|
*/ |
689
|
|
|
public static function get_active_sidebars() { |
690
|
|
|
global $wp_registered_sidebars; |
691
|
|
|
|
692
|
|
|
$sidebars = array(); |
693
|
|
|
|
694
|
|
|
foreach ( $wp_registered_sidebars as $sidebar ) { |
695
|
|
|
// Check if we have inactive sidebars |
696
|
|
|
if ( isset( $sidebar['class'] ) && strpos( $sidebar['class'], 'inactive-sidebar' ) !== false ) { |
697
|
|
|
continue; |
698
|
|
|
} |
699
|
|
|
|
700
|
|
|
$sidebars[] = array( |
701
|
|
|
'id' => $sidebar['id'], |
702
|
|
|
'name' => $sidebar['name'], |
703
|
|
|
); |
704
|
|
|
} |
705
|
|
|
|
706
|
|
|
return $sidebars; |
707
|
|
|
} |
708
|
|
|
} |
709
|
|
|
|