PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 5.4.3
WooCommerce Square v5.4.3
5.4.3 5.4.2 5.4.1 5.4.0 trunk 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.2.0 3.3.0 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.7.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 4.0.0 4.1.0 4.2.0 4.2.1 4.2.2 4.2.3 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.9.0 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.6 4.9.7 4.9.8 4.9.9 5.0.0 5.0.1 5.1.0 5.1.1 5.1.2 5.2.0 5.3.0 5.3.1 5.3.2 5.3.3
woocommerce-square / includes / Emails / WC_Square_Sync_Completed.php
woocommerce-square / includes / Emails Last commit date
Base_Email.php 2 years ago WC_Square_Access_Token_Email.php 1 year ago WC_Square_Gift_Card_Sent.php 1 year ago WC_Square_Sync_Completed.php 1 year ago
WC_Square_Sync_Completed.php
290 lines
1 <?php
2 /**
3 * Sync Completed Email class
4 *
5 * Contains class that adds functionality to send out emails
6 * whenever sync is completed.
7 *
8 * @package WooCommerce Square
9 * @since 2.0.0
10 */
11
12 use WooCommerce\Square\Emails;
13
14 defined( 'ABSPATH' ) || exit;
15
16 /**
17 * Sync completed email.
18 *
19 * @since 2.0.0
20 */
21 class WC_Square_Sync_Completed extends Emails\Base_Email {
22 /**
23 * Email body.
24 *
25 * @var string
26 **/
27 public $body;
28
29 /**
30 * Email constructor.
31 *
32 * @since 2.0.0
33 */
34 public function __construct() {
35 // set properties
36 $this->id = 'wc_square_sync_completed';
37 $this->customer_email = false;
38 $this->title = __( 'Square Sync Completed', 'woocommerce-square' );
39 $this->description = __( 'This email is sent once a manual sync has been completed between WooCommerce and Square', 'woocommerce-square' );
40 $this->subject = _x( '[WooCommerce] Square sync completed', 'Email subject', 'woocommerce-square' );
41 $this->heading = _x( 'Square sync completed for {product_count}', 'Email heading with merge tag placeholder', 'woocommerce-square' );
42 $this->body = _x( 'Square sync completed for {site_title} at {sync_completed_date} {sync_completed_time}.', 'Email body with merge tag placeholders', 'woocommerce-square' );
43
44 $this->enabled_default = 'no';
45
46 // call parent constructor
47 parent::__construct();
48
49 // set default recipient
50 $this->recipient = $this->get_option( 'recipient', get_option( 'admin_email' ) );
51
52 add_filter( 'woocommerce_email_preview_placeholders', array( $this, 'set_dummy_placeholders' ), 10, 2 );
53 }
54
55 /**
56 * Set dummy placeholders for the email.
57 *
58 * @since 4.9.0
59 *
60 * @param array $placeholders Placeholders.
61 * @param string $email_type Email type.
62 * @return array
63 */
64 public function set_dummy_placeholders( $placeholders, $email_type ) {
65 if ( self::class !== $email_type ) {
66 return $placeholders;
67 }
68
69 $placeholders['{product_count}'] = esc_html__( '3 products', 'woocommerce-square' );
70 $placeholders['{sync_completed_date}'] = gmdate( wc_date_format(), strtotime( 'now' ) );
71 $placeholders['{sync_completed_time}'] = gmdate( wc_time_format(), strtotime( 'now' ) );
72
73 return $placeholders;
74 }
75
76 /**
77 * Gets the email heading, adjusted by sync job result status.
78 *
79 * @since 2.0.0
80 *
81 * @param string $status sync job status
82 * @return string
83 */
84 private function get_heading_by_job_status( $status ) {
85 if ( 'failed' === $status ) {
86 $email_heading = esc_html__( 'Square sync failed', 'woocommerce-square' );
87 } else {
88 $email_heading = parent::get_default_heading();
89 }
90
91 /**
92 * Filter hook to filter email heading.
93 *
94 * @see Sync_Completed::get_heading() for filter documentation
95 * @since 2.0.0
96 **/
97 return apply_filters( 'woocommerce_email_heading_' . $this->id, $this->format_string( $email_heading ), $this->object );
98 }
99
100 /**
101 * Gets the email body adjusted by sync job result status.
102 *
103 * @since 2.0.0
104 *
105 * @param string $status sync job status
106 * @param bool $html whether output should be HTML (true) or plain text (false)
107 * @return string may contain HTML
108 */
109 private function get_body_by_job_status( $status, $html ) {
110 $email_body = $this->get_default_body();
111
112 if ( 'failed' === $status ) {
113 if ( true === $html ) {
114 $square = wc_square();
115 $settings_url = $square->get_settings_url();
116 $records_url = add_query_arg( array( 'section' => 'update' ), $settings_url );
117
118 if ( $square->get_settings_handler()->is_debug_enabled() ) {
119 $action = sprintf(
120 /* translators: Placeholders: %1$s - opening <a> HTML link tag, %2$s - closing </a> HTML link tag */
121 esc_html__( '%1$sInspect status logs%2$s', 'woocommerce-square' ),
122 '<a href="' . esc_url( admin_url( 'admin.php?page=wc-status&tab=logs' ) ) . '">',
123 '</a>'
124 );
125 } else {
126 $action = sprintf(
127 /* translators: Placeholders: %1$s - opening <a> HTML link tag, %2$s - closing </a> HTML link tag */
128 esc_html__( '%1$sEnable logging%2$s', 'woocommerce-square' ),
129 '<a href="' . esc_url( $settings_url ) . '">',
130 '</a>'
131 );
132 }
133
134 $email_body .= sprintf(
135 /* translators: Placeholders: %1$s - opening <a> HTML link tag, %2$s - closing </a> HTML link tag, %3$s - additional action */
136 '<br>' . esc_html__( 'The sync job has failed. %1$sClick for more details%2$s, or %3$s.', 'woocommerce-square' ),
137 '<a href="' . esc_url( $records_url ) . '">',
138 '</a>',
139 strtolower( $action )
140 );
141
142 } else { // plain text
143
144 if ( wc_square()->get_settings_handler()->is_debug_enabled() ) {
145 $action = esc_html__( 'Inspect status logs', 'woocommerce-square' );
146 } else {
147 $action = esc_html__( 'Enable Logging', 'woocommerce-square' );
148 }
149
150 $email_body .= sprintf(
151 /* translators: Placeholders: %s - additional action */
152 esc_html__( 'The sync job has failed. Check sync records, or %s.', 'woocommerce-square' ),
153 strtolower( $action )
154 );
155 }
156 }
157
158 /**
159 * Filter hook to filter email body.
160 *
161 * @see Sync_Completed::get_body() for filter documentation
162 * @since 2.0.0
163 **/
164 return $this->format_string( (string) apply_filters( "{$this->id}_body", $email_body, $this ) );
165 }
166
167 /**
168 * Gets the email's related sync job, if set.
169 *
170 * @since 2.0.0
171 *
172 * @return \stdClass|null
173 */
174 private function get_job() {
175 return $this->object && is_object( $this->object ) ? $this->object : null;
176 }
177
178 /**
179 * Triggers the email.
180 *
181 * @since 2.0.0
182 *
183 * @param string|object|\stdClass $job a sync job object or ID
184 */
185 public function trigger( $job ) {
186 if ( $this->is_enabled() && $this->has_recipients() ) {
187 if ( is_string( $job ) || is_numeric( $job ) ) {
188 $job = wc_square()->get_background_job_handler()->get_job( $job );
189 }
190
191 if ( ! $job || ! is_object( $job ) || ! isset( $job->manual, $job->status ) ) {
192 return;
193 }
194
195 $should_send = false;
196 if ( $job->manual && ( 'completed' === $job->status || 'failed' === $job->status ) ) {
197 // for manual jobs, send an email if the job was either completed or failed
198 $should_send = true;
199 } elseif ( ! $job->manual && 'failed' === $job->status ) {
200 // for automated jobs, send an email only if the job failed and it's been a day since the last email
201 $already_sent = get_transient( 'wc_square_failed_sync_email_sent' );
202 if ( false === $already_sent ) {
203 $should_send = true;
204
205 set_transient( 'wc_square_failed_sync_email_sent', true, DAY_IN_SECONDS );
206 }
207 }
208
209 if ( $should_send ) {
210 $this->object = $job;
211
212 $this->parse_merge_tags();
213
214 $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
215 }
216 }
217 }
218
219 /**
220 * Parses the email's body merge tags.
221 *
222 * @since 2.0.0
223 */
224 protected function parse_merge_tags() {
225 $job = $this->get_job();
226
227 if ( ! $job ) {
228 return;
229 }
230
231 $product_count = is_array( $job->processed_product_ids ) ? count( $job->processed_product_ids ) : absint( $job->processed_product_ids );
232 /* translators: Placeholder: %d products count */
233 $product_count = sprintf( _n( '%d product', '%d products', $product_count, 'woocommerce-square' ), $product_count );
234
235 $sync_completed_date = '';
236 $sync_completed_time = '';
237 if ( isset( $job->completed_at ) ) {
238 $sync_completed_date = gmdate( wc_date_format(), strtotime( $job->completed_at ) );
239 $sync_completed_time = gmdate( wc_time_format(), strtotime( $job->completed_at ) );
240 } elseif ( isset( $job->failed_at ) ) {
241 $sync_completed_date = gmdate( wc_date_format(), strtotime( $job->failed_at ) );
242 $sync_completed_time = gmdate( wc_time_format(), strtotime( $job->failed_at ) );
243 }
244
245 // placeholders
246 $email_merge_tags = array(
247 'product_count' => $product_count,
248 'sync_started_date' => isset( $job->started_at ) ? gmdate( wc_date_format(), strtotime( $job->started_at ) ) : '',
249 'sync_started_time' => isset( $job->started_at ) ? gmdate( wc_time_format(), strtotime( $job->started_at ) ) : '',
250 'sync_completed_date' => $sync_completed_date,
251 'sync_completed_time' => $sync_completed_time,
252 );
253
254 foreach ( $email_merge_tags as $find => $replace ) {
255 $this->placeholders[ '{' . $find . '}' ] = $replace;
256 }
257 }
258
259 /**
260 * Gets the arguments that should be passed to an email template.
261 *
262 * @since 2.0.0
263 *
264 * @param array $args optional associative array with additional arguments
265 * @return array
266 */
267 protected function get_template_args( $args = array() ) {
268 $sync_job = $this->get_job();
269 $html = empty( $args['plain_text'] );
270
271 if ( $sync_job && isset( $sync_job->status ) && 'failed' === $sync_job->status ) {
272 $email_heading = $this->get_heading_by_job_status( 'failed' );
273 $email_body = $this->get_body_by_job_status( 'failed', $html );
274 } else {
275 $email_heading = $this->get_heading_by_job_status( 'completed' );
276 $email_body = $this->get_body_by_job_status( 'completed', $html );
277 }
278
279 return array_merge(
280 $args,
281 array(
282 'email' => $this,
283 'email_heading' => $email_heading,
284 'email_body' => $email_body,
285 'additional_content' => $this->get_additional_content(),
286 )
287 );
288 }
289 }
290