PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 3.8.1
WooCommerce Square v3.8.1
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 / Sync_Completed.php
woocommerce-square / includes / Emails Last commit date
Access_Token_Email.php 3 years ago Base_Email.php 3 years ago Sync_Completed.php 3 years ago
Sync_Completed.php
261 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 namespace WooCommerce\Square\Emails;
13
14 use WooCommerce\Square\Framework\Plugin_Compatibility;
15
16 defined( 'ABSPATH' ) || exit;
17
18 /**
19 * Sync completed email.
20 *
21 * @since 2.0.0
22 */
23 class Sync_Completed extends Base_Email {
24 /**
25 * Email constructor.
26 *
27 * @since 2.0.0
28 */
29 public function __construct() {
30 // set properties
31 $this->id = 'wc_square_sync_completed';
32 $this->customer_email = false;
33 $this->title = __( 'Square sync completed', 'woocommerce-square' );
34 $this->description = __( 'This email is sent once a manual sync has been completed between WooCommerce and Square', 'woocommerce-square' );
35 $this->subject = _x( '[WooCommerce] Square sync completed', 'Email subject', 'woocommerce-square' );
36 $this->heading = _x( 'Square sync completed for {product_count}', 'Email heading with merge tag placeholder', 'woocommerce-square' );
37 $this->body = _x( 'Square sync completed for {site_title} at {sync_completed_date} {sync_completed_time}.', 'Email body with merge tag placeholders', 'woocommerce-square' );
38
39 $this->enabled_default = 'no';
40
41 // call parent constructor
42 parent::__construct();
43
44 // set default recipient
45 $this->recipient = $this->get_option( 'recipient', get_option( 'admin_email' ) );
46 }
47
48 /**
49 * Gets the email heading, adjusted by sync job result status.
50 *
51 * @since 2.0.0
52 *
53 * @param string $status sync job status
54 * @return string
55 */
56 private function get_heading_by_job_status( $status ) {
57 if ( 'failed' === $status ) {
58 $email_heading = esc_html__( 'Square sync failed', 'woocommerce-square' );
59 } else {
60 $email_heading = parent::get_default_heading();
61 }
62
63 /**
64 * Filter hook to filter email heading.
65 *
66 * @see Sync_Completed::get_heading() for filter documentation
67 * @since 2.0.0
68 **/
69 return apply_filters( 'woocommerce_email_heading_' . $this->id, $this->format_string( $email_heading ), $this->object );
70 }
71
72 /**
73 * Gets the email body adjusted by sync job result status.
74 *
75 * @since 2.0.0
76 *
77 * @param string $status sync job status
78 * @param bool $html whether output should be HTML (true) or plain text (false)
79 * @return string may contain HTML
80 */
81 private function get_body_by_job_status( $status, $html ) {
82 $email_body = $this->get_default_body();
83
84 if ( 'failed' === $status ) {
85 if ( true === $html ) {
86 $square = wc_square();
87 $settings_url = $square->get_settings_url();
88 $records_url = add_query_arg( array( 'section' => 'update' ), $settings_url );
89
90 if ( $square->get_settings_handler()->is_debug_enabled() ) {
91 $action = sprintf(
92 /* translators: Placeholders: %1$s - opening <a> HTML link tag, %2$s - closing </a> HTML link tag */
93 esc_html__( '%1$sInspect status logs%2$s', 'woocommerce-square' ),
94 '<a href="' . esc_url( admin_url( 'admin.php?page=wc-status&tab=logs' ) ) . '">',
95 '</a>'
96 );
97 } else {
98 $action = sprintf(
99 /* translators: Placeholders: %1$s - opening <a> HTML link tag, %2$s - closing </a> HTML link tag */
100 esc_html__( '%1$sEnable logging%2$s', 'woocommerce-square' ),
101 '<a href="' . esc_url( $settings_url ) . '">',
102 '</a>'
103 );
104 }
105
106 $email_body .= sprintf(
107 /* translators: Placeholders: %1$s - opening <a> HTML link tag, %2$s - closing </a> HTML link tag, %3$s - additional action */
108 '<br>' . esc_html__( 'The sync job has failed. %1$sClick for more details%2$s, or %3$s.', 'woocommerce-square' ),
109 '<a href="' . esc_url( $records_url ) . '">',
110 '</a>',
111 strtolower( $action )
112 );
113
114 } else { // plain text
115
116 if ( wc_square()->get_settings_handler()->is_debug_enabled() ) {
117 $action = esc_html__( 'Inspect status logs', 'woocommerce-square' );
118 } else {
119 $action = esc_html__( 'Enable Logging', 'woocommerce-square' );
120 }
121
122 $email_body .= sprintf(
123 /* translators: Placeholders: %s - additional action */
124 esc_html__( 'The sync job has failed. Check sync records, or %s.', 'woocommerce-square' ),
125 strtolower( $action )
126 );
127 }
128 }
129
130 /**
131 * Filter hook to filter email body.
132 *
133 * @see Sync_Completed::get_body() for filter documentation
134 * @since 2.0.0
135 **/
136 return $this->format_string( (string) apply_filters( "{$this->id}_body", $email_body, $this ) );
137 }
138
139 /**
140 * Gets the email's related sync job, if set.
141 *
142 * @since 2.0.0
143 *
144 * @return \stdClass|null
145 */
146 private function get_job() {
147 return $this->object && is_object( $this->object ) ? $this->object : null;
148 }
149
150 /**
151 * Triggers the email.
152 *
153 * @since 2.0.0
154 *
155 * @param string|object|\stdClass $job a sync job object or ID
156 */
157 public function trigger( $job ) {
158 if ( $this->is_enabled() && $this->has_recipients() ) {
159 if ( is_string( $job ) || is_numeric( $job ) ) {
160 $job = wc_square()->get_background_job_handler()->get_job( $job );
161 }
162
163 if ( ! $job || ! is_object( $job ) || ! isset( $job->manual, $job->status ) ) {
164 return;
165 }
166
167 $should_send = false;
168 if ( $job->manual && ( 'completed' === $job->status || 'failed' === $job->status ) ) {
169 // for manual jobs, send an email if the job was either completed or failed
170 $should_send = true;
171 } elseif ( ! $job->manual && 'failed' === $job->status ) {
172 // for automated jobs, send an email only if the job failed and it's been a day since the last email
173 $already_sent = get_transient( 'wc_square_failed_sync_email_sent' );
174 if ( false === $already_sent ) {
175 $should_send = true;
176
177 set_transient( 'wc_square_failed_sync_email_sent', true, DAY_IN_SECONDS );
178 }
179 }
180
181 if ( $should_send ) {
182 $this->object = $job;
183
184 $this->parse_merge_tags();
185
186 $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
187 }
188 }
189 }
190
191 /**
192 * Parses the email's body merge tags.
193 *
194 * @since 2.0.0
195 */
196 protected function parse_merge_tags() {
197 $job = $this->get_job();
198
199 if ( ! $job ) {
200 return;
201 }
202
203 $product_count = is_array( $job->processed_product_ids ) ? count( $job->processed_product_ids ) : absint( $job->processed_product_ids );
204 /* translators: Placeholder: %d products count */
205 $product_count = sprintf( _n( '%d product', '%d products', $product_count, 'woocommerce-square' ), $product_count );
206
207 $sync_completed_date = '';
208 $sync_completed_time = '';
209 if ( isset( $job->completed_at ) ) {
210 $sync_completed_date = date( wc_date_format(), strtotime( $job->completed_at ) );
211 $sync_completed_time = date( wc_time_format(), strtotime( $job->completed_at ) );
212 } elseif ( isset( $job->failed_at ) ) {
213 $sync_completed_date = date( wc_date_format(), strtotime( $job->failed_at ) );
214 $sync_completed_time = date( wc_time_format(), strtotime( $job->failed_at ) );
215 }
216
217 // placeholders
218 $email_merge_tags = array(
219 'product_count' => $product_count,
220 'sync_started_date' => isset( $job->started_at ) ? date( wc_date_format(), strtotime( $job->started_at ) ) : '',
221 'sync_started_time' => isset( $job->started_at ) ? date( wc_time_format(), strtotime( $job->started_at ) ) : '',
222 'sync_completed_date' => $sync_completed_date,
223 'sync_completed_time' => $sync_completed_time,
224 );
225
226 foreach ( $email_merge_tags as $find => $replace ) {
227 $this->placeholders[ '{' . $find . '}' ] = $replace;
228 }
229 }
230
231 /**
232 * Gets the arguments that should be passed to an email template.
233 *
234 * @since 2.0.0
235 *
236 * @param array $args optional associative array with additional arguments
237 * @return array
238 */
239 protected function get_template_args( $args = array() ) {
240 $sync_job = $this->get_job();
241 $html = empty( $args['plain_text'] );
242
243 if ( $sync_job && isset( $sync_job->status ) && 'failed' === $sync_job->status ) {
244 $email_heading = $this->get_heading_by_job_status( 'failed' );
245 $email_body = $this->get_body_by_job_status( 'failed', $html );
246 } else {
247 $email_heading = $this->get_heading_by_job_status( 'completed' );
248 $email_body = $this->get_body_by_job_status( 'completed', $html );
249 }
250
251 return array_merge(
252 $args,
253 array(
254 'email' => $this,
255 'email_heading' => $email_heading,
256 'email_body' => $email_body,
257 )
258 );
259 }
260 }
261