clone.php 26.5 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 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
<?php

/*
*  ACF Clone Field Class
*
*  All the logic for this field type
*
*  @class 		acf_field_clone
*  @extends		acf_field
*  @package		ACF
*  @subpackage	Fields
*/

if( ! class_exists('acf_field_clone') ) :

class acf_field_clone extends acf_field {
	
	
	/*
	*  __construct
	*
	*  This function will setup the field type data
	*
	*  @type	function
	*  @date	5/03/2014
	*  @since	5.0.0
	*
	*  @param	n/a
	*  @return	n/a
	*/
	
	function __construct() {
		
		// vars
		$this->name = 'clone';
		$this->label = _x('Clone', 'noun', 'acf');
		$this->category = 'layout';
		$this->defaults = array(
			'clone' 		=> '',
			'prefix_label'	=> 0,
			'prefix_name'	=> 0,
			'display'		=> 'seamless',
			'layout'		=> 'block'
		);
		$this->cloning = array();
		
		
		// register filter
		acf_enable_filter('clone');
		
		
		// ajax
		add_action('wp_ajax_acf/fields/clone/query', array($this, 'ajax_query'));
		
		
		// filters
		add_filter('acf/get_fields', 		array($this, 'acf_get_fields'), 5, 2);
		add_filter('acf/prepare_field',		array($this, 'acf_prepare_field'), 10, 1);
		add_filter('acf/clone_field',		array($this, 'acf_clone_field'), 10, 2);
		
		
		// do not delete!
    	parent::__construct();
    	
	}
	
	
	/*
	*  is_enabled
	*
	*  This function will return true if acf_local functionality is enabled
	*
	*  @type	function
	*  @date	14/07/2016
	*  @since	5.4.0
	*
	*  @param	n/a
	*  @return	n/a
	*/
	
	function is_enabled() {
		
		return acf_is_filter_enabled('clone');
		
	}
	
	
	/*
	*  load_field()
	*
	*  This filter is appied to the $field after it is loaded from the database
	*
	*  @type	filter
	*  @since	3.6
	*  @date	23/01/13
	*
	*  @param	$field - the field array holding all the field options
	*
	*  @return	$field - the field array holding all the field options
	*/
	
	function load_field( $field ) {
		
		// bail early if not enabled
		if( !$this->is_enabled() ) return $field;
		
		
		// load sub fields
		// - sub field name's will be modified to include prefix_name settings
		$field['sub_fields'] = $this->get_cloned_fields( $field );
		
		
		// return
		return $field;
		
	}
	
	
	/*
	*  acf_get_fields
	*
	*  This function will hook into the 'acf/get_fields' filter and inject/replace seamless clones fields
	*
	*  @type	function
	*  @date	17/06/2016
	*  @since	5.3.8
	*
	*  @param	$fields (array)
	*  @param	$parent (array)
	*  @return	$fields
	*/
	
	function acf_get_fields( $fields, $parent ) {
		
		// bail early if not enabled
		if( !$this->is_enabled() ) return $fields;
		
		
		// vars
		$i = 0;
		
		
		// loop
		while( $i < count($fields) ) {
			
			// vars
			$field = $fields[ $i ];
			
			
			// $i
			$i++;
			
			
			// bail ealry if not a clone field
			if( $field['type'] != 'clone' ) continue;
			
			
			// bail ealry if not seamless
			if( $field['display'] != 'seamless' ) continue;
			
			
			// replace this clone field with sub fields
			$i--;
			array_splice($fields, $i, 1, $field['sub_fields']);

		}
		
		
		// return
		return $fields;
		
	}
	
	
	/*
	*  get_cloned_fields
	*
	*  This function will return an array of fields for a given clone field
	*
	*  @type	function
	*  @date	28/06/2016
	*  @since	5.3.8
	*
	*  @param	$field (array)
	*  @param	$parent (array)
	*  @return	(array)
	*/
	
