acf_upgrade

Upgrade.

Defined (1)

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

/core/controllers/upgrade.php  
  1. class acf_upgrade 
  2.  
  3. /** 
  4. * __construct 
  5. * @description: 
  6. * @since 3.1.8 
  7. * @created: 23/06/12 
  8. */ 
  9.  
  10. function __construct() 
  11. // actions 
  12. add_action('admin_menu', array($this, 'admin_menu'), 11); 
  13.  
  14.  
  15. // ajax 
  16. add_action('wp_ajax_acf_upgrade', array($this, 'upgrade_ajax')); 
  17.  
  18.  
  19. /** 
  20. * admin_menu 
  21. * @description: 
  22. * @since 3.1.8 
  23. * @created: 23/06/12 
  24. */ 
  25.  
  26. function admin_menu() 
  27. // dont run on plugin activate! 
  28. if( isset($_GET['action']) && $_GET['action'] == 'activate-plugin' ) 
  29. return; 
  30.  
  31.  
  32. // vars 
  33. $plugin_version = apply_filters('acf/get_info', 'version'); 
  34. $acf_version = get_option('acf_version'); 
  35.  
  36.  
  37. // bail early if a new install 
  38. if( empty($acf_version) ) { 
  39.  
  40. update_option('acf_version', $plugin_version ); 
  41. return; 
  42.  
  43.  
  44.  
  45. // bail early if $acf_version is >= $plugin_version 
  46. if( version_compare( $acf_version, $plugin_version, '>=') ) { 
  47.  
  48. return; 
  49.  
  50.  
  51.  
  52. // update version 
  53. update_option('acf_version', $plugin_version ); 
  54.  
  55.  
  56. // update admin page 
  57. add_submenu_page('edit.php?post_type=acf', __('Upgrade', 'acf'), __('Upgrade', 'acf'), 'manage_options', 'acf-upgrade', array($this, 'html') ); 
  58.  
  59.  
  60.  
  61. /** 
  62. * html 
  63. * @description: 
  64. * @since 3.1.8 
  65. * @created: 23/06/12 
  66. */ 
  67.  
  68. function html() 
  69. $version = get_option('acf_version', '1.0.5'); 
  70. $next = false; 
  71.  
  72. // list of starting points 
  73. if( $version < '3.0.0' ) 
  74. $next = '3.0.0'; 
  75. elseif( $version < '3.1.8' ) 
  76. $next = '3.1.8'; 
  77. elseif( $version < '3.2.5' ) 
  78. $next = '3.2.5'; 
  79. elseif( $version < '3.3.3' ) 
  80. $next = '3.3.3'; 
  81. elseif( $version < '3.4.1' ) 
  82. $next = '3.4.1'; 
  83.  
  84. ?> 
  85. <script type="text/javascript"> 
  86. (function($) { 
  87.  
  88. function add_message(messaage) 
  89. $('#wpbody-content').append('<p>' + messaage + '</p>'); 
  90.  
  91. function run_upgrade(version) 
  92. $.ajax({ 
  93. url: ajaxurl,  
  94. data: { 
  95. action : 'acf_upgrade',  
  96. version : version 
  97. },  
  98. type: 'post',  
  99. dataType: 'json',  
  100. success: function(json) { 
  101.  
  102. if(json) 
  103. if(json.status) 
  104. add_message(json.message); 
  105.  
  106. // next update? 
  107. if(json.next) 
  108. run_upgrade(json.next); 
  109. else 
  110. // all done 
  111. add_message('Upgrade Complete! <a href="<?php echo admin_url(); ?>edit.php?post_type=acf">Continue to ACF »</a>'); 
  112. else 
  113. // error! 
  114. add_message('Error: ' + json.message); 
  115. else 
  116. // major error! 
  117. add_message('Sorry. Something went wrong during the upgrade process. Please report this on the support forum'); 
  118. }); 
  119.  
  120. <?php if($next) { echo 'run_upgrade("' . $next . '");'; } ?> 
  121.  
  122. })(jQuery); 
  123. </script> 
  124. <style type="text/css"> 
  125. #message { 
  126. display: none; 
  127. </style> 
  128. <?php 
  129.  
  130. if(!$next) 
  131. echo '<p>No Upgrade Required</p>'; 
  132.  
  133.  
  134. /** 
  135. * upgrade_ajax 
  136. * @description: 
  137. * @since 3.1.8 
  138. * @created: 23/06/12 
  139. */ 
  140.  
  141. function upgrade_ajax() 
  142. // global 
  143. global $wpdb; 
  144.  
  145.  
  146. // tables 
  147. $acf_fields = $wpdb->prefix.'acf_fields'; 
  148. $acf_values = $wpdb->prefix.'acf_values'; 
  149. $acf_rules = $wpdb->prefix.'acf_rules'; 
  150. $wp_postmeta = $wpdb->prefix.'postmeta'; 
  151. $wp_options = $wpdb->prefix.'options'; 
  152.  
  153.  
  154. // vars 
  155. $return = array( 
  156. 'status' => false,  
  157. 'message' => "",  
  158. 'next' => false,  
  159. ); 
  160.  
  161.  
  162. // versions 
  163. switch($_POST['version']) 
  164.  
  165. /**--------------------- 
  166. * 3.0.0 
  167. *--------------------*/ 
  168.  
  169. case '3.0.0': 
  170.  
  171. // upgrade options first as "field_group_layout" will cause get_fields to fail! 
  172.  
  173. // get acf's 
  174. $acfs = get_posts(array( 
  175. 'numberposts' => -1,  
  176. 'post_type' => 'acf',  
  177. 'orderby' => 'menu_order title',  
  178. 'order' => 'asc',  
  179. 'suppress_filters' => false,  
  180. )); 
  181.  
  182. if($acfs) 
  183. foreach($acfs as $acf) 
  184. // position 
  185. update_post_meta($acf->ID, 'position', 'normal'); 
  186.  
  187. //layout 
  188. $layout = get_post_meta($acf->ID, 'field_group_layout', true) ? get_post_meta($acf->ID, 'field_group_layout', true) : 'in_box'; 
  189. if($layout == 'in_box') 
  190. $layout = 'default'; 
  191. else 
  192. $layout = 'no_box'; 
  193. update_post_meta($acf->ID, 'layout', $layout); 
  194. delete_post_meta($acf->ID, 'field_group_layout'); 
  195.  
  196. // show_on_page 
  197. $show_on_page = get_post_meta($acf->ID, 'show_on_page', true) ? get_post_meta($acf->ID, 'show_on_page', true) : array(); 
  198. if($show_on_page) 
  199. $show_on_page = unserialize($show_on_page); 
  200. update_post_meta($acf->ID, 'show_on_page', $show_on_page); 
  201.  
  202.  
  203. $return = array( 
  204. 'status' => true,  
  205. 'message' => "Migrating Options...",  
  206. 'next' => '3.0.0 (step 2)',  
  207. ); 
  208.  
  209. break; 
  210.  
  211. /**--------------------- 
  212. * 3.0.0 
  213. *--------------------*/ 
  214.  
  215. case '3.0.0 (step 2)': 
  216.  
  217. // get acf's 
  218. $acfs = get_posts(array( 
  219. 'numberposts' => -1,  
  220. 'post_type' => 'acf',  
  221. 'orderby' => 'menu_order title',  
  222. 'order' => 'asc',  
  223. 'suppress_filters' => false,  
  224. )); 
  225.  
  226. if($acfs) 
  227. foreach($acfs as $acf) 
  228. // allorany doesn't need to change! 
  229.  
  230. $rules = $wpdb->get_results("SELECT * FROM $acf_rules WHERE acf_id = '$acf->ID' ORDER BY order_no ASC", ARRAY_A); 
  231.  
  232. if($rules) 
  233. foreach($rules as $rule) 
  234. // options rule has changed 
  235. if($rule['param'] == 'options_page') 
  236. $rule['value'] = 'Options'; 
  237.  
  238. add_post_meta($acf->ID, 'rule', $rule); 
  239.  
  240.  
  241. $return = array( 
  242. 'status' => true,  
  243. 'message' => "Migrating Location Rules...",  
  244. 'next' => '3.0.0 (step 3)',  
  245. ); 
  246.  
  247. break; 
  248.  
  249. /**--------------------- 
  250. * 3.0.0 
  251. *--------------------*/ 
  252.  
  253. case '3.0.0 (step 3)': 
  254.  
  255. $message = "Migrating Fields?"; 
  256.  
  257. $parent_id = 0; 
  258. $fields = $wpdb->get_results("SELECT * FROM $acf_fields WHERE parent_id = $parent_id ORDER BY order_no, name", ARRAY_A); 
  259.  
  260. if($fields) 
  261. // loop through fields 
  262. foreach($fields as $field) 
  263.  
  264. // unserialize options 
  265. if(@unserialize($field['options'])) 
  266. $field['options'] = unserialize($field['options']); 
  267. else 
  268. $field['options'] = array(); 
  269.  
  270.  
  271. // sub fields 
  272. if($field['type'] == 'repeater') 
  273. $field['options']['sub_fields'] = array(); 
  274.  
  275. $parent_id = $field['id']; 
  276. $sub_fields = $wpdb->get_results("SELECT * FROM $acf_fields WHERE parent_id = $parent_id ORDER BY order_no, name", ARRAY_A); 
  277.  
  278.  
  279. // if fields are empty, this must be a new or broken acf. 
  280. if(empty($sub_fields)) 
  281. $field['options']['sub_fields'] = array(); 
  282. else 
  283. // loop through fields 
  284. foreach($sub_fields as $sub_field) 
  285. // unserialize options 
  286. if(@unserialize($sub_field['options'])) 
  287. $sub_field['options'] = @unserialize($sub_field['options']); 
  288. else 
  289. $sub_field['options'] = array(); 
  290.  
  291. // merge options with field 
  292. $sub_field = array_merge($sub_field, $sub_field['options']); 
  293.  
  294. unset($sub_field['options']); 
  295.  
  296. // each field has a unique id! 
  297. if(!isset($sub_field['key'])) $sub_field['key'] = 'field_' . $sub_field['id']; 
  298.  
  299. $field['options']['sub_fields'][] = $sub_field; 
  300.  
  301. // end if sub field 
  302.  
  303.  
  304. // merge options with field 
  305. $field = array_merge($field, $field['options']); 
  306.  
  307. unset($field['options']); 
  308.  
  309. // each field has a unique id! 
  310. if(!isset($field['key'])) $field['key'] = 'field_' . $field['id']; 
  311.  
  312. // update field 
  313. $this->parent->update_field( $field['post_id'], $field); 
  314.  
  315. // create field name (field_rand) 
  316. //$message .= print_r($field, true) . '<br /><br />'; 
  317. // end foreach $fields 
  318.  
  319.  
  320. $return = array( 
  321. 'status' => true,  
  322. 'message' => $message,  
  323. 'next' => '3.0.0 (step 4)',  
  324. ); 
  325.  
  326. break; 
  327.  
  328. /**--------------------- 
  329. * 3.0.0 
  330. *--------------------*/ 
  331.  
  332. case '3.0.0 (step 4)': 
  333.  
  334. $message = "Migrating Values..."; 
  335.  
  336. // update normal values 
  337. $values = $wpdb->get_results("SELECT v.field_id, m.post_id, m.meta_key, m.meta_value FROM $acf_values v LEFT JOIN $wp_postmeta m ON v.value = m.meta_id WHERE v.sub_field_id = 0", ARRAY_A); 
  338. if($values) 
  339. foreach($values as $value) 
  340. // options page 
  341. if($value['post_id'] == 0) $value['post_id'] = 999999999; 
  342.  
  343. // unserialize value (relationship, multi select, etc) 
  344. if(@unserialize($value['meta_value'])) 
  345. $value['meta_value'] = unserialize($value['meta_value']); 
  346.  
  347. update_post_meta($value['post_id'], $value['meta_key'], $value['meta_value']); 
  348. update_post_meta($value['post_id'], '_' . $value['meta_key'], 'field_' . $value['field_id']); 
  349.  
  350. // update repeater values 
  351. $values = $wpdb->get_results("SELECT v.field_id, v.sub_field_id, v.order_no, m.post_id, m.meta_key, m.meta_value FROM $acf_values v LEFT JOIN $wp_postmeta m ON v.value = m.meta_id WHERE v.sub_field_id != 0", ARRAY_A); 
  352. if($values) 
  353. $rows = array(); 
  354.  
  355. foreach($values as $value) 
  356. // update row count 
  357. $row = (int) $value['order_no'] + 1; 
  358.  
  359. // options page 
  360. if($value['post_id'] == 0) $value['post_id'] = 999999999; 
  361.  
  362. // unserialize value (relationship, multi select, etc) 
  363. if(@unserialize($value['meta_value'])) 
  364. $value['meta_value'] = unserialize($value['meta_value']); 
  365.  
  366. // current row 
  367. $current_row = isset($rows[$value['post_id']][$value['field_id']]) ? $rows[$value['post_id']][$value['field_id']] : 0; 
  368. if($row > $current_row) $rows[$value['post_id']][$value['field_id']] = (int) $row; 
  369.  
  370. // get field name 
  371. $field_name = $wpdb->get_var($wpdb->prepare("SELECT name FROM $acf_fields WHERE id = %d", $value['field_id'])); 
  372.  
  373. // get sub field name 
  374. $sub_field_name = $wpdb->get_var($wpdb->prepare("SELECT name FROM $acf_fields WHERE id = %d", $value['sub_field_id'])); 
  375.  
  376. // save new value 
  377. $new_meta_key = $field_name . '_' . $value['order_no'] . '_' . $sub_field_name; 
  378. update_post_meta($value['post_id'], $new_meta_key , $value['meta_value']); 
  379.  
  380. // save value hidden field id 
  381. update_post_meta($value['post_id'], '_' . $new_meta_key, 'field_' . $value['sub_field_id']); 
  382.  
  383. foreach($rows as $post_id => $field_ids) 
  384. foreach($field_ids as $field_id => $row_count) 
  385. // get sub field name 
  386. $field_name = $wpdb->get_var($wpdb->prepare("SELECT name FROM $acf_fields WHERE id = %d", $field_id)); 
  387.  
  388. delete_post_meta($post_id, $field_name); 
  389. update_post_meta($post_id, $field_name, $row_count); 
  390. update_post_meta($post_id, '_' . $field_name, 'field_' . $field_id); 
  391.  
  392.  
  393.  
  394. // update version (only upgrade 1 time) 
  395. update_option('acf_version', '3.0.0'); 
  396.  
  397. $return = array( 
  398. 'status' => true,  
  399. 'message' => $message,  
  400. 'next' => '3.1.8',  
  401. ); 
  402.  
  403. break; 
  404.  
  405.  
  406. /**--------------------- 
  407. * 3.1.8 
  408. *--------------------*/ 
  409.  
  410. case '3.1.8': 
  411.  
  412. // vars 
  413. $message = __("Migrating options values from the $wp_postmeta table to the $wp_options table", 'acf') . '...'; 
  414.  
  415. // update normal values 
  416. $rows = $wpdb->get_results( $wpdb->prepare("SELECT meta_key FROM $wp_postmeta WHERE post_id = %d", 999999999) , ARRAY_A); 
  417.  
  418. if($rows) 
  419. foreach($rows as $row) 
  420. // original name 
  421. $field_name = $row['meta_key']; 
  422.  
  423.  
  424. // name 
  425. $new_name = ""; 
  426. if( substr($field_name, 0, 1) == "_" ) 
  427. $new_name = '_options' . $field_name; 
  428. else 
  429. $new_name = 'options_' . $field_name; 
  430.  
  431.  
  432. // value 
  433. $value = get_post_meta( 999999999, $field_name, true ); 
  434.  
  435.  
  436. // update option 
  437. update_option( $new_name, $value ); 
  438.  
  439.  
  440. // deleet old postmeta 
  441. delete_post_meta( 999999999, $field_name ); 
  442.  
  443. // foreach($values as $value) 
  444. // if($values) 
  445.  
  446.  
  447. // update version 
  448. update_option('acf_version', '3.1.8'); 
  449.  
  450. $return = array( 
  451. 'status' => true,  
  452. 'message' => $message,  
  453. 'next' => '3.2.5',  
  454. ); 
  455.  
  456. break; 
  457.  
  458.  
  459. /**--------------------- 
  460. * 3.1.8 
  461. *--------------------*/ 
  462.  
  463. case '3.2.5': 
  464.  
  465. // vars 
  466. $message = __("Modifying field group options 'show on page'", 'acf') . '...'; 
  467.  
  468.  
  469. // get acf's 
  470. $acfs = get_posts(array( 
  471. 'numberposts' => -1,  
  472. 'post_type' => 'acf',  
  473. 'orderby' => 'menu_order title',  
  474. 'order' => 'asc',  
  475. 'suppress_filters' => false,  
  476. )); 
  477.  
  478.  
  479. $show_all = array('the_content', 'discussion', 'custom_fields', 'comments', 'slug', 'author'); 
  480.  
  481.  
  482. // populate acfs 
  483. if($acfs) 
  484. foreach($acfs as $acf) 
  485. $show_on_page = get_post_meta($acf->ID, 'show_on_page', true) ? get_post_meta($acf->ID, 'show_on_page', true) : array(); 
  486.  
  487. $hide_on_screen = array_diff($show_all, $show_on_page); 
  488.  
  489. update_post_meta($acf->ID, 'hide_on_screen', $hide_on_screen); 
  490. delete_post_meta($acf->ID, 'show_on_page'); 
  491.  
  492.  
  493.  
  494. // update version 
  495. update_option('acf_version', '3.2.5'); 
  496.  
  497. $return = array( 
  498. 'status' => true,  
  499. 'message' => $message,  
  500. 'next' => '3.3.3',  
  501. ); 
  502.  
  503. break; 
  504.  
  505.  
  506. /** 
  507. * 3.3.3 
  508. * @description: changed field option: taxonomies filter on relationship / post object and page link fields. 
  509. * @created: 20/07/12 
  510. */ 
  511.  
  512. case '3.3.3': 
  513.  
  514. // vars 
  515. $message = __("Modifying field option 'taxonomy'", 'acf') . '...'; 
  516. $wp_term_taxonomy = $wpdb->prefix.'term_taxonomy'; 
  517. $term_taxonomies = array(); 
  518.  
  519. $rows = $wpdb->get_results("SELECT * FROM $wp_term_taxonomy", ARRAY_A); 
  520.  
  521. if($rows) 
  522. foreach($rows as $row) 
  523. $term_taxonomies[ $row['term_id'] ] = $row['taxonomy'] . ":" . $row['term_id']; 
  524.  
  525.  
  526. // get acf's 
  527. $acfs = get_posts(array( 
  528. 'numberposts' => -1,  
  529. 'post_type' => 'acf',  
  530. 'orderby' => 'menu_order title',  
  531. 'order' => 'asc',  
  532. 'suppress_filters' => false,  
  533. )); 
  534.  
  535. // populate acfs 
  536. if($acfs) 
  537. foreach($acfs as $acf) 
  538. $fields = $this->parent->get_acf_fields($acf->ID); 
  539.  
  540. if( $fields ) 
  541. foreach( $fields as $field ) 
  542.  
  543. // only edit the option: taxonomy 
  544. if( !isset($field['taxonomy']) ) 
  545. continue; 
  546.  
  547.  
  548. if( is_array($field['taxonomy']) ) 
  549. foreach( $field['taxonomy'] as $k => $v ) 
  550.  
  551. // could be "all" 
  552. if( !is_numeric($v) ) 
  553. continue; 
  554.  
  555. $field['taxonomy'][ $k ] = $term_taxonomies[ $v ]; 
  556.  
  557.  
  558. // foreach( $field['taxonomy'] as $k => $v ) 
  559. // if( $field['taxonomy'] ) 
  560.  
  561.  
  562. $this->parent->update_field( $acf->ID, $field); 
  563.  
  564. // foreach( $fields as $field ) 
  565. // if( $fields ) 
  566. // foreach($acfs as $acf) 
  567. // if($acfs) 
  568.  
  569.  
  570. // update version 
  571. update_option('acf_version', '3.3.3'); 
  572.  
  573. $return = array( 
  574. 'status' => true,  
  575. 'message' => $message,  
  576. 'next' => '3.4.1',  
  577. ); 
  578.  
  579. break; 
  580.  
  581.  
  582. /** 
  583. * 3.4.1 
  584. * @description: Move user custom fields from wp_options to wp_usermeta 
  585. * @created: 20/07/12 
  586. */ 
  587.  
  588. case '3.4.1': 
  589.  
  590. // vars 
  591. $message = __("Moving user custom fields from wp_options to wp_usermeta", 'acf') . '...'; 
  592.  
  593. $option_row_ids = array(); 
  594. $option_rows = $wpdb->get_results("SELECT option_id, option_name, option_value FROM $wpdb->options WHERE option_name LIKE 'user%' OR option_name LIKE '\_user%'", ARRAY_A); 
  595.  
  596.  
  597. if( $option_rows ) 
  598. foreach( $option_rows as $k => $row) 
  599. preg_match('/user_([0-9]+)_(.*)/', $row['option_name'], $matches); 
  600.  
  601.  
  602. // if no matches, this is not an acf value, ignore it 
  603. if( !$matches ) 
  604. continue; 
  605.  
  606.  
  607. // add to $delete_option_rows 
  608. $option_row_ids[] = $row['option_id']; 
  609.  
  610.  
  611. // meta_key prefix 
  612. $meta_key_prefix = ""; 
  613. if( substr($row['option_name'], 0, 1) == "_" ) 
  614. $meta_key_prefix = '_'; 
  615.  
  616.  
  617. // update user meta 
  618. update_user_meta( $matches[1], $meta_key_prefix . $matches[2], $row['option_value'] ); 
  619.  
  620.  
  621.  
  622. // clear up some memory ( aprox 14 kb ) 
  623. unset( $option_rows ); 
  624.  
  625.  
  626. // remove $option_row_ids 
  627. if( $option_row_ids ) 
  628. $option_row_ids = implode(', ', $option_row_ids); 
  629.  
  630. $wpdb->query("DELETE FROM $wpdb->options WHERE option_id IN ($option_row_ids)"); 
  631.  
  632.  
  633. // update version 
  634. update_option('acf_version', '3.4.1'); 
  635.  
  636. $return = array( 
  637. 'status' => true,  
  638. 'message' => $message,  
  639. 'next' => false,  
  640. ); 
  641.  
  642. break; 
  643.  
  644.  
  645.  
  646. // return json 
  647. echo json_encode($return); 
  648. die; 
  649.  
  650.  
  651.  
  652.  
  653.