Skip to content

Commit

Permalink
Fix wp-graphql#135. Add Explicit options for adding ACF Field Groups …
Browse files Browse the repository at this point in the history
…to the Schema
  • Loading branch information
rsm0128 committed Mar 13, 2021
1 parent b4581db commit 6cb6193
Show file tree
Hide file tree
Showing 4 changed files with 818 additions and 927 deletions.
35 changes: 12 additions & 23 deletions src/class-acf.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* ACF
*
Expand Down Expand Up @@ -27,7 +28,7 @@ final class ACF {
*/
public static function instance() {

if ( ! isset( self::$instance ) && ! ( self::$instance instanceof ACF ) ) {
if (!isset(self::$instance) && !(self::$instance instanceof ACF)) {
self::$instance = new ACF();
self::$instance->setup_constants();
self::$instance->includes();
Expand All @@ -41,7 +42,7 @@ public static function instance() {
*
* @param ACF $instance The instance of the WPGraphQL\ACF class
*/
do_action( 'graphql_acf_init', self::$instance );
do_action('graphql_acf_init', self::$instance);

/**
* Return the WPGraphQL Instance
Expand All @@ -60,8 +61,7 @@ public static function instance() {
public function __clone() {

// Cloning instances of the class is forbidden.
_doing_it_wrong( __FUNCTION__, esc_html__( 'The \WPGraphQL\ACF class should not be cloned.', 'wp-graphql-acf' ), '0.0.1' );

_doing_it_wrong(__FUNCTION__, esc_html__('The \WPGraphQL\ACF class should not be cloned.', 'wp-graphql-acf'), '0.0.1');
}

/**
Expand All @@ -73,8 +73,7 @@ public function __clone() {
public function __wakeup() {

// De-serializing instances of the class is forbidden.
_doing_it_wrong( __FUNCTION__, esc_html__( 'De-serializing instances of the \WPGraphQL\ACF class is not allowed', 'wp-graphql-acf' ), '0.0.1' );

_doing_it_wrong(__FUNCTION__, esc_html__('De-serializing instances of the \WPGraphQL\ACF class is not allowed', 'wp-graphql-acf'), '0.0.1');
}

/**
Expand All @@ -85,26 +84,20 @@ public function __wakeup() {
*/
private function setup_constants() {

// Plugin version.
if ( ! defined( 'WPGRAPHQL_ACF_VERSION' ) ) {
define( 'WPGRAPHQL_ACF_VERSION', '0.3.0' );
}

// Plugin Folder Path.
if ( ! defined( 'WPGRAPHQL_ACF_PLUGIN_DIR' ) ) {
define( 'WPGRAPHQL_ACF_PLUGIN_DIR', plugin_dir_path( __FILE__ . '/..' ) );
if (!defined('WPGRAPHQL_ACF_PLUGIN_DIR')) {
define('WPGRAPHQL_ACF_PLUGIN_DIR', plugin_dir_path(__FILE__ . '/..'));
}

// Plugin Folder URL.
if ( ! defined( 'WPGRAPHQL_ACF_PLUGIN_URL' ) ) {
define( 'WPGRAPHQL_ACF_PLUGIN_URL', plugin_dir_url( __FILE__ . '/..' ) );
if (!defined('WPGRAPHQL_ACF_PLUGIN_URL')) {
define('WPGRAPHQL_ACF_PLUGIN_URL', plugin_dir_url(__FILE__ . '/..'));
}

// Plugin Root File.
if ( ! defined( 'WPGRAPHQL_ACF_PLUGIN_FILE' ) ) {
define( 'WPGRAPHQL_ACF_PLUGIN_FILE', __FILE__ . '/..' );
if (!defined('WPGRAPHQL_ACF_PLUGIN_FILE')) {
define('WPGRAPHQL_ACF_PLUGIN_FILE', __FILE__ . '/..');
}

}

/**
Expand All @@ -124,14 +117,12 @@ private function includes() {
* cycle
*/
private function actions() {

}

/**
* Setup filters
*/
private function filters() {

}

/**
Expand All @@ -140,11 +131,9 @@ private function filters() {
private function init() {

$config = new Config();
add_action( 'graphql_register_types', [ $config, 'init' ], 10, 1 );
add_action('graphql_register_types', [$config, 'init'], 10, 1);

$acf_settings = new ACF_Settings();
$acf_settings->init();

}

}
47 changes: 29 additions & 18 deletions src/class-acfsettings.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* ACF extension for WP-GraphQL
*
Expand All @@ -23,29 +24,28 @@ 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_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
*/
add_action( 'acf/render_field_settings', [ $this, 'add_field_settings' ], 10, 1 );

add_action('acf/render_field_settings', [$this, 'add_field_settings'], 10, 1);
}

/**
* Add settings to each field to show in GraphQL
*
* @param array $field The field to add the setting to.
*/
public function add_field_settings( $field ) {
public function add_field_settings($field) {

$supported_fields = Config::get_supported_fields();

/**
* If there are no supported fields, or the field is not supported, don't add a setting field.
*/
if ( empty( $supported_fields ) || ! is_array( $supported_fields ) || ! in_array( $field['type'], $supported_fields, true ) ) {
if (empty($supported_fields) || !is_array($supported_fields) || !in_array($field['type'], $supported_fields, true)) {
return;
}

Expand All @@ -55,17 +55,16 @@ public function add_field_settings( $field ) {
acf_render_field_setting(
$field,
[
'label' => __( 'Show in GraphQL', 'wp-graphql-acf' ),
'instructions' => __( 'Whether the field should be queryable via GraphQL', 'wp-graphql-acf' ),
'label' => __('Show in GraphQL', 'wp-graphql-acf'),
'instructions' => __('Whether the field should be queryable via GraphQL', 'wp-graphql-acf'),
'name' => 'show_in_graphql',
'type' => 'true_false',
'ui' => 1,
'default_value' => 1,
'value' => isset( $field['show_in_graphql'] ) ? (bool) $field['show_in_graphql'] : true,
'value' => isset($field['show_in_graphql']) ? (bool) $field['show_in_graphql'] : true,
],
true
);

}

/**
Expand All @@ -76,19 +75,19 @@ public function add_field_settings( $field ) {
*
* @param array $field_group The field group to add settings to.
*/
public function add_field_group_settings( $field_group ) {
public function add_field_group_settings($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.' ),
'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,
'value' => isset($field_group['show_in_graphql']) ? (bool) $field_group['show_in_graphql'] : false,
'ui' => 1,
]
);
Expand All @@ -98,17 +97,29 @@ public function add_field_group_settings( $field_group ) {
*/
acf_render_field_wrap(
[
'label' => __( 'GraphQL Field Name', 'acf' ),
'instructions' => __( 'The name of the field group in the GraphQL Schema.', 'wp-graphql-acf' ),
'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' => true,
'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,
'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 fiedl group on', 'wp-graphql-acf'),
'type' => 'checkbox',
'prefix' => 'acf_field_group',
'name' => 'graphql_types_on',
'value' => !empty($field_group['graphql_types_on']) ? $field_group['graphql_types_on'] : null,
'toggle' => true,
'choices' => $choices
]
);
}

}
Loading

0 comments on commit 6cb6193

Please sign in to comment.