PluginProbe
WANotifier for Forms and Actions / 2.7.7
WANotifier for Forms and Actions v2.7.7
3.1.1 3.1.0 3.0.4 2.7.10 2.7.11 2.7.12 2.7.13 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 3.0.0 3.0.1 3.0.2 3.0.3 trunk 0.1.0 0.1.1 1.0.0 1.0.1 1.0.2 All 67 releases
notifier / includes / functions / functions-notifier-helpers.php

functions-notifier-helpers.php in WANotifier for Forms and Actions 2.7.7, at includes/functions/functions-notifier-helpers.php

247 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * 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 * Sanitize array
87 *
88 * @return Array
89 */
90 function notifier_sanitize_array ( $array ) {
91 foreach ( $array as $key => &$value ) {
92 if ( is_array( $value ) ) {
93 $value = notifier_sanitize_array($value);
94 } else {
95 $value = sanitize_text_field( $value );
96 }
97 }
98 return $array;
99 }
100
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 /**
165 * Sanitize phone number
166 */
167 function notifier_sanitize_phone_number( $phone_number ) {
168 return preg_replace( '/[^\d+]/', '', $phone_number );
169 }
170
171 /**
172 * Validate phone number
173 */
174 function notifier_validate_phone_number( $phone_number ) {
175 return preg_match( '/^\+[1-9]\d{8,15}$/', $phone_number );
176 }
177
178 /**
179 * Maybe add default country code
180 */
181 function notifier_maybe_add_default_country_code( $phone_number ) {
182 if('+' === substr($phone_number, 0, 1) || '' == trim($phone_number)){
183 return $phone_number;
184 }
185
186 $defualt_code = get_option( NOTIFIER_PREFIX . 'default_country_code', '' );
187 if('' == trim($defualt_code)){
188 return $phone_number;
189 }
190
191 $phone_number = notifier_sanitize_phone_number($defualt_code) . $phone_number;
192 return $phone_number;
193 }
194
195 /*
196 * Generate API key.
197 */
198 function notifier_generate_random_key( $length = 25 ) {
199 $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
200 $charactersLength = strlen($characters);
201 $randomString = '';
202 for ( $i = 0; $i < $length; $i++ ) {
203 $randomString .= $characters[rand(0, $charactersLength - 1)];
204 }
205 return $randomString;
206 }
207
208 /**
209 * Converts a UTC date string to the WordPress site's timezone.
210 */
211 function notifier_convert_date_from_utc($utc_date, $format = 'Y-m-d H:i:s') {
212 if ($utc_date === '') {
213 return current_time('mysql');
214 }
215
216 try {
217 $timezone_string = wp_timezone_string() ?: 'UTC';
218 $site_timezone = new DateTimeZone($timezone_string);
219 $date = new DateTime($utc_date, new DateTimeZone('UTC'));
220 $date->setTimezone($site_timezone);
221 return $date->format($format);
222 } catch (Exception $e) {
223 error_log('Error converting UTC date to WordPress timezone: ' . $e->getMessage());
224 return '';
225 }
226 }
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 }
247