acf_admin_field_group::ajax_move_fieldpublicACF 5.0.0

Moves fields between field groups via AJAX.

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

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

Возвращает

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

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

$acf_admin_field_group = new acf_admin_field_group();
$acf_admin_field_group->ajax_move_field();

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

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

Код acf_admin_field_group::ajax_move_field() ACF 6.4.2

public function ajax_move_field() {
	// Disable filters to ensure ACF loads raw data from DB.
	acf_disable_filters();

	// phpcs:disable WordPress.Security.NonceVerification.Missing
	$args = acf_parse_args(
		$_POST,
		array(
			'nonce'          => '',
			'post_id'        => 0,
			'field_id'       => 0,
			'field_group_id' => 0,
		)
	);
	// phpcs:enable WordPress.Security.NonceVerification.Missing

	// Verify nonce.
	if ( ! wp_verify_nonce( $args['nonce'], 'acf_nonce' ) ) {
		die();
	}

	// Verify user capability.
	if ( ! acf_current_user_can_admin() ) {
		die();
	}

	// Move the field if the user has confirmed.
	if ( $args['field_id'] && $args['field_group_id'] ) {
		$field           = acf_get_field( $args['field_id'] );
		$old_field_group = acf_get_field_group( $args['post_id'] );
		$new_field_group = acf_get_field_group( $args['field_group_id'] );

		// Update the field parent and remove conditional logic.
		$field['parent']            = $new_field_group['ID'];
		$field['conditional_logic'] = 0;

		// Update the field in the database.
		acf_update_field( $field );

		// Fire `acf/update_field_group` action hook so JSON can sync if necessary.
		do_action( 'acf/update_field_group', $old_field_group );
		do_action( 'acf/update_field_group', $new_field_group );

		// Output HTML.
		$link = '<a href="' . admin_url( 'post.php?post=' . $new_field_group['ID'] . '&action=edit' ) . '" target="_blank">' . esc_html( $new_field_group['title'] ) . '</a>';

		echo '' .
			'<p><strong>' . esc_html__( 'Move Complete.', 'acf' ) . '</strong></p>' .
			'<p>' . sprintf(
				/* translators: Confirmation message once a field has been moved to a different field group. */
				acf_punctify( __( 'The %1$s field can now be found in the %2$s field group', 'acf' ) ),
				esc_html( $field['label'] ),
				$link  //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			) . '</p>' .
			'<a href="#" class="button button-primary acf-close-popup">' . esc_html__( 'Close Modal', 'acf' ) . '</a>';
		die();
	}

	// Get all field groups.
	$field_groups = acf_get_field_groups();
	$choices      = array();

	if ( ! empty( $field_groups ) ) {
		foreach ( $field_groups as $field_group ) {
			// Bail early if no ID.
			if ( ! $field_group['ID'] ) {
				continue;
			}

			// Bail early if is current.
			if ( $field_group['ID'] == $args['post_id'] ) {
				continue;
			}

			$choices[ $field_group['ID'] ] = $field_group['title'];
		}
	}

	// Render options.
	$field = acf_get_valid_field(
		array(
			'type'       => 'select',
			'name'       => 'acf_field_group',
			'choices'    => $choices,
			'aria-label' => __( 'Please select the destination for this field', 'acf' ),
		)
	);

	echo '<p>' . esc_html__( 'Please select the destination for this field', 'acf' ) . '</p>';
	echo '<form id="acf-move-field-form">';
		acf_render_field_wrap( $field );
		echo '<button type="submit" class="acf-btn">' . esc_html__( 'Move Field', 'acf' ) . '</button>';
	echo '</form>';

	die();
}