| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace JP\CC; |
| 5 |
|
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
|
| 11 |
/** |
| 12 |
* Class Is |
| 13 |
* @package JP\CC |
| 14 |
*/ |
| 15 |
class Is { |
| 16 |
|
| 17 |
/** |
| 18 |
* |
| 19 |
* |
| 20 |
* @param string $who |
| 21 |
* @param array $roles |
| 22 |
* @param array $args context and other args to pass to the filters, generic for now so it could be extended later. |
| 23 |
* |
| 24 |
* @return bool |
| 25 |
*/ |
| 26 |
public static function accessible( $who = '', $roles = array(), $args = array() ) { |
| 27 |
|
| 28 |
if ( is_string( $args ) ) { |
| 29 |
$args = array( 'context' => $args ); |
| 30 |
} |
| 31 |
|
| 32 |
if ( ! is_array( $roles ) ) { |
| 33 |
$roles = array(); |
| 34 |
} |
| 35 |
|
| 36 |
$args = wp_parse_args( $args, array( |
| 37 |
'context' => '', |
| 38 |
) ); |
| 39 |
|
| 40 |
if ( is_string( key( $roles ) ) ) { |
| 41 |
$roles = array_keys( $roles ); |
| 42 |
} |
| 43 |
|
| 44 |
$logged_in = is_user_logged_in(); |
| 45 |
|
| 46 |
$exclude = false; |
| 47 |
|
| 48 |
switch ( $who ) { |
| 49 |
|
| 50 |
case 'logged_in': |
| 51 |
if ( ! $logged_in ) { |
| 52 |
$exclude = true; |
| 53 |
} elseif ( ! empty( $roles ) ) { |
| 54 |
|
| 55 |
// Checks all roles, should not exclude if any are active. |
| 56 |
$valid_role = false; |
| 57 |
|
| 58 |
foreach ( $roles as $role ) { |
| 59 |
if ( current_user_can( $role ) ) { |
| 60 |
$valid_role = true; |
| 61 |
break; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
if ( ! $valid_role ) { |
| 66 |
$exclude = true; |
| 67 |
} |
| 68 |
} |
| 69 |
break; |
| 70 |
|
| 71 |
case 'logged_out': |
| 72 |
$exclude = $logged_in; |
| 73 |
break; |
| 74 |
|
| 75 |
default: |
| 76 |
// Do nothing if the who value does not match logged_in or logged_out. |
| 77 |
} |
| 78 |
|
| 79 |
$exclude = apply_filters( 'jp_cc_is_accessible', $exclude, $who, $roles, $args ); |
| 80 |
|
| 81 |
return ! $exclude; |
| 82 |
} |
| 83 |
|
| 84 |
public static function access_blocked( $who = '', $roles = array(), $args = array() ) { |
| 85 |
return ! static::accessible( $who, $roles, $args ); |
| 86 |
} |
| 87 |
} |
| 88 |
|