| 1 |
<?php |
| 2 |
/** |
| 3 |
* Condition checker |
| 4 |
* |
| 5 |
* @package |
| 6 |
*/ |
| 7 |
namespace WPFunnels\Conditions; |
| 8 |
|
| 9 |
|
| 10 |
use WPFunnels\Traits\SingletonTrait; |
| 11 |
use WPFunnels\Wpfnl_functions; |
| 12 |
class Wpfnl_Condition_Checker { |
| 13 |
|
| 14 |
use SingletonTrait; |
| 15 |
|
| 16 |
|
| 17 |
/** |
| 18 |
* Conditional node logic check |
| 19 |
* |
| 20 |
* @param $funnel_id |
| 21 |
* @param $order |
| 22 |
* @param $condition_identifier |
| 23 |
* @param $current_page_id |
| 24 |
* @param $checker |
| 25 |
* |
| 26 |
* @return bool |
| 27 |
* |
| 28 |
* @since 2.0.2 |
| 29 |
*/ |
| 30 |
public function check_condition( $funnel_id, $order = '', $condition_identifier, $current_page_id = '', $checker = 'accept' ) |
| 31 |
{ |
| 32 |
$group_conditions = get_post_meta( $funnel_id, $condition_identifier, true ); |
| 33 |
|
| 34 |
if ($group_conditions) { |
| 35 |
|
| 36 |
// Loop through group condition. |
| 37 |
foreach ($group_conditions as $group) { |
| 38 |
|
| 39 |
if (empty($group)) { |
| 40 |
continue; |
| 41 |
} |
| 42 |
|
| 43 |
$match_group = true; |
| 44 |
// Loop over rules and determine if all rules match. |
| 45 |
foreach ($group as $rule) { |
| 46 |
|
| 47 |
if (!$this->match_rule( $funnel_id ,$rule, $order, $current_page_id, $checker )) { |
| 48 |
$match_group = false; |
| 49 |
break; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
// If this group matches, show the field group. |
| 54 |
if ($match_group) { |
| 55 |
return true; |
| 56 |
}else{ |
| 57 |
return false; |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
if( $checker == 'accept' ){ |
| 63 |
return true; |
| 64 |
} |
| 65 |
// Return default. |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
/** |
| 71 |
* Check if rule is matched |
| 72 |
* |
| 73 |
* @param $rule |
| 74 |
* @param $order |
| 75 |
* @param $current_page_id |
| 76 |
* @param $checker |
| 77 |
* |
| 78 |
* @return mixed |
| 79 |
* |
| 80 |
* @since 2.0.2 |
| 81 |
*/ |
| 82 |
public function match_rule( $funnel_id , $rule, $order = '' , $current_page_id = '', $checker = 'accept' ) |
| 83 |
{ |
| 84 |
|
| 85 |
if ($rule['field'] == 'downsell') { |
| 86 |
$rule['field'] = 'upsell'; |
| 87 |
} |
| 88 |
$condition = $rule['field']; |
| 89 |
|
| 90 |
if ( false !== strpos($rule['field'] , 'optin')){ |
| 91 |
$condition = 'optin'; |
| 92 |
} |
| 93 |
|
| 94 |
$checker_function = $condition . '_condition_checker'; |
| 95 |
|
| 96 |
return self::$checker_function( $funnel_id ,$rule, $order, $current_page_id, $checker ); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
/** |
| 101 |
* Optin condition checker for conditional step |
| 102 |
* |
| 103 |
* @param Integer $funnel_id |
| 104 |
* @param Array $data |
| 105 |
* @param Object $order |
| 106 |
* @param Integer $current_page_id |
| 107 |
* @param String $checker |
| 108 |
* |
| 109 |
* @return Boolean |
| 110 |
* |
| 111 |
* @since 2.5.7 |
| 112 |
*/ |
| 113 |
public static function optin_condition_checker( $funnel_id, $rule, $data, $current_page_id, $checker = 'accept' ){ |
| 114 |
|
| 115 |
if( is_array($data) && isset($data['step_id']) ){ |
| 116 |
$step_id = str_replace('optin_','',$data['step_id'] ); |
| 117 |
$optin_steps = get_option('optin_data'); |
| 118 |
if( isset( $optin_steps['optin_data'] )){ |
| 119 |
$key = array_search($step_id, array_column($optin_steps['optin_data'], 'step_id')); |
| 120 |
if( false !== $key ){ |
| 121 |
unset($optin_steps[$key]); |
| 122 |
update_option('optin_data', $optin_steps ); |
| 123 |
return true; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
}elseif( is_array($data) && !empty($data['cta']) ){ |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
if( isset($rule['field']) ){ |
| 132 |
$step_id = str_replace('optin_','',$rule['field'] ); |
| 133 |
$optin_steps = get_option('optin_data'); |
| 134 |
if( isset( $optin_steps['optin_data'] )){ |
| 135 |
$key = array_search($step_id, array_column($optin_steps['optin_data'], 'step_id')); |
| 136 |
if( false !== $key ){ |
| 137 |
unset($optin_steps[$key]); |
| 138 |
update_option('optin_data', $optin_steps ); |
| 139 |
return true; |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
return false; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
/** |
| 149 |
* Order bump condition checker |
| 150 |
* |
| 151 |
* @param $data |
| 152 |
* @param $order |
| 153 |
* @param $current_page_id |
| 154 |
* |
| 155 |
* @return bool |
| 156 |
*/ |
| 157 |
public static function orderbump_condition_checker( $funnel_id, $data, $order, $current_page_id, $checker = 'accept' ) |
| 158 |
{ |
| 159 |
|
| 160 |
if ( Wpfnl_functions::is_wc_active() ) { |
| 161 |
$order_bump_accepted = WC()->session->get('order_bump_accepted'); |
| 162 |
WC()->session->set('order_bump_accepted', null); |
| 163 |
$cookie_name = 'wpfnl_order_bump'; |
| 164 |
$cookie = isset( $_COOKIE[$cookie_name] ) ? json_decode( wp_unslash( $_COOKIE[$cookie_name] ), true ) : array(); |
| 165 |
if( isset( $cookie['order_bump_accepted'] ) ){ |
| 166 |
$is_accepted = false; |
| 167 |
if( ( $data['value'] == $cookie['order_bump_accepted'] ) || ( 'no' === $data['value'] && '' === $cookie['order_bump_accepted'] )){ |
| 168 |
$is_accepted = true; |
| 169 |
} |
| 170 |
ob_start(); |
| 171 |
if( isset( $_COOKIE[$cookie_name] ) ){ |
| 172 |
setcookie( $cookie_name, null, strtotime( '-1 days' ), '/', COOKIE_DOMAIN ); |
| 173 |
} |
| 174 |
ob_end_flush(); |
| 175 |
|
| 176 |
return $is_accepted; |
| 177 |
}else{ |
| 178 |
if( 'no' === $data['value'] ){ |
| 179 |
return true; |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
return false; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
/** |
| 189 |
* Cart total condition checker |
| 190 |
* |
| 191 |
* @param $data |
| 192 |
* @param $order |
| 193 |
* @param $current_page_id |
| 194 |
* |
| 195 |
* @return bool |
| 196 |
*/ |
| 197 |
public static function carttotal_condition_checker( $funnel_id, $data, $order, $current_page_id, $checker = 'accept' ) |
| 198 |
{ |
| 199 |
$cart_total = $order->get_total(); |
| 200 |
|
| 201 |
$checker = false; |
| 202 |
|
| 203 |
if( !isset( $data['condition'], $data['value'] ) ){ |
| 204 |
return $checker; |
| 205 |
} |
| 206 |
|
| 207 |
if ( 'greater' === $data['condition'] ) { |
| 208 |
if ($cart_total > $data['value']) { |
| 209 |
$checker = true; |
| 210 |
} |
| 211 |
} elseif ( 'equal' === $data['condition'] ) { |
| 212 |
if ($cart_total == $data['value']) { |
| 213 |
$checker = true; |
| 214 |
} |
| 215 |
} elseif ( 'less' === $data['condition'] ) { |
| 216 |
if ($cart_total < $data['value']) { |
| 217 |
$checker = true; |
| 218 |
} |
| 219 |
} |
| 220 |
return $checker; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
public static function upsell_condition_checker($funnel_id ,$data, $order, $current_page_id, $checker) |
| 226 |
{ |
| 227 |
|
| 228 |
if( !isset( $data['value'] ) ){ |
| 229 |
return false; |
| 230 |
} |
| 231 |
|
| 232 |
if ($data['value'] == 'yes') { |
| 233 |
if ( $checker == 'accept' ) { |
| 234 |
return true; |
| 235 |
} else { |
| 236 |
return false; |
| 237 |
} |
| 238 |
|
| 239 |
} else if ($data['value'] == 'no') { |
| 240 |
if ($checker == 'reject') { |
| 241 |
return true; |
| 242 |
} else { |
| 243 |
return false; |
| 244 |
} |
| 245 |
} |
| 246 |
return false; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
/** |
| 251 |
* Billing country condition checker |
| 252 |
* |
| 253 |
* @param Array |
| 254 |
* @param Object |
| 255 |
* @param String |
| 256 |
* @param String |
| 257 |
* |
| 258 |
* @return Bool |
| 259 |
* @since 2.4.18 |
| 260 |
*/ |
| 261 |
public static function billing_condition_checker( $funnel_id, $data, $order, $current_page_id, $checker ){ |
| 262 |
|
| 263 |
if( !$order || !isset( $data['field'], $data['value'] ) ){ |
| 264 |
return false; |
| 265 |
} |
| 266 |
|
| 267 |
$billing_country = $order->get_billing_country(); |
| 268 |
if( 'billing' !== $data['field'] || $billing_country !== $data['value'] ){ |
| 269 |
return false; |
| 270 |
} |
| 271 |
return true; |
| 272 |
|
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
/** |
| 277 |
* Shipping country condition checker |
| 278 |
* |
| 279 |
* @param Array |
| 280 |
* @param Object |
| 281 |
* @param String |
| 282 |
* @param String |
| 283 |
* |
| 284 |
* @return Bool |
| 285 |
* @since 2.4.18 |
| 286 |
*/ |
| 287 |
public static function shipping_condition_checker( $funnel_id, $data, $order, $current_page_id, $checker ){ |
| 288 |
if( !$order || !isset( $data['field'], $data['value'] ) ){ |
| 289 |
return false; |
| 290 |
} |
| 291 |
|
| 292 |
$shipping_country = $order->get_shipping_country(); |
| 293 |
if( 'shipping' !== $data['field'] || $shipping_country !== $data['value'] ){ |
| 294 |
return false; |
| 295 |
} |
| 296 |
|
| 297 |
return true; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
} |
| 305 |
|