| 1 |
<?php |
| 2 |
namespace Ens\Whatsapp; |
| 3 |
|
| 4 |
/** |
| 5 |
* Class WhatsappSender |
| 6 |
* |
| 7 |
* @package Ens\Whatsapp |
| 8 |
* |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
class WhatsappSender { |
| 12 |
|
| 13 |
protected $to; |
| 14 |
protected $message; |
| 15 |
protected $action_data; |
| 16 |
protected $action_name; |
| 17 |
protected $receiver_type; |
| 18 |
protected $count; |
| 19 |
protected $message_type; |
| 20 |
protected $template; |
| 21 |
|
| 22 |
/** |
| 23 |
* WhatsappSender constructor. |
| 24 |
* |
| 25 |
* Expected $args keys: |
| 26 |
* - action_name string |
| 27 |
* - receiver_type string |
| 28 |
* - to string |
| 29 |
* - message string (used for `text` message_type) |
| 30 |
* - message_type string (`text` or `template`, defaults to `text`) |
| 31 |
* - template array (required when message_type=template): |
| 32 |
* - name string Approved template name in Meta Business Manager. |
| 33 |
* - language string Locale code (default `en_US`). |
| 34 |
* - body_params array Ordered parameter values; placeholders are resolved against action_data. |
| 35 |
* - action_data array |
| 36 |
* - count int |
| 37 |
* |
| 38 |
* @since 1.0.0 |
| 39 |
* |
| 40 |
* @param array $args |
| 41 |
*/ |
| 42 |
public function __construct( array $args ) { |
| 43 |
$this->action_name = $args['action_name'] ?? ''; |
| 44 |
$this->receiver_type = $args['receiver_type'] ?? ''; |
| 45 |
$this->to = $args['to'] ?? ''; |
| 46 |
$this->action_data = $args['action_data'] ?? []; |
| 47 |
$this->count = $args['count'] ?? 0; |
| 48 |
$this->message_type = isset( $args['message_type'] ) && 'template' === $args['message_type'] ? 'template' : 'text'; |
| 49 |
$this->template = isset( $args['template'] ) && is_array( $args['template'] ) ? $args['template'] : null; |
| 50 |
|
| 51 |
$message_content = $this->replace_placeholders( $args['message'] ?? '', $this->action_data ); |
| 52 |
$this->message = apply_filters( |
| 53 |
'notification_sdk_whatsapp_message', |
| 54 |
$message_content, |
| 55 |
$this->receiver_type, |
| 56 |
$this->action_name, |
| 57 |
$this->action_data, |
| 58 |
$this->count |
| 59 |
); |
| 60 |
|
| 61 |
if ( $this->template && ! empty( $this->template['body_params'] ) && is_array( $this->template['body_params'] ) ) { |
| 62 |
$action_data = $this->action_data; |
| 63 |
$this->template['body_params'] = array_map( |
| 64 |
function ( $param ) use ( $action_data ) { |
| 65 |
return $this->replace_placeholders( (string) $param, $action_data ); |
| 66 |
}, |
| 67 |
$this->template['body_params'] |
| 68 |
); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Send the WhatsApp message by firing the dispatch action. |
| 74 |
* |
| 75 |
* @since 1.0.0 |
| 76 |
* |
| 77 |
* @return void |
| 78 |
*/ |
| 79 |
public function send() { |
| 80 |
do_action( |
| 81 |
'notification_sdk_send_whatsapp', |
| 82 |
$this->to, |
| 83 |
$this->message, |
| 84 |
$this->action_data, |
| 85 |
$this->action_name, |
| 86 |
$this->receiver_type, |
| 87 |
$this->count, |
| 88 |
$this->message_type, |
| 89 |
$this->template |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Replace placeholders in a template. |
| 95 |
* |
| 96 |
* @since 1.0.0 |
| 97 |
* |
| 98 |
* @param string $template |
| 99 |
* @param array $data |
| 100 |
* |
| 101 |
* @return string |
| 102 |
*/ |
| 103 |
public function replace_placeholders( $template, $data ) { |
| 104 |
if ( ! is_array( $data ) ) { |
| 105 |
return $template; |
| 106 |
} |
| 107 |
foreach ( $data as $key => $value ) { |
| 108 |
if ( ! is_array( $value ) ) { |
| 109 |
$template = str_replace( '{{' . $key . '}}', $value, $template ); |
| 110 |
$template = str_replace( '{%' . $key . '%}', $value, $template ); |
| 111 |
} |
| 112 |
} |
| 113 |
return $template; |
| 114 |
} |
| 115 |
} |
| 116 |
|