| 1 |
<?php |
| 2 |
/** |
| 3 |
* Restriction utility & helper functions. |
| 4 |
* |
| 5 |
* @package ContentControl |
| 6 |
* @subpackage Functions |
| 7 |
* @copyright (c) 2023 Code Atlantic LLC |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace ContentControl; |
| 11 |
|
| 12 |
defined( 'ABSPATH' ) || exit; |
| 13 |
|
| 14 |
/** |
| 15 |
* Check if admins are excluded from restrictions. |
| 16 |
* |
| 17 |
* @return bool True if admins are excluded, false if not. |
| 18 |
*/ |
| 19 |
function admins_are_excluded() { |
| 20 |
return get_data_version( 'settings' ) > 1 && plugin()->get_option( 'excludeAdmins' ); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Current user is excluded from restrictions. |
| 25 |
* |
| 26 |
* @return bool True if user is excluded, false if not. |
| 27 |
*/ |
| 28 |
function user_is_excluded() { |
| 29 |
return admins_are_excluded() && current_user_can( plugin()->get_permission( 'manage_settings' ) ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Check if user meets requirements. |
| 34 |
* |
| 35 |
* @param string $user_status logged_in or logged_out. |
| 36 |
* @param array|string $user_roles array of roles to check. |
| 37 |
* @param string $role_match any|match|exclude. |
| 38 |
* |
| 39 |
* @return bool True if user meets requirements, false if not. |
| 40 |
*/ |
| 41 |
function user_meets_requirements( $user_status, $user_roles = [], $role_match = 'match' ) { |
| 42 |
if ( empty( $user_status ) ) { |
| 43 |
// Always default to protecting content. |
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
// If roles is string, convert to array. |
| 48 |
if ( is_string( $user_roles ) ) { |
| 49 |
$user_roles = explode( ',', $user_roles ); |
| 50 |
$user_roles = array_map( 'trim', $user_roles ); |
| 51 |
$user_roles = array_map( 'strtolower', $user_roles ); |
| 52 |
} |
| 53 |
|
| 54 |
// If roles is array of keyed roles, convert to array of roles[]. |
| 55 |
if ( is_string( key( $user_roles ) ) ) { |
| 56 |
$user_roles = array_keys( $user_roles ); |
| 57 |
} |
| 58 |
|
| 59 |
$logged_in = is_user_logged_in(); |
| 60 |
|
| 61 |
switch ( $user_status ) { |
| 62 |
case 'logged_in': |
| 63 |
// If not logged in, return false. |
| 64 |
if ( ! $logged_in ) { |
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
// If current user is excluded from restrictions, return true. |
| 69 |
if ( user_is_excluded() ) { |
| 70 |
return true; |
| 71 |
} |
| 72 |
|
| 73 |
// If we got this far, we're logged in. |
| 74 |
if ( 'any' === $role_match || empty( $user_roles ) ) { |
| 75 |
return true; |
| 76 |
} |
| 77 |
|
| 78 |
// true for match, false for exclude. |
| 79 |
$match_value = 'match' === $role_match ? true : false; |
| 80 |
|
| 81 |
// Checks all roles, any match will return. |
| 82 |
foreach ( $user_roles as $role ) { |
| 83 |
if ( current_user_can( $role ) ) { |
| 84 |
return $match_value; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
// If we got this far, we're logged in but don't have the required role. |
| 89 |
return ! $match_value; |
| 90 |
|
| 91 |
case 'logged_out': |
| 92 |
return ! $logged_in; |
| 93 |
|
| 94 |
default: |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Check if a given query can be ignored. |
| 103 |
* |
| 104 |
* @param \WP_Query $query Query object. |
| 105 |
* |
| 106 |
* @return bool True if query can be ignored, false if not. |
| 107 |
*/ |
| 108 |
function query_can_be_ignored( $query = null ) { |
| 109 |
$post_types_to_ignore = [ |
| 110 |
'cc_restriction', |
| 111 |
'wp_template', |
| 112 |
'wp_template_part', |
| 113 |
'wp_global_styles', |
| 114 |
]; |
| 115 |
|
| 116 |
if ( $query->get( 'ignore_restrictions', false ) ) { |
| 117 |
return true; |
| 118 |
} |
| 119 |
|
| 120 |
// Ignore specific core post types. |
| 121 |
if ( in_array( $query->get( 'post_type' ), $post_types_to_ignore, true ) ) { |
| 122 |
return true; |
| 123 |
} |
| 124 |
|
| 125 |
return false !== apply_filters( 'content_control/ignoreable_query', false, $query ); |
| 126 |
} |
| 127 |
|