PageRenderTime 25ms CodeModel.GetById 18ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wordpress-seo/admin/capabilities/class-capability-manager-vip.php

https://bitbucket.org/carloskikea/helpet
PHP | 72 lines | 33 code | 9 blank | 30 comment | 1 complexity | 8793cf1a470323ee882028440aa1714e MD5 | raw file
 1<?php
 2/**
 3 * WPSEO plugin file.
 4 *
 5 * @package WPSEO\Admin\Capabilities
 6 */
 7
 8/**
 9 * VIP implementation of the Capability Manager.
10 */
11final class WPSEO_Capability_Manager_VIP extends WPSEO_Abstract_Capability_Manager {
12	/**
13	 * Adds the registered capabilities to the system.
14	 *
15	 * @return void
16	 */
17	public function add() {
18		$role_capabilities = array();
19		foreach ( $this->capabilities as $capability => $roles ) {
20			$role_capabilities = $this->get_role_capabilities( $role_capabilities, $capability, $roles );
21		}
22
23		foreach ( $role_capabilities as $role => $capabilities ) {
24			wpcom_vip_add_role_caps( $role, $capabilities );
25		}
26	}
27
28	/**
29	 * Removes the registered capabilities from the system
30	 *
31	 * @return void
32	 */
33	public function remove() {
34		// Remove from any role it has been added to.
35		$roles = wp_roles()->get_names();
36		$roles = array_keys( $roles );
37
38		$role_capabilities = array();
39		foreach ( array_keys( $this->capabilities ) as $capability ) {
40			// Allow filtering of roles.
41			$role_capabilities = $this->get_role_capabilities( $role_capabilities, $capability, $roles );
42		}
43
44		foreach ( $role_capabilities as $role => $capabilities ) {
45			wpcom_vip_remove_role_caps( $role, $capabilities );
46		}
47	}
48
49	/**
50	 * Returns the roles which the capability is registered on.
51	 *
52	 * @param array  $role_capabilities List of all roles with their capabilities.
53	 * @param string $capability        Capability to filter roles for.
54	 * @param array  $roles             List of default roles.
55	 *
56	 * @return array List of capabilities.
57	 */
58	protected function get_role_capabilities( $role_capabilities, $capability, $roles ) {
59		// Allow filtering of roles.
60		$filtered_roles = $this->filter_roles( $capability, $roles );
61
62		foreach ( $filtered_roles as $role ) {
63			if ( ! isset( $add_role_caps[ $role ] ) ) {
64				$role_capabilities[ $role ] = array();
65			}
66
67			$role_capabilities[ $role ][] = $capability;
68		}
69
70		return $role_capabilities;
71	}
72}