Skip to main content

Services

Vasyl MartyniukAbout 2 min

Definition

interface AAM_Framework_Service_Interface {

    public get_instance(AAM_Framework_AccessLevel_Interface $access_level, array $settings): AAM_Framework_Service_Interface

}

AAM 7 offers a wide array of services that support control over various WordPress site resources and preferences, like an admin menu, toolbar, metaboxes, capabilities, redirects and more. With this comprehensive toolkit you can easily manage access and adjust preferences as needed.

Creating a new instance of an AAM service is straightforward, thanks to the AAM::api(), which serves as a single entry point for the entire AAM framework. Let’s walk through an example using the URL service, a tool that allows you to manage access to specific URLs on your WordPress site.

Start by calling the single AAM entry point, which provides a structured way to set up new service. If no access level is provided, the current user is assumed.

// This call returns the instance of URL service for current user pre-configured.
// If user is not authenticated, the URL service will be pre-configured for visitor
// access level
$service = AAM::api()->urls();

Each service instance requires a defined access level for which access controls or preferences are configured. Access levels can be customized for specific users, roles, visitors, or for all users collectively (aka default access level).

// Get pre-configured URLs service for the Author role
$service = AAM::api()->urls('role:author');

// Get pre-configured Terms service for user with email john@example.xyz
$service = AAM::api()->terms('user:john@example.xyz')

// If no access level is passed, the current user is assumed
$service = AAM::api()->widgets();

You can pass access level to a service in few different ways as follows.

Pass an instance of access level. When calling any service, you can simply pass an instance of AAM_Framework_AccessLevel_Interface class. For example, in this code we the Backend Menu service to define access controls to the role Editor.

$service = AAM::api()->backend_menu(
    AAM::api()->role('editor')
);

Pass a string representation of access level. Another way to pre-configure a service is to specify access level as a string. For instance, these are just a few ways to return an instance of a service with a string access level.

// Get an instance of the Admin Toolbar service for user with ID 45
$service = AAM::api()->admin_toolbar('user:45');

// Get an instance of the URLs service for visitors
$service = AAM::api()->urls('visitor');

// Get an instance of the Posts service to define access controls to everyone
$service = AAM::api()->posts('all');

This is the list of all supported string values.

String ValueDescription
visitor, guest, anonymousAn instance of the visitor access level.
all, anyone, everyone, defaultAn instance of the default access level.
role:<role_slug>An instance of the role access level. The "role_slug" has to be a valid role slug.
user:<user_identifier>An instance of the user access level. The "user_identifier" has to be either user ID, user_login or user_email.
Virtual Assistant