PluginProbe
Redirection / 5.8.0
Redirection v5.8.0
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / redirection-capabilities.php

redirection-capabilities.php in Redirection 5.8.0, at redirection-capabilities.php

185 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Redirection capabilities
5 *
6 * Pre 4.6
7 * =======
8 * Hook `redirection_role` and return a capability. This gives access to the entire plugin
9 *
10 * Post 4.6
11 * ========
12 * Hook `redirection_role` and return a capability for access to the plugin menu. For example `edit_pages` will allow an editor
13 * Hook `redirection_capability_check` and return a different capability for each check that needs specific permissions.
14 *
15 * For example, if you want to give editors access to create redirects, but nothing else:
16 *
17 * ```php
18 * add_filter( 'redirection_capability_check', function( $capability, $permission_name ) {
19 * if ( $permission_name === 'redirection_cap_redirect_manage' || $permission_name === 'redirection_cap_redirect_add' ) {
20 * return $capability;
21 * }
22 *
23 * return 'manage_options';
24 * } );
25 * ```
26 *
27 * Always default to restrictive and then grant permissions. Don't default to permissive and remove permissions. This way if a new
28 * capability is added your users won't automatically be granted access.
29 *
30 * Capabilities can be filtered with:
31 * - `redirection_capability_check( $capability, $permission_name )` - override `$capability` dependant on `$permission_name`
32 * - `redirection_capability_pages( $pages )` - filters the list of available pages
33 * - `redirection_role( $cap )` - return the role/capability used for overall access to the plugin
34 *
35 * Note some capabilities may give access to data from others. For example, when viewing a page of redirects via `redirection_cap_redirect_manage`
36 * the client will need to access group data.
37 */
38 class Redirection_Capabilities {
39 const FILTER_ALL = 'redirection_capability_all';
40 const FILTER_PAGES = 'redirection_capability_pages';
41 const FILTER_CAPABILITY = 'redirection_capability_check';
42
43 // The default WordPress capability used for all checks
44 const CAP_DEFAULT = 'manage_options';
45
46 // The main capability used to provide access to the plugin
47 const CAP_PLUGIN = 'redirection_role';
48
49 // These capabilities are combined with `redirection_cap_` to form `redirection_cap_redirect_add` etc
50 const CAP_REDIRECT_MANAGE = 'redirection_cap_redirect_manage';
51 const CAP_REDIRECT_ADD = 'redirection_cap_redirect_add';
52 const CAP_REDIRECT_DELETE = 'redirection_cap_redirect_delete';
53
54 const CAP_GROUP_MANAGE = 'redirection_cap_group_manage';
55 const CAP_GROUP_ADD = 'redirection_cap_group_add';
56 const CAP_GROUP_DELETE = 'redirection_cap_group_delete';
57
58 const CAP_404_MANAGE = 'redirection_cap_404_manage';
59 const CAP_404_DELETE = 'redirection_cap_404_delete';
60
61 const CAP_LOG_MANAGE = 'redirection_cap_log_manage';
62 const CAP_LOG_DELETE = 'redirection_cap_log_delete';
63
64 const CAP_IO_MANAGE = 'redirection_cap_io_manage';
65
66 const CAP_OPTION_MANAGE = 'redirection_cap_option_manage';
67
68 const CAP_SUPPORT_MANAGE = 'redirection_cap_support_manage';
69
70 const CAP_SITE_MANAGE = 'redirection_cap_site_manage';
71
72 const CAP_RSS = 'redirection_cap_rss';
73
74 /**
75 * Determine if the current user has access to a named capability.
76 *
77 * @param string $cap_name The capability to check for. See Redirection_Capabilities for constants.
78 * @return bool
79 */
80 public static function has_access( $cap_name ) {
81 // Get the capability using the default plugin access as the base. Old sites overriding `redirection_role` will get access to everything
82 $cap_to_check = apply_filters( self::FILTER_CAPABILITY, self::get_plugin_access(), $cap_name );
83
84 // Check the capability
85 return current_user_can( $cap_to_check );
86 }
87
88 /**
89 * Return the role/capability used for displaying the plugin menu. This is also the base capability for all other checks.
90 *
91 * @return string Role/capability
92 */
93 public static function get_plugin_access() {
94 return apply_filters( self::CAP_PLUGIN, self::CAP_DEFAULT );
95 }
96
97 /**
98 * Return all the pages the user has access to.
99 *
100 * @phpstan-return list<string>
101 * @return array Array of pages
102 */
103 public static function get_available_pages() {
104 $pages = [
105 self::CAP_REDIRECT_MANAGE => 'redirect',
106 self::CAP_GROUP_MANAGE => 'groups',
107 self::CAP_404_MANAGE => '404s',
108 self::CAP_LOG_MANAGE => 'log',
109 self::CAP_IO_MANAGE => 'io',
110 self::CAP_OPTION_MANAGE => 'options',
111 self::CAP_SUPPORT_MANAGE => 'support',
112 self::CAP_SITE_MANAGE => 'site',
113 self::CAP_RSS => 'rss',
114 ];
115
116 $available = [];
117 foreach ( $pages as $key => $page ) {
118 if ( self::has_access( $key ) ) {
119 $available[] = $page;
120 }
121 }
122
123 /** @var list<string> $filtered */
124 $filtered = apply_filters( self::FILTER_PAGES, $available );
125 // @phpstan-ignore arrayValues.list
126 return array_values( $filtered );
127 }
128
129 /**
130 * Return all the capabilities the current user has
131 *
132 * @phpstan-return list<string>
133 * @return array Array of capabilities
134 */
135 public static function get_all_capabilities() {
136 $caps = self::get_every_capability();
137
138 $caps = array_filter(
139 $caps,
140 function ( $cap ) {
141 return self::has_access( $cap );
142 }
143 );
144
145 /**
146 * @var list<string> $filtered
147 */
148 $filtered = apply_filters( self::FILTER_ALL, $caps );
149 // @phpstan-ignore arrayValues.list
150 return array_values( $filtered );
151 }
152
153 /**
154 * Unfiltered list of all the supported capabilities, without influence from the current user
155 *
156 * @phpstan-return list<string>
157 * @return array Array of capabilities
158 */
159 public static function get_every_capability() {
160 return [
161 self::CAP_REDIRECT_MANAGE,
162 self::CAP_REDIRECT_ADD,
163 self::CAP_REDIRECT_DELETE,
164
165 self::CAP_GROUP_MANAGE,
166 self::CAP_GROUP_ADD,
167 self::CAP_GROUP_DELETE,
168
169 self::CAP_404_MANAGE,
170 self::CAP_404_DELETE,
171
172 self::CAP_LOG_MANAGE,
173 self::CAP_LOG_DELETE,
174
175 self::CAP_IO_MANAGE,
176
177 self::CAP_OPTION_MANAGE,
178
179 self::CAP_SUPPORT_MANAGE,
180
181 self::CAP_SITE_MANAGE,
182 ];
183 }
184 }
185