PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.4.0-beta2
Secure Custom Fields v6.4.0-beta2
6.9.5 6.9.4 6.9.3 6.9.2 6.9.1 6.9.0 6.8.9 6.8.7 6.8.8 6.8.6 6.8.4 6.8.5 trunk 6.4.0-beta1 6.4.0-beta2 6.4.1 6.4.1-beta3 6.4.1-beta4 6.4.1-beta5 6.4.1-beta6 6.4.1-beta7 6.4.2 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.5.5 6.5.6 6.5.7 6.6.0 6.7.0 6.7.1 6.8.0 6.8.1 6.8.2 6.8.3
secure-custom-fields / includes / admin / tools / class-acf-admin-tool-import.php
secure-custom-fields / includes / admin / tools Last commit date
class-acf-admin-tool-export.php 1 year ago class-acf-admin-tool-import.php 1 year ago class-acf-admin-tool.php 1 year ago index.php 1 year ago
class-acf-admin-tool-import.php
296 lines
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly
5 }
6
7 if ( ! class_exists( 'ACF_Admin_Tool_Import' ) ) :
8
9 class ACF_Admin_Tool_Import extends ACF_Admin_Tool {
10
11 /**
12 * initialize
13 *
14 * This function will initialize the admin tool
15 *
16 * @date 10/10/17
17 * @since 5.6.3
18 *
19 * @param n/a
20 * @return n/a
21 */
22 function initialize() {
23
24 // vars
25 $this->name = 'import';
26 $this->title = __( 'Import Field Groups', 'acf' );
27 $this->icon = 'dashicons-upload';
28 }
29
30
31 /**
32 * html
33 *
34 * This function will output the metabox HTML
35 *
36 * @date 10/10/17
37 * @since 5.6.3
38 *
39 * @param n/a
40 * @return n/a
41 */
42 function html() {
43
44 ?>
45 <div class="acf-postbox-header">
46 <h2 class="acf-postbox-title"><?php esc_html_e( 'Import', 'acf' ); ?></h2>
47 <div class="acf-tip"><i tabindex="0" class="acf-icon acf-icon-help acf-js-tooltip" title="<?php esc_attr_e( 'Select the Secure Custom Fields JSON file you would like to import. When you click the import button below, SCF will import the items in that file.', 'acf' ); ?>">?</i></div>
48 </div>
49 <div class="acf-postbox-inner">
50 <div class="acf-fields">
51 <?php
52
53 acf_render_field_wrap(
54 array(
55 'label' => __( 'Select File', 'acf' ),
56 'type' => 'file',
57 'name' => 'acf_import_file',
58 'value' => false,
59 'uploader' => 'basic',
60 )
61 );
62
63 ?>
64 </div>
65 <p class="acf-submit">
66 <button type="submit" class="acf-btn" name="import_type" value="json">
67 <?php esc_html_e( 'Import JSON', 'acf' ); ?>
68 </button>
69 </p>
70
71 <?php
72 if ( is_plugin_active( 'custom-post-type-ui/custom-post-type-ui.php' ) && acf_get_setting( 'enable_post_types' ) ) {
73 $cptui_post_types = get_option( 'cptui_post_types' );
74 $cptui_taxonomies = get_option( 'cptui_taxonomies' );
75 $choices = array();
76 $overwrite_warning = false;
77
78 if ( $cptui_post_types ) {
79 $choices['post_types'] = __( 'Post Types', 'acf' );
80 $existing_post_types = acf_get_acf_post_types();
81
82 foreach ( $existing_post_types as $existing_post_type ) {
83 if ( isset( $cptui_post_types[ $existing_post_type['post_type'] ] ) ) {
84 $overwrite_warning = true;
85 }
86 }
87 }
88
89 if ( $cptui_taxonomies ) {
90 $choices['taxonomies'] = __( 'Taxonomies', 'acf' );
91
92 if ( ! $overwrite_warning ) {
93 $existing_taxonomies = acf_get_acf_taxonomies();
94 foreach ( $existing_taxonomies as $existing_taxonomy ) {
95 if ( isset( $cptui_taxonomies[ $existing_taxonomy['taxonomy'] ] ) ) {
96 $overwrite_warning = true;
97 }
98 }
99 }
100 }
101
102 if ( ! empty( $choices ) ) :
103 ?>
104 <div class="acf-fields import-cptui">
105 <?php
106 acf_render_field_wrap(
107 array(
108 'label' => __( 'Import from Custom Post Type UI', 'acf' ),
109 'type' => 'checkbox',
110 'name' => 'acf_import_cptui',
111 'choices' => $choices,
112 'toggle' => true,
113 )
114 );
115 ?>
116 </div>
117 <?php
118 if ( $overwrite_warning ) {
119 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>';
120 }
121 ?>
122 <p class="acf-submit">
123 <button type="submit" class="acf-btn" name="import_type" value="cptui">
124 <?php esc_html_e( 'Import from Custom Post Type UI', 'acf' ); ?>
125 </button>
126 </p>
127 <?php
128 endif;
129 }
130 ?>
131 </div>
132 <?php
133 }
134
135 /**
136 * Imports the selected ACF posts and returns an admin notice on completion.
137 *
138 * @since 5.6.3
139 *
140 * @return ACF_Admin_Notice
141 */
142 public function submit() {
143 //phpcs:disable WordPress.Security.NonceVerification.Missing -- nonce verified before this function is called.
144 if ( 'cptui' === acf_request_arg( 'import_type', '' ) && ! empty( $_POST['acf_import_cptui'] ) ) {
145 $import = acf_sanitize_request_args( $_POST['acf_import_cptui'] ); //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- unslash not needed.
146 return $this->import_cpt_ui( $import );
147 }
148
149 // Check file size.
150 if ( empty( $_FILES['acf_import_file']['size'] ) ) {
151 return acf_add_admin_notice( __( 'No file selected', 'acf' ), 'warning' );
152 }
153
154 $file = acf_sanitize_files_array( $_FILES['acf_import_file'] );
155
156 // Check errors.
157 if ( $file['error'] ) {
158 return acf_add_admin_notice( __( 'Error uploading file. Please try again', 'acf' ), 'warning' );
159 }
160
161 // Check file type.
162 if ( pathinfo( $file['name'], PATHINFO_EXTENSION ) !== 'json' ) {
163 return acf_add_admin_notice( __( 'Incorrect file type', 'acf' ), 'warning' );
164 }
165
166 // Read JSON.
167 $json = file_get_contents( $file['tmp_name'] );
168 $json = json_decode( $json, true );
169
170 // Check if empty.
171 if ( ! $json || ! is_array( $json ) ) {
172 return acf_add_admin_notice( __( 'Import file empty', 'acf' ), 'warning' );
173 }
174
175 // Ensure $json is an array of posts.
176 if ( isset( $json['key'] ) ) {
177 $json = array( $json );
178 }
179
180 // Remember imported post ids.
181 $ids = array();
182
183 // Loop over json.
184 foreach ( $json as $to_import ) {
185 // Search database for existing post.
186 $post_type = acf_determine_internal_post_type( $to_import['key'] );
187 $post = acf_get_internal_post_type_post( $to_import['key'], $post_type );
188
189 if ( $post ) {
190 $to_import['ID'] = $post->ID;
191 }
192
193 // Import the post.
194 $to_import = acf_import_internal_post_type( $to_import, $post_type );
195
196 // Append message.
197 $ids[] = $to_import['ID'];
198 }
199
200 // Count number of imported posts.
201 $total = count( $ids );
202
203 // Generate text.
204 $text = sprintf( _n( 'Imported 1 item', 'Imported %s items', $total, 'acf' ), $total );
205
206 // Add links to text.
207 $links = array();
208 foreach ( $ids as $id ) {
209 $links[] = '<a href="' . get_edit_post_link( $id ) . '">' . get_the_title( $id ) . '</a>';
210 }
211 $text .= ' ' . implode( ', ', $links );
212
213 // Add notice.
214 return acf_add_admin_notice( $text, 'success' );
215 //phpcs:enable WordPress.Security.NonceVerification.Missing
216 }
217
218 /**
219 * Handles the import of CPTUI post types and taxonomies.
220 *
221 * @since 6.1
222 *
223 * @param array $import_args What to import.
224 * @return ACF_Admin_Notice
225 */
226 public function import_cpt_ui( $import_args ) {
227 if ( ! is_array( $import_args ) ) {
228 return acf_add_admin_notice( __( 'Nothing from Custom Post Type UI plugin selected for import.', 'acf' ), 'warning' );
229 }
230
231 $imported = array();
232
233 // Import any post types.
234 if ( in_array( 'post_types', $import_args, true ) ) {
235 $cptui_post_types = get_option( 'cptui_post_types' );
236 $instance = acf_get_internal_post_type_instance( 'acf-post-type' );
237
238 if ( ! is_array( $cptui_post_types ) || ! $instance ) {
239 return acf_add_admin_notice( __( 'Failed to import post types.', 'acf' ), 'warning' );
240 }
241
242 foreach ( $cptui_post_types as $post_type ) {
243 $result = $instance->import_cptui_post_type( $post_type );
244
245 if ( is_array( $result ) && isset( $result['ID'] ) ) {
246 $imported[] = (int) $result['ID'];
247 }
248 }
249 }
250
251 // Import any taxonomies.
252 if ( in_array( 'taxonomies', $import_args, true ) ) {
253 $cptui_taxonomies = get_option( 'cptui_taxonomies' );
254 $instance = acf_get_internal_post_type_instance( 'acf-taxonomy' );
255
256 if ( ! is_array( $cptui_taxonomies ) || ! $instance ) {
257 return acf_add_admin_notice( __( 'Failed to import taxonomies.', 'acf' ), 'warning' );
258 }
259
260 foreach ( $cptui_taxonomies as $taxonomy ) {
261 $result = $instance->import_cptui_taxonomy( $taxonomy );
262
263 if ( is_array( $result ) && isset( $result['ID'] ) ) {
264 $imported[] = (int) $result['ID'];
265 }
266 }
267 }
268
269 if ( ! empty( $imported ) ) {
270 // Generate text.
271 $total = count( $imported );
272 /* translators: %d - number of items imported from CPTUI */
273 $text = sprintf( _n( 'Imported %d item from Custom Post Type UI -', 'Imported %d items from Custom Post Type UI -', $total, 'acf' ), $total );
274
275 // Add links to text.
276 $links = array();
277 foreach ( $imported as $id ) {
278 $links[] = '<a href="' . get_edit_post_link( $id ) . '">' . get_the_title( $id ) . '</a>';
279 }
280
281 $text .= ' ' . implode( ', ', $links );
282 $text .= __( '. The Custom Post Type UI plugin can be deactivated.', 'acf' );
283
284 return acf_add_admin_notice( $text, 'success' );
285 }
286
287 return acf_add_admin_notice( __( 'Nothing to import', 'acf' ), 'warning' );
288 }
289 }
290
291 // Initialize.
292 acf_register_admin_tool( 'ACF_Admin_Tool_Import' );
293 endif; // class_exists check.
294
295 ?>
296