	function get_cloned_fields( $field ) {
		
		// vars
		$fields = array();
		
		
		// bail early if no clone setting
		if( empty($field['clone']) ) return $fields;
		
		
		// bail ealry if already cloning this field (avoid infinite looping)
		if( isset($this->cloning[ $field['key'] ]) ) return $fields;
		
		
		// update local ref
		$this->cloning[ $field['key'] ] = 1;
		
		
		// loop
		foreach( $field['clone'] as $selector ) {
			
			// field group
			if( acf_is_field_group_key($selector) ) {
				
				// vars
				$field_group = acf_get_field_group($selector);
				$field_group_fields = acf_get_fields($field_group);
				
				
				// bail early if no fields
				if( !$field_group_fields ) continue;
				
				
				// append
				$fields = array_merge($fields, $field_group_fields);
				
			// field
			} elseif( acf_is_field_key($selector) ) {
				
				// append
				$fields[] = acf_get_field($selector);
				
			}
			
		}
		
		
		// field has ve been loaded for this $parent, time to remove cloning ref
		unset( $this->cloning[ $field['key'] ] );
		
		
		// clear false values (fields that don't exist)
		$fields = array_filter($fields);
		
		
		// bail early if no sub fields
		if( empty($fields) ) return array();
		
		
		// loop
		// run acf_clone_field() on each cloned field to modify name, key, etc
		foreach( array_keys($fields) as $i ) {
			
			$fields[ $i ] = acf_clone_field( $fields[ $i ], $field );
				
		}
		
		
		// return
		return $fields;
		
	}
	
	
	/*
	*  acf_clone_field
	*
	*  This function is run when cloning a clone field
	*  Important to run the acf_clone_field function on sub fields to pass on settings such as 'parent_layout' 
	*
	*  @type	function
	*  @date	28/06/2016
	*  @since	5.3.8
	*
	*  @param	$field (array)
	*  @param	$clone_field (array)
	*  @return	$field
	*/
	
	function acf_clone_field( $field, $clone_field ) {
		
		// bail early if this field is being cloned by some other kind of field (future proof)
		if( $clone_field['type'] != 'clone' ) return $field;
		
		
		// backup (used later)
		// - backup only once (cloned clone fields can cause issues)
		if( !isset($field['__key']) ) {
			
			$field['__key'] = $field['key'];
			$field['__name'] = $field['_name'];
			$field['__label'] = $field['label'];
			
		}
		
		
		// modify key 
		// - this will allow sub clone fields to correctly load values for the same cloned field
		// - the original key will later be restored by acf/prepare_field allowing conditional logic JS to work
		$field['key'] = $clone_field['key'] . '_' . $field['key'];
		
		
		// modify prefix allowing clone field to save sub fields
		// - only used for parent seamless fields. Block or sub field's prefix will be overriden which also works
		$field['prefix'] .= '[' . $clone_field['key'] . ']';
		
		
		// label_format
		if( $clone_field['prefix_label'] ) {
			
			$field['label'] = $clone_field['label'] . ' ' . $field['label'];
			
		}
		
		
		// name_format
		if( $clone_field['prefix_name'] ) {
			
			//acf_log('== acf_clone_field ==');
			//acf_log('- clone name', $clone_field['name']);
			//acf_log('- clone _name', $clone_field['_name']);
			
			//$name = $field['name'];
			//$_name = $field['_name'];
			
			$field['name'] = $clone_field['name'] . '_' . $field['_name'];
			
			//acf_log('- field name:', $name, '=>', $field['name']);
			
			
			if( $clone_field['display'] == 'seamless' ) {
				
				$field['_name'] = $clone_field['_name'] . '_' . $field['_name'];
				
			}
			
			//acf_log('- field _name:', $_name, '=>', $field['_name']);
			
			//acf_log('');
			
		}
		
		
		// required
		if( $clone_field['required'] ) {
			
			$field['required'] = 1;
			
		}
		
		
		// type specific
		if( $field['type'] == 'clone' ) {
			
			$field = $this->acf_clone_clone_field( $field, $clone_field );
			
		}
		
		
		// return
		return $field;
		
	}
	
	
	/*
	*  acf_clone_clone_field
	*
	*  This function is run when cloning a clone field
	*  Important to run the acf_clone_field function on sub fields to pass on settings such as 'parent_layout' 
	*
	*  @type	function
	*  @date	28/06/2016
	*  @since	5.3.8
	*
	*  @param	$field (array)
	*  @param	$clone_field (array)
	*  @return	$field
	*/
	
