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 |