acf_update_bidirectional_values()ACF 6.2

Process updating bidirectional fields.

Хуков нет.

Возвращает

null. Ничего (null).

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

acf_update_bidirectional_values( $target_item_ids, $post_id, $field, $target_prefix );
$target_item_ids(массив) (обязательный)
The post, user or term IDs which should be updated with the origin item ID.
$post_id(int|строка) (обязательный)
The ACF encoded origin post, user or term ID.
$field(массив) (обязательный)
The field being updated on the origin post, user or term ID.
$target_prefix(строка|false)
The ACF prefix for a post, user or term ID required for the update_field call for this field type.
По умолчанию: false

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

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

Код acf_update_bidirectional_values() ACF 6.4.2

function acf_update_bidirectional_values( $target_item_ids, $post_id, $field, $target_prefix = false ) {

	// Bail early if we're already updating a bidirectional relationship to prevent recursion.
	if ( acf_get_data( 'acf_doing_bidirectional_update' ) ) {
		return;
	}

	// Support disabling bidirectionality globally.
	if ( ! acf_get_setting( 'enable_bidirection' ) ) {
		return;
	}

	if ( empty( $field['bidirectional'] ) || empty( $field['bidirectional_target'] ) ) {
		return;
	}

	$decoded            = acf_decode_post_id( $post_id );
	$item_id            = $decoded['id'];
	$valid_target_types = acf_get_valid_bidirectional_target_types( $decoded['type'] );
	$valid_targets      = array();

	foreach ( $field['bidirectional_target'] as $target_field ) {
		$target_field_object = get_field_object( $target_field );
		if ( empty( $target_field_object ) || ! is_array( $target_field_object ) ) {
			continue;
		}
		if ( in_array( $target_field_object['type'], $valid_target_types, true ) ) {
			$valid_targets[] = $target_field;
		}
	}

	if ( ! empty( $valid_targets ) ) {

		// Get current values for this field.
		$current_values = array_filter( acf_get_array( get_field( $field['key'], $post_id, false ) ) );
		$new_values     = array_filter( acf_get_array( $target_item_ids ) );

		$additions    = array_diff( $new_values, $current_values );
		$subtractions = array_diff( $current_values, $new_values );

		// Prefix additions and subtractions for destinations which aren't posts.
		if ( ! empty( $target_prefix ) ) {
			$mapper       = function ( $v ) use ( $target_prefix ) {
				return $target_prefix . '_' . $v;
			};
			$additions    = array_map( $mapper, $additions );
			$subtractions = array_map( $mapper, $subtractions );
		}

		acf_set_data( 'acf_doing_bidirectional_update', true );

		// Loop over each target, processing additions and removals.
		foreach ( $valid_targets as $target_field ) {
			foreach ( $additions as $addition ) {
				$current_value = acf_get_array( get_field( $target_field, $addition, false ) );
				update_field( $target_field, array_unique( array_merge( $current_value, array( $item_id ) ) ), $addition );
			}

			foreach ( $subtractions as $subtraction ) {
				$current_value = acf_get_array( get_field( $target_field, $subtraction, false ) );
				update_field( $target_field, array_unique( array_diff( $current_value, array( $item_id ) ) ), $subtraction );
			}
		}

		acf_set_data( 'acf_doing_bidirectional_update', false );
	}
}