acf-field-group-functions.php 27.1 KB
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
<?php

// Register store.
acf_register_store( 'field-groups' )->prop( 'multisite', true );

/**
 * acf_get_field_group
 *
 * Retrieves a field group for the given identifier.
 *
 * @date    30/09/13
 * @since   5.0.0
 *
 * @param   (int|string) $id The field group ID, key or name.
 * @return  (array|false) The field group array.
 */
function acf_get_field_group( $id = 0 ) {

	// Allow WP_Post to be passed.
	if ( is_object( $id ) ) {
		$id = $id->ID;
	}

	// Check store.
	$store = acf_get_store( 'field-groups' );
	if ( $store->has( $id ) ) {
		return $store->get( $id );
	}

	// Check local fields first.
	if ( acf_is_local_field_group( $id ) ) {
		$field_group = acf_get_local_field_group( $id );

		// Then check database.
	} else {
		$field_group = acf_get_raw_field_group( $id );
	}

	// Bail early if no field group.
	if ( ! $field_group ) {
		return false;
	}

	// Validate field group.
	$field_group = acf_validate_field_group( $field_group );

	/**
	 * Filters the $field_group array after it has been loaded.
	 *
	 * @date    12/02/2014
	 * @since   5.0.0
	 *
	 * @param   array The field_group array.
	 */
	$field_group = apply_filters( 'acf/load_field_group', $field_group );

	// Store field group using aliasses to also find via key, ID and name.
	$store->set( $field_group['key'], $field_group );
	$store->alias( $field_group['key'], $field_group['ID'] );

	// Return.
	return $field_group;
}

/**
 * acf_get_raw_field_group
 *
 * Retrieves raw field group data for the given identifier.
 *
 * @date    18/1/19
 * @since   5.7.10
 *
 * @param   (int|string) $id The field ID, key or name.
 * @return  (array|false) The field group array.
 */
function acf_get_raw_field_group( $id = 0 ) {

	// Get raw field group from database.
	$post = acf_get_field_group_post( $id );
	if ( ! $post ) {
		return false;
	}

	// Bail early if incorrect post type.
	if ( $post->post_type !== 'acf-field-group' ) {
		return false;
	}

	// Unserialize post_content.
	$field_group = (array) maybe_unserialize( $post->post_content );

	// update attributes
	$field_group['ID']         = $post->ID;
	$field_group['title']      = $post->post_title;
	$field_group['key']        = $post->post_name;
	$field_group['menu_order'] = $post->menu_order;
	$field_group['active']     = in_array( $post->post_status, array( 'publish', 'auto-draft' ) );

	// Return field.
	return $field_group;
}

/**
 * acf_get_field_group_post
 *
 * Retrieves the field group's WP_Post object.
 *
 * @date    18/1/19
 * @since   5.7.10
 *
 * @param   (int|string) $id The field group's ID, key or name.
 * @return  (array|false) The field group's array.
 */
function acf_get_field_group_post( $id = 0 ) {

	// Get post if numeric.
	if ( is_numeric( $id ) ) {
		return get_post( $id );

		// Search posts if is string.
	} elseif ( is_string( $id ) ) {

		// Try cache.
		$cache_key = acf_cache_key( "acf_get_field_group_post:key:$id" );
		$post_id   = wp_cache_get( $cache_key, 'acf' );
		if ( $post_id === false ) {

			// Query posts.
			$posts = get_posts(
				array(
					'posts_per_page'         => 1,
					'post_type'              => 'acf-field-group',
					'post_status'            => array( 'publish', 'acf-disabled', 'trash' ),
					'orderby'                => 'menu_order title',
					'order'                  => 'ASC',
					'suppress_filters'       => false,
					'cache_results'          => true,
					'update_post_meta_cache' => false,
					'update_post_term_cache' => false,
					'acf_group_key'          => $id,
				)
			);

			// Update $post_id with a non false value.
			$post_id = $posts ? $posts[0]->ID : 0;

			// Update cache.
			wp_cache_set( $cache_key, $post_id, 'acf' );
		}

		// Check $post_id and return the post when possible.
		if ( $post_id ) {
			return get_post( $post_id );
		}
	}

	// Return false by default.
	return false;
}

