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