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

feat: ACF v6 support #333

Merged
merged 2 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
32 changes: 23 additions & 9 deletions src/class-acfsettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
*/
class ACF_Settings {

protected $is_acf6_or_higher = false;

/**
* Initialize ACF Settings for the plugin
*/
public function init() {

$this->is_acf6_or_higher = defined( 'ACF_MAJOR_VERSION' ) && version_compare( ACF_MAJOR_VERSION, 6, '>=' );

/**
* Add settings to individual fields to allow each field granular control
* over how it's shown in the GraphQL Schema
Expand Down Expand Up @@ -110,7 +114,10 @@ public function display_metabox( $field_group_post_object ) {
'prefix' => 'acf_field_group',
'value' => isset( $field_group['show_in_graphql'] ) ? (bool) $field_group['show_in_graphql'] : false,
'ui' => 1,
]
],
'div',
'label',
true
);

/**
Expand All @@ -126,7 +133,10 @@ public function display_metabox( $field_group_post_object ) {
'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,
]
],
'div',
'label',
true
);

acf_render_field_wrap(
Expand All @@ -138,7 +148,10 @@ public function display_metabox( $field_group_post_object ) {
'prefix' => 'acf_field_group',
'value' => isset( $field_group['map_graphql_types_from_location_rules'] ) ? (bool) $field_group['map_graphql_types_from_location_rules'] : false,
'ui' => 1,
]
],
'div',
'label',
true
);

$choices = Config::get_all_graphql_types();
Expand All @@ -152,7 +165,10 @@ public function display_metabox( $field_group_post_object ) {
'value' => ! empty( $field_group['graphql_types'] ) ? $field_group['graphql_types'] : [],
'toggle' => true,
'choices' => $choices,
]
],
'div',
'label',
true
);

?>
Expand All @@ -164,7 +180,7 @@ public function display_metabox( $field_group_post_object ) {
if (typeof acf !== 'undefined') {
acf.newPostbox({
'id': 'wpgraphql-acf-meta-box',
'label': 'left'
'label': <?php echo $this->is_acf6_or_higher ? 'top' : "'left'"; ?>
});
}
</script>
Expand Down Expand Up @@ -217,14 +233,12 @@ public function add_field_settings( array $field ) {
public function enqueue_graphql_acf_scripts( string $screen ) {
global $post;

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(
if ( ( $screen === 'post-new.php' || $screen === 'post.php' ) && ( isset( $post->post_type ) && 'acf-field-group' === $post->post_type ) ) {
wp_enqueue_script( 'graphql-acf', plugins_url( 'src/js/main.js', __DIR__ ), array(
'jquery',
'acf-input',
'acf-field-group'
) );
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/class-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,7 @@ public static function get_all_graphql_types() {
$graphql_types[ $interface_name ] = '<span data-interface="'. $interface_name .'">' . $interface_name . ' Interface (' . $config['plural_label'] . ')</span>';
$label = '<span data-implements="'. $interface_name .'"> (' . $config['label'] . ')</span>';
foreach ( $possible_types as $type ) {
$type_label = $type['name'] . $label;
$type_label = $type['name'] . '&nbsp;' . $label;
$type_key = $type['name'];

$graphql_types[ $type_key ] = $type_label;
Expand Down
11 changes: 10 additions & 1 deletion src/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,16 @@ $j(document).ready(function () {
*/
function setGraphqlFieldName() {
var graphqlFieldNameField = $j('#acf_field_group-graphql_field_name');
var fieldGroupTitle = $j('#titlediv #title');

// support for v6+
if ( $j('#title.acf-headerbar-title-field').exists() ) {
var fieldGroupTitle = $j('#title.acf-headerbar-title-field');

// versions of ACF < v6
} else {
var fieldGroupTitle = $j('#titlediv #title');
}

if ('' === graphqlFieldNameField.val()) {
graphqlFieldNameField.val(formatFieldName(fieldGroupTitle.val()));
}
Expand Down