/**
 * acf_is_field_group_key
 *
 * Returns true if the given identifier is a field group key.
 *
 * @date    6/12/2013
 * @since   5.0.0
 *
 * @param   string $id The identifier.
 * @return  bool
 */
function acf_is_field_group_key( $id = '' ) {

	// Check if $id is a string starting with "group_".
	if ( is_string( $id ) && substr( $id, 0, 6 ) === 'group_' ) {
		return true;
	}

	/**
	 * Filters whether the $id is a field group key.
	 *
	 * @date    23/1/19
	 * @since   5.7.10
	 *
	 * @param   bool $bool The result.
	 * @param   string $id The identifier.
	 */
	return apply_filters( 'acf/is_field_group_key', false, $id );
}

/**
 * acf_validate_field_group
 *
 * Ensures the given field group is valid.
 *
 * @date    18/1/19
 * @since   5.7.10
 *
 * @param   array $field The field group array.
 * @return  array
 */
function acf_validate_field_group( $field_group = array() ) {

	// Bail early if already valid.
	if ( is_array( $field_group ) && ! empty( $field_group['_valid'] ) ) {
		return $field_group;
	}

	// Apply defaults.
	$field_group = wp_parse_args(
		$field_group,
		array(
			'ID'                    => 0,
			'key'                   => '',
			'title'                 => '',
			'fields'                => array(),
			'location'              => array(),
			'menu_order'            => 0,
			'position'              => 'normal',
			'style'                 => 'default',
			'label_placement'       => 'top',
			'instruction_placement' => 'label',
			'hide_on_screen'        => array(),
			'active'                => true,
			'description'           => '',
			'show_in_rest'          => false,
		)
	);

	// Convert types.
	$field_group['ID']         = (int) $field_group['ID'];
	$field_group['menu_order'] = (int) $field_group['menu_order'];
	$field_group['active']     = (bool) $field_group['active'];

	// Field group is now valid.
	$field_group['_valid'] = true;

	/**
	 * Filters the $field_group array to validate settings.
	 *
	 * @date    12/02/2014
	 * @since   5.0.0
	 *
	 * @param   array $field_group The field group array.
	 */
	$field_group = apply_filters( 'acf/validate_field_group', $field_group );

	// return
	return $field_group;
}

/**
 * acf_get_valid_field_group
 *
 * Ensures the given field group is valid.
 *
 * @date        28/09/13
 * @since       5.0.0
 *
 * @param   array $field_group The field group array.
 * @return  array
 */
function acf_get_valid_field_group( $field_group = false ) {
	return acf_validate_field_group( $field_group );
}

/**
 * acf_translate_field_group
 *
 * Translates a field group's settings.
 *
 * @date    8/03/2016
 * @since   5.3.2
 *
 * @param   array $field_group The field group array.
 * @return  array
 */
function acf_translate_field_group( $field_group = array() ) {

	// Get settings.
	$l10n            = acf_get_setting( 'l10n' );
	$l10n_textdomain = acf_get_setting( 'l10n_textdomain' );

	// Translate field settings if textdomain is set.
	if ( $l10n && $l10n_textdomain ) {

		$field_group['title'] = acf_translate( $field_group['title'] );

		/**
		 * Filters the $field group array to translate strings.
		 *
		 * @date    12/02/2014
		 * @since   5.0.0
		 *
		 * @param   array $field_group The field group array.
		 */
		$field_group = apply_filters( 'acf/translate_field_group', $field_group );
	}

	// Return field.
	return $field_group;
}

// Translate field groups passing through validation.
add_action( 'acf/validate_field_group', 'acf_translate_field_group' );


/**
 * acf_get_field_groups
 *
 * Returns and array of field_groups for the given $filter.
 *
 * @date    30/09/13
 * @since   5.0.0
 *
 * @param   array $filter An array of args to filter results by.
 * @return  array
 */
function acf_get_field_groups( $filter = array() ) {

	// Vars.
	$field_groups = array();

	// Check database.
	$raw_field_groups = acf_get_raw_field_groups();
	if ( $raw_field_groups ) {
		foreach ( $raw_field_groups as $raw_field_group ) {
			$field_groups[] = acf_get_field_group( $raw_field_group['ID'] );
		}
	}

	/**
	 * Filters the $field_groups array.
	 *
	 * @date    12/02/2014
	 * @since   5.0.0
	 *
	 * @param   array $field_groups The array of field_groups.
	 */
	$field_groups = apply_filters( 'acf/load_field_groups', $field_groups );

	// Filter results.
	if ( $filter ) {
		return acf_filter_field_groups( $field_groups, $filter );
	}

	// Return field groups.
	return $field_groups;
}