	function acf_clone_clone_field( $field, $clone_field ) {
		
		// when cloning a clone field, it is important to also change the _name too
		// this allows sub clone fields to appear correctly in get_row() row array
		// - commented out. This may not be neccessary due to new line 315
		if( $field['prefix_name'] ) {
			
			$clone_field['name'] = $field['_name'];
			$clone_field['_name'] = $field['_name'];
			
		}
		
		
		// bail early if no sub fields
		if( empty($field['sub_fields']) ) return $field;
		
		
		// loop
		foreach( array_keys($field['sub_fields']) as $i ) {
			
			// get sub field
			$sub_field = $field['sub_fields'][ $i ];
			
			
			// clone
			$sub_field = acf_clone_field( $sub_field, $clone_field );
			
			
			// update
			$field['sub_fields'][ $i ] = $sub_field;
			
		}
		
		
		// return
		return $field;
			
	}
	
	
	/*
	*  prepare_field_for_db
	*
	*  description
	*
	*  @type	function
	*  @date	4/11/16
	*  @since	5.5.0
	*
	*  @param	$post_id (int)
	*  @return	$post_id (int)
	*/
	
	function prepare_field_for_db( $field ) {
		
		// bail early if no sub fields
		if( empty($field['sub_fields']) ) return $field;
		
		
		// bail early if name == _name
		// this is a parent clone field and does not require any modification to sub field names
		if( $field['name'] == $field['_name'] ) return $field;
		
		
		// this is a sub field
		// _name = 'my_field'
		//  name = 'rep_0_my_field'
		// modify all sub fields to add 'rep_0_' name prefix (prefix_name setting has already been applied)
		$length = strlen($field['_name']);
		$prefix = substr($field['name'], 0, -$length);
		
		
		// bail ealry if _name is not found at the end of name (unknown potential error)
		if( $prefix . $field['_name'] !== $field['name'] ) return $field;
		
		//acf_log('== prepare_field_for_db ==');
		//acf_log('- clone name:', $field['name']);
		//acf_log('- clone _name:', $field['_name']);
		
		// loop
		foreach( array_keys($field['sub_fields']) as $i ) {
			
			// get sub field
			$sub_field = $field['sub_fields'][ $i ];
			
			//$name = $sub_field['name'];
			$sub_field['name'] = $prefix . $sub_field['name'];
			//acf_log('- field name:', $name, '=>', $sub_field['name']);
			
			// update
			$field['sub_fields'][ $i ] = $sub_field;
			
		}
		//acf_log('');
		
		// return
		return $field;

	}
		
	
	/*
	*  load_value()
	*
	*  This filter is applied to the $value after it is loaded from the db
	*
	*  @type	filter
	*  @since	3.6
	*  @date	23/01/13
	*
	*  @param	$value (mixed) the value found in the database
	*  @param	$post_id (mixed) the $post_id from which the value was loaded
	*  @param	$field (array) the field array holding all the field options
	*  @return	$value
	*/
	
	function load_value( $value, $post_id, $field ) {
		
		// bail early if no sub fields
		if( empty($field['sub_fields']) ) return $value;
		
		
		// modify names
		$field = $this->prepare_field_for_db( $field );


		// load sub fields
		$value = array();
		
		
		// loop
		foreach( array_keys($field['sub_fields']) as $i ) {
			
			// get sub field
			$sub_field = $field['sub_fields'][ $i ];
			
			
			// get value
			$sub_value = acf_get_value( $post_id, $sub_field );
			
			
			// add value
			$value[ $sub_field['key'] ] = $sub_value;
			
		}
		
		
		// return
		return $value;
		
	}
	
	
	/*
	*  format_value()
	*
	*  This filter is appied to the $value after it is loaded from the db and before it is returned to the template
	*
	*  @type	filter
	*  @since	3.6
	*  @date	23/01/13
	*
	*  @param	$value (mixed) the value which was loaded from the database
	*  @param	$post_id (mixed) the $post_id from which the value was loaded
	*  @param	$field (array) the field array holding all the field options
	*
	*  @return	$value (mixed) the modified value
	*/
	
	function format_value( $value, $post_id, $field ) {
		
		// bail early if no value
		if( empty($value) ) return false;
		
		
		// modify names
		$field = $this->prepare_field_for_db( $field );
		
		
		// loop over rows
		foreach( array_keys($field['sub_fields']) as $i ) {
			
			// get sub field
			$sub_field = $field['sub_fields'][ $i ];
			
			
			// extract value
			$sub_value = acf_extract_var( $value, $sub_field['key'] );
			
			
			// format value
			$sub_value = acf_format_value( $sub_value, $post_id, $sub_field );
			
			
			// append to $row
			$value[ $sub_field['__name'] ] = $sub_value;
			
		}
		
		
		// return
		return $value;
		
	}
	
	
	/*
	*  update_value()
	*
	*  This filter is appied to the $value before it is updated in the db
	*
	*  @type	filter
	*  @since	3.6
	*  @date	23/01/13
	*
	*  @param	$value - the value which will be saved in the database
	*  @param	$field - the field array holding all the field options
	*  @param	$post_id - the $post_id of which the value will be saved
	*
	*  @return	$value - the modified value
	*/
	
