Skip to content

Commit

Permalink
Merge pull request #186 from wp-graphql/fix-ide-capability
Browse files Browse the repository at this point in the history
fix: custom user role assignment
  • Loading branch information
josephfusco authored Aug 5, 2024
2 parents 9865d66 + 2bae934 commit 9259ec5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/little-melons-design.md
Original file line number Diff line number Diff line change
@@ -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.
41 changes: 26 additions & 15 deletions wpgraphql-ide.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,32 @@ function show_admin_notice() {
<?php
}

/**
* Assign custom capability to administrator role on plugin activation.
*/
function wpgraphql_ide_activate(): void {
$administrator = get_role( 'administrator' );
if ( $administrator ) {
$administrator->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.
*
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 9259ec5

Please sign in to comment.