/**
 * acf_get_raw_field_groups
 *
 * Returns and array of raw field_group data.
 *
 * @date    18/1/19
 * @since   5.7.10
 *
 * @param   void
 * @return  array
 */
function acf_get_raw_field_groups() {

	// Try cache.
	$cache_key = acf_cache_key( 'acf_get_field_group_posts' );
	$post_ids  = wp_cache_get( $cache_key, 'acf' );
	if ( $post_ids === false ) {

		// Query posts.
		$posts = get_posts(
			array(
				'posts_per_page'         => -1,
				'post_type'              => 'acf-field-group',
				'orderby'                => 'menu_order title',
				'order'                  => 'ASC',
				'suppress_filters'       => false, // Allow WPML to modify the query
				'cache_results'          => true,
				'update_post_meta_cache' => false,
				'update_post_term_cache' => false,
				'post_status'            => array( 'publish', 'acf-disabled' ),
			)
		);

		// Update $post_ids with a non false value.
		$post_ids = array();
		foreach ( $posts as $post ) {
			$post_ids[] = $post->ID;
		}

		// Update cache.
		wp_cache_set( $cache_key, $post_ids, 'acf' );
	}

	// Loop over ids and populate array of field groups.
	$field_groups = array();
	foreach ( $post_ids as $post_id ) {
		$field_groups[] = acf_get_raw_field_group( $post_id );
	}

	// Return field groups.
	return $field_groups;
}

/**
 * acf_filter_field_groups
 *
 * Returns a filtered aray of field groups based on the given $args.
 *
 * @date    29/11/2013
 * @since   5.0.0
 *
 * @param   array $field_groups An array of field groups.
 * @param   array $args An array of location args.
 * @return  array
 */
function acf_filter_field_groups( $field_groups, $args = array() ) {

	// Loop over field groups and check visibility.
	$filtered = array();
	if ( $field_groups ) {
		foreach ( $field_groups as $field_group ) {
			if ( acf_get_field_group_visibility( $field_group, $args ) ) {
				$filtered[] = $field_group;
			}
		}
	}

	// Return filtered.
	return $filtered;
}

/**
 * acf_get_field_group_visibility
 *
 * Returns true if the given field group's location rules match the given $args.
 *
 * @date    7/10/13
 * @since   5.0.0
 *
 * @param   array $field_groups An array of field groups.
 * @param   array $args An array of location args.
 * @return  bool
 */
function acf_get_field_group_visibility( $field_group, $args = array() ) {

	// Check if active.
	if ( ! $field_group['active'] ) {
		return false;
	}

	// Check if location rules exist
	if ( $field_group['location'] ) {

		// Get the current screen.
		$screen = acf_get_location_screen( $args );

		// Loop through location groups.
		foreach ( $field_group['location'] as $group ) {

			// ignore group if no rules.
			if ( empty( $group ) ) {
				continue;
			}

			// Loop over rules and determine if all rules match.
			$match_group = true;
			foreach ( $group as $rule ) {
				if ( ! acf_match_location_rule( $rule, $screen, $field_group ) ) {
					$match_group = false;
					break;
				}
			}

			// If this group matches, show the field group.
			if ( $match_group ) {
				return true;
			}
		}
	}

	// Return default.
	return false;
}

/**
 * acf_update_field_group
 *
 * Updates a field group in the database.
 *
 * @date    21/1/19
 * @since   5.7.10
 *
 * @param   array $field_group The field group array.
 * @return  array
 */
