acf_duplicate_field()ACF 5.0.0

acf_duplicate_field

Duplicates a field.

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

Возвращает

true|false. True if field was duplicated.

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

acf_duplicate_field( $id, $parent_id );
$id((int|string))
The field ID, key or name.
$parent_id(int)
The new parent ID.

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

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

Код acf_duplicate_field() ACF 5.10.2

function acf_duplicate_field( $id = 0, $parent_id = 0 ) {

	// Get the field.
	$field = acf_get_field( $id );

	// Bail early if field was not found.
	if ( ! $field ) {
		return false;
	}

	// Remove ID to avoid update.
	$field['ID'] = 0;

	// Generate key.
	$keys = acf_get_data( 'generated_keys' );
	if ( isset( $keys[ $field['key'] ] ) ) {
		$field['key'] = $keys[ $field['key'] ];
	} else {
		$field['key'] = uniqid( 'field_' );
	}

	// Set parent.
	if ( $parent_id ) {
		$field['parent'] = $parent_id;
	}

	// Update conditional logic references because field keys have changed.
	if ( $field['conditional_logic'] ) {

		// Loop over groups
		foreach ( $field['conditional_logic'] as $group_i => $group ) {

			// Loop over rules
			foreach ( $group as $rule_i => $rule ) {
				$field['conditional_logic'][ $group_i ][ $rule_i ]['field'] = isset( $keys[ $rule['field'] ] ) ? $keys[ $rule['field'] ] : $rule['field'];
			}
		}
	}

	/**
	 * Filters the $field array after it has been duplicated.
	 *
	 * @date    12/02/2014
	 * @since   5.0.0
	 *
	 * @param   array $field The field array.
	 */
	$field = apply_filters( 'acf/duplicate_field', $field );

	// Update and return.
	return acf_update_field( $field );
}