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.8.11

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'];

	// Revisions are never a valid back-reference.
	if ( 'post' === $decoded['type'] && wp_is_post_revision( (int) $item_id ) ) {
		return;
	}

	$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 ) );

		// Every selected item is checked, not just new ones, so a back-reference skipped on an earlier
		// save is added once someone who can edit the target saves.
		$additions    = $new_values;
		$subtractions = array_diff( $current_values, $new_values );

		$target_ids = array_filter( array_map( 'intval', $additions ) );
		switch ( $target_prefix ? $target_prefix : 'post' ) {
			case 'post':
				_prime_post_caches( $target_ids, false, true );
				break;
			case 'user':
				cache_users( $target_ids );
				break;
			case 'term':
				_prime_term_caches( $target_ids, true );
				break;
		}

		// 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 );
		}

		$subtractions = array_filter(
			$subtractions,
			function ( $target_id ) use ( $post_id, $field, $target_prefix ) {
				return acf_can_update_bidirectional_target( $target_id, $post_id, $field, $target_prefix, 'remove' );
			}
		);

		acf_set_data( 'acf_doing_bidirectional_update', true );

		try {
			$can_add = array();

			// 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 ) );
					if ( in_array( (string) $item_id, array_map( 'strval', $current_value ), true ) ) {
						continue;
					}

					if ( ! isset( $can_add[ $addition ] ) ) {
						$can_add[ $addition ] = acf_can_update_bidirectional_target( $addition, $post_id, $field, $target_prefix, 'add' );
					}

					if ( $can_add[ $addition ] ) {
						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 );
				}
			}
		} finally {
			acf_set_data( 'acf_doing_bidirectional_update', false );
		}
	}
}