Skip to content

Commit

Permalink
Full updated implementation for pristas-peter#25 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ThyNameIsMud committed May 10, 2021
1 parent d5703b0 commit 39b6416
Showing 1 changed file with 60 additions and 79 deletions.
139 changes: 60 additions & 79 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand All @@ -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;
}
Expand All @@ -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
Expand All @@ -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
*/
Expand All @@ -177,14 +158,15 @@ function show_admin_notice() {
}

add_action(
'admin_notices',
function() {
?>
<div class="error notice">
<p><?php esc_html_e( 'WPGraphQL, Advanced Custom Fields, WPGraphQL for Advanced Custom Fields and WPGraphQL Gutenberg must be active for "wp-graphql-gutenberg-acf" to work.', 'wp-graphql-gutenberg-acf' ); ?></p>
</div>
<?php
}
'admin_notices',
function () {
?>
<div class="error notice">
<p><?php esc_html_e( 'WPGraphQL, Advanced Custom Fields, WPGraphQL for Advanced Custom Fields and WPGraphQL Gutenberg must be active for "wp-graphql-gutenberg-acf" to work.', 'wp-graphql-gutenberg-acf' ); ?>
</p>
</div>
<?php
}
);
}

Expand Down Expand Up @@ -215,6 +197,5 @@ function can_load_plugin() {
return false;
}


return true;
}

0 comments on commit 39b6416

Please sign in to comment.