Skip to content

Instantly share code, notes, and snippets.

Created March 2, 2015 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/b4f69e203630f447677b to your computer and use it in GitHub Desktop.
Save anonymous/b4f69e203630f447677b to your computer and use it in GitHub Desktop.
Unable to access admin menu backend pages. Page is listed but menu is inaccessible due to insufficient permissions.
<?php
/**
* Class FooPluginLoader
*
* This class handles the whole bootstrapping functionality for wordpress
* Bootstrapping itself is happening below the class, so check out from there what's actually going on ;)
*
* @author Manuel Stosic <manuel.stosic@krankikom.de>
*/
final class FooPluginLoader
{
/**
* Add the required Menus to the WP-Backend
*/
public function initBackendMenues()
{
add_menu_page(
'Foo Profile', // Link Text
'Manage your Foo profile', // Title
'Foo-profile', // Capability (rights)
'Foo-profile', // Slug
[$this, 'renderFooProfile'] // Callback for Link
);
add_menu_page(
'Foo Administration', // Link Text
'Foo Administration', // Title
'Foo-any-profile', // Capability (rights)
'Foo-admin', // Slug
[$this, 'renderFooAdministration'] // Callback for Link
);
}
/**
* Create the output for the Foo Profile Page for the current User
*/
public function renderFooProfile()
{
echo "Some great USER stuff will be happening here.";
}
/**
* Create the output for the Foo Administration Page
*/
public function renderFooAdministration()
{
echo "Some great ADMIN stuff will be happening here.";
}
/**
* Handle everything that needs to be done on Plugin Activation
*
* This includes:
* - Adding required roles / capabilities if they are not yet existing
*/
public function handlePluginActivation()
{
/**
* Add required roles for handling Foo Workflows. Current roles are:
*
* - Foo-user
* - Foo-admin
*/
if (is_null(get_role('Foo-user'))) {
$this->registerRoleFooUser();
}
if (is_null(get_role('Foo-admin'))) {
$this->registerRoleFooAdmin();
}
/**
* Once the main users are there, so are the capabilities. Make sure that the WP administrator role has
* access to the rights as well
*/
$adminRole = get_role('administrator');
if (false === $adminRole->has_cap('Foo-any-profile')) {
$this->registerFooCapabilitiesToAdministrator($adminRole);
}
/**
* Once roles are set up, also insert two dummy users for testing purposes
*
* @todo REMOVE pre live version
*/
if (false === get_user_by('user_login', 'ouser')) {
$this->registerDummyUser('ouser', 'Foo-user');
}
if (false === get_user_by('user_login', 'oadmin')) {
$this->registerDummyUser('oadmin', 'Foo-admin');
}
}
/**
* Registers a new User with some UserData
*
* @see http://codex.wordpress.org/Function_Reference/wp_insert_user
*
* @param $name
* @param $role
* @param array $userData Compatible User Data Array
*
* @return int|WP_Error
*/
protected function registerDummyUser($name, $role, array $userData = [])
{
$newUser = $userData + [
'user_login' => $name,
'user_pass' => $name,
'role' => $role
];
return wp_insert_user($newUser);
}
/**
* This is only supposed to be a temporary function and supposed to be removed after developing purposes
*
* @todo remove after development process
*/
public function handlePluginDeactivation()
{
remove_role('Foo-user');
remove_role('Foo-admin');
}
/**
* Registers the role "Foo-user" with the following capabilities
* - read Foo data on the frontend
* - view Foo profile on the backend
* - edit Foo profile on the backend
*
* @return true|WP_Error
*/
protected function registerRoleFooUser()
{
try {
add_role('Foo-user', 'Foo User', [
'read' => true, // Global Wordpress read rule
'level_0' => true, // Global Wordpress lowest possible user role
'Foo-read' => true, // General allowance to read Foo Data (frontend)
'Foo-profile' => true, // Allowance to VIEW users own Foo Profile in Backend
'Foo-profile-edit' => true // Allowance to EDIT users own Foo Profile in Backend
]);
return true;
} catch (\Exception $e) {
return new WP_Error(
'Foo',
'Error while trying to register new role (Foo-user)',
['exception' => $e->getMessage()]
);
}
}
/**
* Registers the role "Foo-user" with the following capabilities
* - read Foo data on the frontend
* - view ANY Foo profile on the backend
* - edit ANY Foo profile on the backend
*
* @return true|WP_Error
*/
protected function registerRoleFooAdmin()
{
try {
add_role('Foo-admin', 'Foo Administrator', [
'read' => true, // Global Wordpress read rule
'level_0' => true, // Global Wordpress lowest possible user role
'Foo-read' => true, // General allowance to read Foo Data (frontend)
'Foo-any-profile' => true, // Allowance to VIEW users own Foo Profile in Backend
'Foo-any-profile-edit' => true // Allowance to EDIT users own Foo Profile in Backend
]);
return true;
} catch (\Exception $e) {
return new WP_Error(
'Foo',
'Error while trying to register new role (Foo-admin)',
['exception' => $e->getMessage()]
);
}
}
/**
* Adds Foo admin capabilities to administrator role
*
* @param WP_Role $role
*
* @return bool|WP_Error
*/
protected function registerFooCapabilitiesToAdministrator(WP_Role $role = null)
{
try {
$role = $role ?: get_role('administrator');
$role->add_cap('Foo-read', true);
$role->add_cap('Foo-any-profile', true);
$role->add_cap('Foo-any-profile-edit', true);
return true;
} catch (\Exception $e) {
return new WP_Error(
'Foo',
'Error while adding Foo Capabilities to WP Administrator Role',
['exception' => $e->getMessage()]
);
}
}
}
$FooLoader = new FooPluginLoader();
# Register the Plugin Activation Routine
register_activation_hook(__FILE__, function () use ($FooLoader) {
$FooLoader->handlePluginActivation();
});
register_deactivation_hook(__FILE__, function () use ($FooLoader) {
$FooLoader->handlePluginDeactivation();
});
# Register the Admin Menu
add_action('admin_init', function () use ($FooLoader) {
$FooLoader->initBackendMenues();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment