PluginProbe
Advanced Custom Fields (ACF®) / 6.2.8
Advanced Custom Fields (ACF®) v6.2.8
6.8.9 6.8.8 6.8.7 6.8.6 6.8.5 6.8.4 6.8.3 6.8.2 6.8.1 5.8.5 5.8.6 5.8.7 5.8.8 5.8.9 5.9.0 5.9.1 5.9.2 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9 6.0.0 All 230 releases
advanced-custom-fields / includes / acf-wp-functions.php
acf-wp-functions.php
266 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Returns a WordPress object type.
5 *
6 * @date 1/4/20
7 * @since 5.9.0
8 *
9 * @param string $object_type The object type (post, term, user, etc).
10 * @param string $object_subtype Optional object subtype (post type, taxonomy).
11 * @return object
12 */
13 function acf_get_object_type( $object_type, $object_subtype = '' ) {
14 $props = array(
15 'type' => $object_type,
16 'subtype' => $object_subtype,
17 'name' => '',
18 'label' => '',
19 'icon' => '',
20 );
21
22 // Set unique identifier as name.
23 if ( $object_subtype ) {
24 $props['name'] = "$object_type/$object_subtype";
25 } else {
26 $props['name'] = $object_type;
27 }
28
29 // Set label and icon.
30 switch ( $object_type ) {
31 case 'post':
32 if ( $object_subtype ) {
33 $post_type = get_post_type_object( $object_subtype );
34 if ( $post_type ) {
35 $props['label'] = $post_type->labels->name;
36 if ( is_string( $post_type->menu_icon ) && ! preg_match( '/^[\w\-]+$/', $post_type->menu_icon ) ) {
37 $post_type->menu_icon = false;
38 }
39 $props['icon'] = acf_with_default( $post_type->menu_icon, 'dashicons-admin-post' );
40 } else {
41 return false;
42 }
43 } else {
44 $props['label'] = __( 'Posts', 'acf' );
45 $props['icon'] = 'dashicons-admin-post';
46 }
47 break;
48 case 'term':
49 if ( $object_subtype ) {
50 $taxonomy = get_taxonomy( $object_subtype );
51 if ( $taxonomy ) {
52 $props['label'] = $taxonomy->labels->name;
53 } else {
54 return false;
55 }
56 } else {
57 $props['label'] = __( 'Taxonomies', 'acf' );
58 }
59 $props['icon'] = 'dashicons-tag';
60 break;
61 case 'attachment':
62 $props['label'] = __( 'Attachments', 'acf' );
63 $props['icon'] = 'dashicons-admin-media';
64 break;
65 case 'comment':
66 $props['label'] = __( 'Comments', 'acf' );
67 $props['icon'] = 'dashicons-admin-comments';
68 break;
69 case 'widget':
70 $props['label'] = __( 'Widgets', 'acf' );
71 $props['icon'] = 'dashicons-screenoptions';
72 break;
73 case 'menu':
74 $props['label'] = __( 'Menus', 'acf' );
75 $props['icon'] = 'dashicons-admin-appearance';
76 break;
77 case 'menu_item':
78 $props['label'] = __( 'Menu items', 'acf' );
79 $props['icon'] = 'dashicons-admin-appearance';
80 break;
81 case 'user':
82 $props['label'] = __( 'Users', 'acf' );
83 $props['icon'] = 'dashicons-admin-users';
84 break;
85 case 'option':
86 $props['label'] = __( 'Options', 'acf' );
87 $props['icon'] = 'dashicons-admin-generic';
88 break;
89 case 'block':
90 $props['label'] = __( 'Blocks', 'acf' );
91 $props['icon'] = 'dashicons-block-default';
92 break;
93 default:
94 return false;
95 }
96
97 // Convert to object.
98 $object = (object) $props;
99
100 /**
101 * Filters the object type.
102 *
103 * @date 6/4/20
104 * @since 5.9.0
105 *
106 * @param object $object The object props.
107 * @param string $object_type The object type (post, term, user, etc).
108 * @param string $object_subtype Optional object subtype (post type, taxonomy).
109 */
110 return apply_filters( 'acf/get_object_type', $object, $object_type, $object_subtype );
111 }
112
113 /**
114 * Decodes a post_id value such as 1 or "user_1" into an array containing the type and ID.
115 *
116 * @date 25/1/19
117 * @since 5.7.11
118 *
119 * @param (int|string) $post_id The post id.
120 * @return array
121 */
122 function acf_decode_post_id( $post_id = 0 ) {
123 $type = '';
124 $id = 0;
125
126 // Interpret numeric value (123).
127 if ( is_numeric( $post_id ) ) {
128 $type = 'post';
129 $id = $post_id;
130
131 // Interpret string value ("user_123" or "option").
132 } elseif ( is_string( $post_id ) ) {
133 $i = strrpos( $post_id, '_' );
134 if ( $i > 0 ) {
135 $type = substr( $post_id, 0, $i );
136 $id = substr( $post_id, $i + 1 );
137 } else {
138 $type = $post_id;
139 $id = '';
140 }
141
142 // Handle incorrect param type.
143 } else {
144 return compact( 'type', 'id' );
145 }
146
147 // Validate props based on param format.
148 $format = $type . '_' . ( is_numeric( $id ) ? '%d' : '%s' );
149 switch ( $format ) {
150 case 'post_%d':
151 $type = 'post';
152 $id = absint( $id );
153 break;
154 case 'term_%d':
155 $type = 'term';
156 $id = absint( $id );
157 break;
158 case 'attachment_%d':
159 $type = 'post';
160 $id = absint( $id );
161 break;
162 case 'comment_%d':
163 $type = 'comment';
164 $id = absint( $id );
165 break;
166 case 'widget_%s':
167 case 'widget_%d':
168 $type = 'option';
169 $id = $post_id;
170 break;
171 case 'menu_%d':
172 $type = 'term';
173 $id = absint( $id );
174 break;
175 case 'menu_item_%d':
176 $type = 'post';
177 $id = absint( $id );
178 break;
179 case 'user_%d':
180 $type = 'user';
181 $id = absint( $id );
182 break;
183 case 'block_%s':
184 case 'block_%d':
185 $type = 'block';
186 $id = $post_id;
187 break;
188 case 'option_%s':
189 $type = 'option';
190 $id = $post_id;
191 break;
192 case 'blog_%d':
193 case 'site_%d':
194 // Allow backwards compatibility for custom taxonomies.
195 $type = taxonomy_exists( $type ) ? 'term' : 'blog';
196 $id = absint( $id );
197 break;
198 default:
199 // Check for taxonomy name.
200 if ( taxonomy_exists( $type ) && is_numeric( $id ) ) {
201 $type = 'term';
202 $id = absint( $id );
203 break;
204 }
205
206 // Treat unknown post_id format as an option.
207 $type = 'option';
208 $id = $post_id;
209 break;
210 }
211
212 /**
213 * Filters the decoded post_id information.
214 *
215 * @date 25/1/19
216 * @since 5.7.11
217 *
218 * @param array $props An array containing "type" and "id" information.
219 * @param (int|string) $post_id The post id.
220 */
221 return apply_filters( 'acf/decode_post_id', compact( 'type', 'id' ), $post_id );
222 }
223
224 /**
225 * Determine the REST base for a post type or taxonomy object. Note that this is not intended for use
226 * with term or post objects but is, instead, to be used with the underlying WP_Post_Type and WP_Taxonomy
227 * instances.
228 *
229 * @param WP_Post_Type|WP_Taxonomy $type_object
230 * @return string|null
231 */
232 function acf_get_object_type_rest_base( $type_object ) {
233 if ( $type_object instanceof WP_Post_Type || $type_object instanceof WP_Taxonomy ) {
234 return ! empty( $type_object->rest_base ) ? $type_object->rest_base : $type_object->name;
235 }
236
237 return null;
238 }
239
240 /**
241 * Extract the ID of a given object/array. This supports all expected types handled by our update_fields() and
242 * load_fields() callbacks.
243 *
244 * @param WP_Post|WP_User|WP_Term|WP_Comment|array $object
245 * @return integer|mixed|null
246 */
247 function acf_get_object_id( $object ) {
248 if ( is_object( $object ) ) {
249 switch ( get_class( $object ) ) {
250 case WP_User::class:
251 case WP_Post::class:
252 return (int) $object->ID;
253 case WP_Term::class:
254 return (int) $object->term_id;
255 case WP_Comment::class:
256 return (int) $object->comment_ID;
257 }
258 } elseif ( isset( $object['id'] ) ) {
259 return (int) $object['id'];
260 } elseif ( isset( $object['ID'] ) ) {
261 return (int) $object['ID'];
262 }
263
264 return null;
265 }
266