upgrade.php 16.9 KB
Newer Older
imac's avatar
imac committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
<?php

/*
*  Upgrade
*
*  @description: All the functionality for upgrading ACF
*  @since 3.2.6
*  @created: 23/06/12
*/

class acf_upgrade
{

	/*
	*  __construct
	*
	*  @description:
	*  @since 3.1.8
	*  @created: 23/06/12
	*/

	function __construct()
	{
		// actions
		add_action('admin_menu', array($this,'admin_menu'), 11);


		// ajax
		add_action('wp_ajax_acf_upgrade', array($this, 'upgrade_ajax'));
	}


	/*
	*  admin_menu
	*
	*  @description:
	*  @since 3.1.8
	*  @created: 23/06/12
	*/

	function admin_menu()
	{
		// dont run on plugin activate!
		if( isset($_GET['action']) && $_GET['action'] == 'activate-plugin' )
		{
			return;
		}

		
		// vars
		$plugin_version = apply_filters('acf/get_info', 'version');
		$acf_version = get_option('acf_version');
		
		
		// bail early if a new install
		if( empty($acf_version) ) {
		
			update_option('acf_version', $plugin_version );
			return;
			
		}
		
		
		// bail early if $acf_version is >= $plugin_version
		if( version_compare( $acf_version, $plugin_version, '>=') ) {
		
			return;
			
		}
		
		
		// update version
		update_option('acf_version', $plugin_version );
		
		
		// update admin page
		add_submenu_page('edit.php?post_type=acf', __('Upgrade','acf'), __('Upgrade','acf'), 'manage_options','acf-upgrade', array($this,'html') );
	}

	

	/*
	*  html
	*
	*  @description:
	*  @since 3.1.8
	*  @created: 23/06/12
	*/

	function html()
	{
		$version = get_option('acf_version','1.0.5');
		$next = false;

		// list of starting points
		if( $version < '3.0.0' )
		{
			$next = '3.0.0';
		}
		elseif( $version < '3.1.8' )
		{
			$next = '3.1.8';
		}
		elseif( $version < '3.2.5' )
		{
			$next = '3.2.5';
		}
		elseif( $version < '3.3.3' )
		{
			$next = '3.3.3';
		}
		elseif( $version < '3.4.1' )
		{
			$next = '3.4.1';
		}

		?>
		<script type="text/javascript">
		(function($){

			function add_message(messaage)
			{
				$('#wpbody-content').append('<p>' + messaage + '</p>');
			}

			function run_upgrade(version)
			{
				$.ajax({
					url: ajaxurl,
					data: {
						action : 'acf_upgrade',
						version : version
					},
					type: 'post',
					dataType: 'json',
					success: function(json){

						if(json)
						{
							if(json.status)
							{
								add_message(json.message);

								// next update?
								if(json.next)
								{
									run_upgrade(json.next);
								}
								else
								{
									// all done
									add_message('Upgrade Complete! <a href="<?php echo admin_url(); ?>edit.php?post_type=acf">Continue to ACF &raquo;</a>');
								}
							}
							else
							{
								// error!
								add_message('Error: ' + json.message);
							}
						}
						else
						{
							// major error!
							add_message('Sorry. Something went wrong during the upgrade process. Please report this on the support forum');
						}
					}
				});
			}

			<?php if($next){ echo 'run_upgrade("' . $next . '");'; } ?>

		})(jQuery);
		</script>
		<style type="text/css">
			#message {
				display: none;
			}
		</style>
		<?php