	function update_value( $value, $post_id, $field ) {
		
		// bail early if no value
		if( !acf_is_array($value) ) return null;
		
		
		// bail ealry if no sub fields
		if( empty($field['sub_fields']) ) return null;
		
		
		// modify names
		$field = $this->prepare_field_for_db( $field );
		
		
		// loop
		foreach( array_keys($field['sub_fields']) as $i ) {
			
			// vars
			$sub_field = $field['sub_fields'][ $i ];
			$v = false;
			
			
			// key (backend)
			if( isset($value[ $sub_field['key'] ]) ) {
				
				$v = $value[ $sub_field['key'] ];
			
			// name (frontend)
			} elseif( isset($value[ $sub_field['_name'] ]) ) {
				
				$v = $value[ $sub_field['_name'] ];
			
			// empty
			} else {
				
				// input is not set (hidden by conditioanl logic)
				continue;
				
			}
			
			
			// restore original field key
			$sub_field = $this->acf_prepare_field( $sub_field );
			
			
			// update value
			acf_update_value( $v, $post_id, $sub_field );
			
		}
		
		
		// return
		return '';
		
	}
	
	
	/*
	*  render_field()
	*
	*  Create the HTML interface for your field
	*
	*  @param	$field - an array holding all the field's data
	*
	*  @type	action
	*  @since	3.6
	*  @date	23/01/13
	*/
	
	function render_field( $field ) {
		
		// bail early if no sub fields
		if( empty($field['sub_fields']) ) return;
		
		
		// load values
		foreach( array_keys($field['sub_fields']) as $i ) {
			
			// vars
			$sub_field = $field['sub_fields'][ $i ];
			
			
			// add value
			if( isset($field['value'][ $sub_field['key'] ]) ) {
				
				// this is a normal value
				$sub_field['value'] = $field['value'][ $sub_field['key'] ];
				
			} elseif( isset($sub_field['default_value']) ) {
				
				// no value, but this sub field has a default value
				$sub_field['value'] = $sub_field['default_value'];
				
			}
			
			
			// update prefix to allow for nested values
			$sub_field['prefix'] = $field['name'];
			
			
			// restore label
			$sub_field['label'] = $sub_field['__label'];
			
			
			// restore required
			if( $field['required'] ) $sub_field['required'] = 0;
			
			
			// append
			$field['sub_fields'][ $i ] = $sub_field;
		
		}
		
		
		// render
		if( $field['layout'] == 'table' ) {
			
			$this->render_field_table( $field );
			
		} else {
			
			$this->render_field_block( $field );
			
		}
		
	}
	
	
	/*
	*  render_field_block
	*
	*  description
	*
	*  @type	function
	*  @date	12/07/2016
	*  @since	5.4.0
	*
	*  @param	$post_id (int)
	*  @return	$post_id (int)
	*/
	
	function render_field_block( $field ) {
		
		// vars
		$label_placement = $field['layout'] == 'block' ? 'top' : 'left';
		
		
		// html
		echo '<div class="acf-clone-fields acf-fields -'.$label_placement.'">';
			
		foreach( $field['sub_fields'] as $sub_field ) {
			
			acf_render_field_wrap( $sub_field );
			
		}
		
		echo '</div>';
		
	}
	
	
	/*
	*  render_field_table
	*
	*  description
	*
	*  @type	function
	*  @date	12/07/2016
	*  @since	5.4.0
	*
	*  @param	$post_id (int)
	*  @return	$post_id (int)
	*/
	