function acf_update_field_group( $field_group ) {

	// Validate field group.
	$field_group = acf_get_valid_field_group( $field_group );

	// May have been posted. Remove slashes.
	$field_group = wp_unslash( $field_group );

	// Parse types (converts string '0' to int 0).
	$field_group = acf_parse_types( $field_group );

	// Clean up location keys.
	if ( $field_group['location'] ) {

		// Remove empty values and convert to associated array.
		$field_group['location'] = array_filter( $field_group['location'] );
		$field_group['location'] = array_values( $field_group['location'] );
		$field_group['location'] = array_map( 'array_filter', $field_group['location'] );
		$field_group['location'] = array_map( 'array_values', $field_group['location'] );
	}

	// Make a backup of field group data and remove some args.
	$_field_group = $field_group;
	acf_extract_vars( $_field_group, array( 'ID', 'key', 'title', 'menu_order', 'fields', 'active', '_valid' ) );

	// Create array of data to save.
	$save = array(
		'ID'             => $field_group['ID'],
		'post_status'    => $field_group['active'] ? 'publish' : 'acf-disabled',
		'post_type'      => 'acf-field-group',
		'post_title'     => $field_group['title'],
		'post_name'      => $field_group['key'],
		'post_excerpt'   => sanitize_title( $field_group['title'] ),
		'post_content'   => maybe_serialize( $_field_group ),
		'menu_order'     => $field_group['menu_order'],
		'comment_status' => 'closed',
		'ping_status'    => 'closed',
	);

	// Unhook wp_targeted_link_rel() filter from WP 5.1 corrupting serialized data.
	remove_filter( 'content_save_pre', 'wp_targeted_link_rel' );

	// Slash data.
	// WP expects all data to be slashed and will unslash it (fixes '\' character issues).
	$save = wp_slash( $save );

	// Update or Insert.
	if ( $field_group['ID'] ) {
		wp_update_post( $save );
	} else {
		$field_group['ID'] = wp_insert_post( $save );
	}

	// Flush field group cache.
	acf_flush_field_group_cache( $field_group );

	/**
	 * Fires immediately after a field group has been updated.
	 *
	 * @date    12/02/2014
	 * @since   5.0.0
	 *
	 * @param   array $field_group The field group array.
	 */
	do_action( 'acf/update_field_group', $field_group );

	// Return field group.
	return $field_group;
}

/**
 * _acf_apply_unique_field_group_slug
 *
 * Allows full control over 'acf-field-group' slugs.
 *
 * @date    21/1/19
 * @since   5.7.10
 *
 * @param string $slug          The post slug.
 * @param int    $post_ID       Post ID.
 * @param string $post_status   The post status.
 * @param string $post_type     Post type.
 * @param int    $post_parent   Post parent ID
 * @param string $original_slug The original post slug.
 */
function _acf_apply_unique_field_group_slug( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) {

	// Check post type and reset to original value.
	if ( $post_type === 'acf-field-group' ) {
		return $original_slug;
	}

	// Return slug.
	return $slug;
}

// Hook into filter.
add_filter( 'wp_unique_post_slug', '_acf_apply_unique_field_group_slug', 999, 6 );

/**
 * acf_flush_field_group_cache
 *
 * Deletes all caches for this field group.
 *
 * @date    22/1/19
 * @since   5.7.10
 *
 * @param   array $field_group The field group array.
 * @return  void
 */
function acf_flush_field_group_cache( $field_group ) {

	// Delete stored data.
	acf_get_store( 'field-groups' )->remove( $field_group['key'] );

	// Flush cached post_id for this field group's key.
	wp_cache_delete( acf_cache_key( "acf_get_field_group_post:key:{$field_group['key']}" ), 'acf' );

	// Flush cached array of post_ids for collection of field groups.
	wp_cache_delete( acf_cache_key( 'acf_get_field_group_posts' ), 'acf' );
}

/**
 * acf_delete_field_group
 *
 * Deletes a field group from the database.
 *
 * @date    21/1/19
 * @since   5.7.10
 *
 * @param   (int|string) $id The field group ID, key or name.
 * @return  bool True if field group was deleted.
 */
function acf_delete_field_group( $id = 0 ) {

	// Disable filters to ensure ACF loads data from DB.
	acf_disable_filters();

	// Get the field_group.
	$field_group = acf_get_field_group( $id );

	// Bail early if field group was not found.
	if ( ! $field_group || ! $field_group['ID'] ) {
		return false;
	}

	// Delete fields.
	$fields = acf_get_fields( $field_group );
	if ( $fields ) {
		foreach ( $fields as $field ) {
			acf_delete_field( $field['ID'] );
		}
	}

	// Delete post.
	wp_delete_post( $field_group['ID'], true );

	// Flush field group cache.
	acf_flush_field_group_cache( $field_group );

	/**
	 * Fires immediately after a field group has been deleted.
	 *
	 * @date    12/02/2014
	 * @since   5.0.0
	 *
	 * @param   array $field_group The field group array.
	 */
	do_action( 'acf/delete_field_group', $field_group );

	// Return true.
	return true;
}

