acf_remove_disallowed_post_ids() │ ACF 6.8.11

Removes newly selected post IDs that the current user isn't allowed to add to a relational field.

Only runs while ACF is saving submitted values. A new ID must belong to an existing post that the current user can read. IDs already stored in the field, in any Repeater or Flexible Content row, are kept, so a selection the user can't read isn't lost when they save.

Stored values are matched by field key. A value stored under a different key, such as by a field that was later recreated or imported with a new key, isn't matched, so a user who can't read its posts drops them when they save it.

Хуков нет.

Возвращает

mixed. The value with disallowed IDs removed.

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

acf_remove_disallowed_post_ids( $value, $post_id, $field );
$value(разное) (обязательный)
The normalized field value: an ID, or an array of IDs.
$post_id(int|строка) (обязательный)
The ACF post ID being saved.
$field(массив) (обязательный)
The field array.

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

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

Код acf_remove_disallowed_post_ids() ACF 6.8.11

function acf_remove_disallowed_post_ids( $value, $post_id, $field ) {
	if ( empty( $value ) || ! _acf_is_checking_submitted_post_ids() ) {
		return $value;
	}

	$snapshot   = _acf_get_stored_post_ids_snapshot( $post_id );
	$stored_ids = isset( $snapshot['ids'][ $field['key'] ] ) ? $snapshot['ids'][ $field['key'] ] : array();

	// A seamless Clone saved by a form references the cloned field's key, but the REST API saves it under the
	// Clone's prefixed key.
	if ( ! empty( $field['__key'] ) && isset( $snapshot['ids'][ $field['__key'] ] ) ) {
		$stored_ids = $stored_ids + $snapshot['ids'][ $field['__key'] ];
	}

	// Values saved without a field key reference, such as by an import, can only be found by name. This reads
	// the stored value directly, before this field is updated, so `acf/load_value` filters can't add to it.
	if ( ! isset( $snapshot['names'][ $field['name'] ] ) ) {
		$legacy_ids = acf_get_array( acf_get_metadata_by_field( _acf_get_stored_values_post_id( $post_id ), $field ) );
		$stored_ids = $stored_ids + array_fill_keys( array_map( 'strval', array_filter( $legacy_ids, 'is_numeric' ) ), true );
	}

	$is_allowed = function ( $id ) use ( $stored_ids, $field ) {
		// Page Link URLs aren't post IDs, so they're kept. Other fields only store post IDs, and other code,
		// such as bidirectional sync, decodes values like `post_42` or `user_3` as objects to update.
		if ( ! is_numeric( $id ) ) {
			return 'page_link' === $field['type'];
		}

		// Other code, such as bidirectional sync, converts negative IDs to the positive post ID.
		if ( (int) $id <= 0 ) {
			return false;
		}

		// Only check new post IDs. Existing selections are kept.
		if ( isset( $stored_ids[ (string) $id ] ) ) {
			return true;
		}

		return acf_current_user_can_read_post( (int) $id, $field );
	};

	if ( ! is_array( $value ) ) {
		return $is_allowed( $value ) ? $value : '';
	}

	$value = array_filter( $value, $is_allowed );

	return acf_is_sequential_array( $value ) ? array_values( $value ) : $value;
}