Skip to content

Commit

Permalink
Fix custom user role assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
josephfusco committed Aug 5, 2024
1 parent 9865d66 commit 1f50c93
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() {

Check failure on line 94 in wpgraphql-ide.php

View workflow job for this annotation

GitHub Actions / Check code

Function WPGraphQLIDE\wpgraphql_ide_activate() has no return type specified.
$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 1f50c93

Please sign in to comment.