PageRenderTime 26ms CodeModel.GetById 19ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/carloskikea/helpet
PHP | 84 lines | 26 code | 11 blank | 47 comment | 3 complexity | d4812460c1b33900643ea8b1f2b985ac MD5 | raw file
 1<?php
 2/**
 3 * WPSEO plugin file.
 4 *
 5 * @package WPSEO\Admin\Capabilities
 6 */
 7
 8/**
 9 * Abstract Capability Manager shared code.
10 */
11abstract class WPSEO_Abstract_Capability_Manager implements WPSEO_Capability_Manager {
12	/** @var array Registered capabilities */
13	protected $capabilities = array();
14
15	/**
16	 * Registers a capability.
17	 *
18	 * @param string $capability Capability to register.
19	 * @param array  $roles      Roles to add the capability to.
20	 * @param bool   $overwrite        Optional. Use add or overwrite as registration method.
21	 */
22	public function register( $capability, array $roles, $overwrite = false ) {
23		if ( $overwrite || ! isset( $this->capabilities[ $capability ] ) ) {
24			$this->capabilities[ $capability ] = $roles;
25
26			return;
27		}
28
29		// Combine configurations.
30		$this->capabilities[ $capability ] = array_merge( $roles, $this->capabilities[ $capability ] );
31
32		// Remove doubles.
33		$this->capabilities[ $capability ] = array_unique( $this->capabilities[ $capability ] );
34	}
35
36	/**
37	 * Returns the list of registered capabilitities.
38	 *
39	 * @return string[] Registered capabilities.
40	 */
41	public function get_capabilities() {
42		return array_keys( $this->capabilities );
43	}
44
45	/**
46	 * Returns a list of WP_Role roles.
47	 *
48	 * The string array of role names are converted to actual WP_Role objects.
49	 * These are needed to be able to use the API on them.
50	 *
51	 * @param array $roles Roles to retrieve the objects for.
52	 *
53	 * @return WP_Role[] List of WP_Role objects.
54	 */
55	protected function get_wp_roles( array $roles ) {
56		$wp_roles = array_map( 'get_role', $roles );
57
58		return array_filter( $wp_roles );
59	}
60
61	/**
62	 * Filter capability roles.
63	 *
64	 * @param string $capability Capability to filter roles for.
65	 * @param array  $roles      List of roles which can be filtered.
66	 *
67	 * @return array Filtered list of roles for the capability.
68	 */
69	protected function filter_roles( $capability, array $roles ) {
70		/**
71		 * Filter: Allow changing roles that a capability is added to.
72		 *
73		 * @api array $roles The default roles to be filtered.
74		 */
75		$filtered = apply_filters( $capability . '_roles', $roles );
76
77		// Make sure we have the expected type.
78		if ( ! is_array( $filtered ) ) {
79			return array();
80		}
81
82		return $filtered;
83	}
84}