activities_helper.php
3 months ago
agent_helper.php
3 months ago
analytics_helper.php
4 months ago
auth_helper.php
3 months ago
blocks_helper.php
3 months ago
booking_helper.php
3 months ago
bricks_helper.php
3 months ago
bundles_helper.php
3 months ago
calendar_helper.php
3 months ago
carts_helper.php
3 months ago
connector_helper.php
3 months ago
csv_helper.php
3 months ago
customer_helper.php
3 months ago
customer_import_helper.php
3 months ago
database_helper.php
3 months ago
debug_helper.php
3 months ago
defaults_helper.php
3 months ago
elementor_helper.php
3 months ago
email_helper.php
3 months ago
encrypt_helper.php
3 months ago
events_helper.php
3 months ago
form_helper.php
3 months ago
icalendar_helper.php
3 months ago
image_helper.php
3 months ago
invoices_helper.php
3 months ago
license_helper.php
3 months ago
location_helper.php
3 months ago
marketing_systems_helper.php
3 months ago
meeting_systems_helper.php
3 months ago
menu_helper.php
3 months ago
meta_helper.php
3 months ago
migrations_helper.php
3 months ago
money_helper.php
3 months ago
notifications_helper.php
3 months ago
nps_survey_helper.php
3 months ago
order_intent_helper.php
3 months ago
orders_helper.php
3 months ago
otp_helper.php
3 months ago
pages_helper.php
3 months ago
params_helper.php
3 months ago
payments_helper.php
3 months ago
price_breakdown_helper.php
3 months ago
process_jobs_helper.php
3 months ago
processes_helper.php
3 months ago
replacer_helper.php
3 months ago
resource_helper.php
3 months ago
roles_helper.php
3 months ago
router_helper.php
3 months ago
service_helper.php
3 months ago
sessions_helper.php
3 months ago
settings_helper.php
3 months ago
short_links_systems_helper.php
3 months ago
shortcodes_helper.php
3 months ago
sms_helper.php
3 months ago
steps_helper.php
3 months ago
stripe_connect_helper.php
3 months ago
styles_helper.php
3 months ago
support_topics_helper.php
3 months ago
time_helper.php
3 months ago
timeline_helper.php
3 months ago
transaction_helper.php
3 months ago
transaction_intent_helper.php
3 months ago
util_helper.php
3 months ago
version_specific_updates_helper.php
3 months ago
whatsapp_helper.php
3 months ago
work_periods_helper.php
3 months ago
wp_datetime.php
3 months ago
wp_user_helper.php
3 months ago
util_helper.php
910 lines
| 1 | <?php |
| 2 | |
| 3 | class OsUtilHelper { |
| 4 | |
| 5 | public static function get_referrer() { |
| 6 | if ( ! empty( $_SERVER['HTTP_REFERER'] ) ) { |
| 7 | return $_SERVER['HTTP_REFERER']; |
| 8 | } else { |
| 9 | return wp_get_original_referer() || ''; |
| 10 | } |
| 11 | } |
| 12 | |
| 13 | public static function generate_missing_addon_link( $label = '' ) { |
| 14 | if ( empty( $label ) ) { |
| 15 | $label = __( 'Requires upgrade to a premium version', 'latepoint' ); |
| 16 | } |
| 17 | $html = '<a target="_blank" href="' . esc_url( LATEPOINT_UPGRADE_URL ) . '" class="os-add-box" > |
| 18 | <div class="add-box-graphic-w"><div class="add-box-plus"><i class="latepoint-icon latepoint-icon-plus4"></i></div></div> |
| 19 | <div class="add-box-label">' . esc_html( $label ) . '</div> |
| 20 | </a>'; |
| 21 | $html = apply_filters( 'latepoint_missing_addon_link', $html, $label ); |
| 22 | return $html; |
| 23 | } |
| 24 | |
| 25 | public static function extract_plugin_name_from_path( string $plugin_path ): string { |
| 26 | preg_match( '/latepoint-([^-\/]+(?:-[^-\/]+)*)\//', $plugin_path, $matches ); |
| 27 | if ( isset( $matches[1] ) ) { |
| 28 | // Convert hyphenated words to capitalized words |
| 29 | $name = ucwords( str_replace( '-', ' ', $matches[1] ) ); |
| 30 | } else { |
| 31 | $name = 'n/a'; |
| 32 | } |
| 33 | return $name; |
| 34 | } |
| 35 | |
| 36 | public static function first_value_if_array( $value ) { |
| 37 | if ( ! empty( $value ) && is_array( $value ) && isset( $value[0] ) ) { |
| 38 | return $value[0]; |
| 39 | } else { |
| 40 | return $value; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | public static function get_array_of_ids_from_array_of_models( array $models ): array { |
| 45 | $ids = []; |
| 46 | if ( $models ) { |
| 47 | foreach ( $models as $model ) { |
| 48 | if ( property_exists( $model, 'id' ) ) { |
| 49 | $ids[] = $model->id; |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | return $ids; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Cleans array of ids and makes sure they are integers |
| 58 | * |
| 59 | * @param array $ids |
| 60 | * @return array |
| 61 | */ |
| 62 | public static function clean_numeric_ids( array $ids ): array { |
| 63 | $clean_ids = []; |
| 64 | if ( ! $ids ) { |
| 65 | return $clean_ids; |
| 66 | } |
| 67 | foreach ( $ids as $id ) { |
| 68 | if ( filter_var( $id, FILTER_VALIDATE_INT ) ) { |
| 69 | $clean_ids[] = $id; |
| 70 | } |
| 71 | } |
| 72 | return $clean_ids; |
| 73 | } |
| 74 | |
| 75 | public static function explode_and_trim( $string ) { |
| 76 | return preg_split( '/(\s*,*\s*)*,+(\s*,*\s*)*/', $string, -1, PREG_SPLIT_NO_EMPTY ); |
| 77 | } |
| 78 | |
| 79 | public static function replace_single_curly_with_double( string $string ): string { |
| 80 | $string = preg_replace( '/(?<!{){(?!{)/', '{{', $string ); |
| 81 | $string = preg_replace( '/(?<!})}(?!})/', '}}', $string ); |
| 82 | return $string; |
| 83 | } |
| 84 | |
| 85 | public static function compare_model_data_vars( $new_data_vars, $old_data_vars, $parent = false ) { |
| 86 | $level = $parent ? [ $parent ] : []; |
| 87 | $changes = []; |
| 88 | foreach ( $new_data_vars as $key => $var ) { |
| 89 | // check if both empty (could be null and '', that doesn't mean its changed) |
| 90 | if ( empty( $old_data_vars[ $key ] ) && empty( $var ) ) { |
| 91 | continue; |
| 92 | } |
| 93 | if ( ! isset( $old_data_vars[ $key ] ) || $var != $old_data_vars[ $key ] ) { |
| 94 | $changes[ $key ] = [ |
| 95 | 'before' => $old_data_vars[ $key ] ?? '', |
| 96 | 'after' => $var, |
| 97 | ]; |
| 98 | } |
| 99 | } |
| 100 | foreach ( $old_data_vars as $key => $var ) { |
| 101 | // check if both empty (could be null and '', that doesn't mean its changed) |
| 102 | if ( empty( $new_data_vars[ $key ] ) && empty( $var ) ) { |
| 103 | continue; |
| 104 | } |
| 105 | if ( ! isset( $changes[ $key ] ) && ! isset( $new_data_vars[ $key ] ) || $var != $new_data_vars[ $key ] ) { |
| 106 | $changes[ $key ] = [ |
| 107 | 'before' => $var ?? '', |
| 108 | 'after' => $new_data_vars[ $key ] ?? '', |
| 109 | ]; |
| 110 | } |
| 111 | } |
| 112 | return $changes; |
| 113 | } |
| 114 | |
| 115 | public static function ordered_column_html( $order_by, $column ) { |
| 116 | if ( $order_by['column'] == $column ) { |
| 117 | return ' class="ordered-' . $order_by['direction'] . '"'; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | public static function generate_uuid() { |
| 122 | $data = openssl_random_pseudo_bytes( 16 ); |
| 123 | |
| 124 | $data[6] = chr( ord( $data[6] ) & 0x0f | 0x40 ); // set version to 0100 |
| 125 | $data[8] = chr( ord( $data[8] ) & 0x3f | 0x80 ); // set bits 6-7 to 10 |
| 126 | |
| 127 | return vsprintf( '%s%s-%s-%s-%s-%s%s%s', str_split( bin2hex( $data ), 4 ) ); |
| 128 | } |
| 129 | |
| 130 | public static function get_site_url() { |
| 131 | $site_url = network_home_url(); |
| 132 | |
| 133 | /** |
| 134 | * Filtered site url |
| 135 | * |
| 136 | * @param {string} $site_url a site url |
| 137 | * @returns {array} $site_url a filtered site_url |
| 138 | * |
| 139 | * @since 5.2.5 |
| 140 | * @hook latepoint_get_site_url |
| 141 | * |
| 142 | */ |
| 143 | return apply_filters( 'latepoint_get_site_url', $site_url ); |
| 144 | } |
| 145 | |
| 146 | public static function is_off( $value ) { |
| 147 | return ( $value != 'on' ); |
| 148 | } |
| 149 | |
| 150 | public static function is_on( $value ) { |
| 151 | return ( $value == 'on' ); |
| 152 | } |
| 153 | |
| 154 | public static function template_variables_link_html() { |
| 155 | return '<a href="#" class="field-note-info-link open-template-variables-panel"><i class="latepoint-icon latepoint-icon-info"></i><span>' . esc_html__( 'Show Available Variables', 'latepoint' ) . '</span></a>'; |
| 156 | } |
| 157 | |
| 158 | public static function hex2rgba( $color, $opacity = false ) { |
| 159 | $default = 'rgb(0,0,0)'; |
| 160 | if ( empty( $color ) ) { |
| 161 | return $default; |
| 162 | } |
| 163 | |
| 164 | //Sanitize $color if "#" is provided |
| 165 | if ( $color[0] == '#' ) { |
| 166 | $color = substr( $color, 1 ); |
| 167 | } |
| 168 | |
| 169 | //Check if color has 6 or 3 characters and get values |
| 170 | if ( strlen( $color ) == 6 ) { |
| 171 | $hex = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] ); |
| 172 | } elseif ( strlen( $color ) == 3 ) { |
| 173 | $hex = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] ); |
| 174 | } else { |
| 175 | return $default; |
| 176 | } |
| 177 | |
| 178 | //Convert hexadec to rgb |
| 179 | $rgb = array_map( 'hexdec', $hex ); |
| 180 | |
| 181 | //Check if opacity is set(rgba or rgb) |
| 182 | if ( $opacity ) { |
| 183 | if ( abs( $opacity ) > 1 ) { |
| 184 | $opacity = 1.0; |
| 185 | } |
| 186 | $output = 'rgba(' . implode( ',', $rgb ) . ',' . $opacity . ')'; |
| 187 | } else { |
| 188 | $output = 'rgb(' . implode( ',', $rgb ) . ')'; |
| 189 | } |
| 190 | |
| 191 | //Return rgb(a) color string |
| 192 | return $output; |
| 193 | } |
| 194 | |
| 195 | public static function percent_diff( $before, $now ) { |
| 196 | if ( $before == $now ) { |
| 197 | return 0; |
| 198 | } |
| 199 | $sign = ( $before > $now ) ? '-' : '+'; |
| 200 | if ( $before > 0 && $now > 0 ) { |
| 201 | if ( $before > $now ) { |
| 202 | return $sign . round( ( $before - $now ) / $before * 100 ); |
| 203 | } else { |
| 204 | return $sign . round( ( $now - $before ) / $before * 100 ); |
| 205 | } |
| 206 | } else { |
| 207 | return $sign . '100'; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | |
| 212 | public static function get_weekday_numbers() { |
| 213 | return array( 1,2,3,4,5,6,7 ); |
| 214 | } |
| 215 | |
| 216 | public static function is_valid_email( $email ) { |
| 217 | return filter_var( $email, FILTER_VALIDATE_EMAIL ); |
| 218 | } |
| 219 | |
| 220 | public static function merge_default_atts( $defaults = [], $settings = [] ) { |
| 221 | return array_merge( $defaults, array_intersect_key( $settings, $defaults ) ); |
| 222 | } |
| 223 | |
| 224 | public static function random_text( $type = 'nozero', $length = 6 ): string { |
| 225 | switch ( $type ) { |
| 226 | case 'nozero': |
| 227 | $pool = '123456789'; |
| 228 | break; |
| 229 | case 'alnum': |
| 230 | $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 231 | break; |
| 232 | case 'alpha': |
| 233 | $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 234 | break; |
| 235 | case 'hexdec': |
| 236 | $pool = '0123456789abcdef'; |
| 237 | break; |
| 238 | case 'numeric': |
| 239 | $pool = '0123456789'; |
| 240 | break; |
| 241 | case 'distinct': |
| 242 | $pool = '2345679ACDEFHJKLMNPRSTUVWXYZ'; |
| 243 | break; |
| 244 | default: |
| 245 | $pool = (string) $type; |
| 246 | break; |
| 247 | } |
| 248 | |
| 249 | |
| 250 | $crypto_rand_secure = function ( $min, $max ) { |
| 251 | $range = $max - $min; |
| 252 | if ( $range < 0 ) { |
| 253 | return $min; // not so random... |
| 254 | } |
| 255 | $log = log( $range, 2 ); |
| 256 | $bytes = (int) ( $log / 8 ) + 1; // length in bytes |
| 257 | $bits = (int) $log + 1; // length in bits |
| 258 | $filter = (int) ( 1 << $bits ) - 1; // set all lower bits to 1 |
| 259 | do { |
| 260 | $rnd = hexdec( bin2hex( openssl_random_pseudo_bytes( $bytes ) ) ); |
| 261 | $rnd = $rnd & $filter; // discard irrelevant bits |
| 262 | } while ( $rnd >= $range ); |
| 263 | return $min + $rnd; |
| 264 | }; |
| 265 | |
| 266 | $token = ''; |
| 267 | $max = strlen( $pool ); |
| 268 | for ( $i = 0; $i < $length; $i++ ) { |
| 269 | $token .= $pool[ $crypto_rand_secure( 0, $max ) ]; |
| 270 | } |
| 271 | return $token; |
| 272 | } |
| 273 | |
| 274 | public static function ellipse_string( $string, $length ): string { |
| 275 | return mb_substr( $string, 0, $length ) . ( strlen( $string ) > $length ? '...' : '' ); |
| 276 | } |
| 277 | |
| 278 | public static function get_month_name_by_number( $month_number, $short = false ) { |
| 279 | $full_names = [ |
| 280 | __( 'January', 'latepoint' ), |
| 281 | __( 'February', 'latepoint' ), |
| 282 | __( 'March', 'latepoint' ), |
| 283 | __( 'April', 'latepoint' ), |
| 284 | __( 'May', 'latepoint' ), |
| 285 | __( 'June', 'latepoint' ), |
| 286 | __( 'July', 'latepoint' ), |
| 287 | __( 'August', 'latepoint' ), |
| 288 | __( 'September', 'latepoint' ), |
| 289 | __( 'October', 'latepoint' ), |
| 290 | __( 'November', 'latepoint' ), |
| 291 | __( 'December', 'latepoint' ), |
| 292 | ]; |
| 293 | $short_names = [ |
| 294 | __( 'Jan', 'latepoint' ), |
| 295 | __( 'Feb', 'latepoint' ), |
| 296 | __( 'Mar', 'latepoint' ), |
| 297 | __( 'Apr', 'latepoint' ), |
| 298 | _x( 'May', 'short version of May month', 'latepoint' ), |
| 299 | __( 'Jun', 'latepoint' ), |
| 300 | __( 'Jul', 'latepoint' ), |
| 301 | __( 'Aug', 'latepoint' ), |
| 302 | __( 'Sep', 'latepoint' ), |
| 303 | __( 'Oct', 'latepoint' ), |
| 304 | __( 'Nov', 'latepoint' ), |
| 305 | __( 'Dec', 'latepoint' ), |
| 306 | ]; |
| 307 | |
| 308 | if ( empty( $month_number ) ) { |
| 309 | return ''; |
| 310 | } |
| 311 | if ( $short ) { |
| 312 | $month_name = isset( $short_names[ $month_number - 1 ] ) ? $short_names[ $month_number - 1 ] : 'n/a'; |
| 313 | } else { |
| 314 | $month_name = isset( $full_names[ $month_number - 1 ] ) ? $full_names[ $month_number - 1 ] : 'n/a'; |
| 315 | } |
| 316 | return $month_name; |
| 317 | } |
| 318 | |
| 319 | public static function add_resource_link_html( string $label, string $url ): string { |
| 320 | $html = '<a class="create-resource-link-w" href="' . esc_url( $url ) . '"> |
| 321 | <div class="create-resource-link-i"> |
| 322 | <div class="add-resource-graphic-w"> |
| 323 | <div class="add-resource-plus"><i class="latepoint-icon latepoint-icon-plus4"></i></div> |
| 324 | </div> |
| 325 | <div class="add-resource-label">' . esc_html( $label ) . '</div> |
| 326 | </div> |
| 327 | </a>'; |
| 328 | return $html; |
| 329 | } |
| 330 | |
| 331 | public static function translated_months() { |
| 332 | return [ |
| 333 | 'January' => __( 'January', 'latepoint' ), |
| 334 | 'February' => __( 'February', 'latepoint' ), |
| 335 | 'March' => __( 'March', 'latepoint' ), |
| 336 | 'April' => __( 'April', 'latepoint' ), |
| 337 | 'May' => __( 'May', 'latepoint' ), |
| 338 | 'June' => __( 'June', 'latepoint' ), |
| 339 | 'July' => __( 'July', 'latepoint' ), |
| 340 | 'August' => __( 'August', 'latepoint' ), |
| 341 | 'September' => __( 'September', 'latepoint' ), |
| 342 | 'October' => __( 'October', 'latepoint' ), |
| 343 | 'November' => __( 'November', 'latepoint' ), |
| 344 | 'December' => __( 'December', 'latepoint' ), |
| 345 | 'Jan' => __( 'Jan', 'latepoint' ), |
| 346 | 'Feb' => __( 'Feb', 'latepoint' ), |
| 347 | 'Mar' => __( 'Mar', 'latepoint' ), |
| 348 | 'Apr' => __( 'Apr', 'latepoint' ), |
| 349 | 'Jun' => __( 'Jun', 'latepoint' ), |
| 350 | 'Jul' => __( 'Jul', 'latepoint' ), |
| 351 | 'Aug' => __( 'Aug', 'latepoint' ), |
| 352 | 'Sep' => __( 'Sep', 'latepoint' ), |
| 353 | 'Oct' => __( 'Oct', 'latepoint' ), |
| 354 | 'Nov' => __( 'Nov', 'latepoint' ), |
| 355 | 'Dec' => __( 'Dec', 'latepoint' ), |
| 356 | ]; |
| 357 | } |
| 358 | |
| 359 | public static function translate_months( $date_string ) { |
| 360 | return strtr( $date_string, self::translated_months() ); |
| 361 | } |
| 362 | |
| 363 | public static function get_months_for_select() { |
| 364 | $months = []; |
| 365 | for ( $i = 1; $i <= 12; $i++ ) { |
| 366 | $months[] = [ |
| 367 | 'label' => self::get_month_name_by_number( $i ), |
| 368 | 'value' => $i, |
| 369 | ]; |
| 370 | } |
| 371 | return $months; |
| 372 | } |
| 373 | |
| 374 | public static function get_weekday_name_by_number( $weekday_number, $short = false ) { |
| 375 | $weekday_names = [ |
| 376 | __( 'Monday', 'latepoint' ), |
| 377 | __( 'Tuesday', 'latepoint' ), |
| 378 | __( 'Wednesday', 'latepoint' ), |
| 379 | __( 'Thursday', 'latepoint' ), |
| 380 | __( 'Friday', 'latepoint' ), |
| 381 | __( 'Saturday', 'latepoint' ), |
| 382 | __( 'Sunday', 'latepoint' ), |
| 383 | ]; |
| 384 | $weekday_name = isset( $weekday_names[ $weekday_number - 1 ] ) ? $weekday_names[ $weekday_number - 1 ] : 'n/a'; |
| 385 | if ( $short ) { |
| 386 | $weekday_name = mb_substr( $weekday_name, 0, 3 ); |
| 387 | } |
| 388 | return $weekday_name; |
| 389 | } |
| 390 | |
| 391 | // Checks if array is associative |
| 392 | public static function is_array_a( $array ) { |
| 393 | return count( array_filter( array_keys( $array ), 'is_string' ) ) > 0; |
| 394 | } |
| 395 | |
| 396 | |
| 397 | public static function models_to_select_options( array $models, string $value_key, string $label_key ): array { |
| 398 | if ( empty( $models ) ) { |
| 399 | return []; |
| 400 | } |
| 401 | $options = []; |
| 402 | foreach ( $models as $model ) { |
| 403 | if ( method_exists( $model, $label_key ) ) { |
| 404 | $label = call_user_func_array( [ $model, $label_key ], [] ); |
| 405 | } elseif ( property_exists( $model, $label_key ) ) { |
| 406 | $label = $model->$label_key; |
| 407 | } |
| 408 | if ( method_exists( $model, $value_key ) ) { |
| 409 | $value = call_user_func_array( [ $model, $value_key ], [] ); |
| 410 | } elseif ( property_exists( $model, $value_key ) ) { |
| 411 | $value = $model->$value_key; |
| 412 | } |
| 413 | if ( isset( $label ) && isset( $value ) ) { |
| 414 | $options[] = [ |
| 415 | 'value' => $value, |
| 416 | 'label' => $label, |
| 417 | ]; |
| 418 | } |
| 419 | unset( $value ); |
| 420 | unset( $label ); |
| 421 | } |
| 422 | return $options; |
| 423 | } |
| 424 | |
| 425 | |
| 426 | public static function array_to_select_options( array $array, string $value_key, string $label_key ): array { |
| 427 | if ( empty( $array ) ) { |
| 428 | return []; |
| 429 | } |
| 430 | $options = []; |
| 431 | foreach ( $array as $item ) { |
| 432 | if ( isset( $item[ $value_key ] ) && isset( $item[ $label_key ] ) ) { |
| 433 | $options[ $item[ $value_key ] ] = $item[ $label_key ]; |
| 434 | } |
| 435 | } |
| 436 | return $options; |
| 437 | } |
| 438 | |
| 439 | public static function transform_flat_list_for_multi_select( array $flat_list ): array { |
| 440 | $result_list = []; |
| 441 | |
| 442 | foreach ( $flat_list as $key => $value ) { |
| 443 | $result_list[] = [ |
| 444 | 'value' => $key, |
| 445 | 'label' => $value, |
| 446 | ]; |
| 447 | } |
| 448 | |
| 449 | return $result_list; |
| 450 | } |
| 451 | |
| 452 | |
| 453 | /** |
| 454 | * |
| 455 | * Returns phone number in format +18888888888, strips all none plus characters and digits, prepends country code if provided |
| 456 | * |
| 457 | * @param string $number |
| 458 | * @param string $country_code |
| 459 | * @return string |
| 460 | */ |
| 461 | public static function sanitize_phone_number( string $number, string $country_code = '' ): string { |
| 462 | if ( empty( $number ) ) { |
| 463 | return ''; |
| 464 | } |
| 465 | $add_country_code = ( substr( $number, 0, 1 ) == '+' ) ? '+' : $country_code; // figure out if country code is missing |
| 466 | if ( stripos( $number, 'x' ) !== false ) { |
| 467 | $number = substr( $number, 0, stripos( $number, 'x' ) ); // strip extension |
| 468 | } |
| 469 | |
| 470 | $formatted_phone = preg_replace( '/[^\d]/', '', $number ); |
| 471 | if ( ! empty( $formatted_phone ) ) { |
| 472 | return $add_country_code . $formatted_phone; |
| 473 | } else { |
| 474 | return ''; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | public static function get_countries_list(): array { |
| 479 | return [ |
| 480 | 'us' => 'United States', |
| 481 | 'af' => 'Afghanistan', |
| 482 | 'al' => 'Albania', |
| 483 | 'dz' => 'Algeria', |
| 484 | 'as' => 'American Samoa', |
| 485 | 'ad' => 'Andorra', |
| 486 | 'ao' => 'Angola', |
| 487 | 'ai' => 'Anguilla', |
| 488 | 'ag' => 'Antigua and Barbuda', |
| 489 | 'ar' => 'Argentina', |
| 490 | 'am' => 'Armenia (Հայաստան)', |
| 491 | 'aw' => 'Aruba', |
| 492 | 'ac' => 'Ascension Island', |
| 493 | 'au' => 'Australia', |
| 494 | 'at' => 'Austria (Österreich)', |
| 495 | 'az' => 'Azerbaijan (Azərbaycan)', |
| 496 | 'bs' => 'Bahamas', |
| 497 | 'bh' => 'Bahrain (البحرين)', |
| 498 | 'bd' => 'Bangladesh (বাংলাদেশ)', |
| 499 | 'bb' => 'Barbados', |
| 500 | 'by' => 'Belarus (Беларусь)', |
| 501 | 'be' => 'Belgium (België)', |
| 502 | 'bz' => 'Belize', |
| 503 | 'bj' => 'Benin (Bénin)', |
| 504 | 'bm' => 'Bermuda', |
| 505 | 'bt' => 'Bhutan (འབྲུག)', |
| 506 | 'bo' => 'Bolivia', |
| 507 | 'ba' => 'Bosnia and Herzegovina (Босна и Херцеговина)', |
| 508 | 'bw' => 'Botswana', |
| 509 | 'br' => 'Brazil (Brasil)', |
| 510 | 'io' => 'British Indian Ocean Territory', |
| 511 | 'vg' => 'British Virgin Islands', |
| 512 | 'bn' => 'Brunei', |
| 513 | 'bg' => 'Bulgaria (България)', |
| 514 | 'bf' => 'Burkina Faso', |
| 515 | 'bi' => 'Burundi (Uburundi)', |
| 516 | 'kh' => 'Cambodia (កម្ពុជា)', |
| 517 | 'cm' => 'Cameroon (Cameroun)', |
| 518 | 'ca' => 'Canada', |
| 519 | 'cv' => 'Cape Verde (Kabu Verdi)', |
| 520 | 'bq' => 'Caribbean Netherlands', |
| 521 | 'ky' => 'Cayman Islands', |
| 522 | 'cf' => 'Central African Republic (République centrafricaine)', |
| 523 | 'td' => 'Chad (Tchad)', |
| 524 | 'cl' => 'Chile', |
| 525 | 'cn' => 'China (中国)', |
| 526 | 'cx' => 'Christmas Island', |
| 527 | 'cc' => 'Cocos (Keeling) Islands', |
| 528 | 'co' => 'Colombia', |
| 529 | 'km' => 'Comoros (جزر الق� |
| 530 | ر)', |
| 531 | 'cd' => 'Congo (DRC) (Jamhuri ya Kidemokrasia ya Kongo)', |
| 532 | 'cg' => 'Congo (Republic) (Congo-Brazzaville)', |
| 533 | 'ck' => 'Cook Islands', |
| 534 | 'cr' => 'Costa Rica', |
| 535 | 'ci' => 'Côte d’Ivoire', |
| 536 | 'hr' => 'Croatia (Hrvatska)', |
| 537 | 'cu' => 'Cuba', |
| 538 | 'cw' => 'Curaçao', |
| 539 | 'cy' => 'Cyprus (Κύπρος)', |
| 540 | 'cz' => 'Czech Republic (Česká republika)', |
| 541 | 'dk' => 'Denmark (Danmark)', |
| 542 | 'dj' => 'Djibouti', |
| 543 | 'dm' => 'Dominica', |
| 544 | 'do' => 'Dominican Republic (República Dominicana)', |
| 545 | 'ec' => 'Ecuador', |
| 546 | 'eg' => 'Egypt (� |
| 547 | صر)', |
| 548 | 'sv' => 'El Salvador', |
| 549 | 'gq' => 'Equatorial Guinea (Guinea Ecuatorial)', |
| 550 | 'er' => 'Eritrea', |
| 551 | 'ee' => 'Estonia (Eesti)', |
| 552 | 'sz' => 'Eswatini', |
| 553 | 'et' => 'Ethiopia', |
| 554 | 'fk' => 'Falkland Islands (Islas Malvinas)', |
| 555 | 'fo' => 'Faroe Islands (Føroyar)', |
| 556 | 'fj' => 'Fiji', |
| 557 | 'fi' => 'Finland (Suomi)', |
| 558 | 'fr' => 'France', |
| 559 | 'gf' => 'French Guiana (Guyane française)', |
| 560 | 'pf' => 'French Polynesia (Polynésie française)', |
| 561 | 'ga' => 'Gabon', |
| 562 | 'gm' => 'Gambia', |
| 563 | 'ge' => 'Georgia (საქართველო)', |
| 564 | 'de' => 'Germany (Deutschland)', |
| 565 | 'gh' => 'Ghana (Gaana)', |
| 566 | 'gi' => 'Gibraltar', |
| 567 | 'gr' => 'Greece (Ελλάδα)', |
| 568 | 'gl' => 'Greenland (Kalaallit Nunaat)', |
| 569 | 'gd' => 'Grenada', |
| 570 | 'gp' => 'Guadeloupe', |
| 571 | 'gu' => 'Guam', |
| 572 | 'gt' => 'Guatemala', |
| 573 | 'gg' => 'Guernsey', |
| 574 | 'gn' => 'Guinea (Guinée)', |
| 575 | 'gw' => 'Guinea-Bissau (Guiné Bissau)', |
| 576 | 'gy' => 'Guyana', |
| 577 | 'ht' => 'Haiti', |
| 578 | 'hn' => 'Honduras', |
| 579 | 'hk' => 'Hong Kong (香港)', |
| 580 | 'hu' => 'Hungary (Magyarország)', |
| 581 | 'is' => 'Iceland (Ísland)', |
| 582 | 'in' => 'India (भारत)', |
| 583 | 'id' => 'Indonesia', |
| 584 | 'ir' => 'Iran (ایران)', |
| 585 | 'iq' => 'Iraq (العراق)', |
| 586 | 'ie' => 'Ireland', |
| 587 | 'im' => 'Isle of Man', |
| 588 | 'il' => 'Israel (ישראל)', |
| 589 | 'it' => 'Italy (Italia)', |
| 590 | 'jm' => 'Jamaica', |
| 591 | 'jp' => 'Japan (日本)', |
| 592 | 'je' => 'Jersey', |
| 593 | 'jo' => 'Jordan (الأردن)', |
| 594 | 'kz' => 'Kazakhstan (Каза� |
| 595 | стан)', |
| 596 | 'ke' => 'Kenya', |
| 597 | 'ki' => 'Kiribati', |
| 598 | 'xk' => 'Kosovo', |
| 599 | 'kw' => 'Kuwait (الكويت)', |
| 600 | 'kg' => 'Kyrgyzstan (Кыргызстан)', |
| 601 | 'la' => 'Laos (ລາວ)', |
| 602 | 'lv' => 'Latvia (Latvija)', |
| 603 | 'lb' => 'Lebanon (لبنان)', |
| 604 | 'ls' => 'Lesotho', |
| 605 | 'lr' => 'Liberia', |
| 606 | 'ly' => 'Libya (ليبيا)', |
| 607 | 'li' => 'Liechtenstein', |
| 608 | 'lt' => 'Lithuania (Lietuva)', |
| 609 | 'lu' => 'Luxembourg', |
| 610 | 'mo' => 'Macau (澳門)', |
| 611 | 'mk' => 'North Macedonia (Македонија)', |
| 612 | 'mg' => 'Madagascar (Madagasikara)', |
| 613 | 'mw' => 'Malawi', |
| 614 | 'my' => 'Malaysia', |
| 615 | 'mv' => 'Maldives', |
| 616 | 'ml' => 'Mali', |
| 617 | 'mt' => 'Malta', |
| 618 | 'mh' => 'Marshall Islands', |
| 619 | 'mq' => 'Martinique', |
| 620 | 'mr' => 'Mauritania (� |
| 621 | وريتانيا)', |
| 622 | 'mu' => 'Mauritius (Moris)', |
| 623 | 'yt' => 'Mayotte', |
| 624 | 'mx' => 'Mexico (México)', |
| 625 | 'fm' => 'Micronesia', |
| 626 | 'md' => 'Moldova (Republica Moldova)', |
| 627 | 'mc' => 'Monaco', |
| 628 | 'mn' => 'Mongolia (Монгол)', |
| 629 | 'me' => 'Montenegro (Crna Gora)', |
| 630 | 'ms' => 'Montserrat', |
| 631 | 'ma' => 'Morocco (ال� |
| 632 | غرب)', |
| 633 | 'mz' => 'Mozambique (Moçambique)', |
| 634 | 'mm' => 'Myanmar (Burma) (မြန်မာ)', |
| 635 | 'na' => 'Namibia (Namibië)', |
| 636 | 'nr' => 'Nauru', |
| 637 | 'np' => 'Nepal (नेपाल)', |
| 638 | 'nl' => 'Netherlands (Nederland)', |
| 639 | 'nc' => 'New Caledonia (Nouvelle-Calédonie)', |
| 640 | 'nz' => 'New Zealand', |
| 641 | 'ni' => 'Nicaragua', |
| 642 | 'ne' => 'Niger (Nijar)', |
| 643 | 'ng' => 'Nigeria', |
| 644 | 'nu' => 'Niue', |
| 645 | 'nf' => 'Norfolk Island', |
| 646 | 'kp' => 'North Korea (조선 민주주의 인민 공화국)', |
| 647 | 'mp' => 'Northern Mariana Islands', |
| 648 | 'no' => 'Norway (Norge)', |
| 649 | 'om' => 'Oman (عُ� |
| 650 | ان)', |
| 651 | 'pk' => 'Pakistan (پاکستان)', |
| 652 | 'pw' => 'Palau', |
| 653 | 'ps' => 'Palestine (فلسطين)', |
| 654 | 'pa' => 'Panama (Panamá)', |
| 655 | 'pg' => 'Papua New Guinea', |
| 656 | 'py' => 'Paraguay', |
| 657 | 'pe' => 'Peru (Perú)', |
| 658 | 'ph' => 'Philippines', |
| 659 | 'pl' => 'Poland (Polska)', |
| 660 | 'pt' => 'Portugal', |
| 661 | 'pr' => 'Puerto Rico', |
| 662 | 'qa' => 'Qatar (قطر)', |
| 663 | 're' => 'Réunion (La Réunion)', |
| 664 | 'ro' => 'Romania (România)', |
| 665 | 'ru' => 'Russia (Россия)', |
| 666 | 'rw' => 'Rwanda', |
| 667 | 'bl' => 'Saint Barthélemy', |
| 668 | 'sh' => 'Saint Helena', |
| 669 | 'kn' => 'Saint Kitts and Nevis', |
| 670 | 'lc' => 'Saint Lucia', |
| 671 | 'mf' => 'Saint Martin (Saint-Martin (partie française))', |
| 672 | 'pm' => 'Saint Pierre and Miquelon (Saint-Pierre-et-Miquelon)', |
| 673 | 'vc' => 'Saint Vincent and the Grenadines', |
| 674 | 'ws' => 'Samoa', |
| 675 | 'sm' => 'San Marino', |
| 676 | 'st' => 'São Tomé and Príncipe (São Tomé e Príncipe)', |
| 677 | 'sa' => 'Saudi Arabia (ال� |
| 678 | � |
| 679 | لكة العربية السعودية)', |
| 680 | 'sn' => 'Senegal (Sénégal)', |
| 681 | 'rs' => 'Serbia (Србија)', |
| 682 | 'sc' => 'Seychelles', |
| 683 | 'sl' => 'Sierra Leone', |
| 684 | 'sg' => 'Singapore', |
| 685 | 'sx' => 'Sint Maarten', |
| 686 | 'sk' => 'Slovakia (Slovensko)', |
| 687 | 'si' => 'Slovenia (Slovenija)', |
| 688 | 'sb' => 'Solomon Islands', |
| 689 | 'so' => 'Somalia (Soomaaliya)', |
| 690 | 'za' => 'South Africa', |
| 691 | 'kr' => 'South Korea (대한민국)', |
| 692 | 'ss' => 'South Sudan (جنوب السودان)', |
| 693 | 'es' => 'Spain (España)', |
| 694 | 'lk' => 'Sri Lanka (ශ්රී ලංකාව)', |
| 695 | 'sd' => 'Sudan (السودان)', |
| 696 | 'sr' => 'Suriname', |
| 697 | 'sj' => 'Svalbard and Jan Mayen', |
| 698 | 'se' => 'Sweden (Sverige)', |
| 699 | 'ch' => 'Switzerland (Schweiz)', |
| 700 | 'sy' => 'Syria (سوريا)', |
| 701 | 'tw' => 'Taiwan (台灣)', |
| 702 | 'tj' => 'Tajikistan', |
| 703 | 'tz' => 'Tanzania', |
| 704 | 'th' => 'Thailand (ไทย)', |
| 705 | 'tl' => 'Timor-Leste', |
| 706 | 'tg' => 'Togo', |
| 707 | 'tk' => 'Tokelau', |
| 708 | 'to' => 'Tonga', |
| 709 | 'tt' => 'Trinidad and Tobago', |
| 710 | 'tn' => 'Tunisia (تونس)', |
| 711 | 'tr' => 'Turkey (Türkiye)', |
| 712 | 'tm' => 'Turkmenistan', |
| 713 | 'tc' => 'Turks and Caicos Islands', |
| 714 | 'tv' => 'Tuvalu', |
| 715 | 'vi' => 'U.S. Virgin Islands', |
| 716 | 'ug' => 'Uganda', |
| 717 | 'ua' => 'Ukraine (Україна)', |
| 718 | 'ae' => 'United Arab Emirates (الإ� |
| 719 | ارات العربية ال� |
| 720 | تحدة)', |
| 721 | 'gb' => 'United Kingdom', |
| 722 | 'uy' => 'Uruguay', |
| 723 | 'uz' => 'Uzbekistan (Oʻzbekiston)', |
| 724 | 'vu' => 'Vanuatu', |
| 725 | 'va' => 'Vatican City (Città del Vaticano)', |
| 726 | 've' => 'Venezuela', |
| 727 | 'vn' => 'Vietnam (Việt Nam)', |
| 728 | 'wf' => 'Wallis and Futuna (Wallis-et-Futuna)', |
| 729 | 'eh' => 'Western Sahara (الصحراء الغربية)', |
| 730 | 'ye' => 'Yemen (الي� |
| 731 | ن)', |
| 732 | 'zm' => 'Zambia', |
| 733 | 'zw' => 'Zimbabwe', |
| 734 | 'ax' => '� |
| 735 | land Islands', |
| 736 | ]; |
| 737 | } |
| 738 | |
| 739 | public static function is_date_valid( $date_string ) { |
| 740 | return (bool) strtotime( $date_string ); |
| 741 | } |
| 742 | |
| 743 | public static function build_os_params( $params = array(), string $nonce_action = '' ) { |
| 744 | if ( ! empty( $nonce_action ) ) { |
| 745 | $params['_wpnonce'] = wp_create_nonce( $nonce_action ); |
| 746 | } |
| 747 | return http_build_query( $params ); |
| 748 | } |
| 749 | |
| 750 | |
| 751 | public static function get_user_ip(): string { |
| 752 | if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) { |
| 753 | $ip = sanitize_text_field( wp_unslash( $_SERVER['HTTP_CLIENT_IP'] ) ); |
| 754 | } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { |
| 755 | $ip = sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ); |
| 756 | } else { |
| 757 | $ip = sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ?? '' ) ); |
| 758 | } |
| 759 | |
| 760 | // Sanitize the IP address |
| 761 | $ip = filter_var( $ip, FILTER_VALIDATE_IP ); |
| 762 | |
| 763 | if ( $ip === false ) { |
| 764 | // Handle invalid IP case |
| 765 | $ip = 'n/a'; |
| 766 | } |
| 767 | |
| 768 | return $ip; |
| 769 | } |
| 770 | |
| 771 | public static function create_nonce( $string = '' ) { |
| 772 | return wp_create_nonce( 'latepoint_' . $string ); |
| 773 | } |
| 774 | |
| 775 | public static function verify_nonce( $nonce, $string = '' ) { |
| 776 | return wp_verify_nonce( $nonce, 'latepoint_' . $string ); |
| 777 | } |
| 778 | |
| 779 | public static function obfuscate_license( string $license_key ): string { |
| 780 | return preg_replace_callback( |
| 781 | '/-(.*)/', |
| 782 | function ( $matches ) { |
| 783 | return '-' . str_repeat( '*', strlen( $matches[1] ) ); |
| 784 | }, |
| 785 | $license_key |
| 786 | ); |
| 787 | } |
| 788 | |
| 789 | public static function generate_form_id(): string { |
| 790 | return 'new_' . self::random_text(); |
| 791 | } |
| 792 | |
| 793 | public static function pro_feature_block( string $label = '', string $label_code = '' ): string { |
| 794 | $label = ! empty( $label ) ? $label : __( 'Requires upgrade to a premium version', 'latepoint' ); |
| 795 | $html = '<a href="' . esc_url( LATEPOINT_UPGRADE_URL ) . '" class="os-add-box" > |
| 796 | <div class="add-box-graphic-w"><div class="add-box-plus"><i class="latepoint-icon latepoint-icon-plus4"></i></div></div> |
| 797 | <div class="add-box-label">' . esc_html( $label ) . '</div> |
| 798 | </a>'; |
| 799 | /** |
| 800 | * PRO Features block |
| 801 | * |
| 802 | * @since 5.1.2 |
| 803 | * @hook latepoint_pro_feature_block_html |
| 804 | * |
| 805 | * @param {string} $html html of a block |
| 806 | * @param {string} $label label that goes inside |
| 807 | * @param {string} $label_code code used to determine where the block is shown |
| 808 | * @returns {string} The filtered html of a block |
| 809 | */ |
| 810 | return apply_filters( 'latepoint_pro_feature_block_html', $html, $label, $label_code ); |
| 811 | } |
| 812 | |
| 813 | public static function generate_key_to_manage(): string { |
| 814 | return bin2hex( random_bytes( 18 ) ); |
| 815 | } |
| 816 | |
| 817 | public static function get_color_for_variable_by_index( $index ): string { |
| 818 | $colors = self::get_colors_for_variables(); |
| 819 | return $colors[ $index ] ?? '#eee'; |
| 820 | } |
| 821 | |
| 822 | public static function get_colors_for_variables(): array { |
| 823 | $colors = [ '#b6ffc8', '#ffbbbb', '#cbc5ff', '#ffe2a3', '#ffbfe2', '#6dffe3', '#abe4ff', '#eee' ]; |
| 824 | /** |
| 825 | * Generate colors to be used for variables |
| 826 | * |
| 827 | * @since 5.1.3 |
| 828 | * @hook latepoint_get_colors_for_variables |
| 829 | * |
| 830 | * @param {array} $colors array of colors |
| 831 | * @returns {array} The filtered array of colors |
| 832 | */ |
| 833 | return apply_filters( 'latepoint_get_colors_for_variables', $colors ); |
| 834 | } |
| 835 | |
| 836 | public static function generate_css_for_clean_layout(): string { |
| 837 | $html = ''; |
| 838 | $default_css_files = [ 'latepoint-main-front' ]; |
| 839 | $css_files = apply_filters( 'latepoint_clean_layout_css_files', $default_css_files ); |
| 840 | global $wp_styles; |
| 841 | |
| 842 | foreach ( $css_files as $handle ) { |
| 843 | if ( ! isset( $wp_styles->registered[ $handle ] ) ) { |
| 844 | continue; |
| 845 | } |
| 846 | $script_url = $wp_styles->registered[ $handle ]->src; |
| 847 | $script_ver = $wp_styles->registered[ $handle ]->ver; |
| 848 | $full_script_url = $script_url . ( $script_ver ? '?ver=' . $script_ver : '' ); |
| 849 | |
| 850 | $html .= '<link rel="stylesheet" href="' . esc_url( $full_script_url ) . '" media="all"/>'; |
| 851 | if ( isset( $wp_styles->registered[ $handle ]->extra['after'] ) && ! empty( $wp_styles->registered[ $handle ]->extra['after'] ) ) { |
| 852 | $html .= "<style id='{$handle}-inline-css'>\n"; |
| 853 | if ( is_array( $wp_styles->registered[ $handle ]->extra['after'] ) ) { |
| 854 | foreach ( $wp_styles->registered[ $handle ]->extra['after'] as $inline_style ) { |
| 855 | $html .= $inline_style . "\n"; |
| 856 | } |
| 857 | } else { |
| 858 | $html .= $wp_styles->registered[ $handle ]->extra['after'] . "\n"; |
| 859 | } |
| 860 | $html .= '</style>'; |
| 861 | } |
| 862 | } |
| 863 | return $html; |
| 864 | } |
| 865 | |
| 866 | public static function generate_js_for_clean_layout(): string { |
| 867 | $html = ''; |
| 868 | $default_js_files = [ 'jquery-core', 'jquery-migrate', 'latepoint-main-front', 'latepoint-vendor-front' ]; |
| 869 | if ( OsPaymentsHelper::is_payment_processor_enabled( OsStripeConnectHelper::$processor_code ) ) { |
| 870 | $default_js_files[] = 'stripe'; |
| 871 | } |
| 872 | $js_files = apply_filters( 'latepoint_clean_layout_js_files', $default_js_files ); |
| 873 | global $wp_scripts; |
| 874 | |
| 875 | foreach ( $js_files as $handle ) { |
| 876 | if ( ! isset( $wp_scripts->registered[ $handle ] ) ) { |
| 877 | continue; |
| 878 | } |
| 879 | $script_url = $wp_scripts->registered[ $handle ]->src; |
| 880 | $script_ver = $wp_scripts->registered[ $handle ]->ver; |
| 881 | $full_script_url = $script_url . ( $script_ver ? '?ver=' . $script_ver : '' ); |
| 882 | $html .= '<script id="' . esc_attr( $handle ) . '" src="' . esc_url( $full_script_url ) . '" defer="defer"></script>'; |
| 883 | if ( isset( $wp_scripts->registered[ $handle ]->extra['data'] ) && ! empty( $wp_scripts->registered[ $handle ]->extra['data'] ) ) { |
| 884 | $html .= "<script type='text/javascript'>\n"; |
| 885 | $html .= $wp_scripts->registered[ $handle ]->extra['data'] . "\n"; |
| 886 | $html .= "</script>\n"; |
| 887 | } |
| 888 | } |
| 889 | $inline_scripts = apply_filters( 'latepoint_clean_layout_inline_scripts', [] ); |
| 890 | if ( $inline_scripts ) { |
| 891 | foreach ( $inline_scripts as $script ) { |
| 892 | $html .= '<script>' . $script . '</script>'; |
| 893 | } |
| 894 | } |
| 895 | return $html; |
| 896 | } |
| 897 | |
| 898 | public static function get_first_chars( $string, $num_letters = 1 ): string { |
| 899 | if ( empty( $string ) ) { |
| 900 | return ''; |
| 901 | } |
| 902 | |
| 903 | if ( function_exists( 'mb_substr' ) ) { |
| 904 | return mb_substr( $string, 0, $num_letters, 'UTF-8' ); |
| 905 | } |
| 906 | |
| 907 | return substr( $string, 0, $num_letters ); |
| 908 | } |
| 909 | } |
| 910 |