| @@ -49,41 +49,8 @@ | ||
| 49 | 49 | register_post_type($cpt_slug, $args); |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | /** |
| 53 | - * Create new taxonomy for a post type | |
| 54 | - * | |
| 55 | - * @return null | |
| 56 | - */ | |
| 57 | -function notifier_register_taxonomy($taxonomy_slug, $taxonomy_name, $taxonomy_name_plural, $cpt_slug, $args = array()) { | |
| 58 | - $labels = array ( | |
| 59 | - 'name' => $taxonomy_name_plural, | |
| 60 | - 'singular_name' => $taxonomy_name, | |
| 61 | - 'search_items' => "Search {$taxonomy_name_plural}", | |
| 62 | - 'all_items' => "All {$taxonomy_name_plural}", | |
| 63 | - 'parent_item' => "Parent {$taxonomy_name}", | |
| 64 | - 'parent_item_colon' => "Parent {$taxonomy_name}:", | |
| 65 | - 'edit_item' => "Edit {$taxonomy_name}", | |
| 66 | - 'update_item' => "Update {$taxonomy_name}", | |
| 67 | - 'add_new_item' => "Add New {$taxonomy_name}", | |
| 68 | - 'new_item_name' => "New {$taxonomy_name} Name", | |
| 69 | - 'menu_name' => $taxonomy_name_plural, | |
| 70 | - ); | |
| 71 | - | |
| 72 | - $args = wp_parse_args( $args, array ( | |
| 73 | - 'hierarchical' => false, | |
| 74 | - 'labels' => $labels, | |
| 75 | - 'show_ui' => true, | |
| 76 | - 'show_admin_column' => true, | |
| 77 | - 'query_var' => true, | |
| 78 | - 'show_in_rest' => true, | |
| 79 | - 'rewrite' => array( 'slug' => $taxonomy_slug ) | |
| 80 | - )); | |
| 81 | - | |
| 82 | - register_taxonomy($taxonomy_slug, [ $cpt_slug ], $args); | |
| 83 | -} | |
| 84 | - | |
| 85 | -/** | |
| 86 | 53 | * Sanitize array |
| 87 | 54 | * |
| 88 | 55 | * @return Array |
| 89 | 56 | */ |
| @@ -98,72 +65,67 @@ | ||
| 98 | 65 | return $array; |
| 99 | 66 | } |
| 100 | 67 | |
| 101 | 68 | /** |
| 102 | - * Upload image from URL programmatically | |
| 69 | + * Sanitize phone number | |
| 103 | 70 | */ |
| 104 | -function notifier_upload_file_by_url( $image_url ) { | |
| 71 | +function notifier_sanitize_phone_number( $phone_number ) { | |
| 72 | + return preg_replace( '/[^\d+]/', '', $phone_number ); | |
| 73 | +} | |
| 105 | 74 | |
| 106 | - // it allows us to use download_url() and wp_handle_sideload() functions | |
| 107 | - require_once ABSPATH . 'wp-admin/includes/file.php'; | |
| 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 | +} | |
| 108 | 81 | |
| 109 | - // download to temp dir | |
| 110 | - $temp_file = download_url( $image_url ); | |
| 111 | - | |
| 112 | - if ( is_wp_error( $temp_file ) ) { | |
| 113 | - return false; | |
| 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; | |
| 114 | 88 | } |
| 115 | 89 | |
| 116 | - // move the temp file into the uploads directory | |
| 117 | - $file = array( | |
| 118 | - 'name' => basename( parse_url($image_url, PHP_URL_PATH) ), | |
| 119 | - 'type' => mime_content_type( $temp_file ), | |
| 120 | - 'tmp_name' => $temp_file, | |
| 121 | - 'size' => filesize( $temp_file ), | |
| 122 | - ); | |
| 123 | - | |
| 124 | - $sideload = wp_handle_sideload( | |
| 125 | - $file, | |
| 126 | - array( | |
| 127 | - 'test_form' => false // no needs to check 'action' parameter | |
| 128 | - ) | |
| 129 | - ); | |
| 130 | - | |
| 131 | - if ( ! empty( $sideload[ 'error' ] ) ) { | |
| 132 | - // you may return error message if you want | |
| 133 | - return false; | |
| 90 | + $defualt_code = get_option( NOTIFIER_PREFIX . 'default_country_code', '' ); | |
| 91 | + if('' == trim($defualt_code)){ | |
| 92 | + return $phone_number; | |
| 134 | 93 | } |
| 135 | 94 | |
| 136 | - // it is time to add our uploaded image into WordPress media library | |
| 137 | - $attachment_id = wp_insert_attachment( | |
| 138 | - array( | |
| 139 | - 'guid' => $sideload[ 'url' ], | |
| 140 | - 'post_mime_type' => $sideload[ 'type' ], | |
| 141 | - 'post_title' => basename( $sideload[ 'file' ] ), | |
| 142 | - 'post_content' => '', | |
| 143 | - 'post_status' => 'inherit', | |
| 144 | - ), | |
| 145 | - $sideload[ 'file' ] | |
| 146 | - ); | |
| 95 | + $phone_number = notifier_sanitize_phone_number($defualt_code) . $phone_number; | |
| 96 | + return $phone_number; | |
| 97 | +} | |
| 147 | 98 | |
| 148 | - if ( is_wp_error( $attachment_id ) || ! $attachment_id ) { | |
| 149 | - return false; | |
| 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 ) ]; | |
| 150 | 108 | } |
| 151 | - | |
| 152 | - // update medatata, regenerate image sizes | |
| 153 | - require_once ABSPATH . 'wp-admin/includes/image.php'; | |
| 154 | - | |
| 155 | - wp_update_attachment_metadata( | |
| 156 | - $attachment_id, | |
| 157 | - wp_generate_attachment_metadata( $attachment_id, $sideload[ 'file' ] ) | |
| 158 | - ); | |
| 159 | - | |
| 160 | - return $attachment_id; | |
| 161 | - | |
| 109 | + return $randomString; | |
| 162 | 110 | } |
| 163 | 111 | |
| 164 | 112 | /** |
| 165 | - * Sanitize phone number | |
| 113 | + * Converts a UTC date string to the WordPress site's timezone. | |
| 166 | 114 | */ |
| 167 | -function notifier_sanitize_phone_number( $phone_number ) { | |
| 168 | - return preg_replace( '/[^\d+]/', '', $phone_number ); | |
| 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 | + } | |
| 169 | 130 | } |
| 131 | + | |