acf_field_gallery::ajax_update_attachment │ public │ ACF 5.0.0

AJAX handler for updating an attachment.

Метод класса: acf_field_gallery{}

Хуки из метода

Возвращает

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

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

$acf_field_gallery = new acf_field_gallery();
$acf_field_gallery->ajax_update_attachment();

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

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

Код acf_field_gallery::ajax_update_attachment() ACF 6.8.11

public function ajax_update_attachment() {
	$args = acf_request_args(
		array(
			'nonce'     => '',
			'field_key' => '',
		)
	);

	if ( ! acf_verify_ajax( $args['nonce'], $args['field_key'], true, 'gallery' ) ) {
		wp_send_json_error();
	}

	// bail early if no attachments
	if ( empty( $_POST['attachments'] ) ) {
		wp_send_json_error();
	}

	// The sidebar's ACF values, saved to each attachment by save_sidebar_field_values().
	$values = isset( $_POST['acf'] ) && is_array( $_POST['acf'] ) ? $_POST['acf'] : null; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Sanitized by acf_save_post() and WP core when saved.

	// The sidebar has no file inputs.
	acf_discard_uploaded_field_values();

	// loop over attachments
	foreach ( $_POST['attachments'] as $id => $changes ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized by WP core when saved.
		$id = (int) $id;

		if ( ! current_user_can( 'edit_post', $id ) ) {
			wp_send_json_error();
		}

		$post = get_post( $id, ARRAY_A );

		if ( 'attachment' != $post['post_type'] ) {
			wp_send_json_error();
		}

		// save post
		$post = $this->apply_sidebar_changes( $post, $changes );
		wp_update_post( $post );

		/** This filter is documented in wp-admin/includes/media.php */
		// - seems off to run this filter AFTER the update_post function, but there is a reason
		// - when placed BEFORE, an empty post_title will be populated by WP
		// - this filter will still allow 3rd party to save extra image data!
		$post = apply_filters( 'attachment_fields_to_save', $post, $changes );

		// save meta
		if ( null !== $values ) {
			$this->save_sidebar_field_values( $id, $values );
		}
	}

	// return
	wp_send_json_success();
}