PluginProbe ʕ •ᴥ•ʔ
Appointment Booking Plugin – LatePoint | Calendar & Scheduling for WordPress / 5.2.4
Appointment Booking Plugin – LatePoint | Calendar & Scheduling for WordPress v5.2.4
5.6.8 5.6.7 5.6.6 5.6.5 5.6.4 5.6.3 5.6.2 5.6.1 5.6.0 5.5.2 5.5.1 5.5.0 5.4.2 trunk 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.1.8 5.1.9 5.1.91 5.1.92 5.1.93 5.1.94 5.2.0 5.2.1 5.2.10 5.2.11 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 5.2.9 5.3.0 5.3.1 5.3.2 5.4.0 5.4.1
latepoint / lib / helpers / whatsapp_helper.php
latepoint / lib / helpers Last commit date
activities_helper.php 1 year ago agent_helper.php 1 year ago auth_helper.php 9 months ago blocks_helper.php 1 year ago booking_helper.php 1 year ago bricks_helper.php 1 year ago bundles_helper.php 1 year ago calendar_helper.php 9 months ago carts_helper.php 1 year ago connector_helper.php 9 months ago csv_helper.php 9 months ago customer_helper.php 9 months ago customer_import_helper.php 9 months ago database_helper.php 9 months ago debug_helper.php 1 year ago defaults_helper.php 1 year ago elementor_helper.php 1 year ago email_helper.php 9 months ago encrypt_helper.php 1 year ago events_helper.php 9 months ago form_helper.php 9 months ago icalendar_helper.php 1 year ago image_helper.php 1 year ago invoices_helper.php 9 months ago license_helper.php 1 year ago location_helper.php 1 year ago marketing_systems_helper.php 1 year ago meeting_systems_helper.php 1 year ago menu_helper.php 9 months ago meta_helper.php 1 year ago migrations_helper.php 1 year ago money_helper.php 1 year ago notifications_helper.php 9 months ago order_intent_helper.php 9 months ago orders_helper.php 1 year ago otp_helper.php 9 months ago pages_helper.php 1 year ago params_helper.php 1 year ago payments_helper.php 1 year ago price_breakdown_helper.php 1 year ago process_jobs_helper.php 1 year ago processes_helper.php 1 year ago replacer_helper.php 1 year ago resource_helper.php 1 year ago roles_helper.php 9 months ago router_helper.php 1 year ago service_helper.php 1 year ago sessions_helper.php 1 year ago settings_helper.php 9 months ago short_links_systems_helper.php 9 months ago shortcodes_helper.php 9 months ago sms_helper.php 1 year ago steps_helper.php 9 months ago stripe_connect_helper.php 9 months ago styles_helper.php 9 months ago support_topics_helper.php 1 year ago time_helper.php 9 months ago timeline_helper.php 9 months ago transaction_helper.php 1 year ago transaction_intent_helper.php 1 year ago util_helper.php 9 months ago version_specific_updates_helper.php 9 months ago whatsapp_helper.php 1 year ago work_periods_helper.php 1 year ago wp_datetime.php 1 year ago wp_user_helper.php 1 year ago
whatsapp_helper.php
341 lines
1 <?php
2
3 class OsWhatsappHelper {
4
5 static array $templates;
6
7 public static function get_buttons_component_possible_variable_holders( array $template ): array {
8 $holders = [];
9 foreach ( $template['components'] as $component ) {
10 if ( $component['type'] == 'BUTTONS' ) {
11 $buttons = $component['buttons'];
12 foreach ( $buttons as $button ) {
13 switch ( $button['type'] ) {
14 case 'URL':
15 $holders[] = $button['text'];
16 $holders[] = $button['url'];
17 break;
18
19 case 'PHONE_NUMBER':
20 $holders[] = $button['text'];
21 $holders[] = $button['phone_number'];
22 break;
23 }
24 }
25 }
26 }
27
28 return $holders;
29
30 }
31
32 public static function get_template_component_value_by_key( array $template, string $component_type, string $component_key ) {
33 foreach ( $template['components'] as $component ) {
34 if ( $component['type'] == $component_type ) {
35 return $component[ $component_key ];
36 }
37 }
38
39 return null;
40 }
41
42 /**
43 * @return array
44 */
45 public static function get_template( string $template_id ): array {
46 try {
47 $templates = self::get_templates();
48 foreach ( $templates as $template ) {
49 if ( $template['id'] == $template_id ) {
50 return $template;
51 }
52 }
53 } catch ( Exception $e ) {
54 return [];
55 }
56
57 return [];
58 }
59
60 public static function get_template_preview( string $template_id, \LatePoint\Misc\ProcessAction $action ): string {
61 $html = '';
62 $selected_template = \OsWhatsappHelper::get_template( $template_id );
63 $html .= \OsFormHelper::hidden_field( 'process[actions][' . $action->id . '][settings][template_language]', $selected_template['language'] );
64 $html .= \OsFormHelper::hidden_field( 'process[actions][' . $action->id . '][settings][template_parameter_format]', $selected_template['parameter_format'] );
65 $html .= \OsFormHelper::hidden_field( 'process[actions][' . $action->id . '][settings][template_category]', $selected_template['category'] );
66 $html .= \OsFormHelper::hidden_field( 'process[actions][' . $action->id . '][settings][template_name]', $selected_template['name'] );
67
68 $variables_by_type['header'] = \OsWhatsappHelper::extract_variables_from_template( \OsWhatsappHelper::get_template_component_value_by_key( $selected_template, 'HEADER', 'text' ) );
69 $variables_by_type['body'] = \OsWhatsappHelper::extract_variables_from_template( \OsWhatsappHelper::get_template_component_value_by_key( $selected_template, 'BODY', 'text' ) );
70 $variables_by_type['buttons'] = \OsWhatsappHelper::extract_variables_from_button_holders( \OsWhatsappHelper::get_buttons_component_possible_variable_holders( $selected_template ) );
71
72 $html .= '<div class="latepoint-whatsapp-template-preview-wrapper">';
73 $html .= '<div class="latepoint-whatsapp-template-preview-content-wrapper">';
74 $html .= '<div class="latepoint-whatsapp-template-preview-heading">' . __( 'Message Preview', 'latepoint' ) . '</div>';;
75 $html .= '<div class="latepoint-whatsapp-template-preview-messages">';
76 if ( $selected_template ) {
77 $html .= '<div class="latepoint-whatsapp-template-preview-message">';
78 switch ( esc_html( \OsWhatsappHelper::get_template_component_value_by_key( $selected_template, 'HEADER', 'format' ) ) ) {
79 case 'TEXT':
80 $html .= '<div class="latepoint-whatsapp-template-preview-message-header">' . \OsWhatsappHelper::colorize_variables( esc_html( \OsWhatsappHelper::get_template_component_value_by_key( $selected_template, 'HEADER', 'text' ) ) ) . '</div>';
81 break;
82 case 'IMAGE':
83 break;
84 case 'VIDEO':
85 break;
86 case 'DOCUMENT':
87 break;
88 case 'LOCATION':
89 break;
90 }
91 $html .= '<div class="latepoint-whatsapp-template-preview-message-body">' . \OsWhatsappHelper::colorize_variables( esc_html( \OsWhatsappHelper::get_template_component_value_by_key( $selected_template, 'BODY', 'text' ) ), count( $variables_by_type['header'] ) ) . '</div>';
92 $buttons = \OsWhatsappHelper::get_template_component_value_by_key( $selected_template, 'BUTTONS', 'buttons' );
93 if ( $buttons ) {
94 $html .= '<div class="latepoint-whatsapp-template-preview-message-buttons">';
95 $start_index = count( $variables_by_type['header'] ) + count( $variables_by_type['body'] );
96 foreach ( $buttons as $button ) {
97 $html .= '<div class="latepoint-whatsapp-template-preview-message-button latepoint-whatsapp-button-type-' . esc_attr( $button['type'] ) . '">';
98 switch ( $button['type'] ) {
99 case 'PHONE_NUMBER':
100 $html .= '<i class="latepoint-icon latepoint-icon-phone"></i>' . esc_html( $button['text'] ) . '<div class="latepoint-whatsapp-button-action-value">' . \OsWhatsappHelper::colorize_variables( $button['phone_number'], $start_index ) . '</div>';
101 break;
102 case 'URL':
103 $html .= '<i class="latepoint-icon latepoint-icon-external-link"></i>' . esc_html( $button['text'] ) . '<div class="latepoint-whatsapp-button-action-value">' . \OsWhatsappHelper::colorize_variables( $button['url'], $start_index ) . '</div>';
104 break;
105 }
106 $html .= '</div>';
107 }
108 $html .= '</div>';
109 }
110 $html .= '</div>';
111 }
112 $html .= '</div>';
113 $html .= '</div>';
114 if ( ! empty( $variables_by_type['header'] ) || ! empty( $variables_by_type['body'] ) || ! empty( $variables_by_type['buttons'] ) ) {
115
116 $html .= '<div class="latepoint-whatsapp-template-preview-variables-wrapper parameter-format-'.esc_attr(strtolower($selected_template['parameter_format'])).'">';
117 $html .= '<div class="latepoint-whatsapp-template-preview-heading">';
118 $html .= '<div>' . __( 'Assign Variables', 'latepoint' ) . '</div>';
119 $html .= '</div>';
120 $html .= '<div class="latepoint-whatsapp-template-preview-variables-inner">';
121 $smart_variables_link = '<a href="#" class="open-template-variables-panel">' . esc_html__( 'Click here', 'latepoint' ) . '</a>';
122 $html .= '<div class="latepoint-whatsapp-note">' . sprintf( __( 'You have to assign values for each variable that is used in this template. %s to show smart variables that you can use.' ), $smart_variables_link ) . '</div>';
123 $color_index = 0;
124 foreach ( $variables_by_type as $variable_type => $variables ) {
125 if ( ! empty( $variables ) ) {
126 $html .= '<div class="latepoint-whatsapp-variables-header"><div>' . $variable_type . '</div><div class="latepoint-whatsapp-header-line"></div></div>';
127 foreach ( $variables as $variable ) {
128 $html .= '<div class="latepoint-whatsapp-variable-value"><div style="background-color: ' . \OsUtilHelper::get_color_for_variable_by_index( $color_index ) . '">' . $variable . '</div><div>' . \OsFormHelper::text_field( 'process[actions][' . $action->id . '][settings][variables][' . $variable_type . '][' . $variable . ']', false, $action->settings['variables'][ $variable_type ][ $variable ] ?? '', [
129 'theme' => 'simple',
130 'class' => 'size-small',
131 'placeholder' => sprintf( __( 'Enter value for %s', 'latepoint' ), $variable )
132 ] ) . '</div></div>';
133 $color_index ++;
134 }
135 }
136 }
137 $html .= '</div>';
138 $html .= '</div>';
139 }
140 $html .= '</div>';
141
142 return $html;
143 }
144
145 public static function get_templates_list( bool $force_reload = false ): array {
146 try {
147 $templates = self::get_templates( $force_reload );
148 $templates_list = [];
149 foreach ( $templates as $template ) {
150 $name = $template['name'] . '-' . $template['language'] . ' [' . $template['status'] . ']';
151 $templates_list[] = [ 'value' => $template['id'], 'label' => $name ];
152 }
153
154 return $templates_list;
155 } catch ( Exception $e ) {
156 return [ 'error' => $e->getMessage() ];
157 }
158 }
159
160 /**
161 * @return array
162 * @throws Exception
163 */
164 public static function get_templates( bool $force_reload = false ): array {
165 if ( isset( self::$templates ) && ! $force_reload ) {
166 return self::$templates;
167 }
168 try {
169 return apply_filters( 'latepoint_get_whatsapp_templates', [] );
170 } catch ( Exception $e ) {
171 return [];
172 }
173 }
174
175
176 /**
177 * @param $to
178 * @param $content
179 *
180 * @return array [
181 * 'status' => string,
182 * 'message' => string,
183 * 'to' => string,
184 * 'content' => string,
185 * 'processor_code' => string,
186 * 'processor_name' => string,
187 * 'processed_datetime' => string,
188 * 'extra_data' => array
189 * ]
190 */
191 public static function send_whatsapp( string $to, array $data, array $activity_data = [] ): array {
192 $result = [
193 'status' => LATEPOINT_STATUS_ERROR,
194 'message' => __( 'No WhatsApp processor is selected.', 'latepoint' ),
195 'to' => $to,
196 'data' => $data,
197 'processor_code' => '',
198 'processor_name' => '',
199 'processed_datetime' => '',
200 'extra_data' => [
201 'activity_data' => $activity_data
202 ],
203 'errors' => []
204 ];
205
206 if ( OsSettingsHelper::is_whatsapp_allowed() && OsNotificationsHelper::is_notification_type_enabled( 'whatsapp' ) ) {
207 /**
208 * Result of sending an WhatsApp message to a recipient's phone number
209 *
210 * @param {array} $result The array of data describing the send operation
211 * @param {string} $to The recipient's phone number
212 * @param {array} $data The data array holding template message information and variables
213 * @param {array} $activity_data The data array with information about process activity
214 *
215 * @since 5.1.3
216 * @hook latepoint_notifications_send_whatsapp
217 * @returns {array} The array of descriptive data, possibly transformed by hooked WhatsApp processor(s)
218 */
219 $result = apply_filters( 'latepoint_notifications_send_whatsapp', $result, $to, $data, $activity_data );
220 } else {
221 $result['message'] = __( 'WhatsApp notifications are disabled', 'latepoint' );
222 $result['errors'][] = __( 'WhatsApp notifications are disabled', 'latepoint' );
223 }
224
225 self::log_whatsapp( $result );
226
227 return $result;
228 }
229
230 /**
231 * @param $enabled_only
232 *
233 * @return array [
234 * 'code' => [
235 * 'code' => string,
236 * 'label' => string,
237 * 'image_url' => string
238 * ]
239 * ]
240 */
241 public static function get_whatsapp_processors( $enabled_only = false ) {
242 $whatsapp_processors = [];
243
244 /**
245 * Get the list of WhatsApp processors registered in the LatePoint ecosystem
246 *
247 * @param {array} $whatsapp_processors The list of WhatsApp processors being filtered
248 * @param {bool} $enabled_only True when filtering only enabled WhatsApp processors, false otherwise
249 * @returns {array} The filtered list of WhatsApp processors
250 *
251 * @since 5.1.3
252 * @hook latepoint_whatsapp_processors
253 *
254 */
255 return apply_filters( 'latepoint_whatsapp_processors', $whatsapp_processors, $enabled_only );
256 }
257
258 public static function is_whatsapp_processor_enabled( string $whatsapp_processor_code ): bool {
259 return ( OsNotificationsHelper::get_selected_processor_code_by_type( 'whatsapp' ) == $whatsapp_processor_code );
260 }
261
262 /**
263 * @param array $result
264 *
265 * @return OsActivityModel
266 */
267 public static function log_whatsapp( array $result ) {
268 if ( empty( $result['processed_datetime'] ) ) {
269 $result['processed_datetime'] = OsTimeHelper::now_datetime_in_db_format();
270 }
271 $data = [
272 'code' => 'whatsapp_sent',
273 'description' => wp_json_encode( $result )
274 ];
275 if ( ! empty( $result['extra_data']['activity_data'] ) ) {
276 $data = array_merge( $data, $result['extra_data']['activity_data'] );
277 }
278 $activity = OsActivitiesHelper::create_activity( $data );
279
280 return $activity;
281 }
282
283 public static function colorize_variables( string $text, int $starting_index = 0 ): string {
284 $colors = OsUtilHelper::get_colors_for_variables();
285
286 // Create a map to store color assignments for each variable number
287 $variableColors = [];
288
289 return preg_replace_callback( '/\{\{([a-zA-Z0-9_]+)\}\}/', function ( $match ) use ( $colors, &$variableColors, $starting_index ) {
290 $varNumber = $match[1];
291
292 // If this variable hasn't been assigned a color yet, assign the next color
293 if ( ! isset( $variableColors[ $varNumber ] ) ) {
294 $variableColors[ $varNumber ] = $colors[ ( count( $variableColors ) % count( $colors ) ) + $starting_index ];
295 }
296
297 return sprintf( '<span class="latepoint-whatsapp-template-variable" style="background-color: %s" data-variable="{{%s}}">{{%s}}</span>',
298 $variableColors[ $varNumber ],
299 $varNumber,
300 $varNumber
301 );
302 }, $text );
303 }
304
305 public static function extract_variables_from_template( ?string $text ): array {
306 $variables = [];
307 if ( empty( $text ) ) {
308 return $variables;
309 }
310
311 // Match anything between {{ and }}
312 preg_match_all( '/\{\{([^}]+)\}\}/', $text, $matches );
313
314 if ( ! empty( $matches[0] ) ) {
315 // Convert to and from array to get unique values while preserving original order
316 $variables = array_values( array_unique( $matches[0] ) );
317 }
318
319 return $variables;
320 }
321
322 public static function extract_variables_from_button_holders( array $holders ): array {
323 $joined_holders = implode( ', ', $holders );
324
325 return self::extract_variables_from_template( $joined_holders );
326 }
327
328 public static function get_business_id() {
329 /**
330 * Get business ID of a whatsapp account
331 *
332 * @param {string} $business_id WhatsApp Business ID
333 * @returns {string} The filtered business ID
334 *
335 * @since 5.1.3
336 * @hook latepoint_whatsapp_business_id
337 *
338 */
339 return apply_filters( 'latepoint_whatsapp_business_id', '' );
340 }
341 }