| @@ -49,8 +49,41 @@ | ||
| 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 | +/** | |
| 53 | 86 | * Sanitize array |
| 54 | 87 | * |
| 55 | 88 | * @return Array |
| 56 | 89 | */ |
| @@ -65,8 +98,71 @@ | ||
| 65 | 98 | return $array; |
| 66 | 99 | } |
| 67 | 100 | |
| 68 | 101 | /** |
| 102 | + * Upload image from URL programmatically | |
| 103 | + */ | |
| 104 | +function notifier_upload_file_by_url( $image_url ) { | |
| 105 | + | |
| 106 | + // it allows us to use download_url() and wp_handle_sideload() functions | |
| 107 | + require_once ABSPATH . 'wp-admin/includes/file.php'; | |
| 108 | + | |
| 109 | + // download to temp dir | |
| 110 | + $temp_file = download_url( $image_url ); | |
| 111 | + | |
| 112 | + if ( is_wp_error( $temp_file ) ) { | |
| 113 | + return false; | |
| 114 | + } | |
| 115 | + | |
| 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; | |
| 134 | + } | |
| 135 | + | |
| 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 | + ); | |
| 147 | + | |
| 148 | + if ( is_wp_error( $attachment_id ) || ! $attachment_id ) { | |
| 149 | + return false; | |
| 150 | + } | |
| 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 | + | |
| 162 | +} | |
| 163 | + | |
| 164 | +/** | |
| 69 | 165 | * Sanitize phone number |
| 70 | 166 | */ |
| 71 | 167 | function notifier_sanitize_phone_number( $phone_number ) { |
| 72 | 168 | return preg_replace( '/[^\d+]/', '', $phone_number ); |
| @@ -100,12 +196,12 @@ | ||
| 100 | 196 | * Generate API key. |
| 101 | 197 | */ |
| 102 | 198 | function notifier_generate_random_key( $length = 25 ) { |
| 103 | 199 | $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 104 | - $charactersLength = strlen( $characters ); | |
| 200 | + $charactersLength = strlen($characters); | |
| 105 | 201 | $randomString = ''; |
| 106 | 202 | for ( $i = 0; $i < $length; $i++ ) { |
| 107 | - $randomString .= $characters[ random_int( 0, $charactersLength - 1 ) ]; | |
| 203 | + $randomString .= $characters[rand(0, $charactersLength - 1)]; | |
| 108 | 204 | } |
| 109 | 205 | return $randomString; |
| 110 | 206 | } |
| 111 | 207 | |
| @@ -113,9 +209,9 @@ | ||
| 113 | 209 | * Converts a UTC date string to the WordPress site's timezone. |
| 114 | 210 | */ |
| 115 | 211 | function notifier_convert_date_from_utc($utc_date, $format = 'Y-m-d H:i:s') { |
| 116 | 212 | if ($utc_date === '') { |
| 117 | - return current_time( 'mysql' ); | |
| 213 | + return current_time('mysql'); | |
| 118 | 214 | } |
| 119 | 215 | |
| 120 | 216 | try { |
| 121 | 217 | $timezone_string = wp_timezone_string() ?: 'UTC'; |
| @@ -128,4 +224,23 @@ | ||
| 128 | 224 | return ''; |
| 129 | 225 | } |
| 130 | 226 | } |
| 131 | 227 | |
| 228 | +/** | |
| 229 | + * Converts a date string from the WordPress site's timezone to UTC. | |
| 230 | + */ | |
| 231 | +function notifier_convert_date_to_utc($wp_date, $format = 'Y-m-d H:i:s') { | |
| 232 | + if ($wp_date === '') { | |
| 233 | + return current_time('mysql'); | |
| 234 | + } | |
| 235 | + | |
| 236 | + try { | |
| 237 | + $timezone_string = wp_timezone_string() ?: 'UTC'; | |
| 238 | + $site_timezone = new DateTimeZone($timezone_string); | |
| 239 | + $date = new DateTime($wp_date, $site_timezone); | |
| 240 | + $date->setTimezone(new DateTimeZone('UTC')); | |
| 241 | + return $date->format($format); | |
| 242 | + } catch (Exception $e) { | |
| 243 | + error_log('Error converting date from WordPress timezone to UTC: ' . $e->getMessage()); | |
| 244 | + return ''; | |
| 245 | + } | |
| 246 | +} | |