PluginProbe
Pods – Custom Content Types and Fields / 2.8.23.4
Pods – Custom Content Types and Fields v2.8.23.4
2.7.31.4 2.8.23.5 2.9.19.5 3.0.10.5 3.1.4.3 3.2.8.4 3.3.9.2 2.8.23.4 2.9.19.4 3.0.10.4 3.1.4.2 3.2.8.3 3.3.9.1 trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 All 42 releases
pods / components / Migrate-CPTUI / Migrate-CPTUI.php
Migrate-CPTUI.php
490 lines 17.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Name: Migrate: Import from the Custom Post Type UI plugin
4 *
5 * Menu Name: Migrate CPT UI
6 *
7 * Description: Import Custom Post Types and Taxonomies from Custom Post Type UI (<a href="http://webdevstudios.com/plugin/custom-post-type-ui/">http://webdevstudios.com/plugin/custom-post-type-ui/</a>)
8 *
9 * Category: Migration
10 *
11 * Version: 1.0
12 *
13 * Plugin: pods-migrate-custom-post-type-ui/pods-migrate-custom-post-type-ui.php
14 *
15 * @package Pods\Components
16 * @subpackage Migrate-Cptui
17 */
18
19 if ( class_exists( 'Pods_Migrate_CPTUI' ) ) {
20 return;
21 }
22
23 /**
24 * Class Pods_Migrate_CPTUI
25 */
26 class Pods_Migrate_CPTUI extends PodsComponent {
27
28 /** @var array
29 *
30 * Support option names for multiple versions, list from newest to oldest
31 */
32 private $post_option_name_list = array(
33 'cptui_post_types',
34 'cpt_custom_post_types',
35 );
36
37 /** @var array
38 *
39 * Support option names for multiple versions, list from newest to oldest
40 */
41 private $taxonomy_option_name_list = array(
42 'cptui_taxonomies',
43 'cpt_custom_tax_types',
44 );
45
46 private $api = null;
47
48 private $post_option_name = null;
49
50 private $taxonomy_option_name = null;
51
52 private $post_types = array();
53
54 private $taxonomies = array();
55
56 /**
57 * {@inheritdoc}
58 */
59 public function init() {
60
61 $this->post_option_name = $this->get_option_name( $this->post_option_name_list );
62 if ( ! is_null( $this->post_option_name ) ) {
63 $this->post_types = (array) get_option( $this->post_option_name, array() );
64 }
65 $this->taxonomy_option_name = $this->get_option_name( $this->taxonomy_option_name_list );
66 if ( ! is_null( $this->taxonomy_option_name ) ) {
67 $this->taxonomies = (array) get_option( $this->taxonomy_option_name, array() );
68 }
69 }
70
71 /**
72 * Enqueue styles
73 *
74 * @since 2.0.0
75 */
76 public function admin_assets() {
77
78 wp_enqueue_style( 'pods-wizard' );
79 }
80
81 /**
82 * Show the Admin
83 *
84 * @param $options
85 * @param $component
86 */
87 public function admin( $options, $component ) {
88
89 $post_types = (array) $this->post_types;
90 $taxonomies = (array) $this->taxonomies;
91
92 $method = 'migrate';
93 // ajax_migrate
94 pods_view( PODS_DIR . 'components/Migrate-CPTUI/ui/wizard.php', compact( array_keys( get_defined_vars() ) ) );
95 }
96
97 /**
98 * Handle the Migration AJAX
99 *
100 * @param $params
101 */
102 public function ajax_migrate( $params ) {
103
104 $post_types = (array) $this->post_types;
105 $taxonomies = (array) $this->taxonomies;
106
107 $migrate_post_types = array();
108
109 if ( isset( $params->post_type ) && ! empty( $params->post_type ) ) {
110 foreach ( $params->post_type as $post_type => $checked ) {
111 if ( true === (boolean) $checked ) {
112 $migrate_post_types[] = $post_type;
113 }
114 }
115 }
116
117 $migrate_taxonomies = array();
118
119 if ( isset( $params->taxonomy ) && ! empty( $params->taxonomy ) ) {
120 foreach ( $params->taxonomy as $taxonomy => $checked ) {
121 if ( true === (boolean) $checked ) {
122 $migrate_taxonomies[] = $taxonomy;
123 }
124 }
125 }
126
127 foreach ( $post_types as $k => $post_type ) {
128 if ( ! in_array( pods_v( 'name', $post_type ), $migrate_post_types, true ) ) {
129 continue;
130 }
131
132 $id = $this->migrate_post_type( $post_type );
133
134 if ( 0 < $id ) {
135 unset( $post_types[ $k ] );
136 }
137 }
138
139 foreach ( $taxonomies as $k => $taxonomy ) {
140 if ( ! in_array( pods_v( 'name', $taxonomy ), $migrate_taxonomies, true ) ) {
141 continue;
142 }
143
144 $id = $this->migrate_taxonomy( $taxonomy );
145
146 if ( 0 < $id ) {
147 unset( $taxonomies[ $k ] );
148 }
149 }
150
151 if ( 1 === (int) pods_v( 'cleanup', $params, 0 ) ) {
152 if ( ! empty( $post_types ) ) {
153 if ( ! is_null( $this->post_option_name ) ) {
154 update_option( $this->post_option_name, $post_types );
155 }
156 } else {
157 if ( ! is_null( $this->post_option_name ) ) {
158 delete_option( $this->post_option_name );
159 }
160 }
161
162 if ( ! empty( $taxonomies ) ) {
163 if ( ! is_null( $this->taxonomy_option_name ) ) {
164 update_option( $this->taxonomy_option_name, $taxonomies );
165 }
166 } else {
167 if ( ! is_null( $this->taxonomy_option_name ) ) {
168 delete_option( $this->taxonomy_option_name );
169 }
170 }
171 }//end if
172 }
173
174 /**
175 *
176 *
177 * @since 2.0.0
178 *
179 * @param $post_type
180 *
181 * @return bool|int|mixed
182 */
183 private function migrate_post_type( $post_type ) {
184 $supports = [];
185
186 if ( isset( $post_type['supports'] ) && is_array( $post_type['supports'] ) ) {
187 // New style.
188 $supports = $post_type['supports'];
189 } elseif ( isset( $post_type[0] ) && is_array( $post_type[0] ) ) {
190 // Old style.
191 $supports = $post_type[0];
192 }
193
194 $taxonomies = [];
195
196 if ( isset( $post_type['taxonomies'] ) && is_array( $post_type['taxonomies'] ) ) {
197 // New style.
198 $taxonomies = $post_type['taxonomies'];
199 } elseif ( isset( $post_type[1] ) && is_array( $post_type[1] ) ) {
200 // Old style.
201 $taxonomies = $post_type[1];
202 }
203
204 $labels = [];
205
206 if ( isset( $post_type['labels'] ) && is_array( $post_type['labels'] ) ) {
207 // New style.
208 $labels = $post_type['labels'];
209 } elseif ( isset( $post_type[2] ) && is_array( $post_type[2] ) ) {
210 // Old style.
211 $labels = $post_type[2];
212 }
213
214 $params = [
215 'type' => 'post_type',
216 'storage' => 'meta',
217 'object' => '',
218 'name' => pods_v( 'name', $post_type ),
219 'label' => pods_v( 'label', $post_type ),
220 'label_singular' => pods_v( 'singular_label', $post_type ),
221 'description' => pods_v( 'description', $post_type ),
222
223 // Supports arguments.
224 'supports_title' => in_array( 'title', $supports, true ),
225 'supports_editor' => in_array( 'editor', $supports, true ),
226 'supports_excerpt' => in_array( 'excerpt', $supports, true ),
227 'supports_trackbacks' => in_array( 'trackbacks', $supports, true ),
228 'supports_custom_fields' => in_array( 'custom-fields', $supports, true ),
229 'supports_comments' => in_array( 'comments', $supports, true ),
230 'supports_revisions' => in_array( 'revisions', $supports, true ),
231 'supports_thumbnail' => in_array( 'thumbnail', $supports, true ),
232 'supports_author' => in_array( 'author', $supports, true ),
233 'supports_page_attributes' => in_array( 'page-attributes', $supports, true ),
234 'supports_post_formats' => in_array( 'post-formats', $supports, true ),
235 'supports_custom' => pods_v( 'custom_supports', $post_type ),
236
237 // Custom labels.
238 'menu_name' => pods_v( 'menu_name', $labels ),
239 'label_all_items' => pods_v( 'all_items', $labels ),
240 'label_add_new' => pods_v( 'add_new', $labels ),
241 'label_add_new_item' => pods_v( 'add_new_item', $labels ),
242 'label_edit_item' => pods_v( 'edit_item', $labels ),
243 'label_new_item' => pods_v( 'new_item', $labels ),
244 'label_view_item' => pods_v( 'view_item', $labels ),
245 'label_view_items' => pods_v( 'view_items', $labels ),
246 'label_search_items' => pods_v( 'search_items', $labels ),
247 'label_not_found' => pods_v( 'not_found', $labels ),
248 'label_not_found_in_trash' => pods_v( 'not_found_in_trash', $labels ),
249 'label_parent' => pods_v( 'parent', $labels ),
250 'label_featured_image' => pods_v( 'featured_image', $labels ),
251 'label_set_featured_image' => pods_v( 'set_featured_image', $labels ),
252 'label_remove_featured_image' => pods_v( 'remove_featured_image', $labels ),
253 'label_use_featured_image' => pods_v( 'use_featured_image', $labels ),
254 'label_archives' => pods_v( 'archives', $labels ),
255 'label_insert_into_item' => pods_v( 'insert_into_item', $labels ),
256 'label_uploaded_to_this_item' => pods_v( 'uploaded_to_this_item', $labels ),
257 'label_filter_items_list' => pods_v( 'filter_items_list', $labels ),
258 'label_items_list_navigation' => pods_v( 'items_list_navigation', $labels ),
259 'label_items_list' => pods_v( 'items_list', $labels ),
260 'label_attributes' => pods_v( 'attributes', $labels ),
261 'label_name_admin_bar' => pods_v( 'name_admin_bar', $labels ),
262 'label_item_published' => pods_v( 'item_published', $labels ),
263 'label_item_published_privately' => pods_v( 'item_published_privately', $labels ),
264 'label_item_reverted_to_draft' => pods_v( 'item_reverted_to_draft', $labels ),
265 'label_item_scheduled' => pods_v( 'item_scheduled', $labels ),
266 'label_item_updated' => pods_v( 'item_updated', $labels ),
267
268 // Other settings.
269 'rest_base' => pods_v( 'rest_base', $post_type ),
270 'rest_controller_class' => pods_v( 'rest_controller_class', $post_type ),
271 'has_archive_string' => pods_v( 'has_archive_string', $post_type ),
272 'capability_type' => pods_v( 'capability_type', $post_type ),
273 'rewrite_custom_slug' => pods_v( 'rewrite_slug', $post_type ),
274 'query_var_string' => pods_v( 'query_var_slug', $post_type ),
275 'menu_position' => pods_v( 'menu_position', $post_type ),
276 'menu_string' => pods_v( 'show_in_menu_string', $post_type ),
277 'menu_icon' => pods_v( 'menu_icon', $post_type ),
278
279 // Boolean flags (0/1).
280 'public' => (int) pods_v( 'public', $post_type ),
281 'publicly_queryable' => (int) pods_v( 'publicly_queryable', $post_type ),
282 'show_ui' => (int) pods_v( 'show_ui', $post_type ),
283 'show_in_nav_menus' => (int) pods_v( 'show_in_nav_menus', $post_type ),
284 'delete_with_user' => (int) pods_v( 'delete_with_user', $post_type ),
285 'show_in_rest' => (int) pods_v( 'show_in_rest', $post_type ),
286 'has_archive' => (int) pods_v( 'has_archive', $post_type ),
287 'exclude_from_search' => (int) pods_v( 'exclude_from_search', $post_type ),
288 'hierarchical' => (int) pods_v( 'hierarchical', $post_type ),
289 'rewrite' => (int) pods_v( 'rewrite', $post_type ),
290 'rewrite_with_front' => (int) pods_v( 'rewrite_withfront', $post_type ),
291 'query_var' => (int) pods_v( 'query_var', $post_type ),
292 'show_in_menu' => (int) pods_v( 'show_in_menu', $post_type ),
293 ];
294
295 // Migrate built-in taxonomies
296 $builtin = $taxonomies;
297
298 foreach ( $builtin as $taxonomy_name ) {
299 $params[ 'built_in_taxonomies_' . $taxonomy_name ] = 1;
300 }
301
302 if ( ! is_object( $this->api ) ) {
303 $this->api = pods_api();
304 }
305
306 $pod = $this->api->load_pod( array( 'name' => pods_clean_name( $params['name'] ) ), false );
307
308 if ( ! empty( $pod ) ) {
309 return pods_error( sprintf( __( 'Pod with the name %s already exists', 'pods' ), pods_clean_name( $params['name'] ) ) );
310 }
311
312 $id = (int) $this->api->save_pod( $params );
313
314 if ( empty( $id ) ) {
315 return false;
316 }
317
318 $pod = $this->api->load_pod( array( 'id' => $id ), false );
319
320 if ( empty( $pod ) ) {
321 return false;
322 }
323
324 if ( $pod['name'] != $params['name'] ) {
325 $this->api->rename_wp_object_type( $params['type '], $params['name'], $pod['name'] );
326 }
327
328 return $id;
329 }
330
331 /**
332 *
333 *
334 * @since 2.0.0
335 *
336 * @param $taxonomy
337 *
338 * @return bool|int|mixed
339 */
340 private function migrate_taxonomy( $taxonomy ) {
341 $labels = [];
342
343 if ( isset( $taxonomy['labels'] ) && is_array( $taxonomy['labels'] ) ) {
344 // New style.
345 $labels = $taxonomy['labels'];
346 } elseif ( isset( $taxonomy[0] ) && is_array( $taxonomy[0] ) ) {
347 // Old style.
348 $labels = $taxonomy[0];
349 }
350
351 $post_types = [];
352
353 if ( isset( $taxonomy['object_types'] ) && is_array( $taxonomy['object_types'] ) ) {
354 // New style.
355 $post_types = $taxonomy['object_types'];
356 } elseif ( isset( $taxonomy[0] ) && is_array( $taxonomy[0] ) ) {
357 // Old style.
358 $post_types = $taxonomy[0];
359 }
360
361 $default_term = explode( ',', pods_v( 'default_term', $taxonomy ) );
362
363 $params = [
364 'type' => 'taxonomy',
365 'storage' => 'meta',
366 'object' => '',
367 'name' => pods_v( 'name', $taxonomy ),
368 'label' => pods_v( 'label', $taxonomy ),
369 'label_singular' => pods_v( 'singular_label', $taxonomy ),
370 'description' => pods_v( 'description', $taxonomy ),
371
372 // Custom labels.
373 'label_menu_name' => pods_v( 'menu_name', $labels ),
374 'label_all_items' => pods_v( 'all_items', $labels ),
375 'label_edit_item' => pods_v( 'edit_item', $labels ),
376 'label_view_item' => pods_v( 'view_item', $labels ),
377 'label_update_item' => pods_v( 'update_item', $labels ),
378 'label_add_new_item' => pods_v( 'add_new_item', $labels ),
379 'label_new_item_name' => pods_v( 'new_item_name', $labels ),
380 'label_parent_item' => pods_v( 'parent_item', $labels ),
381 'label_parent_item_colon' => pods_v( 'parent_item_colon', $labels ),
382 'label_search_items' => pods_v( 'search_items', $labels ),
383 'label_popular_items' => pods_v( 'popular_items', $labels ),
384 'label_separate_items_with_commas' => pods_v( 'separate_items_with_commas', $labels ),
385 'label_add_or_remove_items' => pods_v( 'add_or_remove_items', $labels ),
386 'label_choose_from_most_used' => pods_v( 'choose_from_most_used', $labels ),
387 'label_no_terms' => pods_v( 'no_terms', $labels ),
388 'label_items_list_navigation' => pods_v( 'items_list_navigation', $labels ),
389 'label_items_list' => pods_v( 'items_list', $labels ),
390 'label_not_found' => pods_v( 'not_found', $labels ),
391 'label_back_to_items' => pods_v( 'back_to_items', $labels ),
392
393 // Other settings.
394 'query_var_string' => pods_v( 'query_var_slug', $taxonomy ),
395 'rewrite_custom_slug' => pods_v( 'rewrite_slug', $taxonomy ),
396 'rest_base' => pods_v( 'rest_base', $taxonomy ), // Not currently used.
397 'rest_controller_class' => pods_v( 'rest_controller_class', $taxonomy ), // Not currently used.
398 'register_meta_box_cb' => pods_v( 'meta_box_cb', $taxonomy ), // Not currently used.
399 'default_term_name' => $default_term[0],
400 'default_term_slug' => $default_term[1],
401 'default_term_description' => $default_term[2],
402
403 // Boolean flags (0/1).
404 'public' => (int) pods_v( 'public', $taxonomy, 1 ),
405 'publicly_queryable' => (int) pods_v( 'publicly_queryable', $taxonomy, 1 ),
406 'hierarchical' => (int) pods_v( 'hierarchical', $taxonomy ),
407 'show_ui' => (int) pods_v( 'show_ui', $taxonomy, 1 ),
408 'show_in_menu' => (int) pods_v( 'show_in_menu', $taxonomy, 1 ),
409 'show_in_nav_menus' => (int) pods_v( 'show_in_nav_menus', $taxonomy, 1 ),
410 'query_var' => (int) pods_v( 'query_var', $taxonomy, 1 ),
411 'rewrite' => (int) pods_v( 'rewrite', $taxonomy, 1 ),
412 'rewrite_with_front' => (int) pods_v( 'rewrite_withfront', $taxonomy ),
413 'rewrite_hierarchical' => (int) pods_v( 'rewrite_hierarchical', $taxonomy ),
414 'show_admin_column' => (int) pods_v( 'show_admin_column', $taxonomy ),
415 'show_in_rest' => (int) pods_v( 'show_in_rest', $taxonomy ),
416 'show_in_quick_edit' => (int) pods_v( 'show_in_quick_edit', $taxonomy ),
417 ];
418
419 // Migrate attach-to
420 $attach = $post_types;
421
422 foreach ( $attach as $type_name ) {
423 $params[ 'built_in_post_types_' . $type_name ] = 1;
424 }
425
426 if ( ! is_object( $this->api ) ) {
427 $this->api = pods_api();
428 }
429
430 $pod = $this->api->load_pod( array( 'name' => pods_clean_name( $params['name'] ) ), false );
431
432 if ( ! empty( $pod ) ) {
433 return pods_error( sprintf( __( 'Pod with the name %s already exists', 'pods' ), pods_clean_name( $params['name'] ) ) );
434 }
435
436 $id = (int) $this->api->save_pod( $params );
437
438 if ( empty( $id ) ) {
439 return false;
440 }
441
442 $pod = $this->api->load_pod( array( 'id' => $id ), false );
443
444 if ( empty( $pod ) ) {
445 return false;
446 }
447
448 if ( $pod['name'] != $params['name'] ) {
449 $this->api->rename_wp_object_type( $params['type '], $params['name'], $pod['name'] );
450 }
451
452 return $id;
453 }
454
455 /**
456 *
457 * @since 2.0.0
458 */
459 public function clean() {
460 if ( ! is_null( $this->post_option_name ) ) {
461 delete_option( $this->post_option_name );
462 }
463
464 if ( ! is_null( $this->taxonomy_option_name ) ) {
465 delete_option( $this->taxonomy_option_name );
466 }
467
468 }
469
470 /**
471 * @param array $option_name_list List of possible option names.
472 *
473 * @return null|string The first found option name, or NULL if none were found
474 */
475 private function get_option_name( $option_name_list ) {
476
477 $option_name_list = (array) $option_name_list;
478
479 foreach ( $option_name_list as $this_option_name ) {
480 if ( null !== get_option( $this_option_name, null ) ) {
481 return $this_option_name;
482 }
483 }
484
485 return null;
486
487 }
488
489 }
490