Pods_Migrate_CPTUI

The Pods - Custom Content Types and Fields Pods Migrate CPTUI class.

Defined (1)

The class is defined in the following location(s).

/components/Migrate-CPTUI/Migrate-CPTUI.php  
  1. class Pods_Migrate_CPTUI extends PodsComponent { 
  2.  
  3. /** @var array 
  4. * Support option names for multiple versions, list from newest to oldest 
  5. */ 
  6. private $post_option_name_list = array( 
  7. 'cptui_post_types',  
  8. 'cpt_custom_post_types' 
  9. ); 
  10.  
  11. /** @var array 
  12. * Support option names for multiple versions, list from newest to oldest 
  13. */ 
  14. private $taxonomy_option_name_list = array( 
  15. 'cptui_taxonomies',  
  16. 'cpt_custom_tax_types' 
  17. ); 
  18.  
  19. private $api = null; 
  20.  
  21. private $post_option_name = null; 
  22.  
  23. private $taxonomy_option_name = null; 
  24.  
  25. private $post_types = array(); 
  26.  
  27. private $taxonomies = array(); 
  28.  
  29. /** 
  30. * Do things like register scripts and stylesheets 
  31. * @since 2.0 
  32. */ 
  33. public function __construct() { 
  34.  
  35. $this->post_option_name = $this->get_option_name( $this->post_option_name_list ); 
  36. if ( ! is_null( $this->post_option_name ) ) { 
  37. $this->post_types = (array) get_option( $this->post_option_name, array() ); 
  38. $this->taxonomy_option_name = $this->get_option_name( $this->taxonomy_option_name_list ); 
  39. if ( ! is_null( $this->taxonomy_option_name ) ) { 
  40. $this->taxonomies = (array) get_option( $this->taxonomy_option_name, array() ); 
  41.  
  42. /** 
  43. * Enqueue styles 
  44. * @since 2.0 
  45. */ 
  46. public function admin_assets () { 
  47. wp_enqueue_style( 'pods-wizard' ); 
  48.  
  49. /** 
  50. * Show the Admin 
  51. */ 
  52. public function admin ( $options, $component ) { 
  53. $post_types = (array) $this->post_types; 
  54. $taxonomies = (array) $this->taxonomies; 
  55.  
  56. $method = 'migrate'; // ajax_migrate 
  57.  
  58. pods_view( PODS_DIR . 'components/Migrate-CPTUI/ui/wizard.php', compact( array_keys( get_defined_vars() ) ) ); 
  59.  
  60. /** 
  61. * Handle the Migration AJAX 
  62. * @param $params 
  63. */ 
  64. public function ajax_migrate ( $params ) { 
  65. $post_types = (array) $this->post_types; 
  66. $taxonomies = (array) $this->taxonomies; 
  67.  
  68. $migrate_post_types = array(); 
  69.  
  70. if ( isset( $params->post_type ) && !empty( $params->post_type ) ) { 
  71. foreach ( $params->post_type as $post_type => $checked ) { 
  72. if ( true === (boolean) $checked ) 
  73. $migrate_post_types[] = $post_type; 
  74.  
  75. $migrate_taxonomies = array(); 
  76.  
  77. if ( isset( $params->taxonomy ) && !empty( $params->taxonomy ) ) { 
  78. foreach ( $params->taxonomy as $taxonomy => $checked ) { 
  79. if ( true === (boolean) $checked ) 
  80. $migrate_taxonomies[] = $taxonomy; 
  81.  
  82. foreach ( $post_types as $k => $post_type ) { 
  83. if ( !in_array( pods_var( 'name', $post_type ), $migrate_post_types ) ) 
  84. continue; 
  85.  
  86. $id = $this->migrate_post_type( $post_type ); 
  87.  
  88. if ( 0 < $id ) 
  89. unset( $post_types[ $k ] ); 
  90.  
  91. foreach ( $taxonomies as $k => $taxonomy ) { 
  92. if ( !in_array( pods_var( 'name', $taxonomy ), $migrate_taxonomies ) ) 
  93. continue; 
  94.  
  95. $id = $this->migrate_taxonomy( $taxonomy ); 
  96.  
  97. if ( 0 < $id ) 
  98. unset( $taxonomies[ $k ] ); 
  99.  
  100. if ( 1 == pods_var( 'cleanup', $params, 0 ) ) { 
  101. if ( ! empty( $post_types ) ) { 
  102. if ( ! is_null( $this->post_option_name ) ) { 
  103. update_option( $this->post_option_name, $post_types ); 
  104. } else { 
  105. if ( ! is_null( $this->post_option_name ) ) { 
  106. delete_option( $this->post_option_name ); 
  107.  
  108. if ( ! empty( $taxonomies ) ) { 
  109. if ( ! is_null( $this->taxonomy_option_name ) ) { 
  110. update_option( $this->taxonomy_option_name, $taxonomies ); 
  111. } else { 
  112. if ( ! is_null( $this->taxonomy_option_name ) ) { 
  113. delete_option( $this->taxonomy_option_name ); 
  114.  
  115. /** 
  116. * @since 2.0 
  117. */ 
  118. private function migrate_post_type ( $post_type ) { 
  119. $params = array( 
  120. 'type' => 'post_type',  
  121. 'storage' => 'meta',  
  122. 'object' => '',  
  123. 'name' => pods_var_raw( 'name', $post_type ),  
  124. 'label' => pods_var_raw( 'label', $post_type ),  
  125. 'label_singular' => pods_var_raw( 'singular_label', $post_type ),  
  126. 'description' => pods_var_raw( 'description', $post_type ),  
  127. 'public' => pods_var_raw( 'public', $post_type ),  
  128. 'show_ui' => (int) pods_var_raw( 'show_ui', $post_type ),  
  129. 'has_archive' => (int) pods_var_raw( 'has_archive', $post_type ),  
  130. 'exclude_from_search' => (int) pods_var_raw( 'exclude_from_search', $post_type ),  
  131. 'capability_type' => pods_var_raw( 'capability_type', $post_type ), //--!! Needs sanity checking? 
  132. 'hierarchical' => (int) pods_var_raw( 'hierarchical', $post_type ),  
  133. 'rewrite' => (int) pods_var_raw( 'rewrite', $post_type ),  
  134. 'rewrite_custom_slug' => pods_var_raw( 'rewrite_slug', $post_type ),  
  135. 'query_var' => (int) pods_var_raw( 'query_var', $post_type ),  
  136. 'menu_position' => (int) pods_var_raw( 'menu_position', $post_type ),  
  137. 'show_in_menu' => (int) pods_var_raw( 'show_in_menu', $post_type ),  
  138. 'menu_string' => pods_var_raw( 'show_in_menu_string', $post_type ),  
  139.  
  140. // 'supports' argument to register_post_type() 
  141. 'supports_title' => ( is_array( $post_type[ 0 ] ) && in_array( 'title', $post_type[ 0 ] ) ),  
  142. 'supports_editor' => ( is_array( $post_type[ 0 ] ) && in_array( 'editor', $post_type[ 0 ] ) ),  
  143. 'supports_excerpt' => ( is_array( $post_type[ 0 ] ) && in_array( 'excerpt', $post_type[ 0 ] ) ),  
  144. 'supports_trackbacks' => ( is_array( $post_type[ 0 ] ) && in_array( 'trackbacks', $post_type[ 0 ] ) ),  
  145. 'supports_custom_fields' => ( is_array( $post_type[ 0 ] ) && in_array( 'custom-fields', $post_type[ 0 ] ) ),  
  146. 'supports_comments' => ( is_array( $post_type[ 0 ] ) && in_array( 'comments', $post_type[ 0 ] ) ),  
  147. 'supports_revisions' => ( is_array( $post_type[ 0 ] ) && in_array( 'revisions', $post_type[ 0 ] ) ),  
  148. 'supports_thumbnail' => ( is_array( $post_type[ 0 ] ) && in_array( 'thumbnail', $post_type[ 0 ] ) ),  
  149. 'supports_author' => ( is_array( $post_type[ 0 ] ) && in_array( 'author', $post_type[ 0 ] ) ),  
  150. 'supports_page_attributes' => ( is_array( $post_type[ 0 ] ) && in_array( 'page-attributes', $post_type[ 0 ] ) ),  
  151.  
  152. // 'labels' argument to register_post_type() 
  153. 'menu_name' => pods_var_raw( 'menu_name', $post_type[ 2 ] ),  
  154. 'label_add_new' => pods_var_raw( 'add_new', $post_type[ 2 ] ),  
  155. 'label_add_new_item' => pods_var_raw( 'add_new_item', $post_type[ 2 ] ),  
  156. 'label_edit' => pods_var_raw( 'edit', $post_type[ 2 ] ),  
  157. 'label_edit_item' => pods_var_raw( 'edit_item', $post_type[ 2 ] ),  
  158. 'label_new_item' => pods_var_raw( 'new_item', $post_type[ 2 ] ),  
  159. 'label_view' => pods_var_raw( 'view', $post_type[ 2 ] ),  
  160. 'label_view_item' => pods_var_raw( 'view_item', $post_type[ 2 ] ),  
  161. 'label_search_items' => pods_var_raw( 'search_items', $post_type[ 2 ] ),  
  162. 'label_not_found' => pods_var_raw( 'not_found', $post_type[ 2 ] ),  
  163. 'label_not_found_in_trash' => pods_var_raw( 'not_found_in_trash', $post_type[ 2 ] ),  
  164. 'label_parent' => pods_var_raw( 'parent', $post_type[ 2 ] ),  
  165. ); 
  166.  
  167. // Migrate built-in taxonomies 
  168. $builtin = $post_type[ 1 ]; 
  169. if ( is_array( $builtin ) ) { 
  170. foreach ( $builtin as $taxonomy_name ) { 
  171. $params[ 'built_in_taxonomies_' . $taxonomy_name ] = 1; 
  172.  
  173. if ( !is_object( $this->api ) ) 
  174. $this->api = pods_api(); 
  175.  
  176. $pod = $this->api->load_pod( array( 'name' => pods_clean_name( $params[ 'name' ] ) ), false ); 
  177.  
  178. if ( !empty( $pod ) ) 
  179. return pods_error( sprintf( __( 'Pod with the name %s already exists', 'pods' ), pods_clean_name( $params[ 'name' ] ) ) ); 
  180.  
  181. $id = (int) $this->api->save_pod( $params ); 
  182.  
  183. if ( empty( $id ) ) 
  184. return false; 
  185.  
  186. $pod = $this->api->load_pod( array( 'id' => $id ), false ); 
  187.  
  188. if ( empty( $pod ) ) 
  189. return false; 
  190.  
  191. if ( $pod[ 'name' ] != $params[ 'name' ] ) 
  192. $this->api->rename_wp_object_type( $params[ 'type ' ], $params[ 'name' ], $pod[ 'name' ] ); 
  193.  
  194. return $id; 
  195.  
  196. /** 
  197. * @since 2.0 
  198. */ 
  199. private function migrate_taxonomy ( $taxonomy ) { 
  200.  
  201. $params = array( 
  202. 'type' => 'taxonomy',  
  203. 'storage' => 'table',  
  204. 'object' => '',  
  205. 'name' => pods_var_raw( 'name', $taxonomy ),  
  206. 'label' => pods_var_raw( 'label', $taxonomy ),  
  207. 'label_singular' => pods_var_raw( 'singular_label', $taxonomy ),  
  208. 'public' => 1,  
  209. 'show_ui' => (int) pods_var_raw( 'show_ui', $taxonomy ),  
  210. 'hierarchical' => (int) pods_var_raw( 'hierarchical', $taxonomy ),  
  211. 'query_var' => (int) pods_var_raw( 'query_var', $taxonomy ),  
  212. 'rewrite' => (int) pods_var_raw( 'rewrite', $taxonomy ),  
  213. 'rewrite_custom_slug' => pods_var_raw( 'rewrite_slug', $taxonomy ),  
  214. 'label_search_items' => pods_var_raw( 'search_items', $taxonomy[ 0 ] ),  
  215. 'label_popular_items' => pods_var_raw( 'popular_items', $taxonomy[ 0 ] ),  
  216. 'label_all_items' => pods_var_raw( 'all_items', $taxonomy[ 0 ] ),  
  217. 'label_parent' => pods_var_raw( 'parent_item', $taxonomy[ 0 ] ),  
  218. 'label_parent_item_colon' => pods_var_raw( 'parent_item_colon', $taxonomy[ 0 ] ),  
  219. 'label_edit' => pods_var_raw( 'edit_item', $taxonomy[ 0 ] ),  
  220. 'label_update_item' => pods_var_raw( 'update_item', $taxonomy[ 0 ] ),  
  221. 'label_add_new' => pods_var_raw( 'add_new_item', $taxonomy[ 0 ] ),  
  222. 'label_new_item' => pods_var_raw( 'new_item_name', $taxonomy[ 0 ] ),  
  223. 'label_separate_items_with_commas' => pods_var_raw( 'separate_items_with_commas', $taxonomy[ 0 ] ),  
  224. 'label_add_or_remove_items' => pods_var_raw( 'add_or_remove_items', $taxonomy[ 0 ] ),  
  225. 'label_choose_from_the_most_used' => pods_var_raw( 'choose_from_most_used', $taxonomy[ 0 ] ) 
  226. ); 
  227.  
  228. // Migrate attach-to 
  229. $attach = $taxonomy[ 1 ]; 
  230. if ( is_array( $attach ) ) { 
  231. foreach ( $attach as $type_name ) { 
  232. $params[ 'built_in_post_types_' . $type_name ] = 1; 
  233.  
  234. if ( !is_object( $this->api ) ) 
  235. $this->api = pods_api(); 
  236.  
  237. $pod = $this->api->load_pod( array( 'name' => pods_clean_name( $params[ 'name' ] ) ), false ); 
  238.  
  239. if ( !empty( $pod ) ) 
  240. return pods_error( sprintf( __( 'Pod with the name %s already exists', 'pods' ), pods_clean_name( $params[ 'name' ] ) ) ); 
  241.  
  242. $id = (int) $this->api->save_pod( $params ); 
  243.  
  244. if ( empty( $id ) ) 
  245. return false; 
  246.  
  247. $pod = $this->api->load_pod( array( 'id' => $id ), false ); 
  248.  
  249. if ( empty( $pod ) ) 
  250. return false; 
  251.  
  252. if ( $pod[ 'name' ] != $params[ 'name' ] ) 
  253. $this->api->rename_wp_object_type( $params[ 'type ' ], $params[ 'name' ], $pod[ 'name' ] ); 
  254.  
  255. return $id; 
  256.  
  257. /** 
  258. * @since 2.0 
  259. */ 
  260. public function clean() { 
  261.  
  262. if ( ! is_null( $this->post_option_name ) ) { 
  263. delete_option( $this->post_option_name ); 
  264.  
  265. if ( ! is_null( $this->taxonomy_option_name ) ) { 
  266. delete_option( $this->taxonomy_option_name ); 
  267.  
  268.  
  269. /** 
  270. * @param array $option_name_list List of possible option names. 
  271. * @return null|string The first found option name, or NULL if none were found 
  272. */ 
  273. private function get_option_name( $option_name_list ) { 
  274.  
  275. $option_name_list = (array) $option_name_list; 
  276.  
  277. foreach ( $option_name_list as $this_option_name ) { 
  278. if ( null !== get_option( $this_option_name, null ) ) { 
  279. return $this_option_name; 
  280.  
  281. return null; 
  282.  
  283.