Skip to content

Commit

Permalink
Merge pull request #89 from hoppinger/bugfix/prevent_duplicates_on_im…
Browse files Browse the repository at this point in the history
…port

Prevent duplicate field groups on import
  • Loading branch information
sebastiaandegeus authored Nov 30, 2017
2 parents 2731837 + 9715762 commit 7e448b4
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/acfwpcli/field_group.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public static function import( $file ) {
$fields = acf_extract_var( $field_group, 'fields' );
$fields = acf_prepare_fields_for_import( $fields );

$db_field_group = self::find_by_key($field_group['key']);

if ($db_field_group) {
$field_group['ID'] = $db_field_group->ID;
}

$field_group = acf_update_field_group( $field_group );

foreach ( $fields as $field ) {
Expand Down Expand Up @@ -44,6 +50,15 @@ public static function find_by_name( $name ) {
return $results;
}

public static function find_by_key( $key ) {
global $wpdb;
$query = "SELECT * FROM {$wpdb->posts} WHERE post_type='acf-field-group' AND post_name = %s";

$results = $wpdb->get_row( $wpdb->prepare( $query, $key ) );

return $results;
}

public static function destroy( $id ) {
global $wpdb;

Expand Down Expand Up @@ -84,8 +99,7 @@ public static function to_array( $id ) {
// prepare fields
$fields = acf_prepare_fields_for_export( $fields );

// extract field group ID
acf_extract_var( $field_group, 'ID' );
$field_group['ID'] = false;

// add to field group
$field_group['fields'] = $fields;
Expand Down

0 comments on commit 7e448b4

Please sign in to comment.