/var/www/hkosl.com/aga/wp-content/plugins/wordpress-seo/admin/roles/class-abstract-role-manager.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
 * WPSEO plugin file.
 *
 * @package WPSEO\Admin\Roles
 */

/**
 * Abstract Role Manager template.
 */
abstract class WPSEO_Abstract_Role_Manager implements WPSEO_Role_Manager {

    
/**
     * Registered roles.
     *
     * @var array
     */
    
protected $roles = array();

    
/**
     * Registers a role.
     *
     * @param string      $role         Role to register.
     * @param string      $display_name Display name to use.
     * @param null|string $template     Optional. Role to base the new role on.
     *
     * @return void
     */
    
public function register$role$display_name$template null ) {
        
$this->roles$role ] =
            (object) array(
                
'display_name' => $display_name,
                
'template'     => $template,
            );
    }

    
/**
     * Returns the list of registered roles.
     *
     * @return string[] List or registered roles.
     */
    
public function get_roles() {
        return 
array_keys$this->roles );
    }

    
/**
     * Adds the registered roles.
     *
     * @return void
     */
    
public function add() {
        foreach ( 
$this->roles as $role => $data ) {
            
$capabilities $this->get_capabilities$data->template );
            
$capabilities $this->filter_existing_capabilties$role$capabilities );

            
$this->add_role$role$data->display_name$capabilities );
        }
    }

    
/**
     * Removes the registered roles.
     *
     * @return void
     */
    
public function remove() {
        
$roles array_keys$this->roles );
        
array_map( array( $this'remove_role' ), $roles );
    }

    
/**
     * Returns the capabilities for the specified role.
     *
     * @param string $role Role to fetch capabilities from.
     *
     * @return array List of capabilities.
     */
    
protected function get_capabilities$role ) {
        if ( ! 
is_string$role ) || empty( $role ) ) {
            return array();
        }

        
$wp_role get_role$role );
        if ( ! 
$wp_role ) {
            return array();
        }

        return 
$wp_role->capabilities;
    }

    
/**
     * Returns true if the capability exists on the role.
     *
     * @param WP_Role $role       Role to check capability against.
     * @param string  $capability Capability to check.
     *
     * @return bool True if the capability is defined for the role.
     */
    
protected function capability_existsWP_Role $role$capability ) {
        return ! 
array_key_exists$capability$role->capabilities );
    }

    
/**
     * Filters out capabilities that are already set for the role.
     *
     * This makes sure we don't override configurations that have been previously set.
     *
     * @param string $role         The role to check against.
     * @param array  $capabilities The capabilities that should be set.
     *
     * @return array Capabilties that can be safely set.
     */
    
protected function filter_existing_capabilties$role, array $capabilities ) {
        if ( 
$capabilities === array() ) {
            return 
$capabilities;
        }

        
$wp_role get_role$role );
        if ( ! 
$wp_role ) {
            return 
$capabilities;
        }

        foreach ( 
$capabilities as $capability => $grant ) {
            if ( 
$this->capability_exists$wp_role$capability ) ) {
                unset( 
$capabilities$capability ] );
            }
        }

        return 
$capabilities;
    }

    
/**
     * Adds a role to the system.
     *
     * @param string $role         Role to add.
     * @param string $display_name Name to display for the role.
     * @param array  $capabilities Capabilities to add to the role.
     *
     * @return void
     */
    
abstract protected function add_role$role$display_name, array $capabilities = array() );

    
/**
     * Removes a role from the system.
     *
     * @param string $role Role to remove.
     *
     * @return void
     */
    
abstract protected function remove_role$role );
}