Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
- Update AJAX callback to get the rules as just values not associativ…
Browse files Browse the repository at this point in the history
…e array

- Update get_rules() method to early return quicker
- Update main.js to trigger the change after check/uncheck has occurred, not only when checking has occurred
  • Loading branch information
jasonbahl committed Apr 22, 2021
1 parent 1bad7d6 commit 8478447
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/class-acfsettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function ajax_callback() {

$all_rules = $rules->get_rules();
if ( isset( $all_rules[ $group_name ] ) ) {
wp_send_json( [ 'graphql_types' => $all_rules[ $group_name ] ] );
wp_send_json( [ 'graphql_types' => array_values( $all_rules[ $group_name ] ) ] );
}
wp_send_json( [ 'graphql_types' => null ] );
}
Expand Down
2 changes: 1 addition & 1 deletion src/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,9 @@ $j(document).ready(function () {
if (types && types.length) {
if (-1 !== $j.inArray(value, types)) {
checkbox.prop('checked', true);
checkbox.trigger("change");
}
}
checkbox.trigger("change");
})

// Signal that the request is finished
Expand Down
32 changes: 20 additions & 12 deletions src/location-rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,33 +116,41 @@ public function unset_graphql_type( string $field_group_name, string $graphql_ty
*/
public function get_rules() {

$mapped_field_groups = isset( $this->mapped_field_groups ) && ! empty( $this->mapped_field_groups ) ? $this->mapped_field_groups : [];

if ( empty( $mapped_field_groups ) ) {
if ( empty( $this->mapped_field_groups ) ) {
return [];
}


if ( empty( $this->unset_types ) ) {
return $mapped_field_groups;
return $this->mapped_field_groups;
}

/**
* Remove any Types that were flagged to unset
*/
foreach ( $this->unset_types as $field_group => $types ) {
if ( ! empty( $types ) ) {
foreach ( $types as $type ) {
if ( isset( $this->mapped_field_groups[ $field_group ] ) ) {
if ( ( $key = array_search( $type, $mapped_field_groups[ $field_group ] ) ) !== false ) {
unset( $mapped_field_groups[ $field_group ][ $key ] );
}
}

// If there are no mapped field groups for the rule being unset, return the mapped groups as is
if ( ! isset( $this->mapped_field_groups[ $field_group ] ) ) {
return $this->mapped_field_groups;
}

// If the types to unset are empty or not an array, return the mapped field groups as is
if ( empty( $types ) || ! is_array( $types ) ) {
return $this->mapped_field_groups;
}

// Loop over the types to unset, find the key of the type in the array, then unset it
foreach ( $types as $type ) {
if ( ( $key = array_search( $type, $this->mapped_field_groups[ $field_group ] ) ) !== false ) {
unset( $this->mapped_field_groups[ $field_group ][ $key ] );
}
}

}

return $mapped_field_groups;
// Return the mapped field groups, with the unset fields (if any) removed
return $this->mapped_field_groups;

}

Expand Down
4 changes: 2 additions & 2 deletions vendor/composer/InstalledVersions.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class InstalledVersions
'aliases' =>
array (
),
'reference' => 'd0ca31f6391c80cd370d4da71aa185658ee3c5b9',
'reference' => '1bad7d6d5214448f8d27bbcd908b24d6552667cb',
'name' => 'wp-graphql/wp-graphql-acf',
),
'versions' =>
Expand All @@ -42,7 +42,7 @@ class InstalledVersions
'aliases' =>
array (
),
'reference' => 'd0ca31f6391c80cd370d4da71aa185658ee3c5b9',
'reference' => '1bad7d6d5214448f8d27bbcd908b24d6552667cb',
),
),
);
Expand Down
4 changes: 2 additions & 2 deletions vendor/composer/installed.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'aliases' =>
array (
),
'reference' => 'd0ca31f6391c80cd370d4da71aa185658ee3c5b9',
'reference' => '1bad7d6d5214448f8d27bbcd908b24d6552667cb',
'name' => 'wp-graphql/wp-graphql-acf',
),
'versions' =>
Expand All @@ -18,7 +18,7 @@
'aliases' =>
array (
),
'reference' => 'd0ca31f6391c80cd370d4da71aa185658ee3c5b9',
'reference' => '1bad7d6d5214448f8d27bbcd908b24d6552667cb',
),
),
);

0 comments on commit 8478447

Please sign in to comment.