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