| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMail\Shortcodes; |
| 4 |
|
| 5 |
use YayMail\Utils\Helpers; |
| 6 |
use YayMail\Utils\SingletonTrait; |
| 7 |
use YayMail\Abstracts\BaseShortcode; |
| 8 |
|
| 9 |
/** |
| 10 |
* Shortcodes for WC core fulfillment emails (WC 10.1, `fulfillments` feature). |
| 11 |
* |
| 12 |
* @since 4.4.7 |
| 13 |
* @method: static FulfillmentShortcodes get_instance() |
| 14 |
*/ |
| 15 |
class FulfillmentShortcodes extends BaseShortcode { |
| 16 |
use SingletonTrait; |
| 17 |
|
| 18 |
protected function __construct() { |
| 19 |
$this->available_email_ids = [ 'customer_fulfillment_created', 'customer_fulfillment_updated', 'customer_fulfillment_deleted' ]; |
| 20 |
parent::__construct(); |
| 21 |
} |
| 22 |
|
| 23 |
public function get_shortcodes() { |
| 24 |
$shortcodes = []; |
| 25 |
$shortcodes[] = [ |
| 26 |
'name' => 'yaymail_fulfillment_details', |
| 27 |
'description' => __( 'Fulfillment Details', 'yaymail' ), |
| 28 |
'group' => 'fulfillments', |
| 29 |
'callback' => [ $this, 'yaymail_fulfillment_details' ], |
| 30 |
]; |
| 31 |
$shortcodes[] = [ |
| 32 |
'name' => 'yaymail_fulfillment_note', |
| 33 |
'description' => __( 'Fulfillment Note From The Store', 'yaymail' ), |
| 34 |
'group' => 'fulfillments', |
| 35 |
'callback' => [ $this, 'yaymail_fulfillment_note' ], |
| 36 |
]; |
| 37 |
return $shortcodes; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Render WC's own fulfillment details (tracking info + items) and fulfillment meta, |
| 42 |
* via the same hooks the WC template fires. |
| 43 |
*/ |
| 44 |
public function yaymail_fulfillment_details( $data ) { |
| 45 |
$render_data = isset( $data['render_data'] ) ? $data['render_data'] : []; |
| 46 |
$order = Helpers::get_order_from_shortcode_data( $render_data ); |
| 47 |
$fulfillment = $render_data['fulfillment'] ?? null; |
| 48 |
|
| 49 |
if ( ! empty( $render_data['is_sample'] ) || empty( $order ) || empty( $fulfillment ) ) { |
| 50 |
return $this->get_sample_fulfillment_details(); |
| 51 |
} |
| 52 |
|
| 53 |
$email = $render_data['email'] ?? ''; |
| 54 |
|
| 55 |
ob_start(); |
| 56 |
do_action( 'woocommerce_email_fulfillment_details', $order, $fulfillment, false, false, $email ); |
| 57 |
do_action( 'woocommerce_email_fulfillment_meta', $order, $fulfillment, false, false, $email ); |
| 58 |
return ob_get_clean(); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Store note passed to the fulfillment updated email. Empty when no note was given. |
| 63 |
*/ |
| 64 |
public function yaymail_fulfillment_note( $data ) { |
| 65 |
$render_data = isset( $data['render_data'] ) ? $data['render_data'] : []; |
| 66 |
|
| 67 |
if ( ! empty( $render_data['is_sample'] ) ) { |
| 68 |
$note = __( 'This is a sample note from the store. Your order has been updated with new tracking information.', 'woocommerce' ); |
| 69 |
} else { |
| 70 |
$note = is_scalar( $render_data['customer_note'] ?? null ) ? trim( (string) $render_data['customer_note'] ) : ''; |
| 71 |
} |
| 72 |
|
| 73 |
if ( '' === $note ) { |
| 74 |
return ''; |
| 75 |
} |
| 76 |
|
| 77 |
return '<p><strong>' . esc_html__( 'Note from the store:', 'woocommerce' ) . '</strong></p><blockquote>' . wp_kses_post( wpautop( wptexturize( $note ) ) ) . '</blockquote>'; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Static preview for the editor, where no real fulfillment exists. |
| 82 |
*/ |
| 83 |
private function get_sample_fulfillment_details() { |
| 84 |
$rows = [ |
| 85 |
__( 'Tracking number', 'yaymail' ) => '1Z999AA10123456784', |
| 86 |
__( 'Shipping provider', 'yaymail' ) => 'UPS', |
| 87 |
__( 'Items', 'yaymail' ) => __( 'Sample product × 1', 'yaymail' ), |
| 88 |
]; |
| 89 |
|
| 90 |
$html = '<table cellspacing="0" cellpadding="6" border="1" style="width: 100%; border-collapse: collapse;">'; |
| 91 |
foreach ( $rows as $label => $value ) { |
| 92 |
$html .= '<tr><th scope="row" style="text-align: left;">' . esc_html( $label ) . '</th><td>' . esc_html( $value ) . '</td></tr>'; |
| 93 |
} |
| 94 |
$html .= '</table>'; |
| 95 |
|
| 96 |
return $html; |
| 97 |
} |
| 98 |
} |
| 99 |
|