From 39b6416cdf65654ddf6331b87585664b7bccbd40 Mon Sep 17 00:00:00 2001 From: Louis Weber Date: Mon, 10 May 2021 10:02:04 -0400 Subject: [PATCH] Full updated implementation for #25 fix --- plugin.php | 139 +++++++++++++++++++++++------------------------------ 1 file changed, 60 insertions(+), 79 deletions(-) diff --git a/plugin.php b/plugin.php index 4ba7535..f6eab7d 100644 --- a/plugin.php +++ b/plugin.php @@ -21,101 +21,86 @@ use WPGraphQLGutenberg\Schema\Types\BlockTypes; // Exit if accessed directly. -if (!defined('ABSPATH')) { +if ( ! defined( 'ABSPATH' ) ) { exit(); } -if (!class_exists('WPGraphQLGutenbergACF') && class_exists('WPGraphQL\ACF\Config')) { - final class WPGraphQLGutenbergACF extends \WPGraphQL\ACF\Config - { +if ( ! class_exists( 'WPGraphQLGutenbergACF' ) && class_exists( 'WPGraphQL\ACF\Config' ) ) { + final class WPGraphQLGutenbergACF extends \WPGraphQL\ACF\Config { + private static $instance; - public static function instance() - { - if (!isset(self::$instance)) { + public static function instance() { + if ( ! isset( self::$instance ) ) { self::$instance = new WPGraphQLGutenbergACF(); } return self::$instance; } - public static function format_graphql_block_type_acf_name($block_name) + public static function format_graphql_block_type_acf_name( $block_name ): string { return \WPGraphQLGutenberg\Schema\Types\BlockTypes::format_block_name( - $block_name - ) . 'Fields'; + $block_name + ) . 'Fields'; } - protected function add_acf_fields_to_block($block_type) - { - $field_groups = acf_get_field_groups([ - 'block' => $block_type['name'], - ]); + protected function add_acf_fields_to_block( $block_type ) { + $field_groups = acf_get_field_groups( + array( + 'block' => $block_type['name'], + ) + ); - if (empty($field_groups) || !is_array($field_groups)) { + if ( empty( $field_groups ) || ! is_array( $field_groups ) ) { return; } - $type_name = BlockTypes::format_block_name($block_type['name']); + $type_name = BlockTypes::format_block_name( $block_type['name'] ); - foreach ($field_groups as $field_group) { - $field_name = isset($field_group['graphql_field_name']) - ? $field_group['graphql_field_name'] - : Config::camel_case($field_group['title']); + foreach ( $field_groups as $field_group ) { + $field_name = isset( $field_group['graphql_field_name'] ) + ? $field_group['graphql_field_name'] + : Config::camel_case( $field_group['title'] ); $field_group['type'] = 'group'; $field_group['name'] = $field_name; - $config = [ - 'name' => $field_name, - 'description' => $field_group['description'], - 'acf_field' => $field_group, - 'acf_field_group' => null, - 'resolve' => function ($root) use ($field_group) { - return isset($root) ? $root : null; - }, - ]; - - $this->register_graphql_field($type_name, $field_name, $config); + $config = array( + 'name' => $field_name, + 'description' => $field_group['description'], + 'acf_field' => $field_group, + 'acf_field_group' => null, + 'resolve' => function ( $root ) use ( $field_group ) { + return isset( $root ) ? $root : null; + }, + ); + + $this->register_graphql_field( $type_name, $field_name, $config ); } } - public function __construct() - { - add_action('acf/init', function () { - add_filter( - 'graphql_acf_get_root_id', - function ($id, $root) { - if ($root instanceof Block) { - acf_setup_meta( - $root['attributes']['data'], - $root['attributes']['id'], - false - ); - - return $root['attributes']['id']; - } - - return $id; - }, - 10, - 2 + public function get_root_id( $id, $root ) { + if ( $root instanceof Block ) { + acf_setup_meta( + $root['attributes']['data'], + $root['attributes']['id'], + false ); - add_filter( - 'graphql_gutenberg_block_type_fields', - function ($fields, $block_type, $type_registry) { - $this->type_registry = $type_registry; + return $root['attributes']['id']; + } // end if + + return $id; + } - if (substr($block_type['name'], 0, 4) === "acf/") { - $this->add_acf_fields_to_block($block_type); - } + public function block_type_fields( $fields, $block_type, $type_registry ) { + $this->type_registry = $type_registry; - return $fields; - }, - 10, - 3 - ); - }); + if ( substr( $block_type['name'], 0, 4 ) === 'acf/' ) { + $this->add_acf_fields_to_block( $block_type ); + } + + return $fields; } } } @@ -127,14 +112,12 @@ function ($fields, $block_type, $type_registry) { * @return WPGraphQLGutenbergACF|void */ function init() { - /** * If dependencies are missing, do not initialize the code */ if ( false === can_load_plugin() ) { // Show the admin notice add_action( 'admin_init', __NAMESPACE__ . '\show_admin_notice' ); - // Bail return; } @@ -158,8 +141,7 @@ function ( $fields, $block_type, $type_registry ) { ); } -add_action( 'init', '\WPGraphQLGutenbergACF\init', 100 ); - +add_action( 'acf/init', '\WPGraphQLGutenbergACF\init', 100 ); /** * Show admin notice to admins if this plugin is active but dependencies are missing @@ -168,7 +150,6 @@ function ( $fields, $block_type, $type_registry ) { * @return bool */ function show_admin_notice() { - /** * For users with lower capabilities, don't show the notice */ @@ -177,14 +158,15 @@ function show_admin_notice() { } add_action( - 'admin_notices', - function() { - ?> -
-

-
- +
+

+

+
+