	function render_field_table( $field ) {
		
?>
<table class="acf-table">
	<thead>
		<tr>
		<?php foreach( $field['sub_fields'] as $sub_field ): 
			
			// prepare field (allow sub fields to be removed)
			$sub_field = acf_prepare_field($sub_field);
			
			
			// bail ealry if no field
			if( !$sub_field ) continue;
			
			
			// vars
			$atts = array();
			$atts['class'] = 'acf-th';
			$atts['data-name'] = $sub_field['_name'];
			$atts['data-type'] = $sub_field['type'];
			$atts['data-key'] = $sub_field['key'];
			
			
			// Add custom width
			if( $sub_field['wrapper']['width'] ) {
			
				$atts['data-width'] = $sub_field['wrapper']['width'];
				$atts['style'] = 'width: ' . $sub_field['wrapper']['width'] . '%;';
				
			}
			
				
			?>
			<th <?php acf_esc_attr_e( $atts ); ?>>
				<?php echo acf_get_field_label( $sub_field ); ?>
				<?php if( $sub_field['instructions'] ): ?>
					<p class="description"><?php echo $sub_field['instructions']; ?></p>
				<?php endif; ?>
			</th>
		<?php endforeach; ?>
		</tr>
	</thead>
	<tbody>
		<tr class="acf-row">
		<?php 
		
		foreach( $field['sub_fields'] as $sub_field ) {
			
			acf_render_field_wrap( $sub_field, 'td' );
			
		}
				
		?>
		</tr>
	</tbody>
</table>
<?php
		
	}
	
	
	/*
	*  render_field_settings()
	*
	*  Create extra options for your field. This is rendered when editing a field.
	*  The value of $field['name'] can be used (like bellow) to save extra data to the $field
	*
	*  @param	$field	- an array holding all the field's data
	*
	*  @type	action
	*  @since	3.6
	*  @date	23/01/13
	*/
	
	function render_field_settings( $field ) {
		
		// temp enable 'local' to allow .json fields to be displayed
		acf_enable_filter('local');
		
		// default_value
		acf_render_field_setting( $field, array(
			'label'			=> __('Fields', 'acf'),
			'instructions'	=> __('Select one or more fields you wish to clone','acf'),
			'type'			=> 'select',
			'name'			=> 'clone',
			'multiple' 		=> 1,
			'allow_null' 	=> 1,
			'choices'		=> $this->get_clone_setting_choices( $field['clone'] ),
			'ui'			=> 1,
			'ajax'			=> 1,
			'ajax_action'	=> 'acf/fields/clone/query',
			'placeholder'	=> '',
		));
		
		acf_disable_filter('local');
		
		
		// display
		acf_render_field_setting( $field, array(
			'label'			=> __('Display','acf'),
			'instructions'	=> __('Specify the style used to render the clone field', 'acf'),
			'type'			=> 'select',
			'name'			=> 'display',
			'class'			=> 'setting-display',
			'choices'		=> array(
				'group'			=> __('Group (displays selected fields in a group within this field)','acf'),
				'seamless'		=> __('Seamless (replaces this field with selected fields)','acf'),
			),
		));
		
		
		// layout
		acf_render_field_setting( $field, array(
			'label'			=> __('Layout','acf'),
			'instructions'	=> __('Specify the style used to render the selected fields', 'acf'),
			'type'			=> 'radio',
			'name'			=> 'layout',
			'layout'		=> 'horizontal',
			'choices'		=> array(
				'block'			=> __('Block','acf'),
				'table'			=> __('Table','acf'),
				'row'			=> __('Row','acf')
			)
		));
		
		
		// prefix_label
		$instructions = __('Labels will be displayed as %s', 'acf');
		$instructions = sprintf($instructions, '<code class="prefix-label-code-1"></code>');
		acf_render_field_setting( $field, array(
			'label'			=> __('Prefix Field Labels','acf'),
			'message'	=> $instructions,
			//'instructions_placement'	=> 'field',
			'name'			=> 'prefix_label',
			'class'			=> 'setting-prefix-label',
			'type'			=> 'true_false',
			'ui'			=> 1,
		));
		
		
		// prefix_name
		$instructions = __('Values will be saved as %s', 'acf');
		$instructions = sprintf($instructions, '<code class="prefix-name-code-1"></code>');
		acf_render_field_setting( $field, array(
			'label'			=> __('Prefix Field Names','acf'),
			'message'	=> $instructions,
			//'instructions_placement'	=> 'field',
			'name'			=> 'prefix_name',
			'class'			=> 'setting-prefix-name',
			'type'			=> 'true_false',
			'ui'			=> 1,
		));
		
	}
	
	
	/*
	*  get_clone_setting_choices
	*
	*  This function will return an array of choices data for Select2
	*
	*  @type	function
	*  @date	17/06/2016
	*  @since	5.3.8
	*
	*  @param	$value (mixed)
	*  @return	(array)
	*/
	
