| 1 |
<?php |
| 2 |
|
| 3 |
namespace FloatingButton\Publisher; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
class Conditions { |
| 8 |
|
| 9 |
public static function init( $result ): bool { |
| 10 |
$param = ! empty( $result->param ) ? maybe_unserialize( $result->param ) : []; |
| 11 |
$check = [ |
| 12 |
'status' => self::status( $result->status ), |
| 13 |
'mode' => self::mode( $result->mode ), |
| 14 |
]; |
| 15 |
|
| 16 |
if ( in_array( false, $check, true ) ) { |
| 17 |
return false; |
| 18 |
} |
| 19 |
|
| 20 |
return true; |
| 21 |
|
| 22 |
} |
| 23 |
|
| 24 |
private static function status( $status ): bool { |
| 25 |
return empty( $status ); |
| 26 |
} |
| 27 |
|
| 28 |
private static function mode( $mode ): bool { |
| 29 |
return empty( $mode ) || current_user_can( 'administrator' ); |
| 30 |
} |
| 31 |
|
| 32 |
private static function users( $param ): bool { |
| 33 |
|
| 34 |
if ( empty( $param['item_user'] ) || $param['item_user'] === '1' ) { |
| 35 |
return true; |
| 36 |
} |
| 37 |
|
| 38 |
if ( $param['item_user'] === '3' ) { |
| 39 |
return ! is_user_logged_in(); |
| 40 |
} |
| 41 |
|
| 42 |
if ( $param['item_user'] === '2' ) { |
| 43 |
|
| 44 |
$users = []; |
| 45 |
|
| 46 |
if ( isset( $param['user_role'] ) ) { |
| 47 |
if ( is_array( $param['user_role'] ) ) { |
| 48 |
foreach ( $param['user_role'] as $key => $value ) { |
| 49 |
if ( empty( $value ) ) { |
| 50 |
continue; |
| 51 |
} |
| 52 |
$users[] = $key; |
| 53 |
} |
| 54 |
} else { |
| 55 |
$users[] = $param['user_role']; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
if ( ! is_user_logged_in() ) { |
| 61 |
return false; |
| 62 |
} |
| 63 |
$current_user = wp_get_current_user(); |
| 64 |
|
| 65 |
$i = 0; |
| 66 |
|
| 67 |
foreach ( $current_user->roles as $value ) { |
| 68 |
if ( in_array( $value, $users, true ) ) { |
| 69 |
$i ++; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
return ! empty( $i ); |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
return true; |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
private static function browser( $param ): bool { |
| 82 |
|
| 83 |
if ( empty( $param['browsers'] ) ) { |
| 84 |
return true; |
| 85 |
} |
| 86 |
|
| 87 |
$browser = self::get_browser_name(); |
| 88 |
switch ( $browser ) { |
| 89 |
case 'Opera': |
| 90 |
$check = empty( $param['browsers']['opera'] ); |
| 91 |
break; |
| 92 |
case 'Edge': |
| 93 |
$check = empty( $param['browsers']['edge'] ); |
| 94 |
break; |
| 95 |
case 'Chrome': |
| 96 |
$check = empty( $param['browsers']['chrome'] ); |
| 97 |
break; |
| 98 |
case 'Safari': |
| 99 |
$check = empty( $param['browsers']['safari'] ); |
| 100 |
break; |
| 101 |
case 'Firefox': |
| 102 |
$check = empty( $param['browsers']['firefox'] ); |
| 103 |
break; |
| 104 |
case 'IE': |
| 105 |
$check = empty( $param['browsers']['ie'] ); |
| 106 |
break; |
| 107 |
case 'Other': |
| 108 |
$check = empty( $param['browsers']['other'] ); |
| 109 |
break; |
| 110 |
default: |
| 111 |
$check = true; |
| 112 |
} |
| 113 |
|
| 114 |
return $check; |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
private static function get_browser_name(): string { |
| 119 |
$user_agent = $_SERVER['HTTP_USER_AGENT']; |
| 120 |
if ( strpos( $user_agent, 'Opera' ) || strpos( $user_agent, 'OPR/' ) ) { |
| 121 |
return 'Opera'; |
| 122 |
} elseif ( strpos( $user_agent, 'Edg' ) ) { |
| 123 |
return 'Edge'; |
| 124 |
} elseif ( strpos( $user_agent, 'Chrome' ) ) { |
| 125 |
return 'Chrome'; |
| 126 |
} elseif ( strpos( $user_agent, 'Safari' ) ) { |
| 127 |
return 'Safari'; |
| 128 |
} elseif ( strpos( $user_agent, 'Firefox' ) ) { |
| 129 |
return 'Firefox'; |
| 130 |
} elseif ( strpos( $user_agent, 'MSIE' ) || strpos( $user_agent, 'Trident/7' ) ) { |
| 131 |
return 'IE'; |
| 132 |
} |
| 133 |
|
| 134 |
return 'Other'; |
| 135 |
} |
| 136 |
|
| 137 |
private static function language( $param ): bool { |
| 138 |
|
| 139 |
if ( empty( $param['depending_language'] ) || empty( $param['lang'] ) ) { |
| 140 |
return true; |
| 141 |
} |
| 142 |
|
| 143 |
$current_locale = get_locale(); |
| 144 |
|
| 145 |
return $current_locale === $param['lang']; |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
private static function schedule( $param ): bool { |
| 150 |
|
| 151 |
if ( empty( $param['weekday'] ) ) { |
| 152 |
return true; |
| 153 |
} |
| 154 |
|
| 155 |
$count = count( $param['weekday'] ); |
| 156 |
|
| 157 |
for ( $i = 0; $i < $count; $i ++ ) { |
| 158 |
if ( self::check_day( $param, $i ) === true && self::check_time( $param, $i ) === true && self::check_date( $param, $i ) ) { |
| 159 |
return true; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
return false; |
| 164 |
} |
| 165 |
|
| 166 |
private static function check_day( $param, $i ): bool { |
| 167 |
|
| 168 |
if ( empty( $param['weekday'][ $i ] ) || $param['weekday'][ $i ] === 'none' ) { |
| 169 |
return true; |
| 170 |
} |
| 171 |
|
| 172 |
$currentDay = date( 'N' ); |
| 173 |
|
| 174 |
return $currentDay === $param['weekday'][ $i ] || empty( $param['weekday'][ $i ] ); |
| 175 |
|
| 176 |
} |
| 177 |
|
| 178 |
private static function check_time( $param, $i ): bool { |
| 179 |
|
| 180 |
$start = (float) str_replace( ':', '.', $param['time_start'][ $i ] ); |
| 181 |
$end = (float) str_replace( ':', '.', $param['time_end'][ $i ] ); |
| 182 |
$current = (float) current_time( 'H.i' ); |
| 183 |
|
| 184 |
return $start <= $current && $current <= $end; |
| 185 |
} |
| 186 |
|
| 187 |
private static function check_date( $param, $i ): bool { |
| 188 |
|
| 189 |
if ( empty( $param['dates'][ $i ] ) ) { |
| 190 |
return true; |
| 191 |
} |
| 192 |
|
| 193 |
$current = date( 'Y-m-d' ); |
| 194 |
$start = ! empty( $param['date_start'][ $i ] ) ? $param['date_start'][ $i ] : $current; |
| 195 |
$end = ! empty( $param['date_end'][ $i ] ) ? $param['date_end'][ $i ] : $current; |
| 196 |
|
| 197 |
return $start <= $current && $current <= $end; |
| 198 |
|
| 199 |
} |
| 200 |
|
| 201 |
} |