acf_import_field_group()ACF 5.0.0

acf_import_field_group

Imports a field group into the databse.

Хуки из функции

Возвращает

Массив. The new field group.

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

acf_import_field_group( $field_group );
$field_group(массив) (обязательный)
The field group array.

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

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

Код acf_import_field_group() ACF 6.0.4

function acf_import_field_group( $field_group ) {

	// Disable filters to ensure data is not modified by local, clone, etc.
	$filters = acf_disable_filters();

	// Validate field group (ensures all settings exist).
	$field_group = acf_get_valid_field_group( $field_group );

	// Prepare group for import (modifies settings).
	$field_group = acf_prepare_field_group_for_import( $field_group );

	// Prepare fields for import (modifies settings).
	$fields = acf_prepare_fields_for_import( $field_group['fields'] );

	// Stores a map of field "key" => "ID".
	$ids = array();

	// If the field group has an ID, review and delete stale fields in the database.
	if ( $field_group['ID'] ) {

		// Load database fields.
		$db_fields = acf_prepare_fields_for_import( acf_get_fields( $field_group ) );

		// Generate map of "index" => "key" data.
		$keys = wp_list_pluck( $fields, 'key' );

		// Loop over db fields and delete those who don't exist in $new_fields.
		foreach ( $db_fields as $field ) {

			// Add field data to $ids map.
			$ids[ $field['key'] ] = $field['ID'];

			// Delete field if not in $keys.
			if ( ! in_array( $field['key'], $keys ) ) {
				acf_delete_field( $field['ID'] );
			}
		}
	}

	// When importing a new field group, insert a temporary post and set the field group's ID.
	// This allows fields to be updated before the field group (field group ID is needed for field parent setting).
	if ( ! $field_group['ID'] ) {
		$field_group['ID'] = wp_insert_post( array( 'post_title' => $field_group['key'] ) );
	}

	// Add field group data to $ids map.
	$ids[ $field_group['key'] ] = $field_group['ID'];

	// Loop over and add fields.
	if ( $fields ) {
		foreach ( $fields as $field ) {

			// Replace any "key" references with "ID".
			if ( isset( $ids[ $field['key'] ] ) ) {
				$field['ID'] = $ids[ $field['key'] ];
			}
			if ( isset( $ids[ $field['parent'] ] ) ) {
				$field['parent'] = $ids[ $field['parent'] ];
			}

			// Save field.
			$field = acf_update_field( $field );

			// Add field data to $ids map for children.
			$ids[ $field['key'] ] = $field['ID'];
		}
	}

	// Save field group.
	$field_group = acf_update_field_group( $field_group );

	// Enable filters again.
	acf_enable_filters( $filters );

	/**
	 * Fires immediately after a field_group has been imported.
	 *
	 * @date    12/02/2014
	 * @since   5.0.0
	 *
	 * @param   array $field_group The field_group array.
	 */
	do_action( 'acf/import_field_group', $field_group );

	// return new field group.
	return $field_group;
}