	function get_clone_setting_choices( $value ) {
		
		// vars
		$choices = array();
		
		
		// bail early if no $value
		if( empty($value) ) return $choices;
		
		
		// force value to array
		$value = acf_get_array( $value );
			
			
		// loop
		foreach( $value as $v ) {
			
			$choices[ $v ] = $this->get_clone_setting_choice( $v );
			
		}
		
		
		// return
		return $choices;
		
	}
	
	
	/*
	*  get_clone_setting_choice
	*
	*  This function will return the label for a given clone choice
	*
	*  @type	function
	*  @date	17/06/2016
	*  @since	5.3.8
	*
	*  @param	$selector (mixed)
	*  @return	(string)
	*/
	
	function get_clone_setting_choice( $selector = '' ) {
		
		// bail early no selector
		if( !$selector ) return '';
		
		
		// ajax_fields
		if( isset($_POST['fields'][ $selector ]) ) {
			
			return $this->get_clone_setting_field_choice( $_POST['fields'][ $selector ] );
						
		}
		
		
		// field
		if( acf_is_field_key($selector) ) {
			
			return $this->get_clone_setting_field_choice( acf_get_field($selector) );
			
		}
		
		
		// group
		if( acf_is_field_group_key($selector) ) {
			
			return $this->get_clone_setting_group_choice( acf_get_field_group($selector) );
			
		} 
		
		
		// return
		return $selector;
		
	}
	
	
	/*
	*  get_clone_setting_field_choice
	*
	*  This function will return the text for a field choice
	*
	*  @type	function
	*  @date	20/07/2016
	*  @since	5.4.0
	*
	*  @param	$field (array)
	*  @return	(string)
	*/
	
	function get_clone_setting_field_choice( $field ) {
		
		// bail early if no field
		if( !$field ) return __('Unknown field', 'acf');
		
		
		// title
		$title = $field['label'] ? $field['label'] : __('(no title)', 'acf');
					
		
		// append type
		$title .= ' (' . $field['type'] . ')';
		
		
		// ancestors
		// - allow for AJAX to send through ancestors count
		$ancestors = isset($field['ancestors']) ? $field['ancestors'] : count(acf_get_field_ancestors($field));
		$title = str_repeat('- ', $ancestors) . $title;
		
		
		// return
		return $title;
		
	}
	
	
	/*
	*  get_clone_setting_group_choice
	*
	*  This function will return the text for a group choice
	*
	*  @type	function
	*  @date	20/07/2016
	*  @since	5.4.0
	*
	*  @param	$field_group (array)
	*  @return	(string)
	*/
	
	function get_clone_setting_group_choice( $field_group ) {
		
		// bail early if no field group
		if( !$field_group ) return __('Unknown field group', 'acf');
		
		
		// return
		return sprintf( __('All fields from %s field group', 'acf'), $field_group['title'] );
		
	}
	
	
	/*
	*  ajax_query
	*
	*  description
	*
	*  @type	function
	*  @date	17/06/2016
	*  @since	5.3.8
	*
	*  @param	$post_id (int)
	*  @return	$post_id (int)
	*/
	
