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