1 | <?php |
||
2 | /** |
||
3 | * @package WPEmerge |
||
4 | * @author Atanas Angelov <[email protected]> |
||
5 | * @copyright 2018 Atanas Angelov |
||
6 | * @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0 |
||
7 | * @link https://wpemerge.com/ |
||
8 | */ |
||
9 | |||
10 | namespace WPEmerge\Middleware; |
||
11 | |||
12 | use Closure; |
||
13 | use WPEmerge; |
||
14 | use WPEmerge\Requests\RequestInterface; |
||
15 | |||
16 | /** |
||
17 | * Redirect users who do not have a capability to a specific URL. |
||
18 | */ |
||
19 | class UserCanMiddleware { |
||
20 | /** |
||
21 | * {@inheritDoc} |
||
22 | */ |
||
23 | 4 | public function handle( RequestInterface $request, Closure $next, $capability = '', $object_id = '0', $url = '' ) { |
|
24 | 4 | $capability = apply_filters( 'wpemerge.middleware.user.can.capability', $capability, $request ); |
|
25 | 4 | $object_id = apply_filters( 'wpemerge.middleware.user.can.object_id', (int) $object_id, $capability, $request ); |
|
26 | 4 | $args = [$capability]; |
|
27 | |||
28 | 4 | if ( $object_id !== 0 ) { |
|
29 | 1 | $args[] = $object_id; |
|
30 | } |
||
31 | |||
32 | 4 | if ( call_user_func_array( 'current_user_can', $args ) ) { |
|
33 | 2 | return $next( $request ); |
|
34 | } |
||
35 | |||
36 | 3 | if ( empty( $url ) ) { |
|
37 | 2 | $url = home_url(); |
|
38 | } |
||
39 | |||
40 | 3 | $url = apply_filters( 'wpemerge.middleware.user.can.redirect_url', $url, $request ); |
|
41 | |||
42 | 3 | return WPEmerge\redirect()->to( $url ); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
43 | } |
||
44 | } |
||
45 |