PodsUpgrade_2_0_0

The Pods - Custom Content Types and Fields PodsUpgrade 2 0 class.

Defined (1)

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

/sql/upgrade/PodsUpgrade_2_0_0.php  
  1. class PodsUpgrade_2_0_0 extends PodsUpgrade { 
  2.  
  3. /** 
  4. * @var string 
  5. */ 
  6. protected $version = '2.0.0'; 
  7.  
  8. /** 
  9. * @return array|bool|int|mixed|null|void 
  10. */ 
  11. public function prepare_pods () { 
  12. /** 
  13. * @var $wpdb WPDB 
  14. */ 
  15. global $wpdb; 
  16.  
  17. if ( !in_array( "{$wpdb->prefix}pod_types", $this->tables ) ) 
  18. return pods_error( __( 'Table not found, it cannot be migrated', 'pods' ) ); 
  19.  
  20. $count = pods_query( "SELECT COUNT(*) AS `count` FROM `@wp_pod_types`", false ); 
  21.  
  22. if ( !empty( $count ) ) 
  23. $count = (int) $count[ 0 ]->count; 
  24. else 
  25. $count = 0; 
  26.  
  27. return $count; 
  28.  
  29. /** 
  30. * @return array|bool|int|mixed|null|void 
  31. */ 
  32. public function prepare_fields () { 
  33. /** 
  34. * @var $wpdb WPDB 
  35. */ 
  36. global $wpdb; 
  37.  
  38. if ( !in_array( "{$wpdb->prefix}pod_fields", $this->tables ) ) 
  39. return pods_error( __( 'Table not found, it cannot be migrated', 'pods' ) ); 
  40.  
  41. $count = pods_query( "SELECT COUNT(*) AS `count` FROM `@wp_pod_fields`", false ); 
  42.  
  43. if ( !empty( $count ) ) 
  44. $count = (int) $count[ 0 ]->count; 
  45. else 
  46. $count = 0; 
  47.  
  48. return $count; 
  49.  
  50. /** 
  51. * @return array|bool|int|mixed|null|void 
  52. */ 
  53. public function prepare_relationships () { 
  54. /** 
  55. * @var $wpdb WPDB 
  56. */ 
  57. global $wpdb; 
  58.  
  59. if ( !in_array( "{$wpdb->prefix}pod_rel", $this->tables ) ) 
  60. return pods_error( __( 'Table not found, it cannot be migrated', 'pods' ) ); 
  61.  
  62. $count = pods_query( "SELECT COUNT(*) AS `count` FROM `@wp_pod_rel`", false ); 
  63.  
  64. if ( !empty( $count ) ) 
  65. $count = (int) $count[ 0 ]->count; 
  66. else 
  67. $count = 0; 
  68.  
  69. return $count; 
  70.  
  71. /** 
  72. * @return array|bool|int|mixed|null|void 
  73. */ 
  74. public function prepare_index () { 
  75. /** 
  76. * @var $wpdb WPDB 
  77. */ 
  78. global $wpdb; 
  79.  
  80. if ( !in_array( "{$wpdb->prefix}pod", $this->tables ) ) 
  81. return pods_error( __( 'Table not found, it cannot be migrated', 'pods' ) ); 
  82.  
  83. $count = pods_query( "SELECT COUNT(*) AS `count` FROM `@wp_pod`", false ); 
  84.  
  85. if ( !empty( $count ) ) 
  86. $count = (int) $count[ 0 ]->count; 
  87. else 
  88. $count = 0; 
  89.  
  90. return $count; 
  91.  
  92. /** 
  93. * @return array|bool|int|mixed|null|void 
  94. */ 
  95. public function prepare_templates () { 
  96. /** 
  97. * @var $wpdb WPDB 
  98. */ 
  99. global $wpdb; 
  100.  
  101. if ( !in_array( "{$wpdb->prefix}pod_templates", $this->tables ) ) 
  102. return pods_error( __( 'Table not found, it cannot be migrated', 'pods' ) ); 
  103.  
  104. $count = pods_query( "SELECT COUNT(*) AS `count` FROM `@wp_pod_templates`", false ); 
  105.  
  106. if ( !empty( $count ) ) 
  107. $count = (int) $count[ 0 ]->count; 
  108. else 
  109. $count = 0; 
  110.  
  111. return $count; 
  112.  
  113. /** 
  114. * @return array|bool|int|mixed|null|void 
  115. */ 
  116. public function prepare_pages () { 
  117. /** 
  118. * @var $wpdb WPDB 
  119. */ 
  120. global $wpdb; 
  121.  
  122. if ( !in_array( "{$wpdb->prefix}pod_pages", $this->tables ) ) 
  123. return pods_error( __( 'Table not found, it cannot be migrated', 'pods' ) ); 
  124.  
  125. $count = pods_query( "SELECT COUNT(*) AS `count` FROM `@wp_pod_pages`", false ); 
  126.  
  127. if ( !empty( $count ) ) 
  128. $count = (int) $count[ 0 ]->count; 
  129. else 
  130. $count = 0; 
  131.  
  132. return $count; 
  133.  
  134. /** 
  135. * @return array|bool|int|mixed|null|void 
  136. */ 
  137. public function prepare_helpers () { 
  138. /** 
  139. * @var $wpdb WPDB 
  140. */ 
  141. global $wpdb; 
  142.  
  143. if ( !in_array( "{$wpdb->prefix}pod_helpers", $this->tables ) ) 
  144. return pods_error( __( 'Table not found, it cannot be migrated', 'pods' ) ); 
  145.  
  146. $count = pods_query( "SELECT COUNT(*) AS `count` FROM `@wp_pod_helpers`", false ); 
  147.  
  148. if ( !empty( $count ) ) 
  149. $count = (int) $count[ 0 ]->count; 
  150. else 
  151. $count = 0; 
  152.  
  153. return $count; 
  154.  
  155. /** 
  156. * @param $params 
  157. * @return array|bool|int|mixed|null|void 
  158. */ 
  159. public function prepare_pod ( $params ) { 
  160. /** 
  161. * @var $wpdb WPDB 
  162. */ 
  163. global $wpdb; 
  164.  
  165. if ( !isset( $params->pod ) ) 
  166. return pods_error( __( 'Invalid Pod.', 'pods' ) ); 
  167.  
  168. $pod = pods_sanitize( pods_clean_name( $params->pod ) ); 
  169.  
  170. if ( !in_array( "{$wpdb->prefix}pod_tbl_{$pod}", $this->tables ) ) 
  171. return pods_error( __( 'Table not found, it cannot be migrated', 'pods' ) ); 
  172.  
  173. $count = pods_query( "SELECT COUNT(*) AS `count` FROM `@wp_pod_tbl_{$pod}`", false ); 
  174.  
  175. if ( !empty( $count ) ) 
  176. $count = (int) $count[ 0 ]->count; 
  177. else 
  178. $count = 0; 
  179.  
  180. $pod_type = pods_query( "SELECT `id` FROM `@wp_pod_types` WHERE `name` = '{$pod}'", false ); 
  181.  
  182. if ( !empty( $pod_type ) ) 
  183. $pod_type = (int) $pod_type[ 0 ]->id; 
  184. else 
  185. return pods_error( __( 'Pod not found, it cannot be migrated', 'pods' ) ); 
  186.  
  187. $fields = array( 'id' ); 
  188.  
  189. $field_rows = pods_query( "SELECT `id`, `name`, `coltype` FROM `@wp_pod_fields` WHERE `datatype` = {$pod_type} ORDER BY `weight`, `name`" ); 
  190.  
  191. if ( !empty( $field_rows ) ) { 
  192. foreach ( $field_rows as $field ) { 
  193. if ( !in_array( $field->coltype, array( 'pick', 'file' ) ) ) 
  194. $fields[] = $field->name; 
  195.  
  196. $columns = PodsData::get_table_columns( "{$wpdb->prefix}pod_tbl_{$pod}" ); 
  197.  
  198. $errors = array(); 
  199.  
  200. foreach ( $columns as $column => $info ) { 
  201. if ( !in_array( $column, $fields ) ) 
  202. $errors[] = "<strong>{$column}</strong> " . __( 'is a field in the table, but was not found in this pod - the field data will not be migrated.', 'pods' ); 
  203.  
  204. foreach ( $fields as $field ) { 
  205. if ( !isset( $columns[ $field ] ) ) 
  206. $errors[] = "<strong>{$field}</strong> " . __( 'is a field in this pod, but was not found in the table - the field data will not be migrated.', 'pods' ); 
  207.  
  208. if ( !empty( $errors ) ) 
  209. return pods_error( implode( '<br />', $errors ) ); 
  210.  
  211. return $count; 
  212.  
  213. /** 
  214. */ 
  215. public function migrate_1_x () { 
  216. $old_version = get_option( 'pods_version' ); 
  217.  
  218. if ( 0 < strlen( $old_version ) ) { 
  219. if ( false === strpos( $old_version, '.' ) ) 
  220. $old_version = pods_version_to_point( $old_version ); 
  221.  
  222. // Last DB change was 1.11 
  223. if ( version_compare( $old_version, '1.11', '<' ) ) { 
  224. do_action( 'pods_update', PODS_VERSION, $old_version ); 
  225.  
  226. if ( false !== apply_filters( 'pods_update_run', null, PODS_VERSION, $old_version ) ) 
  227. include_once( PODS_DIR . 'sql/update-1.x.php' ); 
  228.  
  229. do_action( 'pods_update_post', PODS_VERSION, $old_version ); 
  230.  
  231. return '1'; 
  232.  
  233. /** 
  234. * @return array|string 
  235. */ 
  236. public function migrate_pods () { 
  237. if ( true === $this->check_progress( __FUNCTION__ ) ) 
  238. return '1'; 
  239.  
  240. $sister_ids = (array) get_option( 'pods_framework_upgrade_2_0_sister_ids', array() ); 
  241.  
  242. $migration_limit = (int) apply_filters( 'pods_upgrade_pod_limit', 1 ); 
  243. $migration_limit = max( $migration_limit, 1 ); 
  244.  
  245. $last_id = (int) $this->check_progress( __FUNCTION__ ); 
  246.  
  247. $sql = " 
  248. SELECT * 
  249. FROM `@wp_pod_types` 
  250. WHERE {$last_id} < `id` 
  251. ORDER BY `id` 
  252. LIMIT 0, {$migration_limit} 
  253. "; 
  254.  
  255. $pod_types = pods_query( $sql ); 
  256.  
  257. $last_id = true; 
  258.  
  259. if ( !empty( $pod_types ) ) { 
  260. foreach ( $pod_types as $pod_type ) { 
  261. $field_rows = pods_query( "SELECT * FROM `@wp_pod_fields` WHERE `datatype` = {$pod_type->id} ORDER BY `weight`, `name`" ); 
  262.  
  263. $fields = array( 
  264. array( 
  265. 'name' => 'name',  
  266. 'label' => 'Name',  
  267. 'type' => 'text',  
  268. 'weight' => 0,  
  269. 'options' => array( 
  270. 'required' => 1,  
  271. 'text_max_length' => 128 
  272. ),  
  273. array( 
  274. 'name' => 'created',  
  275. 'label' => 'Date Created',  
  276. 'type' => 'datetime',  
  277. 'options' => array( 
  278. 'datetime_format' => 'ymd_slash',  
  279. 'datetime_time_type' => '12',  
  280. 'datetime_time_format' => 'h_mm_ss_A' 
  281. ),  
  282. 'weight' => 1 
  283. ),  
  284. array( 
  285. 'name' => 'modified',  
  286. 'label' => 'Date Modified',  
  287. 'type' => 'datetime',  
  288. 'options' => array( 
  289. 'datetime_format' => 'ymd_slash',  
  290. 'datetime_time_type' => '12',  
  291. 'datetime_time_format' => 'h_mm_ss_A' 
  292. ),  
  293. 'weight' => 2 
  294. ),  
  295. array( 
  296. 'name' => 'author',  
  297. 'label' => 'Author',  
  298. 'type' => 'pick',  
  299. 'pick_object' => 'user',  
  300. 'options' => array( 
  301. 'pick_format_type' => 'single',  
  302. 'pick_format_single' => 'autocomplete',  
  303. 'default_value' => '{@user.ID}' 
  304. ),  
  305. 'weight' => 3 
  306. ); 
  307.  
  308. $weight = 4; 
  309.  
  310. $found_fields = array(); 
  311.  
  312. foreach ( $field_rows as $row ) { 
  313. if ( 'name' == $row->name ) 
  314. continue; 
  315.  
  316. $old_name = $row->name; 
  317.  
  318. $row->name = pods_clean_name( $row->name ); 
  319.  
  320. if ( in_array( $row->name, array( 'created', 'modified', 'author' ) ) ) 
  321. $row->name .= '2'; 
  322.  
  323. $field_type = $row->coltype; 
  324.  
  325. if ( 'txt' == $field_type ) 
  326. $field_type = 'text'; 
  327. elseif ( 'desc' == $field_type ) 
  328. $field_type = 'wysiwyg'; 
  329. elseif ( 'code' == $field_type ) 
  330. $field_type = 'paragraph'; 
  331. elseif ( 'bool' == $field_type ) 
  332. $field_type = 'boolean'; 
  333. elseif ( 'num' == $field_type ) 
  334. $field_type = 'number'; 
  335. elseif ( 'date' == $field_type ) 
  336. $field_type = 'datetime'; 
  337.  
  338. $field_params = array( 
  339. 'name' => trim( $row->name ),  
  340. 'label' => trim( $row->label ),  
  341. 'description' => trim( $row->comment ),  
  342. 'type' => $field_type,  
  343. 'weight' => $weight,  
  344. 'options' => array( 
  345. 'required' => $row->required,  
  346. 'unique' => $row->unique,  
  347. 'input_helper' => $row->input_helper,  
  348. '_pods_1x_field_name' => $old_name,  
  349. '_pods_1x_field_id' => $row->id 
  350. ); 
  351.  
  352. if ( in_array( $field_params[ 'name' ], $found_fields ) ) 
  353. continue; 
  354.  
  355. $found_fields[] = $field_params[ 'name' ]; 
  356.  
  357. if ( 'pick' == $field_type ) { 
  358. $field_params[ 'pick_object' ] = 'pod-' . $row->pickval; 
  359.  
  360. if ( 'wp_user' == $row->pickval ) 
  361. $field_params[ 'pick_object' ] = 'user'; 
  362. elseif ( 'wp_post' == $row->pickval ) 
  363. $field_params[ 'pick_object' ] = 'post_type-post'; 
  364. elseif ( 'wp_page' == $row->pickval ) 
  365. $field_params[ 'pick_object' ] = 'post_type-page'; 
  366. elseif ( 'wp_taxonomy' == $row->pickval ) 
  367. $field_params[ 'pick_object' ] = 'taxonomy-category'; 
  368.  
  369. $field_params[ 'sister_id' ] = $row->sister_field_id; 
  370.  
  371. $sister_ids[ $row->id ] = $row->sister_field_id; // Old Sister Field ID 
  372. $field_params[ 'options' ][ '_pods_1x_sister_id' ] = $row->sister_field_id; 
  373.  
  374. $field_params[ 'options' ][ 'pick_filter' ] = $row->pick_filter; 
  375. $field_params[ 'options' ][ 'pick_orderby' ] = $row->pick_orderby; 
  376. $field_params[ 'options' ][ 'pick_display' ] = ''; 
  377. $field_params[ 'options' ][ 'pick_size' ] = 'medium'; 
  378.  
  379. if ( 1 == $row->multiple ) { 
  380. $field_params[ 'options' ][ 'pick_format_type' ] = 'multi'; 
  381. $field_params[ 'options' ][ 'pick_format_multi' ] = 'checkbox'; 
  382. $field_params[ 'options' ][ 'pick_limit' ] = 0; 
  383. else { 
  384. $field_params[ 'options' ][ 'pick_format_type' ] = 'single'; 
  385. $field_params[ 'options' ][ 'pick_format_single' ] = 'dropdown'; 
  386. $field_params[ 'options' ][ 'pick_limit' ] = 1; 
  387. elseif ( 'file' == $field_type ) { 
  388. $field_params[ 'options' ][ 'file_format_type' ] = 'multi'; 
  389. $field_params[ 'options' ][ 'file_type' ] = 'any'; 
  390. elseif ( 'number' == $field_type ) 
  391. $field_params[ 'options' ][ 'number_decimals' ] = 2; 
  392. elseif ( 'desc' == $row->coltype ) 
  393. $field_params[ 'options' ][ 'wysiwyg_editor' ] = 'tinymce'; 
  394. elseif ( 'text' == $field_type ) 
  395. $field_params[ 'options' ][ 'text_max_length' ] = 128; 
  396.  
  397. $fields[] = $field_params; 
  398.  
  399. $weight++; 
  400.  
  401. $pod_type->name = pods_sanitize( pods_clean_name( $pod_type->name ) ); 
  402.  
  403. $pod_params = array( 
  404. 'name' => $pod_type->name,  
  405. 'label' => $pod_type->label,  
  406. 'type' => 'pod',  
  407. 'storage' => 'table',  
  408. 'fields' => $fields,  
  409. 'options' => array( 
  410. 'pre_save_helpers' => $pod_type->pre_save_helpers,  
  411. 'post_save_helpers' => $pod_type->post_save_helpers,  
  412. 'pre_delete_helpers' => $pod_type->pre_drop_helpers,  
  413. 'post_delete_helpers' => $pod_type->post_drop_helpers,  
  414. 'show_in_menu' => $pod_type->is_toplevel,  
  415. 'detail_url' => $pod_type->detail_page,  
  416. 'pod_index' => 'name',  
  417. '_pods_1x_pod_id' => $pod_type->id 
  418. ),  
  419. ); 
  420.  
  421. if ( empty( $pod_params[ 'label' ] ) ) 
  422. $pod_params[ 'label' ] = ucwords( str_replace( '_', ' ', $pod_params[ 'name' ] ) ); 
  423.  
  424. $pod_id = $this->api->save_pod( $pod_params ); 
  425.  
  426. if ( 0 < $pod_id ) 
  427. $last_id = $pod_type->id; 
  428. else 
  429. pods_error( 'Error: ' . $pod_id ); 
  430.  
  431. update_option( 'pods_framework_upgrade_2_0_sister_ids', $sister_ids ); 
  432.  
  433. $this->update_progress( __FUNCTION__, $last_id ); 
  434.  
  435. if ( $migration_limit == count( $pod_types ) ) 
  436. return '-2'; 
  437. else 
  438. return '1'; 
  439.  
  440. /** 
  441. * @return string 
  442. */ 
  443. public function migrate_fields () { 
  444. if ( true === $this->check_progress( __FUNCTION__ ) ) 
  445. return '1'; 
  446.  
  447. $sister_ids = (array) get_option( 'pods_framework_upgrade_2_0_sister_ids', array() ); 
  448.  
  449. foreach ( $sister_ids as $old_field_id => $old_sister_id ) { 
  450. $old_field_id = (int) $old_field_id; 
  451. $old_sister_id = (int) $old_sister_id; 
  452.  
  453. $new_field_id = pods_query( "SELECT `post_id` FROM `@wp_postmeta` WHERE `meta_key` = '_pods_1x_field_id' AND `meta_value` = '{$old_field_id}' LIMIT 1" ); 
  454.  
  455. if ( !empty( $new_field_id ) ) { 
  456. $new_field_id = (int) $new_field_id[ 0 ]->post_id; 
  457.  
  458. $new_sister_id = pods_query( "SELECT `post_id` FROM `@wp_postmeta` WHERE `meta_key` = '_pods_1x_field_id' AND `meta_value` = '{$old_sister_id}' LIMIT 1" ); 
  459.  
  460. if ( !empty( $new_sister_id ) ) { 
  461. $new_sister_id = (int) $new_sister_id[ 0 ]->post_id; 
  462.  
  463. update_post_meta( $new_field_id, 'sister_id', $new_sister_id ); 
  464. else 
  465. delete_post_meta( $new_field_id, 'sister_id' ); 
  466.  
  467. // We were off the grid, so let's flush and allow for resync 
  468. $this->api->cache_flush_pods(); 
  469.  
  470. $this->update_progress( __FUNCTION__, true ); 
  471.  
  472. return '1'; 
  473.  
  474. /** 
  475. * @return string 
  476. */ 
  477. public function migrate_relationships () { 
  478. if ( true === $this->check_progress( __FUNCTION__ ) ) 
  479. return '1'; 
  480.  
  481. $migration_limit = (int) apply_filters( 'pods_upgrade_item_limit', 1500 ); 
  482. $migration_limit = max( $migration_limit, 100 ); 
  483.  
  484. $last_id = (int) $this->check_progress( __FUNCTION__ ); 
  485.  
  486. $sql = " 
  487. SELECT `r`.*, `p`.`tbl_row_id` AS `real_id`, `p`.`datatype` 
  488. FROM `@wp_pod_rel` AS `r` 
  489. LEFT JOIN `@wp_pod` AS `p` ON `p`.`id` = `r`.`pod_id` 
  490. WHERE {$last_id} < `r`.`id` 
  491. AND `r`.`pod_id` IS NOT NULL 
  492. AND `r`.`field_id` IS NOT NULL 
  493. AND `p`.`id` IS NOT NULL 
  494. ORDER BY `r`.`id` 
  495. LIMIT 0, {$migration_limit} 
  496. "; 
  497.  
  498. $rel = pods_query( $sql ); 
  499.  
  500. $last_id = true; 
  501.  
  502. $pod_types = pods_query( "SELECT `id`, `name` FROM `@wp_pod_types` ORDER BY `id`" ); 
  503.  
  504. $types = array(); 
  505.  
  506. $x = 0; 
  507.  
  508. if ( !empty( $rel ) && !empty( $pod_types ) ) { 
  509. foreach ( $pod_types as $type ) { 
  510. $type->name = pods_clean_name( $type->name ); 
  511.  
  512. $types[ $type->id ] = $this->api->load_pod( array( 'name' => $type->name ), false ); 
  513.  
  514. if ( empty( $types[ $type->id ] ) ) 
  515. return pods_error( sprintf( __( 'Pod <strong>%s</strong> not found, relationships cannot be migrated', 'pods' ), $type->name ) ); 
  516.  
  517. $pod_fields = pods_query( "SELECT `id`, `name` FROM `@wp_pod_fields` WHERE `datatype` = {$type->id} ORDER BY `id`" ); 
  518.  
  519. $types[ $type->id ][ 'old_fields' ] = array(); 
  520.  
  521. foreach ( $pod_fields as $field ) { 
  522. // Handle name changes 
  523. if ( in_array( $field->name, array( 'created', 'modified', 'author' ) ) ) 
  524. $field->name .= '2'; 
  525.  
  526. $types[ $type->id ][ 'old_fields' ][ $field->id ] = $field->name; 
  527.  
  528. foreach ( $rel as $r ) { 
  529. $r->pod_id = (int) $r->pod_id; 
  530.  
  531. if ( !isset( $types[ $r->datatype ] ) || !isset( $types[ $r->datatype ][ 'old_fields' ][ $r->field_id ] ) ) 
  532. continue; 
  533.  
  534. if ( !isset( $types[ $r->datatype ][ 'fields' ][ $types[ $r->datatype ][ 'old_fields' ][ $r->field_id ] ] ) ) 
  535. continue; 
  536.  
  537. $field = $types[ $r->datatype ][ 'fields' ][ $types[ $r->datatype ][ 'old_fields' ][ $r->field_id ] ]; 
  538.  
  539. if ( !in_array( $field[ 'type' ], array( 'pick', 'file' ) ) ) 
  540. continue; 
  541.  
  542. $pod_id = $types[ $r->datatype ][ 'id' ]; 
  543. $field_id = $field[ 'id' ]; 
  544. $item_id = $r->real_id; 
  545.  
  546. $related_pod_id = 0; 
  547. $related_field_id = 0; 
  548. $related_item_id = $r->tbl_row_id; 
  549.  
  550. if ( 'pick' == $field[ 'type' ] ) { 
  551. $old_sister_id = (int) pods_var( '_pods_1x_sister_id', $field[ 'options' ], 0 ); 
  552.  
  553. if ( 0 < $old_sister_id ) { 
  554. $sql = " 
  555. SELECT `f`.`id`, `f`.`name`, `t`.`name` AS `pod` 
  556. FROM `@wp_pod_fields` AS `f` 
  557. LEFT JOIN `@wp_pod_types` AS `t` ON `t`.`id` = `f`.`datatype` 
  558. WHERE `f`.`id` = " . $old_sister_id . " AND `t`.`id` IS NOT NULL 
  559. ORDER BY `f`.`id` 
  560. LIMIT 1 
  561. "; 
  562.  
  563. $old_field = pods_query( $sql ); 
  564.  
  565. if ( empty( $old_field ) ) 
  566. continue; 
  567.  
  568. $old_field = $old_field[ 0 ]; 
  569.  
  570. $related_field = $this->api->load_field( array( 'name' => $old_field->name, 'pod' => $old_field->pod ) ); 
  571.  
  572. if ( empty( $related_field ) ) 
  573. continue; 
  574.  
  575. $related_pod_id = $related_field[ 'pod_id' ]; 
  576. $related_field_id = $related_field[ 'id' ]; 
  577. elseif ( 'pod' == $field[ 'pick_object' ] && 0 < strlen( $field[ 'pick_val' ] ) ) { 
  578. $related_pod = $this->api->load_pod( array( 'name' => $field[ 'pick_val' ] ), false ); 
  579.  
  580. if ( empty( $related_pod ) ) 
  581. continue; 
  582.  
  583. $related_pod_id = $related_pod[ 'id' ]; 
  584.  
  585. $r->id = (int) $r->id; 
  586. $pod_id = (int) $pod_id; 
  587. $field_id = (int) $field_id; 
  588. $item_id = (int) $item_id; 
  589. $related_pod_id = (int) $related_pod_id; 
  590. $related_field_id = (int) $related_field_id; 
  591. $related_item_id = (int) $related_item_id; 
  592. $r->weight = (int) $r->weight; 
  593.  
  594. $table_data = array( 
  595. 'id' => $r->id,  
  596. 'pod_id' => $pod_id,  
  597. 'field_id' => $field_id,  
  598. 'item_id' => $item_id,  
  599. 'related_pod_id' => $related_pod_id,  
  600. 'related_field_id' => $related_field_id,  
  601. 'related_item_id' => $related_item_id,  
  602. 'weight' => $r->weight,  
  603. ); 
  604.  
  605. $table_formats = array_fill( 0, count( $table_data ), '%d' ); 
  606.  
  607. $sql = PodsData::insert_on_duplicate( "@wp_podsrel", $table_data, $table_formats ); 
  608.  
  609. pods_query( $sql ); 
  610.  
  611. $last_id = $r->id; 
  612.  
  613. $x++; 
  614.  
  615. if ( 10 < $x ) { 
  616. $this->update_progress( __FUNCTION__, $last_id ); 
  617.  
  618. $x = 0; 
  619.  
  620. $this->update_progress( __FUNCTION__, $last_id ); 
  621.  
  622. if ( $migration_limit == count( $rel ) ) 
  623. return '-2'; 
  624. else 
  625. return '1'; 
  626.  
  627. /** 
  628. * @return string 
  629. */ 
  630. public function migrate_settings () { 
  631. return $this->migrate_roles(); 
  632.  
  633. /** 
  634. * @return string 
  635. */ 
  636. public function migrate_roles () { 
  637. if ( true === $this->check_progress( __FUNCTION__ ) ) 
  638. return '1'; 
  639.  
  640. /** 
  641. * @var $wpdb WPDB 
  642. */ 
  643. global $wpdb; 
  644.  
  645. $wp_roles = get_option( "{$wpdb->prefix}user_roles" ); 
  646.  
  647. $old_roles = get_option( 'pods_roles' ); 
  648.  
  649. if ( !is_array( $old_roles ) && !empty( $old_roles ) ) 
  650. $old_roles = @unserialize( $old_roles ); 
  651.  
  652. if ( !is_array( $old_roles ) ) 
  653. $old_roles = array(); 
  654.  
  655. if ( !empty( $old_roles ) ) { 
  656. foreach ( $old_roles as $role => $data ) { 
  657. if ( $role == '_wpnonce' ) 
  658. continue; 
  659.  
  660. if ( !isset( $wp_roles[ $role ] ) ) 
  661. continue; 
  662.  
  663. $caps = $wp_roles[ $role ][ 'capabilities' ]; 
  664.  
  665. foreach ( $data as $cap ) { 
  666. if ( 0 === strpos( 'manage_', $cap ) ) { 
  667. if ( in_array( $cap, array( 'manage_roles' ) ) ) 
  668. continue; 
  669.  
  670. $cap = pods_str_replace( 'manage_', 'pods_', $cap, 1 ); 
  671. $cap = pods_str_replace( 'pod_pages', 'pages', $cap, 1 ); 
  672.  
  673. $caps[ $cap ] = true; 
  674. elseif ( 0 === strpos( 'pod_', $cap ) ) { 
  675. $keys = array( 
  676. pods_str_replace( 'pod_', 'pods_new_', $cap, 1 ),  
  677. pods_str_replace( 'pod_', 'pods_edit_', $cap, 1 ),  
  678. pods_str_replace( 'pod_', 'pods_delete_', $cap, 1 ),  
  679. ); 
  680.  
  681. foreach ( $keys as $key ) { 
  682. $caps[ $key ] = true; 
  683.  
  684. $wp_roles[ $role ][ 'capabilities' ] = $caps; 
  685.  
  686. update_option( "{$wpdb->prefix}user_roles", $wp_roles ); 
  687.  
  688. $this->update_progress( __FUNCTION__, true ); 
  689.  
  690. return '1'; 
  691.  
  692. /** 
  693. * @return array|string 
  694. */ 
  695. public function migrate_templates () { 
  696. if ( true === $this->check_progress( __FUNCTION__ ) ) 
  697. return '1'; 
  698.  
  699. $templates = pods_query( "SELECT * FROM `@wp_pod_templates`", false ); 
  700.  
  701. $results = array(); 
  702.  
  703. if ( !empty( $templates ) ) { 
  704. foreach ( $templates as $template ) { 
  705. unset( $template->id ); 
  706.  
  707. $results[] = $this->api->save_template( $template ); 
  708.  
  709. $this->update_progress( __FUNCTION__, true ); 
  710.  
  711. return '1'; 
  712.  
  713. /** 
  714. * @return array|string 
  715. */ 
  716. public function migrate_pages () { 
  717. if ( true === $this->check_progress( __FUNCTION__ ) ) 
  718. return '1'; 
  719.  
  720. $pages = pods_query( "SELECT * FROM `@wp_pod_pages`", false ); 
  721.  
  722. if ( !empty( $pages ) ) { 
  723. foreach ( $pages as $page ) { 
  724. unset( $page->id ); 
  725.  
  726. $this->api->save_page( $page ); 
  727.  
  728. $this->update_progress( __FUNCTION__, true ); 
  729.  
  730. return '1'; 
  731.  
  732. /** 
  733. * @return array|string 
  734. */ 
  735. public function migrate_helpers () { 
  736. if ( true === $this->check_progress( __FUNCTION__ ) ) 
  737. return '1'; 
  738.  
  739. $helpers = pods_query( "SELECT * FROM `@wp_pod_helpers`", false ); 
  740.  
  741. $notice = false; 
  742.  
  743. if ( !empty( $helpers ) ) { 
  744. foreach ( $helpers as $helper ) { 
  745. unset( $helper->id ); 
  746.  
  747. if ( 'input' == $helper->helper_type ) { 
  748. $helper->status = 'draft'; 
  749.  
  750. $notice = true; 
  751.  
  752. $this->api->save_helper( $helper ); 
  753.  
  754. $this->update_progress( __FUNCTION__, true ); 
  755.  
  756. if ( $notice ) 
  757. return pods_error( __( 'Input Helpers may not function in our new forms, we have imported and disabled them for your review.', 'pods' ) ); 
  758.  
  759. return '1'; 
  760.  
  761. /** 
  762. * @param $params 
  763. * @return mixed|string|void 
  764. */ 
  765. public function migrate_pod ( $params ) { 
  766. /** 
  767. * @var $wpdb WPDB 
  768. */ 
  769. global $wpdb; 
  770.  
  771. if ( !isset( $params->pod ) ) 
  772. return pods_error( __( 'Invalid Pod.', 'pods' ) ); 
  773.  
  774. $pod = pods_sanitize( pods_clean_name( $params->pod ) ); 
  775.  
  776. if ( !in_array( "{$wpdb->prefix}pod_tbl_{$pod}", $this->tables ) ) 
  777. return pods_error( __( 'Table not found, items cannot be migrated', 'pods' ) ); 
  778.  
  779. if ( !in_array( "{$wpdb->prefix}pods_{$pod}", $this->tables ) ) 
  780. return pods_error( __( 'New table not found, items cannot be migrated', 'pods' ) ); 
  781.  
  782. if ( !in_array( "{$wpdb->prefix}pod_types", $this->tables ) ) 
  783. return pods_error( __( 'Pod Types table not found, items cannot be migrated', 'pods' ) ); 
  784.  
  785. if ( !in_array( "{$wpdb->prefix}pod", $this->tables ) ) 
  786. return pods_error( __( 'Pod table not found, items cannot be migrated', 'pods' ) ); 
  787.  
  788. if ( true === $this->check_progress( __FUNCTION__, $pod ) ) 
  789. return '1'; 
  790.  
  791. $pod_data = $this->api->load_pod( array( 'name' => $pod ), false ); 
  792.  
  793. if ( empty( $pod_data ) ) 
  794. return pods_error( sprintf( __( 'Pod <strong>%s</strong> not found, items cannot be migrated', 'pods' ), $pod ) ); 
  795.  
  796. $columns = array(); 
  797. $old_columns = array(); 
  798.  
  799. foreach ( $pod_data[ 'fields' ] as $field ) { 
  800. if ( !in_array( $field[ 'name' ], array( 'created', 'modified', 'author' ) ) && !in_array( $field[ 'type' ], array( 'file', 'pick' ) ) ) { 
  801. $columns[] = pods_sanitize( $field[ 'name' ] ); 
  802. $old_columns[] = pods_var( '_pods_1x_field_name', $field[ 'options' ], $field[ 'name' ], null, false ); 
  803.  
  804. $into = '`id`'; 
  805. $select = '`t`.`id`'; 
  806.  
  807. if ( !empty( $columns ) ) { 
  808. $into .= ', `' . implode( '`, `', $columns ) . '`'; 
  809. $select .= ', `t`.`' . implode( '`, `t`.`', $old_columns ) . '`'; 
  810.  
  811. // Copy content from the old table into the new 
  812. $sql = " 
  813. REPLACE INTO `@wp_pods_{$pod}` 
  814. ( {$into} ) 
  815. ( SELECT {$select} 
  816. FROM `@wp_pod_tbl_{$pod}` AS `t` ) 
  817. "; 
  818.  
  819. pods_query( $sql ); 
  820.  
  821. // Copy index data from the old index table into the new individual table 
  822. $sql = " 
  823. UPDATE `@wp_pods_{$pod}` AS `t` 
  824. LEFT JOIN `@wp_pod_types` AS `x` ON `x`.`name` = '{$pod}' 
  825. LEFT JOIN `@wp_pod` AS `p` ON `p`.`datatype` = `x`.`id` AND `p`.`tbl_row_id` = `t`.`id` 
  826. SET `t`.`created` = `p`.`created`, `t`.`modified` = `p`.`modified` 
  827. WHERE `x`.`id` IS NOT NULL AND `p`.`id` IS NOT NULL 
  828. "; 
  829.  
  830. pods_query( $sql ); 
  831.  
  832. // Copy name data from the old index table into the new individual table (if name empty in indiv table) 
  833. $sql = " 
  834. UPDATE `@wp_pods_{$pod}` AS `t` 
  835. LEFT JOIN `@wp_pod_types` AS `x` ON `x`.`name` = '{$pod}' 
  836. LEFT JOIN `@wp_pod` AS `p` ON `p`.`datatype` = `x`.`id` AND `p`.`tbl_row_id` = `t`.`id` 
  837. SET `t`.`name` = `p`.`name` 
  838. WHERE ( `t`.`name` IS NULL OR `t`.`name` = '' ) AND `x`.`id` IS NOT NULL AND `p`.`id` IS NOT NULL 
  839. "; 
  840.  
  841. pods_query( $sql ); 
  842.  
  843. $this->update_progress( __FUNCTION__, true, $pod ); 
  844.  
  845. return '1'; 
  846.  
  847. /** 
  848. * @return string 
  849. */ 
  850. public function migrate_cleanup () { 
  851. update_option( 'pods_framework_upgraded_1_x', 1 ); 
  852.  
  853. $oldget = $_GET; 
  854.  
  855. $_GET[ 'toggle' ] = 1; 
  856.  
  857. PodsInit::$components->toggle( 'templates' ); 
  858. PodsInit::$components->toggle( 'pages' ); 
  859. PodsInit::$components->toggle( 'helpers' ); 
  860.  
  861. $_GET = $oldget; 
  862.  
  863. $this->api->cache_flush_pods(); 
  864.  
  865. return '1'; 
  866.  
  867. /** 
  868. */ 
  869. public function restart () { 
  870. /** 
  871. * @var $wpdb WPDB 
  872. */ 
  873. global $wpdb; 
  874.  
  875. foreach ( $this->tables as $table ) { 
  876. if ( false !== strpos( $table, "{$wpdb->prefix}pods" ) ) 
  877. pods_query( "TRUNCATE `{$table}`", false ); 
  878.  
  879. delete_option( 'pods_framework_upgrade_2_0' ); 
  880. delete_option( 'pods_framework_upgrade_2_0_sister_ids' ); 
  881. delete_option( 'pods_framework_upgraded_1_x' ); 
  882.  
  883. /** 
  884. */ 
  885. public function cleanup () { 
  886. /** 
  887. * @var $wpdb WPDB 
  888. */ 
  889. global $wpdb; 
  890.  
  891. foreach ( $this->tables as $table ) { 
  892. if ( false !== strpos( $table, "{$wpdb->prefix}pod_" ) || "{$wpdb->prefix}pod" == $table ) 
  893. pods_query( "DROP TABLE `{$table}`", false ); 
  894.  
  895. delete_option( 'pods_roles' ); 
  896. delete_option( 'pods_version' ); 
  897. delete_option( 'pods_framework_upgrade_2_0' ); 
  898. delete_option( 'pods_framework_upgrade_2_0_sister_ids' ); 
  899. delete_option( 'pods_framework_upgraded_1_x' ); 
  900.  
  901. delete_option( 'pods_disable_file_browser' ); 
  902. delete_option( 'pods_files_require_login' ); 
  903. delete_option( 'pods_files_require_login_cap' ); 
  904. delete_option( 'pods_disable_file_upload' ); 
  905. delete_option( 'pods_upload_require_login' ); 
  906. delete_option( 'pods_upload_require_login_cap' ); 
  907.  
  908. pods_query( "DELETE FROM `@wp_postmeta` WHERE `meta_key` LIKE '_pods_1x_%'" );