Skip to content

Commit

Permalink
Clean up groups in PodsMeta::groups_get() by removing duplicate field…
Browse files Browse the repository at this point in the history
…s only when using custom groups

Fixes #6317
  • Loading branch information
sc0ttkclark committed Oct 15, 2023
1 parent 86ba694 commit 19970f2
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions classes/PodsMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,10 @@ public function groups_get( $type, $name, $default_fields = null, $full_objects
}

if ( $has_custom_groups ) {
// Clean up the dynamic groups to prevent duplicate fields showing.
$this->groups_cleanup( $groups, self::$groups[ $type ][ $name ] );

// Now add our custom groups to those dynamic groups.
$groups = array_merge( $groups, self::$groups[ $type ][ $name ] );
}
} elseif ( $has_custom_groups ) {
Expand Down Expand Up @@ -1060,6 +1064,50 @@ public function groups_get( $type, $name, $default_fields = null, $full_objects
return $groups;
}

/**
* Clean up the groups to prevent duplicate fields from showing based on reference groups.
*
* @since 3.0.7
*
* @param array $groups The groups to clean up.
* @param array $reference_groups The groups to reference for fields that take precedence.
*/
public function groups_cleanup( array &$groups, array &$reference_groups ) {
$found_fields = [];

// Remove duplicates fields from reference groups.
foreach ( $reference_groups as $group_key => $reference_group ) {
$group_fields = wp_list_pluck( $reference_group['fields'], 'name' );

foreach ( $group_fields as $field_key => $field_name ) {
// Remove duplicate fields.
if ( isset( $found_fields[ $field_name ] ) ) {
unset( $reference_groups[ $group_key ]['fields'][ $field_key ] );

continue;
}

$found_fields[ $field_name ] = true;
}
}

// Remove duplicates fields from groups.
foreach ( $groups as $group_key => $group ) {
$group_fields = wp_list_pluck( $group['fields'], 'name' );

foreach ( $group_fields as $field_key => $field_name ) {
// Remove duplicate fields.
if ( isset( $found_fields[ $field_name ] ) ) {
unset( $groups[ $group_key ]['fields'][ $field_key ] );

continue;
}

$found_fields[ $field_name ] = true;
}
}
}

/**
* @param $post_type
* @param null $post
Expand Down

0 comments on commit 19970f2

Please sign in to comment.