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