Sync_Completed.php
272 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Square |
| 4 | * |
| 5 | * This source file is subject to the GNU General Public License v3.0 |
| 6 | * that is bundled with this package in the file license.txt. |
| 7 | * It is also available through the world-wide-web at this URL: |
| 8 | * http://www.gnu.org/licenses/gpl-3.0.html |
| 9 | * If you did not receive a copy of the license and are unable to |
| 10 | * obtain it through the world-wide-web, please send an email |
| 11 | * to license@woocommerce.com so we can send you a copy immediately. |
| 12 | * |
| 13 | * DISCLAIMER |
| 14 | * |
| 15 | * Do not edit or add to this file if you wish to upgrade WooCommerce Square to newer |
| 16 | * versions in the future. If you wish to customize WooCommerce Square for your |
| 17 | * needs please refer to https://docs.woocommerce.com/document/woocommerce-square/ |
| 18 | * |
| 19 | * @author WooCommerce |
| 20 | * @copyright Copyright: (c) 2019, Automattic, Inc. |
| 21 | * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 |
| 22 | */ |
| 23 | |
| 24 | namespace WooCommerce\Square\Emails; |
| 25 | |
| 26 | use SkyVerge\WooCommerce\PluginFramework\v5_4_0 as Framework; |
| 27 | |
| 28 | defined( 'ABSPATH' ) || exit; |
| 29 | |
| 30 | /** |
| 31 | * Sync completed email. |
| 32 | * |
| 33 | * @since 2.0.0 |
| 34 | */ |
| 35 | class Sync_Completed extends Base_Email { |
| 36 | /** |
| 37 | * Email constructor. |
| 38 | * |
| 39 | * @since 2.0.0 |
| 40 | */ |
| 41 | public function __construct() { |
| 42 | // set properties |
| 43 | $this->id = 'wc_square_sync_completed'; |
| 44 | $this->customer_email = false; |
| 45 | $this->title = __( 'Square sync completed', 'woocommerce-square' ); |
| 46 | $this->description = __( 'This email is sent once a manual sync has been completed between WooCommerce and Square', 'woocommerce-square' ); |
| 47 | $this->subject = _x( '[WooCommerce] Square sync completed', 'Email subject', 'woocommerce-square' ); |
| 48 | $this->heading = _x( 'Square sync completed for {product_count}', 'Email heading with merge tag placeholder', 'woocommerce-square' ); |
| 49 | $this->body = _x( 'Square sync completed for {site_title} at {sync_completed_date} {sync_completed_time}.', 'Email body with merge tag placeholders', 'woocommerce-square' ); |
| 50 | |
| 51 | $this->enabled_default = 'no'; |
| 52 | |
| 53 | // call parent constructor |
| 54 | parent::__construct(); |
| 55 | |
| 56 | // set default recipient |
| 57 | $this->recipient = $this->get_option( 'recipient', get_option( 'admin_email' ) ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Gets the email heading, adjusted by sync job result status. |
| 62 | * |
| 63 | * @since 2.0.0 |
| 64 | * |
| 65 | * @param string $status sync job status |
| 66 | * @return string |
| 67 | */ |
| 68 | private function get_heading_by_job_status( $status ) { |
| 69 | if ( 'failed' === $status ) { |
| 70 | $email_heading = esc_html__( 'Square sync failed', 'woocommerce-square' ); |
| 71 | } else { |
| 72 | $email_heading = parent::get_default_heading(); |
| 73 | } |
| 74 | |
| 75 | /** @see Sync_Completed::get_heading() for filter documentation */ |
| 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 | /** @see Sync_Completed::get_body() for filter documentation */ |
| 138 | return $this->format_string( (string) apply_filters( "{$this->id}_body", $email_body, $this ) ); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Gets the email's related sync job, if set. |
| 143 | * |
| 144 | * @since 2.0.0 |
| 145 | * |
| 146 | * @return \stdClass|null |
| 147 | */ |
| 148 | private function get_job() { |
| 149 | return $this->object && is_object( $this->object ) ? $this->object : null; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Triggers the email. |
| 154 | * |
| 155 | * @since 2.0.0 |
| 156 | * |
| 157 | * @param string|object|\stdClass $job a sync job object or ID |
| 158 | */ |
| 159 | public function trigger( $job ) { |
| 160 | if ( $this->is_enabled() && $this->has_recipients() ) { |
| 161 | if ( is_string( $job ) || is_numeric( $job ) ) { |
| 162 | $job = wc_square()->get_background_job_handler()->get_job( $job ); |
| 163 | } |
| 164 | |
| 165 | if ( ! $job || ! is_object( $job ) || ! isset( $job->manual, $job->status ) ) { |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | $should_send = false; |
| 170 | if ( $job->manual && ( 'completed' === $job->status || 'failed' === $job->status ) ) { |
| 171 | // for manual jobs, send an email if the job was either completed or failed |
| 172 | $should_send = true; |
| 173 | } elseif ( ! $job->manual && 'failed' === $job->status ) { |
| 174 | // for automated jobs, send an email only if the job failed and it's been a day since the last email |
| 175 | $already_sent = get_transient( 'wc_square_failed_sync_email_sent' ); |
| 176 | if ( false === $already_sent ) { |
| 177 | $should_send = true; |
| 178 | |
| 179 | set_transient( 'wc_square_failed_sync_email_sent', true, DAY_IN_SECONDS ); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | if ( $should_send ) { |
| 184 | $this->object = $job; |
| 185 | |
| 186 | $this->parse_merge_tags(); |
| 187 | |
| 188 | $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Parses the email's body merge tags. |
| 195 | * |
| 196 | * @since 2.0.0 |
| 197 | */ |
| 198 | protected function parse_merge_tags() { |
| 199 | $job = $this->get_job(); |
| 200 | |
| 201 | if ( ! $job ) { |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | $product_count = is_array( $job->processed_product_ids ) ? count( $job->processed_product_ids ) : absint( $job->processed_product_ids ); |
| 206 | /* translators: Placeholder: %d products count */ |
| 207 | $product_count = sprintf( _n( '%d product', '%d products', $product_count, 'woocommerce-square' ), $product_count ); |
| 208 | |
| 209 | $sync_completed_date = ''; |
| 210 | $sync_completed_time = ''; |
| 211 | if ( isset( $job->completed_at ) ) { |
| 212 | $sync_completed_date = date( wc_date_format(), strtotime( $job->completed_at ) ); |
| 213 | $sync_completed_time = date( wc_time_format(), strtotime( $job->completed_at ) ); |
| 214 | } elseif ( isset( $job->failed_at ) ) { |
| 215 | $sync_completed_date = date( wc_date_format(), strtotime( $job->failed_at ) ); |
| 216 | $sync_completed_time = date( wc_time_format(), strtotime( $job->failed_at ) ); |
| 217 | } |
| 218 | |
| 219 | // placeholders |
| 220 | $email_merge_tags = array( |
| 221 | 'product_count' => $product_count, |
| 222 | 'sync_started_date' => isset( $job->started_at ) ? date( wc_date_format(), strtotime( $job->started_at ) ) : '', |
| 223 | 'sync_started_time' => isset( $job->started_at ) ? date( wc_time_format(), strtotime( $job->started_at ) ) : '', |
| 224 | 'sync_completed_date' => $sync_completed_date, |
| 225 | 'sync_completed_time' => $sync_completed_time, |
| 226 | ); |
| 227 | |
| 228 | // TODO update handling when WooCommerce 3.2 is the minimum required version {FN 2019-05-03} |
| 229 | if ( Framework\SV_WC_Plugin_Compatibility::is_wc_version_gte( '3.2' ) ) { |
| 230 | foreach ( $email_merge_tags as $find => $replace ) { |
| 231 | $this->placeholders[ '{' . $find . '}' ] = $replace; |
| 232 | } |
| 233 | } else { |
| 234 | |
| 235 | foreach ( $email_merge_tags as $find => $replace ) { |
| 236 | $this->find[ $find ] = '{' . $find . '}'; |
| 237 | $this->replace[ $find ] = $replace; |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Gets the arguments that should be passed to an email template. |
| 244 | * |
| 245 | * @since 2.0.0 |
| 246 | * |
| 247 | * @param array $args optional associative array with additional arguments |
| 248 | * @return array |
| 249 | */ |
| 250 | protected function get_template_args( $args = array() ) { |
| 251 | $sync_job = $this->get_job(); |
| 252 | $html = empty( $args['plain_text'] ); |
| 253 | |
| 254 | if ( $sync_job && isset( $sync_job->status ) && 'failed' === $sync_job->status ) { |
| 255 | $email_heading = $this->get_heading_by_job_status( 'failed' ); |
| 256 | $email_body = $this->get_body_by_job_status( 'failed', $html ); |
| 257 | } else { |
| 258 | $email_heading = $this->get_heading_by_job_status( 'completed' ); |
| 259 | $email_body = $this->get_body_by_job_status( 'completed', $html ); |
| 260 | } |
| 261 | |
| 262 | return array_merge( |
| 263 | $args, |
| 264 | array( |
| 265 | 'email' => $this, |
| 266 | 'email_heading' => $email_heading, |
| 267 | 'email_body' => $email_body, |
| 268 | ) |
| 269 | ); |
| 270 | } |
| 271 | } |
| 272 |