		if(!$next)
		{
			echo '<p>No Upgrade Required</p>';
		}
	}


	/*
	*  upgrade_ajax
	*
	*  @description:
	*  @since 3.1.8
	*  @created: 23/06/12
	*/

	function upgrade_ajax()
	{
		// global
		global $wpdb;


		// tables
		$acf_fields = $wpdb->prefix.'acf_fields';
		$acf_values = $wpdb->prefix.'acf_values';
		$acf_rules = $wpdb->prefix.'acf_rules';
		$wp_postmeta = $wpdb->prefix.'postmeta';
		$wp_options = $wpdb->prefix.'options';


		// vars
		$return = array(
			'status'	=>	false,
			'message'	=>	"",
			'next'		=>	false,
		);


		// versions
		switch($_POST['version'])
		{

			/*---------------------
			*
			*	3.0.0
			*
			*--------------------*/

			case '3.0.0':

				// upgrade options first as "field_group_layout" will cause get_fields to fail!

				// get acf's
				$acfs = get_posts(array(
					'numberposts' 	=> -1,
					'post_type' 	=> 'acf',
					'orderby' 		=> 'menu_order title',
					'order' 		=> 'asc',
					'suppress_filters' => false,
				));

				if($acfs)
				{
					foreach($acfs as $acf)
					{
						// position
						update_post_meta($acf->ID, 'position', 'normal');

						//layout
						$layout = get_post_meta($acf->ID, 'field_group_layout', true) ? get_post_meta($acf->ID, 'field_group_layout', true) : 'in_box';
						if($layout == 'in_box')
						{
							$layout = 'default';
						}
						else
						{
							$layout = 'no_box';
						}
						update_post_meta($acf->ID, 'layout', $layout);
						delete_post_meta($acf->ID, 'field_group_layout');

						// show_on_page
						$show_on_page = get_post_meta($acf->ID, 'show_on_page', true) ? get_post_meta($acf->ID, 'show_on_page', true) : array();
						if($show_on_page)
				 		{
				 			$show_on_page = unserialize($show_on_page);
				 		}
				 		update_post_meta($acf->ID, 'show_on_page', $show_on_page);

					}
				}

			    $return = array(
			    	'status'	=>	true,
					'message'	=>	"Migrating Options...",
					'next'		=>	'3.0.0 (step 2)',
			    );

			break;

			/*---------------------
			*
			*	3.0.0
			*
			*--------------------*/

			case '3.0.0 (step 2)':

				// get acf's
				$acfs = get_posts(array(
					'numberposts' 	=> -1,
					'post_type' 	=> 'acf',
					'orderby' 		=> 'menu_order title',
					'order' 		=> 'asc',
					'suppress_filters' => false,
				));

				if($acfs)
				{
					foreach($acfs as $acf)
					{
						// allorany doesn't need to change!

			 			$rules = $wpdb->get_results("SELECT * FROM $acf_rules WHERE acf_id = '$acf->ID' ORDER BY order_no ASC", ARRAY_A);

						if($rules)
						{
							foreach($rules as $rule)
							{
								// options rule has changed
								if($rule['param'] == 'options_page')
								{
									$rule['value'] = 'Options';
								}

								add_post_meta($acf->ID, 'rule', $rule);
							}
						}

					}
				}

			    $return = array(
			    	'status'	=>	true,
					'message'	=>	"Migrating Location Rules...",
					'next'		=>	'3.0.0 (step 3)',
			    );

			break;

			/*---------------------
			*
			*	3.0.0
			*
			*--------------------*/

			case '3.0.0 (step 3)':

				$message = "Migrating Fields?";

			    $parent_id = 0;
			    $fields = $wpdb->get_results("SELECT * FROM $acf_fields WHERE parent_id = $parent_id ORDER BY order_no, name", ARRAY_A);

			 	if($fields)
			 	{
					// loop through fields
				 	foreach($fields as $field)
				 	{

						// unserialize options
						if(@unserialize($field['options']))
						{
							$field['options'] = unserialize($field['options']);
						}
						else
						{
							$field['options'] = array();
						}


				 		// sub fields
				 		if($field['type'] == 'repeater')
				 		{
				 			$field['options']['sub_fields'] = array();

				 			$parent_id = $field['id'];
				 			$sub_fields = $wpdb->get_results("SELECT * FROM $acf_fields WHERE parent_id = $parent_id ORDER BY order_no, name", ARRAY_A);


				 			// if fields are empty, this must be a new or broken acf.
						 	if(empty($sub_fields))
						 	{
						 		$field['options']['sub_fields'] = array();
						 	}
						 	else
						 	{
						 		// loop through fields
							 	foreach($sub_fields as $sub_field)
							 	{
							 		// unserialize options
							 		if(@unserialize($sub_field['options']))
									{
										$sub_field['options'] = @unserialize($sub_field['options']);
									}
									else
									{
										$sub_field['options'] = array();
									}

									// merge options with field
							 		$sub_field = array_merge($sub_field, $sub_field['options']);

							 		unset($sub_field['options']);

									// each field has a unique id!
									if(!isset($sub_field['key'])) $sub_field['key'] = 'field_' . $sub_field['id'];

									$field['options']['sub_fields'][] = $sub_field;
								}
						 	}

				 		}
				 		// end if sub field


				 		// merge options with field
				 		$field = array_merge($field, $field['options']);

				 		unset($field['options']);

				 		// each field has a unique id!
						if(!isset($field['key'])) $field['key'] = 'field_' . $field['id'];

						// update field
						$this->parent->update_field( $field['post_id'], $field);

				 		// create field name (field_rand)
				 		//$message .= print_r($field, true) . '<br /><br />';
				 	}
				 	// end foreach $fields
			 	}


				$return = array(
			    	'status'	=>	true,
					'message'	=>	$message,
					'next'		=>	'3.0.0 (step 4)',
			    );

			break;

			/*---------------------
			*
			*	3.0.0
			*
			*--------------------*/

			case '3.0.0 (step 4)':

				$message = "Migrating Values...";

				// update normal values
				$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);
				if($values)
				{
					foreach($values as $value)
					{
						// options page
						if($value['post_id'] == 0) $value['post_id'] = 999999999;

						// unserialize value (relationship, multi select, etc)
						if(@unserialize($value['meta_value']))
						{
							$value['meta_value'] = unserialize($value['meta_value']);
						}

						update_post_meta($value['post_id'], $value['meta_key'], $value['meta_value']);
						update_post_meta($value['post_id'], '_' . $value['meta_key'], 'field_' . $value['field_id']);
					}
				}

				// update repeater values
				$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);
				if($values)
				{
					$rows = array();

					foreach($values as $value)
					{
						// update row count
						$row = (int) $value['order_no'] + 1;

						// options page
						if($value['post_id'] == 0) $value['post_id'] = 999999999;

						// unserialize value (relationship, multi select, etc)
						if(@unserialize($value['meta_value']))
						{
							$value['meta_value'] = unserialize($value['meta_value']);
						}

						// current row
						$current_row = isset($rows[$value['post_id']][$value['field_id']]) ? $rows[$value['post_id']][$value['field_id']] : 0;
						if($row > $current_row) $rows[$value['post_id']][$value['field_id']] = (int) $row;

						// get field name
						$field_name = $wpdb->get_var($wpdb->prepare("SELECT name FROM $acf_fields WHERE id = %d", $value['field_id']));

						// get sub field name
						$sub_field_name = $wpdb->get_var($wpdb->prepare("SELECT name FROM $acf_fields WHERE id = %d", $value['sub_field_id']));

						// save new value
						$new_meta_key = $field_name . '_' . $value['order_no'] . '_' . $sub_field_name;
						update_post_meta($value['post_id'], $new_meta_key , $value['meta_value']);

						// save value hidden field id
						update_post_meta($value['post_id'], '_' . $new_meta_key, 'field_' . $value['sub_field_id']);
					}

					foreach($rows as $post_id => $field_ids)
					{
						foreach($field_ids as $field_id => $row_count)
						{
							// get sub field name
							$field_name = $wpdb->get_var($wpdb->prepare("SELECT name FROM $acf_fields WHERE id = %d", $field_id));

							delete_post_meta($post_id, $field_name);
							update_post_meta($post_id, $field_name, $row_count);
							update_post_meta($post_id, '_' . $field_name, 'field_' . $field_id);

						}
					}

				}

				// update version (only upgrade 1 time)
				update_option('acf_version','3.0.0');

			    $return = array(
			    	'status'	=>	true,
					'message'	=>	$message,
					'next'		=>	'3.1.8',
			    );

			break;


			/*---------------------
			*
			*	3.1.8
			*
			*--------------------*/

			case '3.1.8':

				// vars
				$message = __("Migrating options values from the $wp_postmeta table to the $wp_options table",'acf') . '...';

				// update normal values
				$rows = $wpdb->get_results( $wpdb->prepare("SELECT meta_key FROM $wp_postmeta WHERE post_id = %d", 999999999) , ARRAY_A);

				if($rows)
				{
					foreach($rows as $row)
					{
						// original name
						$field_name = $row['meta_key'];


						//  name
						$new_name = "";
						if( substr($field_name, 0, 1) == "_" )
						{
							 $new_name = '_options' . $field_name;
						}
						else
						{
							$new_name = 'options_' . $field_name;
						}


						// value
						$value = get_post_meta( 999999999, $field_name, true );


						// update option
						update_option( $new_name, $value );


						// deleet old postmeta
						delete_post_meta( 999999999, $field_name );

					}
					// foreach($values as $value)
				}
				// if($values)


				// update version
				update_option('acf_version','3.1.8');

			    $return = array(
			    	'status'	=>	true,
					'message'	=>	$message,
					'next'		=>	'3.2.5',
			    );

			break;


			/*---------------------
			*
			*	3.1.8
			*
			*--------------------*/

			case '3.2.5':

				// vars
				$message = __("Modifying field group options 'show on page'",'acf') . '...';


				// get acf's
				$acfs = get_posts(array(
					'numberposts' 	=> -1,
					'post_type' 	=> 'acf',
					'orderby' 		=> 'menu_order title',
					'order' 		=> 'asc',
					'suppress_filters' => false,
				));


				$show_all = array('the_content', 'discussion', 'custom_fields', 'comments', 'slug', 'author');


				// populate acfs
				if($acfs)
				{
					foreach($acfs as $acf)
					{
						$show_on_page = get_post_meta($acf->ID, 'show_on_page', true) ? get_post_meta($acf->ID, 'show_on_page', true) : array();

						$hide_on_screen = array_diff($show_all, $show_on_page);

						update_post_meta($acf->ID, 'hide_on_screen', $hide_on_screen);
						delete_post_meta($acf->ID, 'show_on_page');

					}
				}


				// update version
				update_option('acf_version','3.2.5');

			    $return = array(
			    	'status'	=>	true,
					'message'	=>	$message,
					'next'		=>	'3.3.3',
			    );

			break;


			/*
			*  3.3.3
			*
			*  @description: changed field option: taxonomies filter on relationship / post object and page link fields.
			*  @created: 20/07/12
			*/

			case '3.3.3':

				// vars
				$message = __("Modifying field option 'taxonomy'",'acf') . '...';
				$wp_term_taxonomy = $wpdb->prefix.'term_taxonomy';
				$term_taxonomies = array();

				$rows = $wpdb->get_results("SELECT * FROM $wp_term_taxonomy", ARRAY_A);

				if($rows)
				{
					foreach($rows as $row)
					{
						$term_taxonomies[ $row['term_id'] ] = $row['taxonomy'] . ":" . $row['term_id'];
					}
				}


				// get acf's
				$acfs = get_posts(array(
					'numberposts' 	=> -1,
					'post_type' 	=> 'acf',
					'orderby' 		=> 'menu_order title',
					'order' 		=> 'asc',
					'suppress_filters' => false,
				));

				// populate acfs
				if($acfs)
				{
				foreach($acfs as $acf)
				{
					$fields = $this->parent->get_acf_fields($acf->ID);

					if( $fields )
					{
					foreach( $fields as $field )
					{

						// only edit the option: taxonomy
						if( !isset($field['taxonomy']) )
						{
							continue;
						}


						if( is_array($field['taxonomy']) )
						{
						foreach( $field['taxonomy'] as $k => $v )
						{

							// could be "all"
							if( !is_numeric($v) )
							{
								continue;
							}

							$field['taxonomy'][ $k ] = $term_taxonomies[ $v ];


						}
						// foreach( $field['taxonomy'] as $k => $v )
						}
						// if( $field['taxonomy'] )


						$this->parent->update_field( $acf->ID, $field);

					}
					// foreach( $fields as $field )
					}
					// if( $fields )
				}
				// foreach($acfs as $acf)
				}
				// if($acfs)


				// update version
				update_option('acf_version','3.3.3');

				$return = array(
			    	'status'	=>	true,
					'message'	=>	$message,
					'next'		=>	'3.4.1',
			    );

			break;


			/*
			*  3.4.1
			*
			*  @description: Move user custom fields from wp_options to wp_usermeta
			*  @created: 20/07/12
			*/

			case '3.4.1':

				// vars
				$message = __("Moving user custom fields from wp_options to wp_usermeta",'acf') . '...';

				$option_row_ids = array();
				$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);


				if( $option_rows )
				{
					foreach( $option_rows as $k => $row)
					{
						preg_match('/user_([0-9]+)_(.*)/', $row['option_name'], $matches);


						// if no matches, this is not an acf value, ignore it
						if( !$matches )
						{
							continue;
						}


						// add to $delete_option_rows
						$option_row_ids[] = $row['option_id'];


						// meta_key prefix
						$meta_key_prefix = "";
						if( substr($row['option_name'], 0, 1) == "_" )
						{
							$meta_key_prefix = '_';
						}


						// update user meta
						update_user_meta( $matches[1], $meta_key_prefix . $matches[2], $row['option_value'] );

					}
				}


				// clear up some memory ( aprox 14 kb )
				unset( $option_rows );


				// remove $option_row_ids
				if( $option_row_ids )
				{
					$option_row_ids = implode(', ', $option_row_ids);

					$wpdb->query("DELETE FROM $wpdb->options WHERE option_id IN ($option_row_ids)");
				}


				// update version
				update_option('acf_version','3.4.1');

				$return = array(
			    	'status'	=>	true,
					'message'	=>	$message,
					'next'		=>	false,
			    );

			break;


		}

		// return json
		echo json_encode($return);
		die;

	}




}

new acf_upgrade();

?>