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.2.1 4.2.2 4.3.0 4.3.1 4.3.2 4.3.3 4.3.4 4.3.5 4.3.6 4.3.7 4.3.8 4.3.9 4.4.0 4.4.1 4.4.10 4.4.11 4.4.12 4.4.2 4.4.3 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9 5.10 5.10.1 5.10.2 5.11 5.11.1 5.11.2 5.11.3 5.11.4 5.12 5.12.1 5.12.2 5.12.3 5.12.4 5.12.5 5.12.6 5.6.10 5.6.2 5.6.3 5.6.4 5.6.5 5.6.6 5.6.7 5.6.8 5.6.9 5.7.0 5.7.1 5.7.10 5.7.12 5.7.13 5.7.2 5.7.3 5.7.4 5.7.5 5.7.6 5.7.7 5.7.8 5.7.9 5.8.0 5.8.1 5.8.10 5.8.11 5.8.12 5.8.13 5.8.14 5.8.2 5.8.3 5.8.4
advanced-custom-fields / includes / admin / tools / class-acf-admin-tool-import.php
advanced-custom-fields / includes / admin / tools Last commit date
class-acf-admin-tool-export.php 6 months ago class-acf-admin-tool-import.php 6 months ago class-acf-admin-tool.php 6 months ago index.php 2 years ago
class-acf-admin-tool-import.php
307 lines
1 <?php
2 /**
3 * @package ACF
4 * @author WP Engine
5 *
6 * © 2026 Advanced Custom Fields (ACF®). All rights reserved.
7 * "ACF" is a trademark of WP Engine.
8 * Licensed under the GNU General Public License v2 or later.
9 * https://www.gnu.org/licenses/gpl-2.0.html
10 */
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit; // Exit if accessed directly
14 }
15
16 if ( ! class_exists( 'ACF_Admin_Tool_Import' ) ) :
17
18 class ACF_Admin_Tool_Import extends ACF_Admin_Tool {
19
20 /**
21 * initialize
22 *
23 * This function will initialize the admin tool
24 *
25 * @date 10/10/17
26 * @since 5.6.3
27 *
28 * @param n/a
29 * @return n/a
30 */
31 function initialize() {
32
33 // vars
34 $this->name = 'import';
35 $this->title = __( 'Import Field Groups', 'acf' );
36 $this->icon = 'dashicons-upload';
37 }
38
39
40 /**
41 * html
42 *
43 * This function will output the metabox HTML
44 *
45 * @date 10/10/17
46 * @since 5.6.3
47 *
48 * @param n/a
49 * @return n/a
50 */
51 function html() {
52
53 ?>
54 <div class="acf-postbox-header">
55 <h2 class="acf-postbox-title"><?php esc_html_e( 'Import', 'acf' ); ?></h2>
56 <div class="acf-tip"><i tabindex="0" class="acf-icon acf-icon-help acf-js-tooltip" title="<?php esc_attr_e( 'Choose an ACF JSON file to import. Use only files from trusted sources, then click Import.', 'acf' ); ?>">?</i></div>
57 </div>
58 <div class="acf-postbox-inner">
59 <div class="acf-fields">
60 <?php
61
62 acf_render_field_wrap(
63 array(
64 'label' => __( 'Select JSON File', 'acf' ),
65 'type' => 'file',
66 'name' => 'acf_import_file',
67 'value' => false,
68 'uploader' => 'basic',
69 'mime_types' => 'application/json,application/x-json,application/x-javascript,text/javascript,text/x-javascript,text/json',
70 'instructions' => __( 'Import JSON containing field groups, post types, or taxonomies (trusted sources only)', 'acf' ),
71 )
72 );
73
74 ?>
75 </div>
76 <p class="acf-submit">
77 <button type="submit" class="acf-btn" name="import_type" value="json">
78 <?php esc_html_e( 'Import JSON', 'acf' ); ?>
79 </button>
80 </p>
81
82 <?php
83 if ( is_plugin_active( 'custom-post-type-ui/custom-post-type-ui.php' ) && acf_get_setting( 'enable_post_types' ) ) {
84 $cptui_post_types = get_option( 'cptui_post_types' );
85 $cptui_taxonomies = get_option( 'cptui_taxonomies' );
86 $choices = array();
87 $overwrite_warning = false;
88
89 if ( $cptui_post_types ) {
90 $choices['post_types'] = __( 'Post Types', 'acf' );
91 $existing_post_types = acf_get_acf_post_types();
92
93 foreach ( $existing_post_types as $existing_post_type ) {
94 if ( isset( $cptui_post_types[ $existing_post_type['post_type'] ] ) ) {
95 $overwrite_warning = true;
96 }
97 }
98 }
99
100 if ( $cptui_taxonomies ) {
101 $choices['taxonomies'] = __( 'Taxonomies', 'acf' );
102
103 if ( ! $overwrite_warning ) {
104 $existing_taxonomies = acf_get_acf_taxonomies();
105 foreach ( $existing_taxonomies as $existing_taxonomy ) {
106 if ( isset( $cptui_taxonomies[ $existing_taxonomy['taxonomy'] ] ) ) {
107 $overwrite_warning = true;
108 }
109 }
110 }
111 }
112
113 if ( ! empty( $choices ) ) :
114 ?>
115 <div class="acf-fields import-cptui">
116 <?php
117 acf_render_field_wrap(
118 array(
119 'label' => __( 'Import from Custom Post Type UI', 'acf' ),
120 'type' => 'checkbox',
121 'name' => 'acf_import_cptui',
122 'choices' => $choices,
123 'toggle' => true,
124 )
125 );
126 ?>
127 </div>
128 <?php
129 if ( $overwrite_warning ) {
130 echo '<p class="acf-inline-notice notice notice-info">' . esc_html__( 'Importing a Post Type or Taxonomy with the same key as one that already exists will overwrite the settings for the existing Post Type or Taxonomy with those of the import.', 'acf' ) . '</p>';
131 }
132 ?>
133 <p class="acf-submit">
134 <button type="submit" class="acf-btn" name="import_type" value="cptui">
135 <?php esc_html_e( 'Import from Custom Post Type UI', 'acf' ); ?>
136 </button>
137 </p>
138 <?php
139 endif;
140 }
141 ?>
142 </div>
143 <?php
144 }
145
146 /**
147 * Imports the selected ACF posts and returns an admin notice on completion.
148 *
149 * @since 5.6.3
150 *
151 * @return ACF_Admin_Notice
152 */
153 public function submit() {
154 //phpcs:disable WordPress.Security.NonceVerification.Missing -- nonce verified before this function is called.
155 if ( 'cptui' === acf_request_arg( 'import_type', '' ) && ! empty( $_POST['acf_import_cptui'] ) ) {
156 $import = acf_sanitize_request_args( $_POST['acf_import_cptui'] ); //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- unslash not needed.
157 return $this->import_cpt_ui( $import );
158 }
159
160 // Check file size.
161 if ( empty( $_FILES['acf_import_file']['size'] ) ) {
162 return acf_add_admin_notice( __( 'No file selected', 'acf' ), 'warning' );
163 }
164
165 $file = acf_sanitize_files_array( $_FILES['acf_import_file'] );
166
167 // Check errors.
168 if ( $file['error'] ) {
169 return acf_add_admin_notice( __( 'Error uploading file. Please try again', 'acf' ), 'warning' );
170 }
171
172 // Check file type.
173 if ( pathinfo( $file['name'], PATHINFO_EXTENSION ) !== 'json' ) {
174 return acf_add_admin_notice( __( 'Incorrect file type', 'acf' ), 'warning' );
175 }
176
177 // Read JSON.
178 $json = file_get_contents( $file['tmp_name'] );
179 $json = json_decode( $json, true );
180
181 // Check if empty.
182 if ( ! $json || ! is_array( $json ) ) {
183 return acf_add_admin_notice( __( 'Import file empty', 'acf' ), 'warning' );
184 }
185
186 // Ensure $json is an array of posts.
187 if ( isset( $json['key'] ) ) {
188 $json = array( $json );
189 }
190
191 // Remember imported post ids.
192 $ids = array();
193
194 // Loop over json.
195 foreach ( $json as $to_import ) {
196 // Search database for existing post.
197 $post_type = acf_determine_internal_post_type( $to_import['key'] );
198 $post = acf_get_internal_post_type_post( $to_import['key'], $post_type );
199
200 if ( $post ) {
201 $to_import['ID'] = $post->ID;
202 }
203
204 // Import the post.
205 $to_import = acf_import_internal_post_type( $to_import, $post_type );
206
207 // Append message.
208 $ids[] = $to_import['ID'];
209 }
210
211 // Count number of imported posts.
212 $total = count( $ids );
213
214 // Generate text.
215 $text = sprintf( _n( 'Imported 1 item', 'Imported %s items', $total, 'acf' ), $total );
216
217 // Add links to text.
218 $links = array();
219 foreach ( $ids as $id ) {
220 $links[] = '<a href="' . esc_url( get_edit_post_link( $id ) ) . '">' . esc_html( get_the_title( $id ) ) . '</a>';
221 }
222 $text .= ' ' . implode( ', ', $links );
223
224 // Add notice.
225 return acf_add_admin_notice( $text, 'success' );
226 //phpcs:enable WordPress.Security.NonceVerification.Missing
227 }
228
229 /**
230 * Handles the import of CPTUI post types and taxonomies.
231 *
232 * @since 6.1
233 *
234 * @param array $import_args What to import.
235 * @return ACF_Admin_Notice
236 */
237 public function import_cpt_ui( $import_args ) {
238 if ( ! is_array( $import_args ) ) {
239 return acf_add_admin_notice( __( 'Nothing from Custom Post Type UI plugin selected for import.', 'acf' ), 'warning' );
240 }
241
242 $imported = array();
243
244 // Import any post types.
245 if ( in_array( 'post_types', $import_args, true ) ) {
246 $cptui_post_types = get_option( 'cptui_post_types' );
247 $instance = acf_get_internal_post_type_instance( 'acf-post-type' );
248
249 if ( ! is_array( $cptui_post_types ) || ! $instance ) {
250 return acf_add_admin_notice( __( 'Failed to import post types.', 'acf' ), 'warning' );
251 }
252
253 foreach ( $cptui_post_types as $post_type ) {
254 $result = $instance->import_cptui_post_type( $post_type );
255
256 if ( is_array( $result ) && isset( $result['ID'] ) ) {
257 $imported[] = (int) $result['ID'];
258 }
259 }
260 }
261
262 // Import any taxonomies.
263 if ( in_array( 'taxonomies', $import_args, true ) ) {
264 $cptui_taxonomies = get_option( 'cptui_taxonomies' );
265 $instance = acf_get_internal_post_type_instance( 'acf-taxonomy' );
266
267 if ( ! is_array( $cptui_taxonomies ) || ! $instance ) {
268 return acf_add_admin_notice( __( 'Failed to import taxonomies.', 'acf' ), 'warning' );
269 }
270
271 foreach ( $cptui_taxonomies as $taxonomy ) {
272 $result = $instance->import_cptui_taxonomy( $taxonomy );
273
274 if ( is_array( $result ) && isset( $result['ID'] ) ) {
275 $imported[] = (int) $result['ID'];
276 }
277 }
278 }
279
280 if ( ! empty( $imported ) ) {
281 // Generate text.
282 $total = count( $imported );
283 /* translators: %d - number of items imported from CPTUI */
284 $text = sprintf( _n( 'Imported %d item from Custom Post Type UI -', 'Imported %d items from Custom Post Type UI -', $total, 'acf' ), $total );
285
286 // Add links to text.
287 $links = array();
288 foreach ( $imported as $id ) {
289 $links[] = '<a href="' . esc_url( get_edit_post_link( $id ) ) . '">' . esc_html( get_the_title( $id ) ) . '</a>';
290 }
291
292 $text .= ' ' . implode( ', ', $links );
293 $text .= __( '. The Custom Post Type UI plugin can be deactivated.', 'acf' );
294
295 return acf_add_admin_notice( $text, 'success' );
296 }
297
298 return acf_add_admin_notice( __( 'Nothing to import', 'acf' ), 'warning' );
299 }
300 }
301
302 // Initialize.
303 acf_register_admin_tool( 'ACF_Admin_Tool_Import' );
304 endif; // class_exists check.
305
306 ?>
307