ACF_Post_Type::import_cptui_post_typepublicACF 6.1

Imports a post type from CPTUI.

Метод класса: ACF_Post_Type{}

Хуков нет.

Возвращает

Массив.

Использование

$ACF_Post_Type = new ACF_Post_Type();
$ACF_Post_Type->import_cptui_post_type( $args );
$args(массив) (обязательный)
Arguments from CPTUI.

Список изменений

С версии 6.1 Введена.

Код ACF_Post_Type::import_cptui_post_type() ACF 6.4.2

public function import_cptui_post_type( $args ) {
	$acf_args = $this->get_settings_array();

	// Convert string boolean values to proper booleans.
	foreach ( $args as $key => $value ) {
		if ( in_array( $value, array( 'true', 'false' ), true ) ) {
			$args[ $key ] = filter_var( $value, FILTER_VALIDATE_BOOLEAN );
		}
	}

	if ( isset( $args['name'] ) ) {
		$acf_args['post_type'] = (string) $args['name'];
		unset( $args['name'] );
	}

	if ( isset( $args['labels'] ) ) {
		$acf_args['labels'] = array_merge( $acf_args['labels'], $args['labels'] );
		unset( $args['labels'] );
	}

	// ACF uses "name" as title, and stores in labels array.
	if ( isset( $args['label'] ) ) {
		$acf_args['title']          = (string) $args['label'];
		$acf_args['labels']['name'] = (string) $args['label'];
		unset( $args['label'] );
	}

	if ( isset( $args['singular_label'] ) ) {
		$acf_args['labels']['singular_name'] = (string) $args['singular_label'];
		unset( $args['singular_label'] );
	}

	if ( isset( $args['show_in_menu_string'] ) ) {
		$acf_args['admin_menu_parent'] = (string) $args['show_in_menu_string'];
		unset( $args['show_in_menu_string'] );
	}

	if ( isset( $args['rewrite'] ) ) {
		$rewrite = (bool) $args['rewrite'];

		if ( ! $rewrite ) {
			$acf_args['rewrite']['permalink_rewrite'] = 'no_permalink';
		} elseif ( ! empty( $args['rewrite_slug'] ) ) {
			$acf_args['rewrite']['permalink_rewrite'] = 'custom_permalink';
		} else {
			$acf_args['rewrite']['permalink_rewrite'] = 'post_type_key';
		}

		unset( $args['rewrite'] );
	}

	if ( isset( $args['rewrite_slug'] ) ) {
		$acf_args['rewrite']['slug'] = (string) $args['rewrite_slug'];
		unset( $args['rewrite_slug'] );
	}

	if ( isset( $args['rewrite_withfront'] ) ) {
		$acf_args['rewrite']['with_front'] = (bool) $args['rewrite_withfront'];
		unset( $args['rewrite_withfront'] );
	}

	// TODO: Investigate CPTUI usage of with_feeds, pages settings.
	// ACF handles capability type differently.
	if ( isset( $args['capability_type'] ) ) {
		if ( 'post' !== trim( $args['capability_type'] ) ) {
			$acf_args['rename_capabilities'] = true;

			$capabilities = explode( ',', $args['capability_type'] );
			$capabilities = array_map( 'trim', $capabilities );

			$acf_args['singular_capability_name'] = $capabilities[0];

			if ( count( $capabilities ) > 1 ) {
				$acf_args['plural_capability_name'] = $capabilities[1];
			}
		}

		unset( $args['capability_type'] );
	}

	// ACF names the has_archive slug differently.
	if ( isset( $args['has_archive_string'] ) ) {
		$acf_args['has_archive_slug'] = (string) $args['has_archive_string'];
		unset( $args['has_archive_string'] );
	}

	// ACF handles the query var and query var slug/name differently.
	if ( isset( $args['query_var'] ) ) {
		if ( ! $args['query_var'] ) {
			$acf_args['query_var'] = 'none';
		} elseif ( ! empty( $args['query_var_slug'] ) ) {
			$acf_args['query_var'] = 'custom_query_var';
		} else {
			$acf_args['query_var'] = 'post_type_key';
		}

		unset( $args['query_var'] );
	}

	if ( isset( $args['query_var_slug'] ) ) {
		$acf_args['query_var_name'] = (string) $args['query_var_slug'];
		unset( $args['query_var_slug'] );
	}

	$acf_args = wp_parse_args( $args, $acf_args );

	// ACF doesn't yet handle custom supports, so we're tacking onto the regular supports.
	if ( isset( $args['custom_supports'] ) ) {
		$custom_supports = explode( ',', (string) $args['custom_supports'] );
		$custom_supports = array_filter( array_map( 'trim', $custom_supports ) );

		if ( ! empty( $custom_supports ) ) {
			$acf_args['supports'] = array_merge( $acf_args['supports'], $custom_supports );
		}

		unset( $acf_args['custom_supports'] );
	}

	$acf_args['key']                    = uniqid( 'post_type_' );
	$acf_args['advanced_configuration'] = true;
	$acf_args['import_source']          = 'cptui';
	$acf_args['import_date']            = time();

	$existing_post_types = acf_get_acf_post_types();

	foreach ( $existing_post_types as $existing_post_type ) {
		// Post type already exists, so we need to update rather than import.
		if ( $acf_args['post_type'] === $existing_post_type['post_type'] ) {
			$acf_args        = $this->prepare_post_for_import( $acf_args );
			$acf_args['ID']  = $existing_post_type['ID'];
			$acf_args['key'] = $existing_post_type['key'];
			return $this->update_post( $acf_args );
		}
	}

	// Import the post normally.
	return $this->import_post( $acf_args );
}