/**
 * acf_trash_field_group
 *
 * Trashes a field group from the database.
 *
 * @date    2/10/13
 * @since   5.0.0
 *
 * @param   (int|string) $id The field group ID, key or name.
 * @return  bool True if field group was trashed.
 */
function acf_trash_field_group( $id = 0 ) {

	// Disable filters to ensure ACF loads data from DB.
	acf_disable_filters();

	// Get the field_group.
	$field_group = acf_get_field_group( $id );

	// Bail early if field_group was not found.
	if ( ! $field_group || ! $field_group['ID'] ) {
		return false;
	}

	// Trash fields.
	$fields = acf_get_fields( $field_group );
	if ( $fields ) {
		foreach ( $fields as $field ) {
			acf_trash_field( $field['ID'] );
		}
	}

	// Trash post.
	wp_trash_post( $field_group['ID'], true );

	// Flush field group cache.
	acf_flush_field_group_cache( $field_group );

	/**
	 * Fires immediately after a field_group has been trashed.
	 *
	 * @date    12/02/2014
	 * @since   5.0.0
	 *
	 * @param   array $field_group The field_group array.
	 */
	do_action( 'acf/trash_field_group', $field_group );

	// Return true.
	return true;
}

/**
 * acf_untrash_field_group
 *
 * Restores a field_group from the trash.
 *
 * @date    2/10/13
 * @since   5.0.0
 *
 * @param   (int|string) $id The field_group ID, key or name.
 * @return  bool True if field_group was trashed.
 */
function acf_untrash_field_group( $id = 0 ) {

	// Disable filters to ensure ACF loads data from DB.
	acf_disable_filters();

	// Get the field_group.
	$field_group = acf_get_field_group( $id );

	// Bail early if field_group was not found.
	if ( ! $field_group || ! $field_group['ID'] ) {
		return false;
	}

	// Untrash fields.
	$fields = acf_get_fields( $field_group );
	if ( $fields ) {
		foreach ( $fields as $field ) {
			acf_untrash_field( $field['ID'] );
		}
	}

	// Untrash post.
	wp_untrash_post( $field_group['ID'], true );

	// Flush field group cache.
	acf_flush_field_group_cache( $field_group );

	/**
	 * Fires immediately after a field_group has been trashed.
	 *
	 * @date    12/02/2014
	 * @since   5.0.0
	 *
	 * @param   array $field_group The field_group array.
	 */
	do_action( 'acf/untrash_field_group', $field_group );

	// Return true.
	return true;
}

/**
 * Filter callback which returns the previous post_status instead of "draft" for the "acf-field-group" post type.
 *
 * Prior to WordPress 5.6.0, this filter was not needed as restored posts were always assigned their original status.
 *
 * @since 5.9.5
 *
 * @param string $new_status      The new status of the post being restored.
 * @param int    $post_id         The ID of the post being restored.
 * @param string $previous_status The status of the post at the point where it was trashed.
 * @return string.
 */
function _acf_untrash_field_group_post_status( $new_status, $post_id, $previous_status ) {
	return ( get_post_type( $post_id ) === 'acf-field-group' ) ? $previous_status : $new_status;
}

add_action( 'wp_untrash_post_status', '_acf_untrash_field_group_post_status', 10, 3 );

/**
 * acf_is_field_group
 *
 * Returns true if the given params match a field group.
 *
 * @date    21/1/19
 * @since   5.7.10
 *
 * @param   array $field_group The field group array.
 * @param   mixed $id An optional identifier to search for.
 * @return  bool
 */
function acf_is_field_group( $field_group = false ) {
	return (
		is_array( $field_group )
		&& isset( $field_group['key'] )
		&& isset( $field_group['title'] )
	);
}

/**
 * acf_duplicate_field_group
 *
 * Duplicates a field group.
 *
 * @date    16/06/2014
 * @since   5.0.0
 *
 * @param   (int|string) $id The field_group ID, key or name.
 * @param   int          $new_post_id Optional post ID to override.
 * @return  array The new field group.
 */
