| @@ -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,71 +65,8 @@ | ||
| 98 | 65 | return $array; |
| 99 | 66 | } |
| 100 | 67 | |
| 101 | 68 | /** |
| 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 | -/** | |
| 165 | 69 | * Sanitize phone number |
| 166 | 70 | */ |
| 167 | 71 | function notifier_sanitize_phone_number( $phone_number ) { |
| 168 | 72 | return preg_replace( '/[^\d+]/', '', $phone_number ); |
| @@ -196,12 +100,12 @@ | ||
| 196 | 100 | * Generate API key. |
| 197 | 101 | */ |
| 198 | 102 | function notifier_generate_random_key( $length = 25 ) { |
| 199 | 103 | $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 200 | - $charactersLength = strlen($characters); | |
| 104 | + $charactersLength = strlen( $characters ); | |
| 201 | 105 | $randomString = ''; |
| 202 | 106 | for ( $i = 0; $i < $length; $i++ ) { |
| 203 | - $randomString .= $characters[rand(0, $charactersLength - 1)]; | |
| 107 | + $randomString .= $characters[ random_int( 0, $charactersLength - 1 ) ]; | |
| 204 | 108 | } |
| 205 | 109 | return $randomString; |
| 206 | 110 | } |
| 207 | 111 | |
| @@ -209,9 +113,9 @@ | ||
| 209 | 113 | * Converts a UTC date string to the WordPress site's timezone. |
| 210 | 114 | */ |
| 211 | 115 | function notifier_convert_date_from_utc($utc_date, $format = 'Y-m-d H:i:s') { |
| 212 | 116 | if ($utc_date === '') { |
| 213 | - return current_time('mysql'); | |
| 117 | + return current_time( 'mysql' ); | |
| 214 | 118 | } |
| 215 | 119 | |
| 216 | 120 | try { |
| 217 | 121 | $timezone_string = wp_timezone_string() ?: 'UTC'; |
| @@ -224,23 +128,4 @@ | ||
| 224 | 128 | return ''; |
| 225 | 129 | } |
| 226 | 130 | } |
| 227 | 131 | |
| 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 | -} | |