	function ajax_query() {
		
		// validate
		if( !acf_verify_ajax() ) die();
		
		
		// disable field to allow clone fields to appear selectable
		acf_disable_filter('clone');
		
		
   		// options
   		$options = acf_parse_args($_POST, array(
			'post_id'	=> 0,
			'paged'		=> 0,
			's'			=> '',
			'title'		=> '',
			'fields'	=> array()
		));
		
		
		// vars
		$results = array();
		$s = false;
		$i = -1;
		$limit = 20;
		$range_start = $limit * ($options['paged']-1); 	//	0,	20,	40
		$range_end = $range_start + ($limit-1);			//	19,	39,	59
		
		
		// search
		if( $options['s'] !== '' ) {
			
			// strip slashes (search may be integer)
			$s = wp_unslash( strval($options['s']) );
			
		}		
		
		
		// load groups
		$field_groups = acf_get_field_groups();
		$field_group = false;
		
		
		// bail early if no field groups
		if( empty($field_groups) ) die();
		
		
		// move current field group to start
		foreach( array_keys($field_groups) as $j ) {
			
			// check ID
			if( $field_groups[ $j ]['ID'] !== $options['post_id'] ) continue;
			
			
			// extract field group and move to start
			$field_group = acf_extract_var($field_groups, $j);
			
			
			// field group found, stop looking
			break;
			
		}
		
		
		// if field group was not found, this is a new field group (not yet saved)
		if( !$field_group ) {
			
			$field_group = array(
				'ID'	=> $options['post_id'],
				'title'	=> $options['title'],
				'key'	=> '',
			);
			
		}
		
		
		// move current field group to start of list
		array_unshift($field_groups, $field_group);
		
		
		// loop
		foreach( $field_groups as $field_group ) {
			
			// vars
			$fields = false;
			$ignore_s = false;
			$data = array(
				'text'		=> $field_group['title'],
				'children'	=> array()
			);
			
			
			// get fields
			if( $field_group['ID'] == $options['post_id'] ) {
				
				$fields = $options['fields'];
				
			} else {
				
				$fields = acf_get_fields( $field_group );
				$fields = acf_prepare_fields_for_import( $fields );
			
			}
			
			
			// bail early if no fields
			if( !$fields ) continue;
			
			
			// show all children for field group search match
			if( $s !== false && stripos($data['text'], $s) !== false ) {
				
				$ignore_s = true;
				
			}
			
			
			// populate children
			$children = array();
			$children[] = $field_group['key'];
			foreach( $fields as $field ) { $children[] = $field['key']; }
			
			
			// loop
			foreach( $children as $child ) {
				
				// bail ealry if no key (fake field group or corrupt field)
				if( !$child ) continue;
				
				
				// vars
				$text = false;
				
				
				// bail early if is search, and $text does not contain $s
				if( $s !== false && !$ignore_s ) {
					
					// get early
					$text = $this->get_clone_setting_choice( $child );
					
					
					// search
					if( stripos($text, $s) === false ) continue;
					
				}
				
				
				// $i
				$i++;
				
				
				// bail early if $i is out of bounds
				if( $i < $range_start || $i > $range_end ) continue;
				
				
				
				// load text
				if( $text === false ) $text = $this->get_clone_setting_choice( $child );
				
				
				// append
				$data['children'][] = array(
					'id'	=> $child,
					'text'	=> $text
				);
				
			}
			
			
			// bail early if no children
			// - this group contained fields, but none shown on this page
			if( empty($data['children']) ) continue;
			
			
			// append
			$results[] = $data;
			
			
			// end loop if $i is out of bounds
			// - no need to look further
			if( $i > $range_end ) break;
				
		}
		
		
		// return
		acf_send_ajax_results(array(
			'results'	=> $results,
			'limit'		=> $limit
		));
		
	}
	
	
	/*
	*  acf_prepare_field
	*
	*  This function will restore a field's key ready for input
	*
	*  @type	function
	*  @date	6/09/2016
	*  @since	5.4.0
	*
	*  @param	$field (array)
	*  @return	$field
	*/
	
	function acf_prepare_field( $field ) {
		
		// bail ealry if not cloned
		if( empty($field['__key']) ) return $field;
		
		
		// restore
		$field['key'] = $field['__key'];
		
		
		// return
		return $field;
		
	}
	
	
	/*
	*  validate_value
	*
	*  description
	*
	*  @type	function
	*  @date	11/02/2014
	*  @since	5.0.0
	*
	*  @param	$post_id (int)
	*  @return	$post_id (int)
	*/
	
	function validate_value( $valid, $value, $field, $input ){
		
		// bail early if no $value
		if( empty($value) ) return $valid;
		
		
		// bail early if no sub fields
		if( empty($field['sub_fields']) ) return $valid;
		
		
		// loop
		foreach( array_keys($field['sub_fields']) as $i ) {
			
			// get sub field
			$sub_field = $field['sub_fields'][ $i ];
			$k = $sub_field['key'];
			
			
			// bail early if valu enot set (conditional logic?)
			if( !isset($value[ $k ]) ) continue;
			
			
			// validate
			acf_validate_value( $value[ $k ], $sub_field, "{$input}[{$k}]" );
			
		}
		
		
		// return
		return $valid;
		
	}
	
}


// initialize
acf_register_field_type( new acf_field_clone() );

endif; // class_exists check

?>