Migrate-CPTUI.php
382 lines
| 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_var( '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_var( '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 == pods_var( '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 | |
| 185 | $params = array( |
| 186 | 'type' => 'post_type', |
| 187 | 'storage' => 'meta', |
| 188 | 'object' => '', |
| 189 | 'name' => pods_var_raw( 'name', $post_type ), |
| 190 | 'label' => pods_var_raw( 'label', $post_type ), |
| 191 | 'label_singular' => pods_var_raw( 'singular_label', $post_type ), |
| 192 | 'description' => pods_var_raw( 'description', $post_type ), |
| 193 | 'public' => pods_var_raw( 'public', $post_type ), |
| 194 | 'show_ui' => (int) pods_var_raw( 'show_ui', $post_type ), |
| 195 | 'has_archive' => (int) pods_var_raw( 'has_archive', $post_type ), |
| 196 | 'exclude_from_search' => (int) pods_var_raw( 'exclude_from_search', $post_type ), |
| 197 | 'capability_type' => pods_var_raw( 'capability_type', $post_type ), |
| 198 | // --!! Needs sanity checking? |
| 199 | 'hierarchical' => (int) pods_var_raw( 'hierarchical', $post_type ), |
| 200 | 'rewrite' => (int) pods_var_raw( 'rewrite', $post_type ), |
| 201 | 'rewrite_custom_slug' => pods_var_raw( 'rewrite_slug', $post_type ), |
| 202 | 'query_var' => (int) pods_var_raw( 'query_var', $post_type ), |
| 203 | 'menu_position' => (int) pods_var_raw( 'menu_position', $post_type ), |
| 204 | 'show_in_menu' => (int) pods_var_raw( 'show_in_menu', $post_type ), |
| 205 | 'menu_string' => pods_var_raw( 'show_in_menu_string', $post_type ), |
| 206 | |
| 207 | // 'supports' argument to register_post_type() |
| 208 | 'supports_title' => ( is_array( $post_type[0] ) && in_array( 'title', $post_type[0], true ) ), |
| 209 | 'supports_editor' => ( is_array( $post_type[0] ) && in_array( 'editor', $post_type[0], true ) ), |
| 210 | 'supports_excerpt' => ( is_array( $post_type[0] ) && in_array( 'excerpt', $post_type[0], true ) ), |
| 211 | 'supports_trackbacks' => ( is_array( $post_type[0] ) && in_array( 'trackbacks', $post_type[0], true ) ), |
| 212 | 'supports_custom_fields' => ( is_array( $post_type[0] ) && in_array( 'custom-fields', $post_type[0], true ) ), |
| 213 | 'supports_comments' => ( is_array( $post_type[0] ) && in_array( 'comments', $post_type[0], true ) ), |
| 214 | 'supports_revisions' => ( is_array( $post_type[0] ) && in_array( 'revisions', $post_type[0], true ) ), |
| 215 | 'supports_thumbnail' => ( is_array( $post_type[0] ) && in_array( 'thumbnail', $post_type[0], true ) ), |
| 216 | 'supports_author' => ( is_array( $post_type[0] ) && in_array( 'author', $post_type[0], true ) ), |
| 217 | 'supports_page_attributes' => ( is_array( $post_type[0] ) && in_array( 'page-attributes', $post_type[0], true ) ), |
| 218 | |
| 219 | // 'labels' argument to register_post_type() |
| 220 | 'menu_name' => pods_var_raw( 'menu_name', $post_type[2] ), |
| 221 | 'label_add_new' => pods_var_raw( 'add_new', $post_type[2] ), |
| 222 | 'label_add_new_item' => pods_var_raw( 'add_new_item', $post_type[2] ), |
| 223 | 'label_edit' => pods_var_raw( 'edit', $post_type[2] ), |
| 224 | 'label_edit_item' => pods_var_raw( 'edit_item', $post_type[2] ), |
| 225 | 'label_new_item' => pods_var_raw( 'new_item', $post_type[2] ), |
| 226 | 'label_view' => pods_var_raw( 'view', $post_type[2] ), |
| 227 | 'label_view_item' => pods_var_raw( 'view_item', $post_type[2] ), |
| 228 | 'label_search_items' => pods_var_raw( 'search_items', $post_type[2] ), |
| 229 | 'label_not_found' => pods_var_raw( 'not_found', $post_type[2] ), |
| 230 | 'label_not_found_in_trash' => pods_var_raw( 'not_found_in_trash', $post_type[2] ), |
| 231 | 'label_parent' => pods_var_raw( 'parent', $post_type[2] ), |
| 232 | ); |
| 233 | |
| 234 | // Migrate built-in taxonomies |
| 235 | $builtin = $post_type[1]; |
| 236 | if ( is_array( $builtin ) ) { |
| 237 | foreach ( $builtin as $taxonomy_name ) { |
| 238 | $params[ 'built_in_taxonomies_' . $taxonomy_name ] = 1; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | if ( ! is_object( $this->api ) ) { |
| 243 | $this->api = pods_api(); |
| 244 | } |
| 245 | |
| 246 | $pod = $this->api->load_pod( array( 'name' => pods_clean_name( $params['name'] ) ), false ); |
| 247 | |
| 248 | if ( ! empty( $pod ) ) { |
| 249 | return pods_error( sprintf( __( 'Pod with the name %s already exists', 'pods' ), pods_clean_name( $params['name'] ) ) ); |
| 250 | } |
| 251 | |
| 252 | $id = (int) $this->api->save_pod( $params ); |
| 253 | |
| 254 | if ( empty( $id ) ) { |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | $pod = $this->api->load_pod( array( 'id' => $id ), false ); |
| 259 | |
| 260 | if ( empty( $pod ) ) { |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | if ( $pod['name'] != $params['name'] ) { |
| 265 | $this->api->rename_wp_object_type( $params['type '], $params['name'], $pod['name'] ); |
| 266 | } |
| 267 | |
| 268 | return $id; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * |
| 273 | * |
| 274 | * @since 2.0.0 |
| 275 | * |
| 276 | * @param $taxonomy |
| 277 | * |
| 278 | * @return bool|int|mixed |
| 279 | */ |
| 280 | private function migrate_taxonomy( $taxonomy ) { |
| 281 | |
| 282 | $params = array( |
| 283 | 'type' => 'taxonomy', |
| 284 | 'storage' => 'table', |
| 285 | 'object' => '', |
| 286 | 'name' => pods_var_raw( 'name', $taxonomy ), |
| 287 | 'label' => pods_var_raw( 'label', $taxonomy ), |
| 288 | 'label_singular' => pods_var_raw( 'singular_label', $taxonomy ), |
| 289 | 'public' => 1, |
| 290 | 'show_ui' => (int) pods_var_raw( 'show_ui', $taxonomy ), |
| 291 | 'hierarchical' => (int) pods_var_raw( 'hierarchical', $taxonomy ), |
| 292 | 'query_var' => (int) pods_var_raw( 'query_var', $taxonomy ), |
| 293 | 'rewrite' => (int) pods_var_raw( 'rewrite', $taxonomy ), |
| 294 | 'rewrite_custom_slug' => pods_var_raw( 'rewrite_slug', $taxonomy ), |
| 295 | 'label_search_items' => pods_var_raw( 'search_items', $taxonomy[0] ), |
| 296 | 'label_popular_items' => pods_var_raw( 'popular_items', $taxonomy[0] ), |
| 297 | 'label_all_items' => pods_var_raw( 'all_items', $taxonomy[0] ), |
| 298 | 'label_parent' => pods_var_raw( 'parent_item', $taxonomy[0] ), |
| 299 | 'label_parent_item_colon' => pods_var_raw( 'parent_item_colon', $taxonomy[0] ), |
| 300 | 'label_edit' => pods_var_raw( 'edit_item', $taxonomy[0] ), |
| 301 | 'label_update_item' => pods_var_raw( 'update_item', $taxonomy[0] ), |
| 302 | 'label_add_new' => pods_var_raw( 'add_new_item', $taxonomy[0] ), |
| 303 | 'label_new_item' => pods_var_raw( 'new_item_name', $taxonomy[0] ), |
| 304 | 'label_separate_items_with_commas' => pods_var_raw( 'separate_items_with_commas', $taxonomy[0] ), |
| 305 | 'label_add_or_remove_items' => pods_var_raw( 'add_or_remove_items', $taxonomy[0] ), |
| 306 | 'label_choose_from_the_most_used' => pods_var_raw( 'choose_from_most_used', $taxonomy[0] ), |
| 307 | ); |
| 308 | |
| 309 | // Migrate attach-to |
| 310 | $attach = $taxonomy[1]; |
| 311 | if ( is_array( $attach ) ) { |
| 312 | foreach ( $attach as $type_name ) { |
| 313 | $params[ 'built_in_post_types_' . $type_name ] = 1; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | if ( ! is_object( $this->api ) ) { |
| 318 | $this->api = pods_api(); |
| 319 | } |
| 320 | |
| 321 | $pod = $this->api->load_pod( array( 'name' => pods_clean_name( $params['name'] ) ), false ); |
| 322 | |
| 323 | if ( ! empty( $pod ) ) { |
| 324 | return pods_error( sprintf( __( 'Pod with the name %s already exists', 'pods' ), pods_clean_name( $params['name'] ) ) ); |
| 325 | } |
| 326 | |
| 327 | $id = (int) $this->api->save_pod( $params ); |
| 328 | |
| 329 | if ( empty( $id ) ) { |
| 330 | return false; |
| 331 | } |
| 332 | |
| 333 | $pod = $this->api->load_pod( array( 'id' => $id ), false ); |
| 334 | |
| 335 | if ( empty( $pod ) ) { |
| 336 | return false; |
| 337 | } |
| 338 | |
| 339 | if ( $pod['name'] != $params['name'] ) { |
| 340 | $this->api->rename_wp_object_type( $params['type '], $params['name'], $pod['name'] ); |
| 341 | } |
| 342 | |
| 343 | return $id; |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * |
| 348 | * @since 2.0.0 |
| 349 | */ |
| 350 | public function clean() { |
| 351 | |
| 352 | if ( ! is_null( $this->post_option_name ) ) { |
| 353 | delete_option( $this->post_option_name ); |
| 354 | } |
| 355 | |
| 356 | if ( ! is_null( $this->taxonomy_option_name ) ) { |
| 357 | delete_option( $this->taxonomy_option_name ); |
| 358 | } |
| 359 | |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * @param array $option_name_list List of possible option names. |
| 364 | * |
| 365 | * @return null|string The first found option name, or NULL if none were found |
| 366 | */ |
| 367 | private function get_option_name( $option_name_list ) { |
| 368 | |
| 369 | $option_name_list = (array) $option_name_list; |
| 370 | |
| 371 | foreach ( $option_name_list as $this_option_name ) { |
| 372 | if ( null !== get_option( $this_option_name, null ) ) { |
| 373 | return $this_option_name; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | return null; |
| 378 | |
| 379 | } |
| 380 | |
| 381 | } |
| 382 |