function acf_duplicate_field_group( $id = 0, $new_post_id = 0 ) {

	// Disable filters to ensure ACF loads data from DB.
	acf_disable_filters();

	// Get the field_group.
	$field_group = acf_get_field_group( $id );

	// Bail early if field_group was not found.
	if ( ! $field_group || ! $field_group['ID'] ) {
		return false;
	}

	// Get fields.
	$fields = acf_get_fields( $field_group );

	// Update attributes.
	$field_group['ID']  = $new_post_id;
	$field_group['key'] = uniqid( 'group_' );

	// Add (copy) to title when apropriate.
	if ( ! $new_post_id ) {
		$field_group['title'] .= ' (' . __( 'copy', 'acf' ) . ')';
	}

	// When importing a new field group, insert a temporary post and set the field group's ID.
	// This allows fields to be updated before the field group (field group ID is needed for field parent setting).
	if ( ! $field_group['ID'] ) {
		$field_group['ID'] = wp_insert_post( array( 'post_title' => $field_group['key'] ) );
	}

	// Duplicate fields.
	$duplicates = acf_duplicate_fields( $fields, $field_group['ID'] );

	// Save field group.
	$field_group = acf_update_field_group( $field_group );

	/**
	 * Fires immediately after a field_group has been duplicated.
	 *
	 * @date    12/02/2014
	 * @since   5.0.0
	 *
	 * @param   array $field_group The field_group array.
	 */
	do_action( 'acf/duplicate_field_group', $field_group );

	// return
	return $field_group;
}

/**
 * acf_get_field_group_style
 *
 * Returns the CSS styles generated from field group settings.
 *
 * @date    20/10/13
 * @since   5.0.0
 *
 * @param   array $field_group The field group array.
 * @return  string.
 */
function acf_get_field_group_style( $field_group ) {

	// Vars.
	$style    = '';
	$elements = array(
		'permalink'       => '#edit-slug-box',
		'the_content'     => '#postdivrich',
		'excerpt'         => '#postexcerpt',
		'custom_fields'   => '#postcustom',
		'discussion'      => '#commentstatusdiv',
		'comments'        => '#commentsdiv',
		'slug'            => '#slugdiv',
		'author'          => '#authordiv',
		'format'          => '#formatdiv',
		'page_attributes' => '#pageparentdiv',
		'featured_image'  => '#postimagediv',
		'revisions'       => '#revisionsdiv',
		'categories'      => '#categorydiv',
		'tags'            => '#tagsdiv-post_tag',
		'send-trackbacks' => '#trackbacksdiv',
	);

	// Loop over field group settings and generate list of selectors to hide.
	if ( is_array( $field_group['hide_on_screen'] ) ) {
		$hide = array();
		foreach ( $field_group['hide_on_screen'] as $k ) {
			if ( isset( $elements[ $k ] ) ) {
				$id     = $elements[ $k ];
				$hide[] = $id;
				$hide[] = '#screen-meta label[for=' . substr( $id, 1 ) . '-hide]';
			}
		}
		$style = implode( ', ', $hide ) . ' {display: none;}';
	}

	/**
	 * Filters the generated CSS styles.
	 *
	 * @date    12/02/2014
	 * @since   5.0.0
	 *
	 * @param   string $style The CSS styles.
	 * @param   array $field_group The field group array.
	 */
	return apply_filters( 'acf/get_field_group_style', $style, $field_group );
}

/**
 * acf_get_field_group_edit_link
 *
 * Checks if the current user can edit the field group and returns the edit url.
 *
 * @date    23/9/18
 * @since   5.7.7
 *
 * @param   int $post_id The field group ID.
 * @return  string
 */
function acf_get_field_group_edit_link( $post_id ) {
	if ( $post_id && acf_current_user_can_admin() ) {
		return admin_url( 'post.php?post=' . $post_id . '&action=edit' );
	}
	return '';
}

/**
 * acf_prepare_field_group_for_export
 *
 * Returns a modified field group ready for export.
 *
 * @date    11/03/2014
 * @since   5.0.0
 *
 * @param   array $field_group The field group array.
 * @return  array
 */
