ACF_Admin_Tool_Import::submitpublicACF 5.6.3

Imports the selected ACF posts and returns an admin notice on completion.

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

Хуков нет.

Возвращает

ACF_Admin_Notice.

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

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

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

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

Код ACF_Admin_Tool_Import::submit() ACF 6.4.2

public function submit() {
	//phpcs:disable WordPress.Security.NonceVerification.Missing -- nonce verified before this function is called.
	if ( 'cptui' === acf_request_arg( 'import_type', '' ) && ! empty( $_POST['acf_import_cptui'] ) ) {
		$import = acf_sanitize_request_args( $_POST['acf_import_cptui'] ); //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- unslash not needed.
		return $this->import_cpt_ui( $import );
	}

	// 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 posts.
	if ( isset( $json['key'] ) ) {
		$json = array( $json );
	}

	// Remember imported post ids.
	$ids = array();

	// Loop over json.
	foreach ( $json as $to_import ) {
		// Search database for existing post.
		$post_type = acf_determine_internal_post_type( $to_import['key'] );
		$post      = acf_get_internal_post_type_post( $to_import['key'], $post_type );

		if ( $post ) {
			$to_import['ID'] = $post->ID;
		}

		// Import the post.
		$to_import = acf_import_internal_post_type( $to_import, $post_type );

		// Append message.
		$ids[] = $to_import['ID'];
	}

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

	// Generate text.
	$text = sprintf( _n( 'Imported 1 item', 'Imported %s items', $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.
	return acf_add_admin_notice( $text, 'success' );
	//phpcs:enable WordPress.Security.NonceVerification.Missing
}