PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.2
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / admin / roles / class-abstract-role-manager.php

class-abstract-role-manager.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.2, at admin/roles/class-abstract-role-manager.php

150 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPSEO plugin file.
4 *
5 * @package WPSEO\Admin\Roles
6 */
7
8 /**
9 * Abstract Role Manager template.
10 */
11 abstract class WPSEO_Abstract_Role_Manager implements WPSEO_Role_Manager {
12
13 /**
14 * Registered roles.
15 *
16 * @var array
17 */
18 protected $roles = [];
19
20 /**
21 * Registers a role.
22 *
23 * @param string $role Role to register.
24 * @param string $display_name Display name to use.
25 * @param string|null $template Optional. Role to base the new role on.
26 *
27 * @return void
28 */
29 public function register( $role, $display_name, $template = null ) {
30 $this->roles[ $role ] = (object) [
31 'display_name' => $display_name,
32 'template' => $template,
33 ];
34 }
35
36 /**
37 * Returns the list of registered roles.
38 *
39 * @return string[] List or registered roles.
40 */
41 public function get_roles() {
42 return array_keys( $this->roles );
43 }
44
45 /**
46 * Adds the registered roles.
47 *
48 * @return void
49 */
50 public function add() {
51 foreach ( $this->roles as $role => $data ) {
52 $capabilities = $this->get_capabilities( $data->template );
53 $capabilities = $this->filter_existing_capabilties( $role, $capabilities );
54
55 $this->add_role( $role, $data->display_name, $capabilities );
56 }
57 }
58
59 /**
60 * Removes the registered roles.
61 *
62 * @return void
63 */
64 public function remove() {
65 $roles = array_keys( $this->roles );
66 array_map( [ $this, 'remove_role' ], $roles );
67 }
68
69 /**
70 * Returns the capabilities for the specified role.
71 *
72 * @param string $role Role to fetch capabilities from.
73 *
74 * @return array List of capabilities.
75 */
76 protected function get_capabilities( $role ) {
77 if ( ! is_string( $role ) || empty( $role ) ) {
78 return [];
79 }
80
81 $wp_role = get_role( $role );
82 if ( ! $wp_role ) {
83 return [];
84 }
85
86 return $wp_role->capabilities;
87 }
88
89 /**
90 * Returns true if the capability exists on the role.
91 *
92 * @param WP_Role $role Role to check capability against.
93 * @param string $capability Capability to check.
94 *
95 * @return bool True if the capability is defined for the role.
96 */
97 protected function capability_exists( WP_Role $role, $capability ) {
98 return ! array_key_exists( $capability, $role->capabilities );
99 }
100
101 /**
102 * Filters out capabilities that are already set for the role.
103 *
104 * This makes sure we don't override configurations that have been previously set.
105 *
106 * @param string $role The role to check against.
107 * @param array $capabilities The capabilities that should be set.
108 *
109 * @return array Capabilties that can be safely set.
110 */
111 protected function filter_existing_capabilties( $role, array $capabilities ) {
112 if ( $capabilities === [] ) {
113 return $capabilities;
114 }
115
116 $wp_role = get_role( $role );
117 if ( ! $wp_role ) {
118 return $capabilities;
119 }
120
121 foreach ( $capabilities as $capability => $grant ) {
122 if ( $this->capability_exists( $wp_role, $capability ) ) {
123 unset( $capabilities[ $capability ] );
124 }
125 }
126
127 return $capabilities;
128 }
129
130 /**
131 * Adds a role to the system.
132 *
133 * @param string $role Role to add.
134 * @param string $display_name Name to display for the role.
135 * @param array $capabilities Capabilities to add to the role.
136 *
137 * @return void
138 */
139 abstract protected function add_role( $role, $display_name, array $capabilities = [] );
140
141 /**
142 * Removes a role from the system.
143 *
144 * @param string $role Role to remove.
145 *
146 * @return void
147 */
148 abstract protected function remove_role( $role );
149 }
150