function acf_prepare_field_group_for_export( $field_group = array() ) {

	// Remove args.
	acf_extract_vars( $field_group, array( 'ID', 'local', '_valid' ) );

	// Prepare fields.
	$field_group['fields'] = acf_prepare_fields_for_export( $field_group['fields'] );

	/**
	 * Filters the $field_group array before being returned to the export tool.
	 *
	 * @date    12/02/2014
	 * @since   5.0.0
	 *
	 * @param   array $field_group The $field group array.
	 */
	return apply_filters( 'acf/prepare_field_group_for_export', $field_group );
}

/**
 * acf_prepare_field_group_for_import
 *
 * Prepares a field group for the import process.
 *
 * @date    21/11/19
 * @since   5.8.8
 *
 * @param   array $field_group The field group array.
 * @return  array
 */
function acf_prepare_field_group_for_import( $field_group ) {

	// Update parent and menu_order properties for all fields.
	if ( $field_group['fields'] ) {
		foreach ( $field_group['fields'] as $i => &$field ) {
			$field['parent']     = $field_group['key'];
			$field['menu_order'] = $i;
		}
	}

	/**
	 * Filters the $field_group array before being returned to the import tool.
	 *
	 * @date    21/11/19
	 * @since   5.8.8
	 *
	 * @param   array $field_group The field group array.
	 */
	return apply_filters( 'acf/prepare_field_group_for_import', $field_group );
}

/**
 * acf_import_field_group
 *
 * Imports a field group into the databse.
 *
 * @date    11/03/2014
 * @since   5.0.0
 *
 * @param   array $field_group The field group array.
 * @return  array The new field group.
 */
function acf_import_field_group( $field_group ) {

	// Disable filters to ensure data is not modified by local, clone, etc.
	$filters = acf_disable_filters();

	// Validate field group (ensures all settings exist).
	$field_group = acf_get_valid_field_group( $field_group );

	// Prepare group for import (modifies settings).
	$field_group = acf_prepare_field_group_for_import( $field_group );

	// Prepare fields for import (modifies settings).
	$fields = acf_prepare_fields_for_import( $field_group['fields'] );

	// Stores a map of field "key" => "ID".
	$ids = array();

	// If the field group has an ID, review and delete stale fields in the database.
	if ( $field_group['ID'] ) {

		// Load database fields.
		$db_fields = acf_prepare_fields_for_import( acf_get_fields( $field_group ) );

		// Generate map of "index" => "key" data.
		$keys = wp_list_pluck( $fields, 'key' );

		// Loop over db fields and delete those who don't exist in $new_fields.
		foreach ( $db_fields as $field ) {

			// Add field data to $ids map.
			$ids[ $field['key'] ] = $field['ID'];

			// Delete field if not in $keys.
			if ( ! in_array( $field['key'], $keys ) ) {
				acf_delete_field( $field['ID'] );
			}
		}
	}

	// When importing a new field group, insert a temporary post and set the field group's ID.
	// This allows fields to be updated before the field group (field group ID is needed for field parent setting).
	if ( ! $field_group['ID'] ) {
		$field_group['ID'] = wp_insert_post( array( 'post_title' => $field_group['key'] ) );
	}

	// Add field group data to $ids map.
	$ids[ $field_group['key'] ] = $field_group['ID'];

	// Loop over and add fields.
	if ( $fields ) {
		foreach ( $fields as $field ) {

			// Replace any "key" references with "ID".
			if ( isset( $ids[ $field['key'] ] ) ) {
				$field['ID'] = $ids[ $field['key'] ];
			}
			if ( isset( $ids[ $field['parent'] ] ) ) {
				$field['parent'] = $ids[ $field['parent'] ];
			}

			// Save field.
			$field = acf_update_field( $field );

			// Add field data to $ids map for children.
			$ids[ $field['key'] ] = $field['ID'];
		}
	}

	// Save field group.
	$field_group = acf_update_field_group( $field_group );

	// Enable filters again.
	acf_enable_filters( $filters );

	/**
	 * Fires immediately after a field_group has been imported.
	 *
	 * @date    12/02/2014
	 * @since   5.0.0
	 *
	 * @param   array $field_group The field_group array.
	 */
	do_action( 'acf/import_field_group', $field_group );

	// return new field group.
	return $field_group;
}