Checker.php
4 years ago
PostContent.php
4 years ago
Redirect.php
4 years ago
RestrictionShortcode.php
4 years ago
index.php
5 years ago
Checker.php
95 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\ContentProtection\Frontend; |
| 4 | |
| 5 | |
| 6 | use ProfilePress\Core\ContentProtection\ContentConditions; |
| 7 | |
| 8 | class Checker |
| 9 | { |
| 10 | public static function is_blocked($who_can_access = 'everyone', $roles = [], $wp_users = []) |
| 11 | { |
| 12 | if ('login' == $who_can_access) { |
| 13 | |
| 14 | if ( ! is_user_logged_in()) return true; |
| 15 | |
| 16 | if ( ! empty($roles)) { |
| 17 | |
| 18 | $user_roles = wp_get_current_user()->roles; |
| 19 | |
| 20 | if (!empty(array_intersect($roles, $user_roles))) return false; |
| 21 | } |
| 22 | |
| 23 | if ( ! empty($wp_users)) { |
| 24 | |
| 25 | $users = array_map('absint', $wp_users); |
| 26 | |
| 27 | if ( in_array(get_current_user_id(), $users)) return false; |
| 28 | } |
| 29 | |
| 30 | if(empty($roles) && empty($wp_users)) return false; |
| 31 | |
| 32 | // returning true to make users and user role combined rule OR instead of AND |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | if ('logout' == $who_can_access) { |
| 37 | |
| 38 | if (is_user_logged_in()) return true; |
| 39 | } |
| 40 | |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @param $protection_rule |
| 46 | * @param bool $is_redirect set to true if this is a redirect check and not post content check. |
| 47 | * |
| 48 | * @return bool |
| 49 | */ |
| 50 | public static function content_match($protection_rule, $is_redirect = false) |
| 51 | { |
| 52 | $content_match = false; |
| 53 | |
| 54 | if (empty($protection_rule)) return $content_match; |
| 55 | |
| 56 | // All Groups Must Return True. Break if any is false and set $loadable to false. |
| 57 | foreach ($protection_rule as $group => $conditions) { |
| 58 | |
| 59 | // Groups are false until a condition proves true. |
| 60 | $group_check = false; |
| 61 | |
| 62 | // At least one group condition must be true. Break this loop if any condition is true. |
| 63 | foreach ($conditions as $condition) { |
| 64 | |
| 65 | $match = self::check_condition($condition['condition'], ppress_var($condition, 'value', []), $is_redirect); |
| 66 | |
| 67 | // If any condition passes, set $group_check true and break. |
| 68 | if ($match) { |
| 69 | $group_check = true; |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // If any group of conditions doesn't pass, popup is not loadable. |
| 75 | if ( ! $group_check) { |
| 76 | $content_match = false; |
| 77 | break; |
| 78 | } else { |
| 79 | $content_match = true; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return $content_match; |
| 84 | |
| 85 | } |
| 86 | |
| 87 | public static function check_condition($condition_id, $rule_saved_value, $is_redirect = false) |
| 88 | { |
| 89 | $condition = ContentConditions::get_instance()->get_condition($condition_id); |
| 90 | |
| 91 | if ( ! $condition) return false; |
| 92 | |
| 93 | return call_user_func($condition['callback'], $condition_id, $rule_saved_value, $is_redirect); |
| 94 | } |
| 95 | } |