acf_can_update_bidirectional_target() │ ACF 6.8.11

Checks whether a bidirectional update may add or remove a reference on a target post, user or term.

Targets that don't exist, or aren't the object type this field links to, are always skipped. When adding a reference while ACF is saving submitted values, through acf_save_post() or the REST API, the current user must also be able to edit the target post, assign the target term, or edit the target user. Users can also be linked from a post by anyone who can edit others' posts of that post type, as they could set the user as the post's author. Removals only take the origin's own ID out of the target, so they aren't permission checked. Values saved by code with update_field(), or from a trusted context (see acf_is_trusted_save_context()), aren't checked.

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

Возвращает

boolean.

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

acf_can_update_bidirectional_target( $target_id, $post_id, $field, $target_prefix, $action );
$target_id(int|строка) (обязательный)
The ACF encoded target post, user or term ID.
$post_id(int|строка) (обязательный)
The ACF encoded origin post, user or term ID.
$field(массив) (обязательный)
The field being updated on the origin.
$target_prefix(строка|false)
The ACF prefix for the target object type, or false for posts.
По умолчанию: false
$action(строка)
Either 'add' or 'remove'.
По умолчанию: 'add'

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

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

Код acf_can_update_bidirectional_target() ACF 6.8.11

function acf_can_update_bidirectional_target( $target_id, $post_id, $field, $target_prefix = false, $action = 'add' ) {
	$decoded    = acf_decode_post_id( $target_id );
	$id         = (int) $decoded['id'];
	$can_update = false;

	if ( $id && $decoded['type'] === ( $target_prefix ? $target_prefix : 'post' ) ) {
		switch ( $decoded['type'] ) {
			case 'post':
				$exists = (bool) get_post( $id );
				$cap    = 'edit_post';
				break;
			case 'user':
				$exists = (bool) get_userdata( $id );
				$cap    = 'edit_user';
				break;
			case 'term':
				// Terms that still share an ID across taxonomies need the taxonomy to be found.
				$term   = get_term( $id, $field['taxonomy'] ?? '' );
				$exists = $term instanceof WP_Term;
				$cap    = 'assign_term';

				// Core's assign_term check looks the term up without a taxonomy, so it can't resolve shared IDs.
				if ( $exists ) {
					$unqualified = get_term( $id );
					if ( ! $unqualified instanceof WP_Term || $unqualified->taxonomy !== $term->taxonomy ) {
						$cap = get_taxonomy( $term->taxonomy )->cap->assign_terms;
					}
				}
				break;
			default:
				$exists = false;
				$cap    = '';
		}

		$needs_permission = 'add' === $action && acf_get_data( 'acf_saving_submitted_values' );
		$can_update       = $exists && ( ! $needs_permission || current_user_can( $cap, $id ) );

		if ( ! $can_update && $exists && 'user' === $decoded['type'] ) {
			// On multisite, only edit_user may reach users outside this site and super admins.
			$is_site_user = ! is_multisite() || ( is_user_member_of_blog( $id ) && ! is_super_admin( $id ) );

			// As with setting a post's author, anyone who can edit others' posts of the origin's type may link users of this site.
			$origin     = acf_decode_post_id( $post_id );
			$post_type  = 'post' === $origin['type'] ? get_post_type_object( (string) get_post_type( $origin['id'] ) ) : null;
			$can_update = $is_site_user && $post_type && current_user_can( $post_type->cap->edit_others_posts );

			// On multisite, edit_user is limited to super admins, so site admins may also link users of their own site.
			if ( ! $can_update && is_multisite() ) {
				$can_update = $is_site_user && current_user_can( 'promote_users' );
			}
		}
	}

	/**
	 * Filters whether a bidirectional update may add or remove a reference on a target.
	 *
	 * @since 6.8.11
	 *
	 * @param boolean        $can_update Whether the target may be updated.
	 * @param integer|string $target_id  The ACF encoded target post, user or term ID.
	 * @param integer|string $post_id    The ACF encoded origin post, user or term ID.
	 * @param array          $field      The field being updated on the origin.
	 * @param string         $action     Either 'add' or 'remove'.
	 */
	return (bool) apply_filters( 'acf/bidirectional/can_update_target', $can_update, $target_id, $post_id, $field, $action );
}