| 1 |
<?php |
| 2 |
/** |
| 3 |
* Notifier Helper Functions |
| 4 |
* |
| 5 |
* @package Wa_Notifier |
| 6 |
*/ |
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; // Exit if accessed directly |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* Create new post type |
| 13 |
* |
| 14 |
* @return null |
| 15 |
*/ |
| 16 |
function notifier_register_post_type($cpt_slug, $cpt_name, $cpt_name_plural, $args = []) { |
| 17 |
$labels = array ( |
| 18 |
'name' => $cpt_name_plural, |
| 19 |
'singular_name' => $cpt_name, |
| 20 |
'menu_name' => $cpt_name_plural, |
| 21 |
'parent_item_colon' => "Parent {$cpt_name}", |
| 22 |
'all_items' => "All {$cpt_name_plural}", |
| 23 |
'view_item' => "View {$cpt_name}", |
| 24 |
'add_new_item' => "Add New {$cpt_name}", |
| 25 |
'add_new' => 'Add New', |
| 26 |
'edit_item' => "Edit {$cpt_name}", |
| 27 |
'update_item' => "Update {$cpt_name}", |
| 28 |
'search_items' => "Search {$cpt_name}", |
| 29 |
'not_found' => "No {$cpt_name_plural} found", |
| 30 |
'not_found_in_trash' => "No {$cpt_name_plural} found in Trash" |
| 31 |
); |
| 32 |
$args = wp_parse_args($args, array ( |
| 33 |
'label' => $cpt_slug, |
| 34 |
'description' => "{$cpt_name_plural} Post Type", |
| 35 |
'labels' => $labels, |
| 36 |
'supports' => array( 'title'), |
| 37 |
'hierarchical' => false, |
| 38 |
'public' => false, |
| 39 |
'show_ui' => true, |
| 40 |
'show_in_menu' => false, |
| 41 |
'show_in_nav_menus' => false, |
| 42 |
'show_in_admin_bar' => false, |
| 43 |
'menu_position' => 5, |
| 44 |
'can_export' => true, |
| 45 |
'has_archive' => false, |
| 46 |
'capability_type' => 'post', |
| 47 |
'show_in_rest' => true, |
| 48 |
)); |
| 49 |
register_post_type($cpt_slug, $args); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Sanitize array |
| 54 |
* |
| 55 |
* @return Array |
| 56 |
*/ |
| 57 |
function notifier_sanitize_array ( $array ) { |
| 58 |
foreach ( $array as $key => &$value ) { |
| 59 |
if ( is_array( $value ) ) { |
| 60 |
$value = notifier_sanitize_array($value); |
| 61 |
} else { |
| 62 |
$value = sanitize_text_field( $value ); |
| 63 |
} |
| 64 |
} |
| 65 |
return $array; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Sanitize phone number |
| 70 |
*/ |
| 71 |
function notifier_sanitize_phone_number( $phone_number ) { |
| 72 |
return preg_replace( '/[^\d+]/', '', $phone_number ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Validate phone number |
| 77 |
*/ |
| 78 |
function notifier_validate_phone_number( $phone_number ) { |
| 79 |
return preg_match( '/^\+[1-9]\d{8,15}$/', $phone_number ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Maybe add default country code |
| 84 |
*/ |
| 85 |
function notifier_maybe_add_default_country_code( $phone_number ) { |
| 86 |
if('+' === substr($phone_number, 0, 1) || '' == trim($phone_number)){ |
| 87 |
return $phone_number; |
| 88 |
} |
| 89 |
|
| 90 |
$defualt_code = get_option( NOTIFIER_PREFIX . 'default_country_code', '' ); |
| 91 |
if('' == trim($defualt_code)){ |
| 92 |
return $phone_number; |
| 93 |
} |
| 94 |
|
| 95 |
$phone_number = notifier_sanitize_phone_number($defualt_code) . $phone_number; |
| 96 |
return $phone_number; |
| 97 |
} |
| 98 |
|
| 99 |
/* |
| 100 |
* Generate API key. |
| 101 |
*/ |
| 102 |
function notifier_generate_random_key( $length = 25 ) { |
| 103 |
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 104 |
$charactersLength = strlen( $characters ); |
| 105 |
$randomString = ''; |
| 106 |
for ( $i = 0; $i < $length; $i++ ) { |
| 107 |
$randomString .= $characters[ random_int( 0, $charactersLength - 1 ) ]; |
| 108 |
} |
| 109 |
return $randomString; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Converts a UTC date string to the WordPress site's timezone. |
| 114 |
*/ |
| 115 |
function notifier_convert_date_from_utc($utc_date, $format = 'Y-m-d H:i:s') { |
| 116 |
if ($utc_date === '') { |
| 117 |
return current_time( 'mysql' ); |
| 118 |
} |
| 119 |
|
| 120 |
try { |
| 121 |
$timezone_string = wp_timezone_string() ?: 'UTC'; |
| 122 |
$site_timezone = new DateTimeZone($timezone_string); |
| 123 |
$date = new DateTime($utc_date, new DateTimeZone('UTC')); |
| 124 |
$date->setTimezone($site_timezone); |
| 125 |
return $date->format($format); |
| 126 |
} catch (Exception $e) { |
| 127 |
error_log('Error converting UTC date to WordPress timezone: ' . $e->getMessage()); |
| 128 |
return ''; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|