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

Added user role location #59

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
48 changes: 48 additions & 0 deletions src/class-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function init() {
$this->add_acf_fields_to_media_items();
$this->add_acf_fields_to_individual_posts();
$this->add_acf_fields_to_users();
$this->add_acf_fields_to_user_roles();
}

/**
Expand Down Expand Up @@ -1330,6 +1331,53 @@ protected function add_acf_fields_to_users() {

}

function add_acf_fields_to_user_roles(){


/**
* Get the field groups for every user role
*/

global $wp_roles;

$field_groups = [];

foreach( $wp_roles->roles as $role_slug => $role ) {

$field_role_groups = acf_get_field_groups([
'user_role' => $role_slug,
]);

$field_groups = array_merge( $field_groups, $field_role_groups );
}

/**
* Get a unique list of groups that match the user role location rules
*/

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;
$description = $field_group['description'] ? $field_group['description'] . ' | ' : '';
$config = [
'name' => $field_name,
'description' => $description . sprintf( __( 'Added to the GraphQL Schema because the ACF Field Group "%1$s" was assigned to User Role', 'wp-graphql-acf' ), $field_group['title'] ),
'acf_field' => $field_group,
'acf_field_group' => null,
'resolve' => function( $root ) use ( $field_group ) {
return isset( $root ) ? $root : null;
}
];

$this->register_graphql_field( 'User', $field_name, $config );

}


}

protected function add_acf_fields_to_options_pages() {
// @todo: Coming soon
}
Expand Down