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

Fix #135. Add Explicit options for adding ACF Field Groups to the Schema #229

Merged
merged 24 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6cb6193
Fix #135. Add Explicit options for adding ACF Field Groups to the Schema
rsm0128 Jan 19, 2021
46696e3
Fixed formatting issues. Reverted phpcs auto formatting error and fol…
rsm0128 Jan 19, 2021
fc010f7
fixed spelling issue. Thanks @drewbaker
rsm0128 Jan 23, 2021
3c9349b
updated unit test with new graphql_types_on field
rsm0128 Mar 15, 2021
1271ed9
Aded Explicit Options WpUnit test case
rsm0128 Mar 15, 2021
36b03c0
updated plugin version check
rsm0128 Mar 15, 2021
629dfac
fixed typo issue with user register fields
rsm0128 Mar 20, 2021
078cc98
Added contentNode and termNode support for the explicit option
rsm0128 Mar 20, 2021
7f1ebdd
- add plaftform_check to .gitignore
jasonbahl Mar 20, 2021
f51d294
- Update GraphQL Settings fields to use the GraphQL Type names as val…
jasonbahl Mar 31, 2021
0d2e191
- Add the `ContentTemplate` interface to the Checkboxes
jasonbahl Mar 31, 2021
c6e3888
- Fix typo in JS for unsetting checked state
jasonbahl Mar 31, 2021
8f56a30
- add filter to `ContentTemplate` fields to pass the source node alon…
jasonbahl Apr 5, 2021
6ef9cc1
- removes activation script that tries to upgrade location rules
jasonbahl Apr 6, 2021
b202db1
- Update test to include nav menu registration
jasonbahl Apr 6, 2021
d7cb493
- Update test message linking to WPGraphQL Issue
jasonbahl Apr 6, 2021
21a0b8f
- Update ACF Settings tp include an AJAX action/callback for determin…
jasonbahl Apr 9, 2021
63afa2f
Merge commit '3bf411f270c591a89dce3b41acb655046139a10e' into bugfix-135
jasonbahl Apr 9, 2021
4ed2017
- Move GraphQL Field Group Settings into their own box in the ACF Fie…
jasonbahl Apr 12, 2021
faa5701
- remove dummy code from wp-graphql-acf
jasonbahl Apr 14, 2021
8bde1b2
- update testing matrix
jasonbahl Apr 14, 2021
e35e895
- update testing matrix
jasonbahl Apr 14, 2021
d0ca31f
- Update tests to test with multiple versions of WPGraphQL
jasonbahl Apr 14, 2021
bf4c781
- Update .gitignore to not ignore the platform check
jasonbahl Apr 20, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
- Move GraphQL Field Group Settings into their own box in the ACF Fie…
…ld Group page

- Add Ajax callback for determining rules
- Update Location Rules to check for rule conflicts, and support many edge cases, such as assigning a Field Group to a post_status
- Update JS helpers for the GraphQL Field Group settings. Add conditional logic to the field that allows users to manually chose GraphQL fields or map based on location rules
- Reset location rules when manual graphql type mapping is unchecked.
  • Loading branch information
