PluginProbe
YayMail – WooCommerce Email Customizer / 4.4.7
YayMail – WooCommerce Email Customizer v4.4.7
4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 trunk 1.9.6 2.1.4 2.1.5 3.2.2 3.2.6 3.2.7.1 3.2.8.1 3.2.9 3.3 3.3.1 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4 All 58 releases
yaymail / src / Shortcodes / StockNotificationShortcodes.php

StockNotificationShortcodes.php in YayMail – WooCommerce Email Customizer 4.4.7, at src/Shortcodes/StockNotificationShortcodes.php

177 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace YayMail\Shortcodes;
4
5 use YayMail\Utils\SingletonTrait;
6 use YayMail\Abstracts\BaseShortcode;
7
8 /**
9 * Shortcodes for WC core Back-in-Stock emails (WC 10.2, `customer_stock_notifications` feature).
10 * Values come from the WC template args: notification, product, verification_link,
11 * verification_expiration_threshold, button_link, unsubscribe_link, is_guest.
12 *
13 * @since 4.4.7
14 * @method: static StockNotificationShortcodes get_instance()
15 */
16 class StockNotificationShortcodes extends BaseShortcode {
17 use SingletonTrait;
18
19 protected function __construct() {
20 $this->available_email_ids = [ 'customer_stock_notification_verify', 'customer_stock_notification_verified', 'customer_stock_notification' ];
21 parent::__construct();
22 }
23
24 public function get_shortcodes() {
25 $shortcodes = [];
26 $shortcodes[] = [
27 'name' => 'yaymail_stock_notification_product',
28 'description' => __( 'Product (Image, Title, Attributes, Price)', 'yaymail' ),
29 'group' => 'stock_notifications',
30 'callback' => [ $this, 'yaymail_stock_notification_product' ],
31 ];
32 $shortcodes[] = [
33 'name' => 'yaymail_stock_notification_product_name',
34 'description' => __( 'Product Name', 'yaymail' ),
35 'group' => 'stock_notifications',
36 'callback' => [ $this, 'yaymail_stock_notification_product_name' ],
37 ];
38 $shortcodes[] = [
39 'name' => 'yaymail_stock_notification_product_url',
40 'description' => __( 'Product URL', 'yaymail' ),
41 'group' => 'stock_notifications',
42 'callback' => [ $this, 'yaymail_stock_notification_product_url' ],
43 ];
44 $shortcodes[] = [
45 'name' => 'yaymail_stock_notification_verification_url',
46 'description' => __( 'Sign-up Verification URL', 'yaymail' ),
47 'group' => 'stock_notifications',
48 'callback' => [ $this, 'yaymail_stock_notification_verification_url' ],
49 ];
50 $shortcodes[] = [
51 'name' => 'yaymail_stock_notification_verification_expiration',
52 'description' => __( 'Verification Link Expiration', 'yaymail' ),
53 'group' => 'stock_notifications',
54 'callback' => [ $this, 'yaymail_stock_notification_verification_expiration' ],
55 ];
56 $shortcodes[] = [
57 'name' => 'yaymail_stock_notification_unsubscribe_url',
58 'description' => __( 'Unsubscribe URL', 'yaymail' ),
59 'group' => 'stock_notifications',
60 'callback' => [ $this, 'yaymail_stock_notification_unsubscribe_url' ],
61 ];
62 $shortcodes[] = [
63 'name' => 'yaymail_stock_notification_unsubscribe_text',
64 'description' => __( 'Unsubscribe / Manage Notifications Text', 'yaymail' ),
65 'group' => 'stock_notifications',
66 'callback' => [ $this, 'yaymail_stock_notification_unsubscribe_text' ],
67 ];
68 return $shortcodes;
69 }
70
71 /**
72 * Real product from the email args, or any published product for the editor preview.
73 *
74 * @return \WC_Product|null
75 */
76 private function get_product( $render_data ) {
77 if ( ! empty( $render_data['product'] ) && $render_data['product'] instanceof \WC_Product ) {
78 return $render_data['product'];
79 }
80 if ( empty( $render_data['is_sample'] ) ) {
81 return null;
82 }
83 $products = wc_get_products(
84 [
85 'limit' => 1,
86 'status' => 'publish',
87 ]
88 );
89 return ! empty( $products ) ? $products[0] : null;
90 }
91
92 public function yaymail_stock_notification_product( $data ) {
93 $render_data = isset( $data['render_data'] ) ? $data['render_data'] : [];
94 $product = $this->get_product( $render_data );
95 $notification = $render_data['notification'] ?? null;
96
97 if ( empty( $product ) ) {
98 return '';
99 }
100
101 if ( ! empty( $notification ) ) {
102 // WC's own product block: image, title, variation attributes, price.
103 ob_start();
104 do_action( 'woocommerce_email_stock_notification_product', $product, $notification, false, $render_data['email'] ?? null );
105 return ob_get_clean();
106 }
107
108 // Editor preview: no notification object, so build a simple equivalent.
109 $image = wp_get_attachment_image_url( $product->get_image_id(), 'woocommerce_thumbnail' );
110 $html = '<div class="yaymail-stock-notification-product">';
111 if ( $image ) {
112 $html .= '<p><img src="' . esc_url( $image ) . '" alt="' . esc_attr( $product->get_name() ) . '" width="160" /></p>';
113 }
114 $html .= '<p><strong>' . esc_html( $product->get_name() ) . '</strong></p>';
115 $html .= '<p>' . wp_kses_post( $product->get_price_html() ) . '</p>';
116 $html .= '</div>';
117 return $html;
118 }
119
120 public function yaymail_stock_notification_product_name( $data ) {
121 $render_data = isset( $data['render_data'] ) ? $data['render_data'] : [];
122 $product = $this->get_product( $render_data );
123 return $product ? esc_html( $product->get_name() ) : '';
124 }
125
126 public function yaymail_stock_notification_product_url( $data ) {
127 $render_data = isset( $data['render_data'] ) ? $data['render_data'] : [];
128 if ( ! empty( $render_data['button_link'] ) ) {
129 return esc_url( $render_data['button_link'] );
130 }
131 $product = $this->get_product( $render_data );
132 return $product ? esc_url( $product->get_permalink() ) : esc_url( home_url() );
133 }
134
135 public function yaymail_stock_notification_verification_url( $data ) {
136 $render_data = isset( $data['render_data'] ) ? $data['render_data'] : [];
137 return ! empty( $render_data['verification_link'] ) ? esc_url( $render_data['verification_link'] ) : esc_url( home_url() );
138 }
139
140 public function yaymail_stock_notification_verification_expiration( $data ) {
141 $render_data = isset( $data['render_data'] ) ? $data['render_data'] : [];
142 if ( ! empty( $render_data['verification_expiration_threshold'] ) ) {
143 return esc_html( $render_data['verification_expiration_threshold'] );
144 }
145 // translators: %s is the time duration in minutes.
146 return esc_html( sprintf( _n( '%s minute', '%s minutes', 60, 'woocommerce' ), 60 ) );
147 }
148
149 public function yaymail_stock_notification_unsubscribe_url( $data ) {
150 $render_data = isset( $data['render_data'] ) ? $data['render_data'] : [];
151 return ! empty( $render_data['unsubscribe_link'] ) ? esc_url( $render_data['unsubscribe_link'] ) : esc_url( home_url() );
152 }
153
154 /**
155 * Same sentence WC prints: guests get an unsubscribe link, customers a manage-notifications link.
156 */
157 public function yaymail_stock_notification_unsubscribe_text( $data ) {
158 $render_data = isset( $data['render_data'] ) ? $data['render_data'] : [];
159 if ( empty( $render_data['unsubscribe_link'] ) && empty( $render_data['is_sample'] ) ) {
160 return '';
161 }
162
163 $link = $this->yaymail_stock_notification_unsubscribe_url( $data );
164 $is_guest = ! empty( $render_data['is_guest'] );
165
166 if ( ! $is_guest ) {
167 $link_tag = sprintf( '<a href="%1$s">%2$s</a>', $link, esc_html( _x( 'click here', 'unsubscribe cta for stock notifications for existing customers', 'woocommerce' ) ) );
168 // translators: %s: unsubscribe link.
169 return wp_kses_post( sprintf( __( 'To manage your notifications, %s to log in to your account.', 'woocommerce' ), $link_tag ) );
170 }
171
172 $link_tag = sprintf( '<a href="%1$s">%2$s</a>', $link, esc_html( _x( 'click here', 'unsubscribe cta for stock notifications for guests', 'woocommerce' ) ) );
173 // translators: %s: unsubscribe link.
174 return wp_kses_post( sprintf( __( 'To stop receiving these messages, %s to unsubscribe.', 'woocommerce' ), $link_tag ) );
175 }
176 }
177