ACF_Post_Type::get_post_type_args │ public │ ACF 6.1
Parses ACF post type settings and returns an array of post type args that can be easily handled by register_post_type().
Omits settings that line up with the WordPress defaults to reduce the size of the array passed to register_post_type(), which might be exported.
Метод класса: ACF_Post_Type{}
Хуки из метода
Возвращает
Массив.
Использование
$ACF_Post_Type = new ACF_Post_Type(); $ACF_Post_Type->get_post_type_args( $post, $escape_labels );
- $post(массив) (обязательный)
- The main ACF post type settings array.
- $escape_labels(true|false)
- Determines if the label values should be escaped.
По умолчанию:true
Список изменений
| С версии 6.1 | Введена. |
Код ACF_Post_Type::get_post_type_args() ACF Post Type::get post type args ACF 6.4.2
public function get_post_type_args( $post, $escape_labels = true ) {
$args = array();
// Make sure any provided labels are escaped strings and not empty.
$labels = array_filter( $post['labels'] );
$labels = array_map( 'strval', $labels );
if ( $escape_labels ) {
$labels = array_map( 'esc_html', $labels );
}
if ( ! empty( $labels ) ) {
$args['labels'] = $labels;
}
// Description is an optional string.
if ( ! empty( $post['description'] ) ) {
$args['description'] = (string) $post['description'];
}
// ACF requires the public setting to decide other settings.
$args['public'] = ! empty( $post['public'] );
// WordPress and ACF both default to false, so this can be omitted if still false.
if ( ! empty( $post['hierarchical'] ) ) {
$args['hierarchical'] = true;
}
// WordPress defaults to the opposite of $args['public'].
$exclude_from_search = (bool) $post['exclude_from_search'];
if ( $exclude_from_search === $args['public'] ) {
$args['exclude_from_search'] = $exclude_from_search;
}
// WordPress defaults to the same as $args['public'].
$publicly_queryable = (bool) $post['publicly_queryable'];
if ( $publicly_queryable !== $args['public'] ) {
$args['publicly_queryable'] = $publicly_queryable;
}
// WordPress defaults to the same as $args['public'].
$show_ui = (bool) $post['show_ui'];
if ( $show_ui !== $args['public'] ) {
$args['show_ui'] = $show_ui;
}
// WordPress defaults to the same as $args['show_ui'], can be string or boolean.
$show_in_menu = (bool) $post['show_in_menu'];
if ( $show_in_menu !== $show_ui ) {
$args['show_in_menu'] = $show_in_menu;
}
// WordPress also accepts a string for $args['show_in_menu'] to change the menu parent.
if ( $show_in_menu && ! empty( $post['admin_menu_parent'] ) ) {
$args['show_in_menu'] = (string) $post['admin_menu_parent'];
}
// WordPress defaults to the same as $args['public'].
$show_in_nav_menus = (bool) $post['show_in_nav_menus'];
if ( $show_in_nav_menus !== $args['public'] ) {
$args['show_in_nav_menus'] = $show_in_nav_menus;
}
// WordPress defaults to the same as $show_in_menu.
$show_in_admin_bar = (bool) $post['show_in_admin_bar'];
if ( $show_in_admin_bar !== $show_in_menu ) {
$args['show_in_admin_bar'] = $show_in_admin_bar;
}
// ACF defaults to true, but can be overridden.
$show_in_rest = (bool) $post['show_in_rest'];
$args['show_in_rest'] = $show_in_rest;
// WordPress defaults to $post_type.
$rest_base = (string) $post['rest_base'];
if ( ! empty( $rest_base ) && $rest_base !== $post['post_type'] ) {
$args['rest_base'] = $rest_base;
}
// WordPress defaults to "wp/v2".
$rest_namespace = (string) $post['rest_namespace'];
if ( ! empty( $rest_namespace ) && 'wp/v2' !== $rest_namespace ) {
$args['rest_namespace'] = $post['rest_namespace'];
}
// WordPress defaults to "WP_REST_Posts_Controller".
$rest_controller_class = (string) $post['rest_controller_class'];
if ( ! empty( $rest_controller_class ) && 'WP_REST_Posts_Controller' !== $rest_controller_class ) {
$args['rest_controller_class'] = $rest_controller_class;
}
// WordPress defaults to `null` (below the comments menu item).
$menu_position = (int) $post['menu_position'];
if ( $menu_position ) {
$args['menu_position'] = $menu_position;
}
// Set the default for the icon.
$args['menu_icon'] = 'dashicons-admin-post';
// Override that default if a value is provided.
if ( ! empty( $post['menu_icon'] ) ) {
if ( is_string( $post['menu_icon'] ) ) {
$args['menu_icon'] = $post['menu_icon'];
}
if ( is_array( $post['menu_icon'] ) ) {
if ( $post['menu_icon']['type'] === 'media_library' ) {
$args['menu_icon'] = wp_get_attachment_image_url( $post['menu_icon']['value'] );
} else {
$args['menu_icon'] = $post['menu_icon']['value'];
}
}
}
// WordPress defaults to "post" for `$args['capability_type']`, but can also take an array.
$rename_capabilities = (bool) $post['rename_capabilities'];
if ( $rename_capabilities ) {
$singular_capability_name = (string) $post['singular_capability_name'];
$plural_capability_name = (string) $post['plural_capability_name'];
$capability_type = 'post';
if ( ! empty( $singular_capability_name ) && ! empty( $plural_capability_name ) ) {
$capability_type = array( $singular_capability_name, $plural_capability_name );
} elseif ( ! empty( $singular_capability_name ) ) {
$capability_type = $singular_capability_name;
}
if ( $capability_type !== 'post' && $capability_type !== array( 'post', 'posts' ) ) {
$args['capability_type'] = $capability_type;
$args['map_meta_cap'] = true;
}
}
// TODO: We don't handle the `capabilities` arg at the moment, but may in the future.
// WordPress defaults to the "title" and "editor" supports, but none can be provided by passing false (WP 3.5+).
$supports = is_array( $post['supports'] ) ? $post['supports'] : array();
$supports = array_unique( array_filter( array_map( 'strval', $supports ) ) );
if ( empty( $supports ) ) {
$args['supports'] = false;
} else {
$args['supports'] = $supports;
}
// Handle register meta box callbacks safely
if ( ! empty( $post['register_meta_box_cb'] ) ) {
$args['register_meta_box_cb'] = array( $this, 'build_safe_context_for_metabox_cb' );
}
// WordPress doesn't register any default taxonomies.
$taxonomies = $post['taxonomies'];
if ( ! is_array( $taxonomies ) ) {
$taxonomies = (array) $taxonomies;
}
$taxonomies = array_filter( $taxonomies );
if ( ! empty( $taxonomies ) ) {
$args['taxonomies'] = $taxonomies;
}
// WordPress and ACF default to false, true or a string can also be provided.
$has_archive = (bool) $post['has_archive'];
if ( $has_archive ) {
$has_archive_slug = (string) $post['has_archive_slug'];
if ( ! empty( $has_archive_slug ) ) {
$args['has_archive'] = $has_archive_slug;
} else {
$args['has_archive'] = true;
}
}
// The rewrite arg can be a boolean or array of further settings. WordPress and ACF default to true.
$rewrite = (array) $post['rewrite'];
$rewrite_enabled = true;
$rewrite_args = array();
// ACF-specific select, defaults to "post_type_key".
$rewrite['permalink_rewrite'] = isset( $rewrite['permalink_rewrite'] ) ? (string) $rewrite['permalink_rewrite'] : 'post_type_key';
if ( 'no_permalink' === $rewrite['permalink_rewrite'] ) {
$rewrite_enabled = false;
}
// Rewrite slug defaults to $post_type key if custom rewrites are not enabled.
if ( ! empty( $rewrite['slug'] ) && $rewrite['slug'] !== $post['post_type'] && 'custom_permalink' === $rewrite['permalink_rewrite'] ) {
$rewrite_args['slug'] = (string) $rewrite['slug'];
}
// WordPress defaults to true.
if ( isset( $rewrite['with_front'] ) && ! $rewrite['with_front'] && $rewrite_enabled ) {
$rewrite_args['with_front'] = false;
}
// WordPress defaults to value of `$args['has_archive']`.
if ( isset( $rewrite['feeds'] ) && (bool) $rewrite['feeds'] !== $has_archive && $rewrite_enabled ) {
$rewrite_args['feeds'] = (bool) $rewrite['feeds'];
}
// WordPress defaults to true.
if ( isset( $rewrite['pages'] ) && ! $rewrite['pages'] && $rewrite_enabled ) {
$rewrite_args['pages'] = false;
}
// Assemble rewrite args.
if ( ! empty( $rewrite_args ) ) {
$args['rewrite'] = $rewrite_args;
} elseif ( ! $rewrite_enabled ) {
$args['rewrite'] = false;
}
// WordPress and ACF default to $post_type key, a boolean can also be used.
$query_var = (string) $post['query_var'];
if ( 'custom_query_var' === $query_var ) {
$query_var_name = (string) $post['query_var_name'];
if ( ! empty( $query_var_name ) && $query_var_name !== $post['post_type'] ) {
$args['query_var'] = $query_var_name;
}
} elseif ( 'none' === $query_var ) {
$args['query_var'] = false;
}
// WordPress and ACF default to true.
$can_export = (bool) $post['can_export'];
if ( ! $can_export ) {
$args['can_export'] = false;
}
// ACF defaults to false, while WordPress defaults to omitting (deletes only if author support is added).
$args['delete_with_user'] = (bool) $post['delete_with_user'];
return apply_filters( 'acf/post_type/registration_args', $args, $post );
}