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

Commit

Permalink
Merge branch 'master' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonbahl committed Apr 23, 2021
2 parents 7ceb546 + 49bf955 commit a0ec93c
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 76 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
148 changes: 89 additions & 59 deletions src/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,59 @@ $j = jQuery.noConflict();

$j(document).ready(function () {

var GraphqlLocationManager = new acf.Model({
id: 'graphqlLocationManager',
wait: 'ready',
events: {
'click .add-location-rule': 'onClickAddRule',
'click .add-location-group': 'onClickAddGroup',
'click .remove-location-rule': 'onClickRemoveRule',
'change .refresh-location-rule': 'onChangeRemoveRule',
'change .rule-groups .operator select': 'onChangeRemoveRule',
'change .rule-groups .value select': 'onChangeRemoveRule',
},
requestPending: false,
initialize: function () {
this.$el = $j('#acf-field-group-locations');
this.getGraphqlTypes();
},

onClickAddRule: function (e, $el) {
this.getGraphqlTypes();
},

onClickRemoveRule: function (e, $el) {
this.getGraphqlTypes();
},

onChangeRemoveRule: function (e, $el) {
setTimeout(function () {
GraphqlLocationManager.getGraphqlTypes();
}, 500);

},

onClickAddGroup: function (e, $el) {
this.getGraphqlTypes();
},

isRequestPending: function() {
return this.requestPending;
},

startRequest: function () {
this.requestPending = true;
},

finishRequest: function() {
this.requestPending = false;
},

getGraphqlTypes: function () {
getGraphqlTypesFromLocationRules();
},
});

/**
* Set the visibility of the GraphQL Fields based on the `show_in_graphql`
* field state.
Expand All @@ -23,6 +76,7 @@ $j(document).ready(function () {

showInGraphQLCheckbox.on('change', function () {
setGraphqlFieldVisibility();
getGraphqlTypesFromLocationRules();
})

}
Expand Down Expand Up @@ -183,8 +237,10 @@ $j(document).ready(function () {

function getGraphqlTypesFromLocationRules() {

var form = $j('#post :input');
var serialized = form.serialize();
var showInGraphQLCheckbox = $j('#acf_field_group-show_in_graphql');
var form = $j('#post');
var formInputs = $j('#post :input');
var serialized = formInputs.serialize();
var checkboxes = $j('.acf-field[data-name="graphql_types"] .acf-checkbox-list input[type="checkbox"]');
var manualMapTypes = $j('#acf_field_group-map_graphql_types_from_location_rules');

Expand All @@ -194,25 +250,39 @@ $j(document).ready(function () {
return;
}

$j.post(ajaxurl, {
action: 'get_acf_field_group_graphql_types',
data: serialized
}, function (res) {
var types = res && res['graphql_types'] ? res['graphql_types'] : [];

checkboxes.each(function (i, el) {
var checkbox = $j(this);
var value = $j(this).val();
checkbox.prop('checked', false);
if (types && types.length) {
if (-1 !== $j.inArray(value, types)) {
checkbox.prop('checked', true);
checkbox.trigger("change");
if ( ! showInGraphQLCheckbox.is(':checked') ) {
return;
}

if ( 'pending' !== form.attr('data-request-pending') ) {

// Start the request
form.attr('data-request-pending', 'pending' );

// Make the request
$j.post(ajaxurl, {
action: 'get_acf_field_group_graphql_types',
data: serialized
}, function (res) {
var types = res && res['graphql_types'] ? res['graphql_types'] : [];

checkboxes.each(function (i, el) {
var checkbox = $j(this);
var value = $j(this).val();
checkbox.prop('checked', false);
if (types && types.length) {
if (-1 !== $j.inArray(value, types)) {
checkbox.prop('checked', true);
}
}
}
})
checkbox.trigger("change");
})

});
// Signal that the request is finished
form.removeAttr('data-request-pending');

});
}

};

Expand All @@ -223,44 +293,4 @@ $j(document).ready(function () {
setGraphqlFieldName();
graphqlMapTypesFromLocations();

var GraphqlLocationManager = new acf.Model({
id: 'graphqlLocationManager',
wait: 'ready',
events: {
'click .add-location-rule': 'onClickAddRule',
'click .add-location-group': 'onClickAddGroup',
'click .remove-location-rule': 'onClickRemoveRule',
'change .refresh-location-rule': 'onChangeRemoveRule',
'change .rule-groups .operator select': 'onChangeRemoveRule',
'change .rule-groups .value select': 'onChangeRemoveRule',
},
initialize: function () {
this.$el = $j('#acf-field-group-locations');
this.getGraphqlTypes();
},

onClickAddRule: function (e, $el) {
this.getGraphqlTypes();
},

onClickRemoveRule: function (e, $el) {
this.getGraphqlTypes();
},

onChangeRemoveRule: function (e, $el) {
setTimeout(function () {
GraphqlLocationManager.getGraphqlTypes();
}, 500);

},

onClickAddGroup: function (e, $el) {
this.getGraphqlTypes();
},

getGraphqlTypes: function () {
getGraphqlTypesFromLocationRules();
},
})

});
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 a0ec93c

Please sign in to comment.