| 1 |
<?php |
| 2 |
/** |
| 3 |
* Shopper service |
| 4 |
* |
| 5 |
* @package SeQura/WC |
| 6 |
* @subpackage SeQura/WC/Services |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SeQura\WC\Services\Shopper; |
| 10 |
|
| 11 |
/** |
| 12 |
* Handle use cases related to Shopper |
| 13 |
*/ |
| 14 |
class Shopper_Service implements Interface_Shopper_Service { |
| 15 |
|
| 16 |
private const META_KEY_DATE_OF_BIRTH = 'sequra_dob'; |
| 17 |
|
| 18 |
/** |
| 19 |
* Get customer IP |
| 20 |
*/ |
| 21 |
public function get_ip(): string { |
| 22 |
// phpcs:disable WordPressVIPMinimum.Variables.ServerVariables.UserControlledHeaders, WordPressVIPMinimum.Variables.RestrictedVariables.cache_constraints___SERVER__REMOTE_ADDR__ |
| 23 |
if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) { |
| 24 |
return \sanitize_text_field( \wp_unslash( $_SERVER['HTTP_CLIENT_IP'] ) ); |
| 25 |
} elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { |
| 26 |
return \sanitize_text_field( \wp_unslash( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ); |
| 27 |
} elseif ( ! empty( $_SERVER['REMOTE_ADDR'] ) ) { |
| 28 |
return \sanitize_text_field( \wp_unslash( $_SERVER['REMOTE_ADDR'] ) ); |
| 29 |
} |
| 30 |
// phpcs:enable WordPressVIPMinimum.Variables.ServerVariables.UserControlledHeaders, WordPressVIPMinimum.Variables.RestrictedVariables.cache_constraints___SERVER__REMOTE_ADDR__ |
| 31 |
return ''; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Get User Agent |
| 36 |
*/ |
| 37 |
public function get_user_agent(): string { |
| 38 |
// phpcs:disable WordPressVIPMinimum.Variables.RestrictedVariables.cache_constraints___SERVER__HTTP_USER_AGENT__ |
| 39 |
if ( ! empty( $_SERVER['HTTP_USER_AGENT'] ) ) { |
| 40 |
return \sanitize_text_field( \wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ); |
| 41 |
} |
| 42 |
// phpcs:enable WordPressVIPMinimum.Variables.RestrictedVariables.cache_constraints___SERVER__HTTP_USER_AGENT__ |
| 43 |
return ''; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Check if the User Agent is a bot |
| 48 |
*/ |
| 49 |
public function is_bot(): bool { |
| 50 |
return ! empty( preg_match( '/bot|crawl|slurp|spider|mediapartners|GoogleOther|Google-Safety|FeedFetcher-Google|Google-Read-Aloud|Google-Site-Verification|Google-InspectionTool/i', $this->get_user_agent() ) ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Get customer date of birth |
| 55 |
*/ |
| 56 |
public function get_date_of_birth( int $customer_id ): string { |
| 57 |
return strval( \get_user_meta( $customer_id, self::META_KEY_DATE_OF_BIRTH, true ) ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Check if the shopper is using a mobile device |
| 62 |
*/ |
| 63 |
public function is_using_mobile(): bool { |
| 64 |
// phpcs:ignore WordPressVIPMinimum.Variables.RestrictedVariables.cache_constraints___SERVER__HTTP_USER_AGENT__ |
| 65 |
$user_agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? \sanitize_text_field( $_SERVER['HTTP_USER_AGENT'] ) : ''; |
| 66 |
|
| 67 |
if ( empty( $user_agent ) ) { |
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
$regex_match = '/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|' |
| 72 |
. 'htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|' |
| 73 |
. 'blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|' |
| 74 |
. 'symbian|smartphone|mmp|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|' |
| 75 |
. 'jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220' |
| 76 |
. ')/i'; |
| 77 |
|
| 78 |
if ( preg_match( $regex_match, strtolower( $user_agent ) ) ) { |
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
$http_accept = isset( $_SERVER['HTTP_ACCEPT'] ) ? strtolower( \sanitize_text_field( $_SERVER['HTTP_ACCEPT'] ) ) : ''; |
| 83 |
|
| 84 |
if ( strpos( $http_accept, 'application/vnd.wap.xhtml+xml' ) > 0 |
| 85 |
|| isset( $_SERVER['HTTP_X_WAP_PROFILE'] ) |
| 86 |
|| isset( $_SERVER['HTTP_PROFILE'] ) ) { |
| 87 |
return true; |
| 88 |
} |
| 89 |
|
| 90 |
$mobile_ua = strtolower( substr( $user_agent, 0, 4 ) ); |
| 91 |
$mobile_agents = array( |
| 92 |
'w3c ', |
| 93 |
'acs-', |
| 94 |
'alav', |
| 95 |
'alca', |
| 96 |
'amoi', |
| 97 |
'audi', |
| 98 |
'avan', |
| 99 |
'benq', |
| 100 |
'bird', |
| 101 |
'blac', |
| 102 |
'blaz', |
| 103 |
'brew', |
| 104 |
'cell', |
| 105 |
'cldc', |
| 106 |
'cmd-', |
| 107 |
'dang', |
| 108 |
'doco', |
| 109 |
'eric', |
| 110 |
'hipt', |
| 111 |
'inno', |
| 112 |
'ipaq', |
| 113 |
'java', |
| 114 |
'jigs', |
| 115 |
'kddi', |
| 116 |
'keji', |
| 117 |
'leno', |
| 118 |
'lg-c', |
| 119 |
'lg-d', |
| 120 |
'lg-g', |
| 121 |
'lge-', |
| 122 |
'maui', |
| 123 |
'maxo', |
| 124 |
'midp', |
| 125 |
'mits', |
| 126 |
'mmef', |
| 127 |
'mobi', |
| 128 |
'mot-', |
| 129 |
'moto', |
| 130 |
'mwbp', |
| 131 |
'nec-', |
| 132 |
'newt', |
| 133 |
'noki', |
| 134 |
'oper', |
| 135 |
'palm', |
| 136 |
'pana', |
| 137 |
'pant', |
| 138 |
'phil', |
| 139 |
'play', |
| 140 |
'port', |
| 141 |
'prox', |
| 142 |
'qwap', |
| 143 |
'sage', |
| 144 |
'sams', |
| 145 |
'sany', |
| 146 |
'sch-', |
| 147 |
'sec-', |
| 148 |
'send', |
| 149 |
'seri', |
| 150 |
'sgh-', |
| 151 |
'shar', |
| 152 |
'sie-', |
| 153 |
'siem', |
| 154 |
'smal', |
| 155 |
'smar', |
| 156 |
'sony', |
| 157 |
'sph-', |
| 158 |
'symb', |
| 159 |
't-mo', |
| 160 |
'teli', |
| 161 |
'tim-', |
| 162 |
'tosh', |
| 163 |
'tsm-', |
| 164 |
'upg1', |
| 165 |
'upsi', |
| 166 |
'vk-v', |
| 167 |
'voda', |
| 168 |
'wap-', |
| 169 |
'wapa', |
| 170 |
'wapi', |
| 171 |
'wapp', |
| 172 |
'wapr', |
| 173 |
'webc', |
| 174 |
'winw', |
| 175 |
'winw', |
| 176 |
'xda ', |
| 177 |
'xda-', |
| 178 |
); |
| 179 |
|
| 180 |
if ( in_array( $mobile_ua, $mobile_agents, true ) ) { |
| 181 |
return true; |
| 182 |
} |
| 183 |
|
| 184 |
if ( isset( $_SERVER['ALL_HTTP'] ) && strpos( strtolower( \sanitize_text_field( $_SERVER['ALL_HTTP'] ) ), 'OperaMini' ) > 0 ) { |
| 185 |
return true; |
| 186 |
} |
| 187 |
|
| 188 |
return false; |
| 189 |
} |
| 190 |
} |
| 191 |
|