ACF_Admin_Tool_Import::submit()publicACF 5.6.3

submit

This function will run when the tool's form has been submit

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

Хуков нет.

Возвращает

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

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

$ACF_Admin_Tool_Import = new ACF_Admin_Tool_Import();
$ACF_Admin_Tool_Import->submit();

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

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

Код ACF_Admin_Tool_Import::submit() ACF 6.0.4

function submit() {

	// Check file size.
	if ( empty( $_FILES['acf_import_file']['size'] ) ) {
		return acf_add_admin_notice( __( 'No file selected', 'acf' ), 'warning' );
	}

	$file = acf_sanitize_files_array( $_FILES['acf_import_file'] );

	// Check errors.
	if ( $file['error'] ) {
		return acf_add_admin_notice( __( 'Error uploading file. Please try again', 'acf' ), 'warning' );
	}

	// Check file type.
	if ( pathinfo( $file['name'], PATHINFO_EXTENSION ) !== 'json' ) {
		return acf_add_admin_notice( __( 'Incorrect file type', 'acf' ), 'warning' );
	}

	// Read JSON.
	$json = file_get_contents( $file['tmp_name'] );
	$json = json_decode( $json, true );

	// Check if empty.
	if ( ! $json || ! is_array( $json ) ) {
		return acf_add_admin_notice( __( 'Import file empty', 'acf' ), 'warning' );
	}

	// Ensure $json is an array of groups.
	if ( isset( $json['key'] ) ) {
		$json = array( $json );
	}

	// Remeber imported field group ids.
	$ids = array();

	// Loop over json
	foreach ( $json as $field_group ) {

		// Search database for existing field group.
		$post = acf_get_field_group_post( $field_group['key'] );
		if ( $post ) {
			$field_group['ID'] = $post->ID;
		}

		// Import field group.
		$field_group = acf_import_field_group( $field_group );

		// append message
		$ids[] = $field_group['ID'];
	}

	// Count number of imported field groups.
	$total = count( $ids );

	// Generate text.
	$text = sprintf( _n( 'Imported 1 field group', 'Imported %s field groups', $total, 'acf' ), $total );

	// Add links to text.
	$links = array();
	foreach ( $ids as $id ) {
		$links[] = '<a href="' . get_edit_post_link( $id ) . '">' . get_the_title( $id ) . '</a>';
	}
	$text .= ' ' . implode( ', ', $links );

	// Add notice
	acf_add_admin_notice( $text, 'success' );
}