ACF_Admin_Internal_Post_Type::ajax_link_field_groupspublicACF 6.1

Powers the modal for linking field groups to newly-created CPTs/taxonomies.

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

Хуков нет.

Возвращает

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

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

$ACF_Admin_Internal_Post_Type = new ACF_Admin_Internal_Post_Type();
$ACF_Admin_Internal_Post_Type->ajax_link_field_groups();

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

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

Код ACF_Admin_Internal_Post_Type::ajax_link_field_groups() ACF 6.4.2

<?php
public function ajax_link_field_groups() {
	// 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_groups' => array(),
		)
	);
	// phpcs:enable WordPress.Security.NonceVerification.Missing

	// Verify nonce and user capability.
	if ( ! wp_verify_nonce( $args['nonce'], 'acf_nonce' ) || ! acf_current_user_can_admin() || ! $args['post_id'] ) {
		die();
	}

	$post_type  = get_post_type( $args['post_id'] );
	$saved_post = acf_get_internal_post_type( $args['post_id'], $post_type );

	// Link the selected field groups.
	if ( is_array( $args['field_groups'] ) && ! empty( $args['field_groups'] ) && $saved_post ) {
		foreach ( $args['field_groups'] as $field_group_id ) {
			$field_group = acf_get_field_group( $field_group_id );

			if ( ! is_array( $field_group ) ) {
				continue;
			}

			if ( 'acf-post-type' === $post_type ) {
				$param = 'post_type';
				$value = $saved_post['post_type'];
			} elseif ( 'acf-taxonomy' === $post_type ) {
				$param = 'taxonomy';
				$value = $saved_post['taxonomy'];
			} else {
				$param = 'options_page';
				$value = $saved_post['menu_slug'];
			}

			$field_group['location'][] = array(
				array(
					'param'    => $param,
					'operator' => '==',
					'value'    => $value,
				),
			);

			acf_update_field_group( $field_group );
		}

		ob_start();
		?>
		<p class="acf-link-successful">
			<?php
			$link_successful_text = _n(
				'Field group linked successfully.',
				'Field groups linked successfully.',
				count( $args['field_groups'] ),
				'acf'
			);
			echo esc_html( $link_successful_text );
			?>
		</p>
		<div class="acf-actions">
			<button type="button" class="acf-btn acf-btn-secondary acf-close-popup"><?php esc_html_e( 'Close Modal', 'acf' ); ?></button>
		</div>
		<?php
		$content = ob_get_clean();
		wp_send_json_success( array( 'content' => $content ) );
	}

	// Render the field group select.
	$field_groups = acf_get_field_groups();
	$choices      = array();

	if ( ! empty( $field_groups ) ) {
		foreach ( $field_groups as $field_group ) {
			if ( ! $field_group['ID'] ) {
				continue;
			}

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

	$instructions = sprintf(
		/* translators: %s - either "post type" or "taxonomy" */
		__( 'Add this %s to the location rules of the selected field groups.', 'acf' ),
		'acf-post-type' === $post_type ? __( 'post type', 'acf' ) : __( 'taxonomy', 'acf' )
	);

	$field = acf_get_valid_field(
		array(
			'type'         => 'select',
			'name'         => 'acf_field_groups',
			'choices'      => $choices,
			'aria-label'   => __( 'Please select the field groups to link.', 'acf' ),
			'placeholder'  => __( 'Select one or many field groups...', 'acf' ),
			'label'        => __( 'Field Group(s)', 'acf' ),
			'instructions' => $instructions,
			'ui'           => true,
			'multiple'     => true,
			'allow_null'   => true,
		)
	);

	ob_start();
	?>
	<form id="acf-link-field-groups-form">
		<?php acf_render_field_wrap( $field, 'div', 'field' ); ?>
		<div class="acf-actions">
			<button type="button" class="acf-btn acf-btn-secondary acf-close-popup"><?php esc_html_e( 'Cancel', 'acf' ); ?></button>
			<button type="submit" class="acf-btn acf-btn-primary"><?php esc_html_e( 'Done', 'acf' ); ?></button>
		</div>
	</form>
	<?php
	$content = ob_get_clean();

	wp_send_json_success(
		array(
			'content' => $content,
			'title'   => esc_html__( 'Link Existing Field Groups', 'acf' ),
		)
	);
}