class-capabilities.php
1 year ago
class-compatibility.php
1 year ago
class-install.php
1 year ago
class-uninstall.php
1 year ago
index.php
1 year ago
class-capabilities.php
170 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The class is responsible for managing and registering capabilities in WordPress. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.47.0 |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Installation; |
| 11 | |
| 12 | defined( 'ABSPATH' ) || exit; |
| 13 | |
| 14 | /** |
| 15 | * Capabilities manager. |
| 16 | */ |
| 17 | class Capabilities { |
| 18 | |
| 19 | /** |
| 20 | * Registered capabilities. |
| 21 | * |
| 22 | * @var array |
| 23 | */ |
| 24 | private $capabilities = []; |
| 25 | |
| 26 | /** |
| 27 | * Registered capabilities by role. |
| 28 | * |
| 29 | * @var array |
| 30 | */ |
| 31 | private $role_capabilities = []; |
| 32 | |
| 33 | /** |
| 34 | * The constructor |
| 35 | */ |
| 36 | public function __construct() { |
| 37 | $this->register_defaults(); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Set default capabilities. |
| 42 | * |
| 43 | * @return void |
| 44 | */ |
| 45 | public function register_defaults(): void { |
| 46 | $this->register( |
| 47 | 'advanced_ads_manage_options', |
| 48 | esc_html__( 'Allows changing plugin options', 'advanced-ads' ), |
| 49 | 'administrator' |
| 50 | ); |
| 51 | |
| 52 | $this->register( |
| 53 | 'advanced_ads_see_interface', |
| 54 | esc_html__( 'Allows access to the Advanced Ads backend', 'advanced-ads' ), |
| 55 | 'administrator' |
| 56 | ); |
| 57 | |
| 58 | $this->register( |
| 59 | 'advanced_ads_edit_ads', |
| 60 | esc_html__( 'Allows editing ads', 'advanced-ads' ), |
| 61 | 'administrator' |
| 62 | ); |
| 63 | |
| 64 | $this->register( |
| 65 | 'advanced_ads_manage_placements', |
| 66 | esc_html__( 'Allows changing the placements page', 'advanced-ads' ), |
| 67 | 'administrator' |
| 68 | ); |
| 69 | |
| 70 | $this->register( |
| 71 | 'advanced_ads_place_ads', |
| 72 | esc_html__( 'Enables shortcode button', 'advanced-ads' ), |
| 73 | 'administrator' |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Register a capability. |
| 79 | * |
| 80 | * @param string $capability Capability slug. |
| 81 | * @param string $title Capability title. |
| 82 | * @param array|string $roles Roles to assigned. |
| 83 | * |
| 84 | * @return void |
| 85 | */ |
| 86 | public function register( $capability, $title, $roles ): void { |
| 87 | $this->capabilities[ $capability ] = $title; |
| 88 | |
| 89 | foreach ( (array) $roles as $role ) { |
| 90 | $this->role_capabilities[ $role ] = $this->role_capabilities[ $role ] ?? []; |
| 91 | $this->role_capabilities[ $role ][] = $capability; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get capabilities. |
| 97 | * |
| 98 | * @return array |
| 99 | */ |
| 100 | public function get_capabilities(): array { |
| 101 | return $this->capabilities; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Create capabilities. |
| 106 | * |
| 107 | * @return void |
| 108 | */ |
| 109 | public function create_capabilities(): void { |
| 110 | foreach ( $this->get_roles() as $role ) { |
| 111 | $this->loop_capabilities( $role, 'add_cap' ); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Remove capabilities. |
| 117 | * |
| 118 | * @return void |
| 119 | */ |
| 120 | public function remove_capabilities(): void { |
| 121 | foreach ( $this->get_roles() as $role ) { |
| 122 | $this->loop_capabilities( $role, 'remove_cap' ); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Reset capabilities. |
| 128 | * |
| 129 | * @return void |
| 130 | */ |
| 131 | public function reset_capabilities(): void { |
| 132 | $this->remove_capabilities(); |
| 133 | $this->create_capabilities(); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Get roles names |
| 138 | * |
| 139 | * @return array |
| 140 | */ |
| 141 | private function get_roles(): array { |
| 142 | $roles = wp_roles(); |
| 143 | return array_keys( $roles->get_names() ); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Loop capabilities and perform action. |
| 148 | * |
| 149 | * @param string $role_slug Role slug. |
| 150 | * @param string $perform Action to perform. |
| 151 | * |
| 152 | * @return void |
| 153 | */ |
| 154 | private function loop_capabilities( $role_slug, $perform ): void { |
| 155 | // Early bail!! |
| 156 | if ( ! isset( $this->role_capabilities[ $role_slug ] ) ) { |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | $role = get_role( $role_slug ); |
| 161 | if ( ! $role ) { |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | foreach ( $this->role_capabilities[ $role_slug ] as $cap ) { |
| 166 | $role->$perform( $cap ); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 |