PluginProbe
Redirection / 4.7.2
Redirection v4.7.2
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 4.7.2, at redirection-capabilities.php

168 lines 5.3 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` 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 /**
73 * Determine if the current user has access to a named capability.
74 *
75 * @param string $cap_name The capability to check for. See Redirection_Capabilities for constants.
76 * @return boolean
77 */
78 public static function has_access( $cap_name ) {
79 // Get the capability using the default plugin access as the base. Old sites overriding `redirection_role` will get access to everything
80 $cap_to_check = apply_filters( self::FILTER_CAPABILITY, self::get_plugin_access(), $cap_name );
81
82 // Check the capability
83 return current_user_can( $cap_to_check );
84 }
85
86 /**
87 * Return the role/capability used for displaying the plugin menu. This is also the base capability for all other checks.
88 *
89 * @return string Role/capability
90 */
91 public static function get_plugin_access() {
92 return apply_filters( self::CAP_PLUGIN, self::CAP_DEFAULT );
93 }
94
95 /**
96 * Return all the pages the user has access to.
97 *
98 * @return array Array of pages
99 */
100 public static function get_available_pages() {
101 $pages = [
102 self::CAP_REDIRECT_MANAGE => 'redirect',
103 self::CAP_GROUP_MANAGE => 'groups',
104 self::CAP_404_MANAGE => '404s',
105 self::CAP_LOG_MANAGE => 'log',
106 self::CAP_IO_MANAGE => 'io',
107 self::CAP_OPTION_MANAGE => 'options',
108 self::CAP_SUPPORT_MANAGE => 'support',
109 self::CAP_SITE_MANAGE => 'site',
110 ];
111
112 $available = [];
113 foreach ( $pages as $key => $page ) {
114 if ( self::has_access( $key ) ) {
115 $available[] = $page;
116 }
117 }
118
119 return array_values( apply_filters( self::FILTER_PAGES, $available ) );
120 }
121
122 /**
123 * Return all the capabilities the current user has
124 *
125 * @return array Array of capabilities
126 */
127 public static function get_all_capabilities() {
128 $caps = self::get_every_capability();
129
130 $caps = array_filter( $caps, function( $cap ) {
131 return self::has_access( $cap );
132 } );
133
134 return array_values( apply_filters( self::FILTER_ALL, $caps ) );
135 }
136
137 /**
138 * Unfiltered list of all the supported capabilities, without influence from the current user
139 *
140 * @return array Array of capabilities
141 */
142 public static function get_every_capability() {
143 return [
144 self::CAP_REDIRECT_MANAGE,
145 self::CAP_REDIRECT_ADD,
146 self::CAP_REDIRECT_DELETE,
147
148 self::CAP_GROUP_MANAGE,
149 self::CAP_GROUP_ADD,
150 self::CAP_GROUP_DELETE,
151
152 self::CAP_404_MANAGE,
153 self::CAP_404_DELETE,
154
155 self::CAP_LOG_MANAGE,
156 self::CAP_LOG_DELETE,
157
158 self::CAP_IO_MANAGE,
159
160 self::CAP_OPTION_MANAGE,
161
162 self::CAP_SUPPORT_MANAGE,
163
164 self::CAP_SITE_MANAGE,
165 ];
166 }
167 }
168