Skip to main content

How to define a custom statement effect?

Vasyl MartyniukLess than 1 minute

Incorporating custom statement effects into your code serves to enhance its verbosity and readability, though it may seem excessive otherwise.

Consider a scenario where you're developing a membership platform, associating user memberships with specific policies. For instance, the policy snippet below denotes that a user possesses the "Gold" membership:

{
    "Statement": {
        "Resource": "Membership:Gold",
        "Effect": "attach"
    }
}

By implementing this policy and assigning it to a user, you can subsequently verify programmatically whether the user belongs to the Gold membership group, as illustrated in the PHP snippet below:

if (AAM::api()->getAccessPolicyManager()->isAttach('Membership:Gold')) {
    // Yes, this user has the Gold membership
}

However, from a grammatical standpoint, "isAttach" function should ideally be spelled "isAttached." To address this, you can utilize the AAM core filter aam_access_policy_effects_filter to establish the necessary translation. Essentially, this informs the system that "attached" serves as an alias for "attach".

add_filter('aam_access_policy_effects_filter', function($stems) {
    return array_merge($stems, [
        'attached' => 'attach'
    ]);
})

Once again, these customizations primarily serve aesthetic purposes, contributing to the overall readability of your code.