URLs Access
Introduction
Definition
class AAM_Framework_Service_Urls {
public deny(string|array $url_schema, array $redirect = null) : bool
public allow(string|array $url_schema) : bool
public reset(string|array $url_schema = null) : bool
public is_denied(string $url) : bool
public is_allowed(string $url) : bool
public get_redirect(string $url) : array|null
}
restrict
Restrict access to any given URL or array of URLs. The alias method is deny. The result of the execution is a boolean true if permissions are stored successfully.
$service = AAM::api()->urls();
$service->restrict('/golden-membership/');
The second argument $redirect accepts the configurations that define how to handle user workflow when access is restricted to the given URL.
| Property | Description |
|---|---|
type | Redirect type. The allowed values are "custom_message", "page_redirect", "url_redirect", "trigger_callback", "login_redirect" and premium type "conditional". |
message | The custom message if the type is "custom_message". The message can be a plain text or rich HTML. The value is sanitized with esc_js WordPress core function. |
redirect_page_id | The valid and existing page ID if the type is page_redirect. |
redirect_url | Valid URL or relative path on the website if the type is "url_redirect". |
callback | The valid callback function if the type is "trigger_callback". The value is sanitized with the is_callable PHP core function and checks only for valid syntax. |
condition | This is the premium feature and specifies condition that if met, denies access to given URL. |
http_redirect_code | Optional. HTTP redirect code in case of URL or page redirect. |
Few more examples with defined custom redirect behavior.
// Redirect visitor to the login page and back upon successful authentication. Here
// we define a wildcard rule that is available only with premium add-on
AAM::api()->urls(
AAM::api()->visitor()
)->restrict('/course/*', [
'type' => 'login_redirect'
]);
// Redirect to a different URL user with email emily@example.xyz
AAM::api()->urls(
AAM::api()->user('emily@example.xyz')
)->restrict('/department-page', [
'type' => 'url_redirect',
'redirect_url' => '/department'
]);
is_restricted
Make a decision if provided URL is restricted.
$service = AAM::api()->urls();
// Check if the user (either authenticated or not) has access to currently
// viewed page
if ($service->is_restricted($_SERVER['REQUEST_URI'])) {
// Do something here. Access is denied.
}
get_redirect
Get return configurations that are used to define user workflow when access is denied to a given URL. The alias method is redirect. If no redirect is defined, the null value is returned.
AAM plugin uses this method to return redirect configurations when access to currently viewed URL is denied and passes these configurations to the do_redirect method in the AAM_Framework_Utility_Redirect framework helper class.
// Checking if home page is restricted to the current user and if so, redirect user
// accordingly
$home_url = home_url();
$service = AAM::api()->urls();
if ($service->is_restricted($home_url)) {
AAM_Framework_Utility_Redirect::do_redirect(
$service->redirect($home_url)
);
}
access_mode
Set or get current access mode. If the first argument $mode is a string value, the access mode will be set. Otherwise - return current access mode. There are supported only two modes: default and restricted.
// The following example makes the entire website private and allows only the
// home & login pages for visitors
$service = AAM::api()->urls(
AAM::api()->visitor
);
// Set the restriction mode
if ($service->set_access_mode('restricted')) {
// Allow only home and standard WordPress login pages
$service->allow([
'/',
'/wp-login.php'
]);
}