| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace JP\CC\Site; |
| 5 |
|
| 6 |
use JP\CC\Options; |
| 7 |
use JP\CC\Conditions; |
| 8 |
use JP\CC\Is; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
|
| 15 |
class Restrictions { |
| 16 |
|
| 17 |
public static $protected_posts = array(); |
| 18 |
|
| 19 |
public static function init() { |
| 20 |
add_action( 'template_redirect', array( __CLASS__, 'template_redirect' ) ); |
| 21 |
} |
| 22 |
|
| 23 |
public static function get_rules( $post_id ) { |
| 24 |
return isset( static::$protected_posts[ $post_id ] ) ? static::$protected_posts[ $post_id ] : false; |
| 25 |
} |
| 26 |
|
| 27 |
public static function template_redirect() { |
| 28 |
global $post; |
| 29 |
|
| 30 |
$restriction = static::restricted_content(); |
| 31 |
|
| 32 |
if ( ! $restriction ) { |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
// Cache the post if restricted. |
| 37 |
if ( is_singular() && ! is_archive()) { |
| 38 |
static::$protected_posts[ $post->ID ] = $restriction; |
| 39 |
} |
| 40 |
|
| 41 |
switch ( $restriction['protection_method'] ) { |
| 42 |
case 'redirect': |
| 43 |
static::redirect( $restriction ); |
| 44 |
break; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
public static function restricted_content() { |
| 49 |
|
| 50 |
$restrictions = Options::get( 'restrictions' ); |
| 51 |
|
| 52 |
$restriced_content = false; |
| 53 |
|
| 54 |
if ( ! $restrictions || empty( $restrictions ) ) { |
| 55 |
return $restriced_content; |
| 56 |
} |
| 57 |
|
| 58 |
foreach ( $restrictions as $restriction ) { |
| 59 |
if ( static::content_match( $restriction ) ) { |
| 60 |
$roles = ! empty( $restriction['roles'] ) ? $restriction['roles'] : array(); |
| 61 |
|
| 62 |
if ( Is::access_blocked( $restriction['who'], $roles, array( 'context' => 'content_restrictions' ) ) ) { |
| 63 |
$restriced_content = $restriction; |
| 64 |
} |
| 65 |
break; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
return $restriced_content; |
| 70 |
} |
| 71 |
|
| 72 |
public static function redirect( $restriction ) { |
| 73 |
|
| 74 |
if ( empty( $restriction['redirect_type'] ) ) { |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
$redirect = false; |
| 79 |
|
| 80 |
switch ( $restriction['redirect_type'] ) { |
| 81 |
case 'login': |
| 82 |
$redirect = wp_login_url( static::current_url() ); |
| 83 |
break; |
| 84 |
|
| 85 |
case 'home': |
| 86 |
$redirect = home_url(); |
| 87 |
break; |
| 88 |
|
| 89 |
case 'custom': |
| 90 |
$redirect = $restriction['redirect_url']; |
| 91 |
break; |
| 92 |
} |
| 93 |
|
| 94 |
if ( $redirect ) { |
| 95 |
wp_redirect( $redirect ); |
| 96 |
exit; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
public static function content_match( $restriction ) { |
| 101 |
|
| 102 |
$content_match = false; |
| 103 |
|
| 104 |
if ( empty( $restriction['conditions'] ) ) { |
| 105 |
return $content_match; |
| 106 |
} |
| 107 |
|
| 108 |
// All Groups Must Return True. Break if any is false and set $loadable to false. |
| 109 |
foreach ( $restriction['conditions'] as $group => $conditions ) { |
| 110 |
|
| 111 |
// Groups are false until a condition proves true. |
| 112 |
$group_check = false; |
| 113 |
|
| 114 |
// At least one group condition must be true. Break this loop if any condition is true. |
| 115 |
foreach ( $conditions as $condition ) { |
| 116 |
|
| 117 |
// The not operand value is missing, set it to false. |
| 118 |
if ( ! isset ( $condition['not_operand'] ) ) { |
| 119 |
$condition['not_operand'] = false; |
| 120 |
} |
| 121 |
|
| 122 |
$match = ( ! $condition['not_operand'] && static::check_condition( $condition ) ) || ( $condition['not_operand'] && ! static::check_condition( $condition ) ); |
| 123 |
|
| 124 |
// If any condition passes, set $group_check true and break. |
| 125 |
if ( $match ) { |
| 126 |
$group_check = true; |
| 127 |
break; |
| 128 |
} |
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
// If any group of conditions doesn't pass, popup is not loadable. |
| 133 |
if ( ! $group_check ) { |
| 134 |
$content_match = false; |
| 135 |
break; |
| 136 |
} else { |
| 137 |
$content_match = true; |
| 138 |
} |
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
return $content_match; |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* @return string |
| 148 |
*/ |
| 149 |
public static function current_url() { |
| 150 |
$protocol = ( ! empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] !== 'off' ) || $_SERVER['SERVER_PORT'] == 443 ? 'https://' : 'http://'; |
| 151 |
|
| 152 |
return $protocol . $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"]; |
| 153 |
} |
| 154 |
|
| 155 |
public static function check_condition( $settings = array() ) { |
| 156 |
$condition = Conditions::instance()->get_condition( $settings['target'] ); |
| 157 |
|
| 158 |
if ( ! $condition ) { |
| 159 |
return false; |
| 160 |
} |
| 161 |
|
| 162 |
return call_user_func( $condition['callback'], $settings ); |
| 163 |
} |
| 164 |
|
| 165 |
} |
| 166 |
|