From 1f50c93e5d48f5a081c6e6ae98eeaeda1ba202db Mon Sep 17 00:00:00 2001 From: Joe Fusco Date: Mon, 5 Aug 2024 13:18:02 -0400 Subject: [PATCH] Fix custom user role assignment --- .changeset/little-melons-design.md | 5 ++++ wpgraphql-ide.php | 41 +++++++++++++++++++----------- 2 files changed, 31 insertions(+), 15 deletions(-) create mode 100644 .changeset/little-melons-design.md diff --git a/.changeset/little-melons-design.md b/.changeset/little-melons-design.md new file mode 100644 index 0000000..fb6c252 --- /dev/null +++ b/.changeset/little-melons-design.md @@ -0,0 +1,5 @@ +--- +"wpgraphql-ide": patch +--- + +Fixes issue where custom capability was not being assigned to the administrator role. This now happens on plugin activation. diff --git a/wpgraphql-ide.php b/wpgraphql-ide.php index 086e946..919425d 100644 --- a/wpgraphql-ide.php +++ b/wpgraphql-ide.php @@ -88,6 +88,32 @@ function show_admin_notice() { add_cap( 'manage_graphql_ide' ); + } +} +register_activation_hook( __FILE__, __NAMESPACE__ . '\\wpgraphql_ide_activate' ); + +/** + * Adds custom capabilities to specified roles. + */ +function add_custom_capabilities(): void { + $capabilities = get_custom_capabilities(); + $current_hash = generate_capabilities_hash( $capabilities ); + + if ( ! has_capabilities_hash_changed( $current_hash ) ) { + return; + } + + update_roles_capabilities( $capabilities ); + save_capabilities_hash( $current_hash ); +} + /** * Retrieves the custom capabilities and their associated roles for the plugin. * @@ -146,21 +172,6 @@ function save_capabilities_hash( $current_hash ): void { update_option( 'wpgraphql_ide_capabilities', $current_hash ); } -/** - * Adds custom capabilities to specified roles. - */ -function add_custom_capabilities(): void { - $capabilities = get_custom_capabilities(); - $current_hash = generate_capabilities_hash( $capabilities ); - - if ( ! has_capabilities_hash_changed( $current_hash ) ) { - return; - } - - update_roles_capabilities( $capabilities ); - save_capabilities_hash( $current_hash ); -} - /** * Checks if the current user has the capability required to load scripts and styles for the GraphQL IDE. *