acf-wp-functions.php 6.4 KB

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