jasonbahl committed Apr 12, 2021
commit 4ed201784cdd8e098e710fd4d189d01a81c88a69
125 changes: 49 additions & 76 deletions src/class-acfsettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ class ACF_Settings {
*/
public function init() {

/**
* Creates a field group setting to allow a field group to be
* shown in the GraphQL Schema.
*/
// add_action( 'acf/render_field_group_settings', [ $this, 'add_field_group_settings' ], 10, 1 );

/**
* Add settings to individual fields to allow each field granular control
* over how it's shown in the GraphQL Schema
Expand All @@ -42,15 +36,20 @@ public function init() {
add_action( 'add_meta_boxes', [ $this, 'register_meta_boxes' ] );

/**
* Register an AJAX callback
* Register an AJAX action and callback for converting ACF Location rules to GraphQL Types
*/
add_action( 'wp_ajax_get_acf_field_group_graphql_types', [ $this, 'ajax_callback' ] );

}

/**
* Handle the AJAX callback for converting ACF Location settings to GraphQL Types
*
* @return void
*/
public function ajax_callback() {

if ( isset( $_POST['data' ] ) ) {
if ( isset( $_POST['data'] ) ) {

$form_data = [];

Expand All @@ -61,19 +60,40 @@ public function ajax_callback() {
}

$field_group = isset( $form_data['acf_field_group'] ) ? $form_data['acf_field_group'] : [];
$rules = new LocationRules( [ $field_group ] );
$rules = new LocationRules( [ $field_group ] );
$rules->determine_location_rules();
wp_send_json( $rules->get_rules() );

$group_name = isset( $field_group['graphql_field_name'] ) ? $field_group['graphql_field_name'] : $field_group['title'];
$group_name = $rules->format_field_name( $group_name );

$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' => null ] );
}

echo __( 'No location rules were found', 'wp-graphql-acf' );
wp_die();
}

/**
* Register the GraphQL Settings metabox for the ACF Field Group post type
*
* @return void
*/
public function register_meta_boxes() {
add_meta_box( 'wpgraphql-acf-meta-box', __( 'GraphQL', 'wp-graphql-acf' ), [ $this, 'display_metabox' ], [ 'acf-field-group' ] );
add_meta_box( 'wpgraphql-acf-meta-box', __( 'GraphQL', 'wp-graphql-acf' ), [
$this,
'display_metabox'
], [ 'acf-field-group' ] );
}

/**
* Display the GraphQL Settings Metabox on the Field Group admin page
*
* @param $field_group_post_object
*/
public function display_metabox( $field_group_post_object ) {

global $field_group;
Expand Down Expand Up @@ -111,12 +131,12 @@ public function display_metabox( $field_group_post_object ) {

acf_render_field_wrap(
[
'label' => __( 'Use ACF Location Rules to map to the GraphQL Schema', 'acf' ),
'instructions' => __( 'By default, the ACF Field group will be added to the GraphQL Schema based on the field group\'s location rules. Turning this off will allow you to manually select which GraphQL Types the Field Group should show on in the GraphQL Schema.', 'wp-graphql-acf' ),
'label' => __( 'Manually Set GraphQL Types for Field Group', 'acf' ),
'instructions' => __( 'By default, ACF Field groups are added to the GraphQL Schema based on the field group\'s location rules. Checking this box will let you manually control the GraphQL Types the field group should be shown on in the GraphQL Schema using the checkboxes below, and the Location Rules will no longer effect the GraphQL Types.', 'wp-graphql-acf' ),
'type' => 'true_false',
'name' => 'manual_type_selection',
'name' => 'map_graphql_types_from_location_rules',
'prefix' => 'acf_field_group',
'value' => isset( $field_group['manual_type_selection'] ) ? (bool) $field_group['manual_type_selection'] : true,
'value' => isset( $field_group['map_graphql_types_from_location_rules'] ) ? (bool) $field_group['map_graphql_types_from_location_rules'] : false,
'ui' => 1,
]
);
Expand All @@ -129,18 +149,19 @@ public function display_metabox( $field_group_post_object ) {
'type' => 'checkbox',
'prefix' => 'acf_field_group',
'name' => 'graphql_types',
'value' => ! empty( $field_group['graphql_types'] ) ? $field_group['graphql_types'] : null,
'value' => ! empty( $field_group['graphql_types'] ) ? $field_group['graphql_types'] : [],
'toggle' => true,
'choices' => $choices,
]
);

?>
<div class="acf-hidden">
<input type="hidden" name="acf_field_group[key]" value="<?php echo $field_group['key']; ?>" />
<input type="hidden" name="acf_field_group[key]"
value="<?php echo $field_group['key']; ?>"/>
</div>
<script type="text/javascript">
if( typeof acf !== 'undefined' ) {
if (typeof acf !== 'undefined') {
acf.newPostbox({
'id': 'wpgraphql-acf-meta-box',
'label': 'left'
Expand Down Expand Up @@ -187,70 +208,22 @@ public function add_field_settings( array $field ) {
}

/**
* This adds a setting to the ACF Field groups to activate a field group in GraphQL.
* This enqueues admin script.
*
* If a field group is set to active and is set to "show_in_graphql", the fields in the field
* group will be exposed to the GraphQL Schema based on the matching location rules.
* @param string $screen The screen that scripts are being enqueued to
*
* @param array $field_group The field group to add settings to.
*/
public function add_field_group_settings( array $field_group ) {

/**
* Render a field in the Field Group settings to allow for a Field Group to be shown in GraphQL.
*/
acf_render_field_wrap(
[
'label' => __( 'Show in GraphQL', 'acf' ),
'instructions' => __( 'If the field group is active, and this is set to show, the fields in this group will be available in the WPGraphQL Schema based on the respective Location rules.' ),
'type' => 'true_false',
'name' => 'show_in_graphql',
'prefix' => 'acf_field_group',
'value' => isset( $field_group['show_in_graphql'] ) ? (bool) $field_group['show_in_graphql'] : false,
'ui' => 1,
]
);

/**
* Render a field in the Field Group settings to allow for a Field Group to be shown in GraphQL.
*/
acf_render_field_wrap(
[
'label' => __( 'GraphQL Field Name', 'acf' ),
'instructions' => __( 'The name of the field group in the GraphQL Schema.', 'wp-graphql-acf' ),
'type' => 'text',
'prefix' => 'acf_field_group',
'name' => 'graphql_field_name',
'required' => isset( $field_group['show_in_graphql'] ) ? (bool) $field_group['show_in_graphql'] : false,
'placeholder' => ! empty( $field_group['graphql_field_name'] ) ? $field_group['graphql_field_name'] : null,
'value' => ! empty( $field_group['graphql_field_name'] ) ? $field_group['graphql_field_name'] : null,
]
);

$choices = Config::get_all_graphql_types();
acf_render_field_wrap(
[
'label' => __( 'GraphQL Types to Show the Field Group On', 'wp-graphql-acf' ),
'instructions' => __( 'Select the Types in the WPGraphQl Schema to show the fields in this field group on', 'wp-graphql-acf' ),
'type' => 'checkbox',
'prefix' => 'acf_field_group',
'name' => 'graphql_types',
'value' => ! empty( $field_group['graphql_types'] ) ? $field_group['graphql_types'] : null,
'toggle' => true,
'choices' => $choices,
]
);
}

/**
* This enqueues admin script.
* @return void
*/
public function enqueue_graphql_acf_scripts( $hook ) {
public function enqueue_graphql_acf_scripts( string $screen ) {
global $post;

if ( $hook == 'post-new.php' || $hook == 'post.php' ) {
if ( $screen == 'post-new.php' || $screen == 'post.php' ) {
if ( 'acf-field-group' === $post->post_type ) {
wp_enqueue_script( 'graphql-acf', plugins_url( 'src/js/main.js', dirname( __FILE__ ) ), array( 'jquery', 'acf-input', 'acf-field-group' ) );
wp_enqueue_script( 'graphql-acf', plugins_url( 'src/js/main.js', dirname( __FILE__ ) ), array(
'jquery',
'acf-input',
'acf-field-group'
) );
}
}
}
Expand Down
Loading