| 1 |
<?php |
| 2 |
namespace MasterAddons\Inc\Admin\PopupBuilder; |
| 3 |
|
| 4 |
if (!defined('ABSPATH')) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
class Popup_Conditions { |
| 9 |
|
| 10 |
private $conditions = []; |
| 11 |
|
| 12 |
public function __construct() { |
| 13 |
$this->init_conditions(); |
| 14 |
} |
| 15 |
|
| 16 |
private function init_conditions() { |
| 17 |
$this->conditions = [ |
| 18 |
'pages' => [ |
| 19 |
'label' => __('Pages', 'master-addons'), |
| 20 |
'options' => [ |
| 21 |
'all' => __('All Pages', 'master-addons'), |
| 22 |
'home' => __('Home Page', 'master-addons'), |
| 23 |
'blog' => __('Blog Page', 'master-addons'), |
| 24 |
'archive' => __('Archive Pages', 'master-addons'), |
| 25 |
'search' => __('Search Results', 'master-addons'), |
| 26 |
'404' => __('404 Page', 'master-addons'), |
| 27 |
'specific' => __('Specific Pages', 'master-addons'), |
| 28 |
] |
| 29 |
], |
| 30 |
'posts' => [ |
| 31 |
'label' => __('Posts', 'master-addons'), |
| 32 |
'options' => [ |
| 33 |
'all_posts' => __('All Posts', 'master-addons'), |
| 34 |
'specific_posts' => __('Specific Posts', 'master-addons'), |
| 35 |
'post_categories' => __('Post Categories', 'master-addons'), |
| 36 |
'post_tags' => __('Post Tags', 'master-addons'), |
| 37 |
] |
| 38 |
], |
| 39 |
'users' => [ |
| 40 |
'label' => __('User Status', 'master-addons'), |
| 41 |
'options' => [ |
| 42 |
'all_users' => __('All Users', 'master-addons'), |
| 43 |
'logged_in' => __('Logged In', 'master-addons'), |
| 44 |
'logged_out' => __('Logged Out', 'master-addons'), |
| 45 |
'first_time' => __('First Time Visitors', 'master-addons'), |
| 46 |
'returning' => __('Returning Visitors', 'master-addons'), |
| 47 |
] |
| 48 |
], |
| 49 |
'devices' => [ |
| 50 |
'label' => __('Devices', 'master-addons'), |
| 51 |
'options' => [ |
| 52 |
'all_devices' => __('All Devices', 'master-addons'), |
| 53 |
'desktop' => __('Desktop', 'master-addons'), |
| 54 |
'tablet' => __('Tablet', 'master-addons'), |
| 55 |
'mobile' => __('Mobile', 'master-addons'), |
| 56 |
] |
| 57 |
], |
| 58 |
'referrer' => [ |
| 59 |
'label' => __('Referrer', 'master-addons'), |
| 60 |
'options' => [ |
| 61 |
'all_referrers' => __('All Referrers', 'master-addons'), |
| 62 |
'search_engines' => __('Search Engines', 'master-addons'), |
| 63 |
'social_media' => __('Social Media', 'master-addons'), |
| 64 |
'direct' => __('Direct Traffic', 'master-addons'), |
| 65 |
'specific_url' => __('Specific URL', 'master-addons'), |
| 66 |
] |
| 67 |
], |
| 68 |
'time' => [ |
| 69 |
'label' => __('Time & Date', 'master-addons'), |
| 70 |
'options' => [ |
| 71 |
'always' => __('Always', 'master-addons'), |
| 72 |
'date_range' => __('Date Range', 'master-addons'), |
| 73 |
'time_range' => __('Time Range', 'master-addons'), |
| 74 |
'days_of_week' => __('Days of Week', 'master-addons'), |
| 75 |
] |
| 76 |
], |
| 77 |
'woocommerce' => [ |
| 78 |
'label' => __('WooCommerce', 'master-addons'), |
| 79 |
'options' => [ |
| 80 |
'shop' => __('Shop Page', 'master-addons'), |
| 81 |
'product' => __('Product Pages', 'master-addons'), |
| 82 |
'cart' => __('Cart Page', 'master-addons'), |
| 83 |
'checkout' => __('Checkout Page', 'master-addons'), |
| 84 |
'cart_empty' => __('Cart is Empty', 'master-addons'), |
| 85 |
'cart_has_items' => __('Cart Has Items', 'master-addons'), |
| 86 |
] |
| 87 |
] |
| 88 |
]; |
| 89 |
} |
| 90 |
|
| 91 |
public function check_conditions($popup_id) { |
| 92 |
$popup = $this->get_popup_settings($popup_id); |
| 93 |
|
| 94 |
if (!$popup || $popup['status'] !== 'active') { |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
$conditions = isset($popup['conditions']) ? $popup['conditions'] : []; |
| 99 |
|
| 100 |
// Check page conditions |
| 101 |
if (!$this->check_page_conditions($conditions)) { |
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
// Check user conditions |
| 106 |
if (!$this->check_user_conditions($conditions)) { |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
// Check device conditions |
| 111 |
if (!$this->check_device_conditions($conditions)) { |
| 112 |
return false; |
| 113 |
} |
| 114 |
|
| 115 |
// Check time conditions |
| 116 |
if (!$this->check_time_conditions($conditions)) { |
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
// Check referrer conditions |
| 121 |
if (!$this->check_referrer_conditions($conditions)) { |
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
// Check WooCommerce conditions if WooCommerce is active |
| 126 |
if (class_exists('WooCommerce')) { |
| 127 |
if (!$this->check_woocommerce_conditions($conditions)) { |
| 128 |
return false; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
// Check frequency conditions |
| 133 |
if (!$this->check_frequency_conditions($popup_id, $conditions)) { |
| 134 |
return false; |
| 135 |
} |
| 136 |
|
| 137 |
return true; |
| 138 |
} |
| 139 |
|
| 140 |
private function check_page_conditions($conditions) { |
| 141 |
if (!isset($conditions['pages']) || $conditions['pages'] === 'all') { |
| 142 |
return true; |
| 143 |
} |
| 144 |
|
| 145 |
$page_condition = $conditions['pages']; |
| 146 |
|
| 147 |
switch ($page_condition) { |
| 148 |
case 'home': |
| 149 |
return is_front_page() || is_home(); |
| 150 |
case 'blog': |
| 151 |
return is_home() || is_archive() || is_single(); |
| 152 |
case 'archive': |
| 153 |
return is_archive(); |
| 154 |
case 'search': |
| 155 |
return is_search(); |
| 156 |
case '404': |
| 157 |
return is_404(); |
| 158 |
case 'specific': |
| 159 |
if (isset($conditions['specific_pages']) && is_array($conditions['specific_pages'])) { |
| 160 |
return is_page($conditions['specific_pages']) || is_single($conditions['specific_pages']); |
| 161 |
} |
| 162 |
break; |
| 163 |
} |
| 164 |
|
| 165 |
return false; |
| 166 |
} |
| 167 |
|
| 168 |
private function check_user_conditions($conditions) { |
| 169 |
if (!isset($conditions['users']) || $conditions['users'] === 'all_users') { |
| 170 |
return true; |
| 171 |
} |
| 172 |
|
| 173 |
$user_condition = $conditions['users']; |
| 174 |
|
| 175 |
switch ($user_condition) { |
| 176 |
case 'logged_in': |
| 177 |
return is_user_logged_in(); |
| 178 |
case 'logged_out': |
| 179 |
return !is_user_logged_in(); |
| 180 |
case 'first_time': |
| 181 |
return !isset($_COOKIE['jltma_popup_visitor']); |
| 182 |
case 'returning': |
| 183 |
return isset($_COOKIE['jltma_popup_visitor']); |
| 184 |
} |
| 185 |
|
| 186 |
return false; |
| 187 |
} |
| 188 |
|
| 189 |
private function check_device_conditions($conditions) { |
| 190 |
if (!isset($conditions['devices']) || $conditions['devices'] === 'all_devices') { |
| 191 |
return true; |
| 192 |
} |
| 193 |
|
| 194 |
$device = $this->detect_device(); |
| 195 |
return $conditions['devices'] === $device; |
| 196 |
} |
| 197 |
|
| 198 |
private function check_time_conditions($conditions) { |
| 199 |
if (!isset($conditions['time']) || $conditions['time'] === 'always') { |
| 200 |
return true; |
| 201 |
} |
| 202 |
|
| 203 |
$current_time = current_time('timestamp'); |
| 204 |
|
| 205 |
switch ($conditions['time']) { |
| 206 |
case 'date_range': |
| 207 |
if (isset($conditions['start_date']) && isset($conditions['end_date'])) { |
| 208 |
$start = strtotime($conditions['start_date']); |
| 209 |
$end = strtotime($conditions['end_date']); |
| 210 |
return $current_time >= $start && $current_time <= $end; |
| 211 |
} |
| 212 |
break; |
| 213 |
|
| 214 |
case 'time_range': |
| 215 |
if (isset($conditions['start_time']) && isset($conditions['end_time'])) { |
| 216 |
$current_hour = wp_date('H:i', $current_time); |
| 217 |
return $current_hour >= $conditions['start_time'] && $current_hour <= $conditions['end_time']; |
| 218 |
} |
| 219 |
break; |
| 220 |
|
| 221 |
case 'days_of_week': |
| 222 |
if (isset($conditions['days']) && is_array($conditions['days'])) { |
| 223 |
$current_day = strtolower(wp_date('l', $current_time)); |
| 224 |
return in_array($current_day, $conditions['days']); |
| 225 |
} |
| 226 |
break; |
| 227 |
} |
| 228 |
|
| 229 |
return false; |
| 230 |
} |
| 231 |
|
| 232 |
private function check_referrer_conditions($conditions) { |
| 233 |
if (!isset($conditions['referrer']) || $conditions['referrer'] === 'all_referrers') { |
| 234 |
return true; |
| 235 |
} |
| 236 |
|
| 237 |
$referrer = isset($_SERVER['HTTP_REFERER']) ? esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) : ''; |
| 238 |
|
| 239 |
switch ($conditions['referrer']) { |
| 240 |
case 'search_engines': |
| 241 |
$search_engines = ['google.', 'bing.', 'yahoo.', 'duckduckgo.', 'baidu.']; |
| 242 |
foreach ($search_engines as $engine) { |
| 243 |
if (strpos($referrer, $engine) !== false) { |
| 244 |
return true; |
| 245 |
} |
| 246 |
} |
| 247 |
return false; |
| 248 |
|
| 249 |
case 'social_media': |
| 250 |
$social_sites = ['facebook.', 'twitter.', 'instagram.', 'linkedin.', 'pinterest.']; |
| 251 |
foreach ($social_sites as $site) { |
| 252 |
if (strpos($referrer, $site) !== false) { |
| 253 |
return true; |
| 254 |
} |
| 255 |
} |
| 256 |
return false; |
| 257 |
|
| 258 |
case 'direct': |
| 259 |
return empty($referrer); |
| 260 |
|
| 261 |
case 'specific_url': |
| 262 |
if (isset($conditions['specific_referrer'])) { |
| 263 |
return strpos($referrer, $conditions['specific_referrer']) !== false; |
| 264 |
} |
| 265 |
break; |
| 266 |
} |
| 267 |
|
| 268 |
return false; |
| 269 |
} |
| 270 |
|
| 271 |
private function check_woocommerce_conditions($conditions) { |
| 272 |
if (!isset($conditions['woocommerce']) || empty($conditions['woocommerce'])) { |
| 273 |
return true; |
| 274 |
} |
| 275 |
|
| 276 |
$woo_condition = $conditions['woocommerce']; |
| 277 |
|
| 278 |
switch ($woo_condition) { |
| 279 |
case 'shop': |
| 280 |
return is_shop(); |
| 281 |
case 'product': |
| 282 |
return is_product(); |
| 283 |
case 'cart': |
| 284 |
return is_cart(); |
| 285 |
case 'checkout': |
| 286 |
return is_checkout(); |
| 287 |
case 'cart_empty': |
| 288 |
return WC()->cart && WC()->cart->is_empty(); |
| 289 |
case 'cart_has_items': |
| 290 |
return WC()->cart && !WC()->cart->is_empty(); |
| 291 |
} |
| 292 |
|
| 293 |
return false; |
| 294 |
} |
| 295 |
|
| 296 |
private function check_frequency_conditions($popup_id, $conditions) { |
| 297 |
if (!isset($conditions['show_frequency'])) { |
| 298 |
return true; |
| 299 |
} |
| 300 |
|
| 301 |
$frequency = $conditions['show_frequency']; |
| 302 |
$cookie_name = 'jltma_popup_shown_' . $popup_id; |
| 303 |
|
| 304 |
switch ($frequency) { |
| 305 |
case 'always': |
| 306 |
return true; |
| 307 |
|
| 308 |
case 'once_session': |
| 309 |
return !isset($_COOKIE[$cookie_name]); |
| 310 |
|
| 311 |
case 'once_day': |
| 312 |
if (isset($_COOKIE[$cookie_name])) { |
| 313 |
$last_shown = absint( sanitize_text_field( wp_unslash( $_COOKIE[$cookie_name] ) ) ); |
| 314 |
return (time() - $last_shown) > DAY_IN_SECONDS; |
| 315 |
} |
| 316 |
return true; |
| 317 |
|
| 318 |
case 'once_week': |
| 319 |
if (isset($_COOKIE[$cookie_name])) { |
| 320 |
$last_shown = absint( sanitize_text_field( wp_unslash( $_COOKIE[$cookie_name] ) ) ); |
| 321 |
return (time() - $last_shown) > WEEK_IN_SECONDS; |
| 322 |
} |
| 323 |
return true; |
| 324 |
|
| 325 |
case 'once': |
| 326 |
return !isset($_COOKIE[$cookie_name]); |
| 327 |
} |
| 328 |
|
| 329 |
return true; |
| 330 |
} |
| 331 |
|
| 332 |
private function detect_device() { |
| 333 |
$user_agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : ''; |
| 334 |
|
| 335 |
if (preg_match('/(tablet|ipad|playbook)|(android(?!.*(mobi|opera mini)))/i', $user_agent)) { |
| 336 |
return 'tablet'; |
| 337 |
} |
| 338 |
|
| 339 |
if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android|iemobile)/i', $user_agent)) { |
| 340 |
return 'mobile'; |
| 341 |
} |
| 342 |
|
| 343 |
return 'desktop'; |
| 344 |
} |
| 345 |
|
| 346 |
private function get_popup_settings($popup_id) { |
| 347 |
// Get popup post |
| 348 |
$popup_post = get_post($popup_id); |
| 349 |
|
| 350 |
if (!$popup_post || $popup_post->post_type !== 'jltma_popup') { |
| 351 |
return false; |
| 352 |
} |
| 353 |
|
| 354 |
// Get settings from post meta |
| 355 |
$settings = get_post_meta($popup_id, '_jltma_popup_settings', true); |
| 356 |
$conditions = get_post_meta($popup_id, '_jltma_popup_conditions', true); |
| 357 |
$status = get_post_meta($popup_id, '_jltma_popup_status', true) ?: ($popup_post->post_status === 'publish' ? 'active' : 'inactive'); |
| 358 |
|
| 359 |
// Decode JSON if needed |
| 360 |
if (is_string($settings)) { |
| 361 |
$settings = json_decode($settings, true); |
| 362 |
} |
| 363 |
if (is_string($conditions)) { |
| 364 |
$conditions = json_decode($conditions, true); |
| 365 |
} |
| 366 |
|
| 367 |
$popup = [ |
| 368 |
'id' => $popup_id, |
| 369 |
'name' => $popup_post->post_title, |
| 370 |
'status' => $status, |
| 371 |
'settings' => $settings ?: [], |
| 372 |
'conditions' => $conditions ?: [] |
| 373 |
]; |
| 374 |
|
| 375 |
return $popup; |
| 376 |
} |
| 377 |
|
| 378 |
public function get_all_conditions() { |
| 379 |
return $this->conditions; |
| 380 |
} |
| 381 |
} |