acf-wp-functions.php
6.37 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
/**
* Returns a WordPress object type.
*
* @date 1/4/20
* @since 5.9.0
*
* @param string $object_type The object type (post, term, user, etc).
* @param string $object_subtype Optional object subtype (post type, taxonomy).
* @return object
*/
function acf_get_object_type( $object_type, $object_subtype = '' ) {
$props = array(
'type' => $object_type,
'subtype' => $object_subtype,
'name' => '',
'label' => '',
'icon' => '',
);
// Set unique identifier as name.
if ( $object_subtype ) {
$props['name'] = "$object_type/$object_subtype";
} else {
$props['name'] = $object_type;
}
// Set label and icon.
switch ( $object_type ) {
case 'post':
if ( $object_subtype ) {
$post_type = get_post_type_object( $object_subtype );
if ( $post_type ) {
$props['label'] = $post_type->labels->name;
$props['icon'] = acf_with_default( $post_type->menu_icon, 'dashicons-admin-post' );
} else {
return false;
}
} else {
$props['label'] = __( 'Posts', 'acf' );
$props['icon'] = 'dashicons-admin-post';
}
break;
case 'term':
if ( $object_subtype ) {
$taxonomy = get_taxonomy( $object_subtype );
if ( $taxonomy ) {
$props['label'] = $taxonomy->labels->name;
} else {
return false;
}
} else {
$props['label'] = __( 'Taxonomies', 'acf' );
}
$props['icon'] = 'dashicons-tag';
break;
case 'attachment':
$props['label'] = __( 'Attachments', 'acf' );
$props['icon'] = 'dashicons-admin-media';
break;
case 'comment':
$props['label'] = __( 'Comments', 'acf' );
$props['icon'] = 'dashicons-admin-comments';
break;
case 'widget':
$props['label'] = __( 'Widgets', 'acf' );
$props['icon'] = 'dashicons-screenoptions';
break;
case 'menu':
$props['label'] = __( 'Menus', 'acf' );
$props['icon'] = 'dashicons-admin-appearance';
break;
case 'menu_item':
$props['label'] = __( 'Menu items', 'acf' );
$props['icon'] = 'dashicons-admin-appearance';
break;
case 'user':
$props['label'] = __( 'Users', 'acf' );
$props['icon'] = 'dashicons-admin-users';
break;
case 'option':
$props['label'] = __( 'Options', 'acf' );
$props['icon'] = 'dashicons-admin-generic';
break;
case 'block':
$props['label'] = __( 'Blocks', 'acf' );
$props['icon'] = acf_version_compare( 'wp', '>=', '5.5' ) ? 'dashicons-block-default' : 'dashicons-layout';
break;
default:
return false;
}
// Convert to object.
$object = (object) $props;
/**
* Filters the object type.
*
* @date 6/4/20
* @since 5.9.0
*
* @param object $object The object props.
* @param string $object_type The object type (post, term, user, etc).
* @param string $object_subtype Optional object subtype (post type, taxonomy).
*/
return apply_filters( 'acf/get_object_type', $object, $object_type, $object_subtype );
}
/**
* Decodes a post_id value such as 1 or "user_1" into an array containing the type and ID.
*
* @date 25/1/19
* @since 5.7.11
*
* @param (int|string) $post_id The post id.
* @return array
*/
function acf_decode_post_id( $post_id = 0 ) {
$type = '';
$id = 0;
// Interpret numeric value (123).
if ( is_numeric( $post_id ) ) {
$type = 'post';
$id = $post_id;
// Interpret string value ("user_123" or "option").
} elseif ( is_string( $post_id ) ) {
$i = strrpos( $post_id, '_' );
if ( $i > 0 ) {
$type = substr( $post_id, 0, $i );
$id = substr( $post_id, $i + 1 );
} else {
$type = $post_id;
$id = '';
}
// Handle incorrect param type.
} else {
return compact( 'type', 'id' );
}
// Validate props based on param format.
$format = $type . '_' . ( is_numeric( $id ) ? '%d' : '%s' );
switch ( $format ) {
case 'post_%d':
$type = 'post';
$id = absint( $id );
break;
case 'term_%d':
$type = 'term';
$id = absint( $id );
break;
case 'attachment_%d':
$type = 'post';
$id = absint( $id );
break;
case 'comment_%d':
$type = 'comment';
$id = absint( $id );
break;
case 'widget_%s':
case 'widget_%d':
$type = 'option';
$id = $post_id;
break;
case 'menu_%d':
$type = 'term';
$id = absint( $id );
break;
case 'menu_item_%d':
$type = 'post';
$id = absint( $id );
break;
case 'user_%d':
$type = 'user';
$id = absint( $id );
break;
case 'block_%s':
case 'block_%d':
$type = 'block';
$id = $post_id;
break;
case 'option_%s':
$type = 'option';
$id = $post_id;
break;
case 'blog_%d':
case 'site_%d':
// Allow backwards compatibility for custom taxonomies.
$type = taxonomy_exists( $type ) ? 'term' : 'blog';
$id = absint( $id );
break;
default:
// Check for taxonomy name.
if ( taxonomy_exists( $type ) && is_numeric( $id ) ) {
$type = 'term';
$id = absint( $id );
break;
}
// Treat unknown post_id format as an option.
$type = 'option';
$id = $post_id;
break;
}
/**
* Filters the decoded post_id information.
*
* @date 25/1/19
* @since 5.7.11
*
* @param array $props An array containing "type" and "id" information.
* @param (int|string) $post_id The post id.
*/
return apply_filters( 'acf/decode_post_id', compact( 'type', 'id' ), $post_id );
}
/**
* Determine the REST base for a post type or taxonomy object. Note that this is not intended for use
* with term or post objects but is, instead, to be used with the underlying WP_Post_Type and WP_Taxonomy
* instances.
*
* @param WP_Post_Type|WP_Taxonomy $type_object
* @return string|null
*/
function acf_get_object_type_rest_base( $type_object ) {
if ( $type_object instanceof WP_Post_Type || $type_object instanceof WP_Taxonomy ) {
return ! empty( $type_object->rest_base ) ? $type_object->rest_base : $type_object->name;
}
return null;
}
/**
* Extract the ID of a given object/array. This supports all expected types handled by our update_fields() and
* load_fields() callbacks.
*
* @param WP_Post|WP_User|WP_Term|array $object
* @return int|mixed|null
*/
function acf_get_object_id( $object ) {
if ( is_object( $object ) ) {
switch ( get_class( $object ) ) {
case WP_User::class:
case WP_Post::class:
return (int) $object->ID;
case WP_Term::class:
return (int) $object->term_id;
}
} elseif ( isset( $object['id'] ) ) {
return (int) $object['id'];
} elseif ( isset( $object['ID'] ) ) {
return (int) $object['ID'];
}
return null;
}