From ede55a322edd16ee22f07a7842a7a484ff57a949 Mon Sep 17 00:00:00 2001 From: Andrew Minion Date: Fri, 30 Aug 2019 11:07:46 -0500 Subject: [PATCH 1/4] =?UTF-8?q?don=E2=80=99t=20require=20graphql=20name=20?= =?UTF-8?q?if=20the=20form=20isn=E2=80=99t=20active=20for=20graphql?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/class-acfsettings.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/class-acfsettings.php b/src/class-acfsettings.php index ae19d78..9827702 100644 --- a/src/class-acfsettings.php +++ b/src/class-acfsettings.php @@ -103,7 +103,7 @@ public function add_field_group_settings( $field_group ) { 'type' => 'text', 'prefix' => 'acf_field_group', 'name' => 'graphql_field_name', - 'required' => true, + '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, ] From 32d770edc5bb97193cf9ecfd47b4e1a0b2b029d6 Mon Sep 17 00:00:00 2001 From: Andrew Minion Date: Fri, 30 Aug 2019 11:40:55 -0500 Subject: [PATCH 2/4] =?UTF-8?q?don=E2=80=99t=20require=20name=20if=20field?= =?UTF-8?q?=20group=20isn=E2=80=99t=20used?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/js/wp-graphql-acf.js | 34 ++++++++++++++++++++++++++++++++++ src/class-acfsettings.php | 16 ++++++++++++++++ wp-graphql-acf.php | 1 + 3 files changed, 51 insertions(+) create mode 100644 assets/js/wp-graphql-acf.js diff --git a/assets/js/wp-graphql-acf.js b/assets/js/wp-graphql-acf.js new file mode 100644 index 0000000..983609b --- /dev/null +++ b/assets/js/wp-graphql-acf.js @@ -0,0 +1,34 @@ +/* global jquery */ +'use strict'; + +(function ($) { + $(document).ready(function () { + + /** + * Toggle “GraphQL Field Name” rqeuirement based on “Show in GraphQL” toggle. + */ + $('#acf_field_group-show_in_graphql').on('change', function () { + var graphqlFieldNameWrap = $('.acf-field[data-name="graphql_field_name"]'), + graphqlLabel = graphqlFieldNameWrap.find('label'), + graphqlInput = $('#acf_field_group-graphql_field_name'); + + if ($(this).is(':checked')) { + + // Add span.acf-required if necessary. + if (graphqlFieldNameWrap.find('.acf-required').length === 0) { + graphqlLabel.append('*'); + } + + // Toggle required attributes and visual features. + graphqlFieldNameWrap.addClass('is-required'); + graphqlLabel.find('.acf-required').show(); + graphqlInput.attr('required', true); + } else { + graphqlFieldNameWrap.removeClass('is-required'); + graphqlLabel.find('.acf-required').hide(); + graphqlInput.attr('required', false); + } + + }); + }); +}(jQuery)); diff --git a/src/class-acfsettings.php b/src/class-acfsettings.php index 9827702..3de158c 100644 --- a/src/class-acfsettings.php +++ b/src/class-acfsettings.php @@ -31,6 +31,17 @@ public function init() { */ add_action( 'acf/render_field_settings', [ $this, 'add_field_settings' ], 10, 1 ); + /** + * Register admin scripts. + */ + add_action('admin_enqueue_scripts', [$this, 'register_assets']); + } + + /** + * Register admin JS. + */ + public function register_assets() { + wp_register_script( 'wp-graphql-acf', plugin_dir_url( WPGRAPHQL_ACF_FILE ) . 'assets/js/wp-graphql-acf.js', array( 'jquery' ) ); } /** @@ -49,6 +60,11 @@ public function add_field_settings( $field ) { return; } + /** + * Enqueue the admin JS. + */ + wp_enqueue_script( 'wp-graphql-acf' ); + /** * Render the "show_in_graphql" setting for the field. */ diff --git a/wp-graphql-acf.php b/wp-graphql-acf.php index 083dda7..52a370e 100644 --- a/wp-graphql-acf.php +++ b/wp-graphql-acf.php @@ -25,6 +25,7 @@ * Define constants */ const WPGRAPHQL_REQUIRED_MIN_VERSION = '0.3.2'; +const WPGRAPHQL_ACF_FILE = __FILE__; /** * Initialize the plugin From ed0f75aa385d5f1b3b9f600975cd0109f6fbfa83 Mon Sep 17 00:00:00 2001 From: Andrew Minion Date: Fri, 30 Aug 2019 11:42:51 -0500 Subject: [PATCH 3/4] fix formatting --- src/class-acfsettings.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/class-acfsettings.php b/src/class-acfsettings.php index 3de158c..492dfa7 100644 --- a/src/class-acfsettings.php +++ b/src/class-acfsettings.php @@ -34,7 +34,7 @@ public function init() { /** * Register admin scripts. */ - add_action('admin_enqueue_scripts', [$this, 'register_assets']); + add_action( 'admin_enqueue_scripts', [ $this, 'register_assets' ] ); } /** From b59be2bbf9b32fc14ed02a45261949ea4b12d13d Mon Sep 17 00:00:00 2001 From: Andrew Minion Date: Fri, 30 Aug 2019 11:53:45 -0500 Subject: [PATCH 4/4] optimize JS loading --- src/class-acfsettings.php | 2 +- wp-graphql-acf.php | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/class-acfsettings.php b/src/class-acfsettings.php index 492dfa7..a7257bb 100644 --- a/src/class-acfsettings.php +++ b/src/class-acfsettings.php @@ -41,7 +41,7 @@ public function init() { * Register admin JS. */ public function register_assets() { - wp_register_script( 'wp-graphql-acf', plugin_dir_url( WPGRAPHQL_ACF_FILE ) . 'assets/js/wp-graphql-acf.js', array( 'jquery' ) ); + wp_register_script( 'wp-graphql-acf', plugin_dir_url( WPGRAPHQL_ACF_FILE ) . 'assets/js/wp-graphql-acf.js', array( 'jquery' ), WPGRAPHQL_ACF_VERSION, true ); } /** diff --git a/wp-graphql-acf.php b/wp-graphql-acf.php index 52a370e..80f1a8d 100644 --- a/wp-graphql-acf.php +++ b/wp-graphql-acf.php @@ -25,6 +25,8 @@ * Define constants */ const WPGRAPHQL_REQUIRED_MIN_VERSION = '0.3.2'; + +const WPGRAPHQL_ACF_VERSION = '0.2.1'; // Used for JS cache-busting. const WPGRAPHQL_ACF_FILE = __FILE__; /**