| 1 |
<?php |
| 2 |
|
| 3 |
namespace Texty\Notifications\WC; |
| 4 |
|
| 5 |
use Texty\Notifications\Notification; |
| 6 |
use WC_Order_Item_Product; |
| 7 |
|
| 8 |
class Base extends Notification { |
| 9 |
|
| 10 |
/** |
| 11 |
* @var object |
| 12 |
*/ |
| 13 |
protected $order; |
| 14 |
|
| 15 |
/** |
| 16 |
* Set the user ID |
| 17 |
* |
| 18 |
* @param int $order_id |
| 19 |
* |
| 20 |
* @return self |
| 21 |
*/ |
| 22 |
public function set_order( $order ) { |
| 23 |
$this->order = $order; |
| 24 |
|
| 25 |
return $this; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Return the message |
| 30 |
* |
| 31 |
* @return string |
| 32 |
*/ |
| 33 |
public function get_message() { |
| 34 |
$message = parent::get_message_raw(); |
| 35 |
|
| 36 |
if ( ! $this->order ) { |
| 37 |
return $message; |
| 38 |
} |
| 39 |
|
| 40 |
foreach ( $this->replacement_keys() as $search => $method ) { |
| 41 |
$value = method_exists( $this->order, $method ) ? $this->order->$method() : ''; |
| 42 |
|
| 43 |
if ( 'order_total' === $search ) { |
| 44 |
$value = wp_strip_all_tags( html_entity_decode( $value ) ); |
| 45 |
} |
| 46 |
|
| 47 |
if ( 'items' === $search ) { |
| 48 |
$value = $this->get_items(); |
| 49 |
} |
| 50 |
|
| 51 |
$message = str_replace( '{' . $search . '}', $value, $message ); |
| 52 |
} |
| 53 |
|
| 54 |
$message = $this->replace_global_keys( $message ); |
| 55 |
|
| 56 |
return $message; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Get product items from the order |
| 61 |
* |
| 62 |
* @return string |
| 63 |
*/ |
| 64 |
protected function get_items() { |
| 65 |
$products = []; |
| 66 |
|
| 67 |
foreach ( $this->order->get_items() as $item ) { |
| 68 |
if ( ! $item instanceof WC_Order_Item_Product ) { |
| 69 |
continue; |
| 70 |
} |
| 71 |
|
| 72 |
$product = $item->get_product(); |
| 73 |
$products[] = sprintf( '%s x %d', $product->get_name(), $item->get_quantity() ); |
| 74 |
} |
| 75 |
|
| 76 |
$names = implode( "\n", $products ); |
| 77 |
|
| 78 |
return $names; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Return recipients |
| 83 |
* |
| 84 |
* @return array |
| 85 |
*/ |
| 86 |
public function get_recipients() { |
| 87 |
return $this->get_numbers_by_roles(); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Get replacement keys |
| 92 |
* |
| 93 |
* @return array |
| 94 |
*/ |
| 95 |
public function replacement_keys() { |
| 96 |
return [ |
| 97 |
'order_id' => 'get_id', |
| 98 |
'items' => 'products', |
| 99 |
'date' => 'get_date_paid', |
| 100 |
'status' => 'get_status', |
| 101 |
'payment_method' => 'get_payment_method_title', |
| 102 |
'shipping_method' => 'get_shipping_method', |
| 103 |
'transaction_id' => 'get_transaction_id', |
| 104 |
'billing_name' => 'get_formatted_billing_full_name', |
| 105 |
'billing_email' => 'get_billing_email', |
| 106 |
'order_total' => 'get_formatted_order_total', |
| 107 |
'shipping_total' => 'get_shipping_total', |
| 108 |
'tax_total' => 'get_total_tax', |
| 109 |
'discount' => 'get_discount_total', |
| 110 |
]; |
| 111 |
} |
| 112 |
|
| 113 |
public function send() { |
| 114 |
if ( ! $this->enabled() ) { |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
$meta_key = '_texty_' . $this->get_id(); |
| 119 |
$has_sent = $this->order->get_meta( $meta_key, true ); |
| 120 |
|
| 121 |
// if we've already sent the message, don't send again |
| 122 |
if ( $has_sent ) { |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
// mark as sent |
| 127 |
$this->order->add_meta_data( $meta_key, 1 ); |
| 128 |
$this->order->save_meta_data(); |
| 129 |
|
| 130 |
if ( 'user' === $this->get_type() ) { |
| 131 |
$number = $this->order->get_billing_phone(); |
| 132 |
|
| 133 |
$recipients = $number ? [ $number ] : []; |
| 134 |
} else { |
| 135 |
$recipients = $this->get_recipients(); |
| 136 |
} |
| 137 |
|
| 138 |
if ( ! $recipients ) { |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
$content = $this->get_message(); |
| 143 |
$gateway = texty()->gateways(); |
| 144 |
|
| 145 |
foreach ( $recipients as $number ) { |
| 146 |
$gateway->send( $number, $content ); |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|