array( title, to, subject, heading, text, blocks[, more] ). Defaults; the shop's own versions * are in dayslot_settings['emails'][ id ]. */ function dayslot_emails() { return apply_filters( 'dayslot_emails', array( 'date_change' => array( 'title' => __( 'New delivery date (when you change it)', 'dayslot' ), 'to' => 'customer', /* translators: {shop}, {order}: placeholders, keep them */ 'subject' => __( '[{shop}] New delivery date for order #{order}', 'dayslot' ), 'heading' => __( 'Your delivery date has changed', 'dayslot' ), 'text' => __( 'Hi {name}, the delivery date for your order #{order} has been changed: {details} If this doesn\'t suit you, just reply to this email.', 'dayslot' ), 'blocks' => array( 'details', 'calendar', 'contact' ), ), ) ); } /** * Blocks an e-mail can show: id => label. */ function dayslot_email_blocks() { return array( 'details' => __( 'Delivery details (date, time, address)', 'dayslot' ), 'products' => __( 'Products with photos', 'dayslot' ), 'prices' => __( 'Prices and total', 'dayslot' ), 'calendar' => __( '"Add to calendar" links', 'dayslot' ), 'contact' => __( 'Shop contact in the footer', 'dayslot' ), ); } /** * One e-mail with the shop's changes: array( subject, heading, text, blocks ). */ function dayslot_email_template( $id ) { $all = dayslot_emails(); if ( ! isset( $all[ $id ] ) ) { return null; } // The shop's own texts and blocks (set on the Pro E-mails tab). $own = (array) dayslot_get_settings()['emails']; $own = isset( $own[ $id ] ) && is_array( $own[ $id ] ) ? $own[ $id ] : array(); $out = array(); foreach ( array( 'subject', 'heading', 'text' ) as $key ) { $out[ $key ] = isset( $own[ $key ] ) && '' !== trim( (string) $own[ $key ] ) ? (string) $own[ $key ] : $all[ $id ][ $key ]; } $out['blocks'] = isset( $own['blocks'] ) && is_array( $own['blocks'] ) ? $own['blocks'] : ( isset( $all[ $id ]['blocks'] ) ? $all[ $id ]['blocks'] : array() ); return $out; } /** * Colours for the plain (WooCommerce) look. */ function dayslot_email_palette() { return array( 'id' => 'woo', 'text' => '#1f2430', 'muted' => '#5b6270', 'accent' => get_option( 'woocommerce_email_base_color', '#242e42' ), 'on' => '#ffffff', 'box' => '#f6f7f9', 'line' => '#e6e8eb', 'btnr' => 8, ); } /** * The look of the e-mails: the WooCommerce one; Pro adds its own designs (filter "dayslot_email_look"). */ function dayslot_email_look( $style = null, $color = null ) { return (array) apply_filters( 'dayslot_email_look', array( 'id' => 'woo' ), $style, $color ); } /** * The delivery rows of an order as a table. */ function dayslot_email_details( $order, $t = null ) { $t = $t ? $t : dayslot_email_look(); $muted = isset( $t['muted'] ) ? $t['muted'] : '#636363'; $box = isset( $t['box'] ) ? $t['box'] : '#f6f7f9'; $line = isset( $t['line'] ) ? $t['line'] : '#e5e5e5'; $rows = dayslot_order_summary_rows( $order ); unset( $rows['dayslot_price'] ); if ( ! $rows ) { return ''; } $html = ''; $last = count( $rows ) - 1; $i = 0; foreach ( $rows as $row ) { $sep = $i++ < $last ? 'border-bottom:1px solid ' . $line . ';' : ''; $html .= ''; } return $html . '
' . esc_html( $row['label'] ) . '' . esc_html( $row['value'] ) . '
'; } /** * Products (with photos) and, optionally, the prices. */ function dayslot_email_products( $order, $t, $prices ) { $html = '

' . esc_html__( 'Your order', 'dayslot' ) . '

'; $args = array( 'currency' => $order->get_currency() ); foreach ( $order->get_items() as $item ) { $product = $item->get_product(); $img = $product && $product->get_image_id() ? wp_get_attachment_image_url( $product->get_image_id(), 'thumbnail' ) : ''; $pic = $img ? '' : '
'; $price = $prices ? '' : ''; $html .= '' . $price . ''; } $html .= '
' . wp_kses_post( wc_price( $order->get_line_total( $item, true ), $args ) ) . '
' . $pic . '' . esc_html( $item->get_name() ) . '
× ' . esc_html( $item->get_quantity() ) . '
'; if ( $prices ) { $html .= dayslot_email_prices( $order, $t ); } return $html; } function dayslot_email_prices( $order, $t ) { $args = array( 'currency' => $order->get_currency() ); $html = ''; $html .= ''; $html .= ''; return $html . '
' . esc_html( $order->get_shipping_method() ? $order->get_shipping_method() : __( 'Delivery', 'dayslot' ) ) . '' . wp_kses_post( wc_price( (float) $order->get_shipping_total() + (float) $order->get_shipping_tax(), $args ) ) . '
' . esc_html__( 'Total', 'dayslot' ) . '' . wp_kses_post( wc_price( $order->get_total(), $args ) ) . '
'; } /** * A button for e-mails (in the shop's look). */ function dayslot_email_button( $url, $label, $t = null ) { $t = $t ? $t : dayslot_email_look(); if ( 'woo' === $t['id'] ) { $t = dayslot_email_palette(); } $outline = 'minimal' === ( isset( $t['id'] ) ? $t['id'] : '' ); $style = 'display:inline-block;padding:13px 26px;border-radius:' . (int) $t['btnr'] . 'px;text-decoration:none;font-weight:bold;' . ( $outline ? 'border:1.5px solid ' . $t['accent'] . ';color:' . $t['accent'] . ';background:transparent' : 'background:' . $t['accent'] . ';color:' . $t['on'] ); return '' . esc_html( $label ) . ''; } /** * Placeholder values for an order. */ function dayslot_email_values( $order ) { $values = array( '{shop}' => wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) ); if ( $order ) { $c = dayslot_order_choice( $order ); $values = array_merge( $values, array( '{name}' => $order->get_billing_first_name(), '{order}' => $order->get_order_number(), '{when}' => $c['date'] ? dayslot_format_date( $c['date'] ) . ( $c['slot'] ? ', ' . dayslot_format_slot( $c['slot'] ) : '' ) : '', '{place}' => 'pickup' === $c['mode'] && function_exists( 'dayslot_pickup_address' ) ? dayslot_pickup_address( $c['location'] ) : '', ) ); } return $values; } /** * Build an e-mail: array( subject, heading, body, html ). body = the content only (text + blocks); * html = the whole e-mail in the chosen look ('' for the WooCommerce look). $html_values = placeholders filled * with ready HTML (e.g. {button}); $plain = more text placeholders. */ function dayslot_email_compose( $id, $order = null, $plain = array(), $html_values = array(), $look = null ) { $tpl = dayslot_email_template( $id ); if ( ! $tpl ) { return null; } $t = $look ? $look : dayslot_email_look(); $woo = 'woo' === $t['id']; $values = array_merge( dayslot_email_values( $order ), $plain ); $blocks = array_flip( (array) $tpl['blocks'] ); $body = esc_html( strtr( $tpl['text'], $values ) ); // The delivery details go where the text says {details}, else under the text. $details = $order && isset( $blocks['details'] ) ? dayslot_email_details( $order, $woo ? null : $t ) : ''; if ( false !== strpos( $body, '{details}' ) ) { $body = str_replace( '{details}', "\n\n" . $details . "\n\n", $body ); $details = ''; } // Ready-made HTML (buttons) goes in as its own paragraph; a button the text lost is added at the end. foreach ( $html_values as $key => $value ) { if ( false === strpos( $body, esc_html( $key ) ) ) { $body .= "\n\n" . esc_html( $key ); } $body = str_replace( esc_html( $key ), "\n\n" . $value . "\n\n", $body ); } $body = preg_replace( "/\n{3,}/", "\n\n", trim( $body ) ); $out = wpautop( $body ); if ( ! $woo ) { $out = str_replace( '

', '

', $out ); } $out .= $details; if ( $order && isset( $blocks['products'] ) ) { $out .= dayslot_email_products( $order, $woo ? dayslot_email_palette() : $t, isset( $blocks['prices'] ) ); } elseif ( $order && isset( $blocks['prices'] ) ) { $out .= dayslot_email_prices( $order, $woo ? dayslot_email_palette() : $t ); } if ( $order && isset( $blocks['calendar'] ) && function_exists( 'dayslot_customer_event' ) && 'yes' === dayslot_get_settings()['add_to_calendar'] ) { $event = dayslot_customer_event( $order ); if ( $event ) { $link = 'font-weight:bold;color:' . ( $woo ? '#242e42' : $t['accent'] ); if ( ! $woo && in_array( $t['id'], array( 'soft', 'elegant' ), true ) ) { $link = 'font-weight:bold;color:' . ( isset( $t['brand'] ) ? $t['brand'] : $t['text'] ); } $out .= '

' . esc_html__( 'Add to calendar:', 'dayslot' ) . ' Google · Apple / Outlook

'; } } $subject = wp_strip_all_tags( strtr( $tpl['subject'], $values ) ); $heading = wp_strip_all_tags( strtr( $tpl['heading'], $values ) ); return array( 'subject' => $subject, 'heading' => $heading, 'body' => $out, 'html' => $woo || ! function_exists( 'dayslot_pro_email_wrap' ) ? '' : dayslot_pro_email_wrap( $t, $heading, $out, $order, isset( $blocks['contact'] ) ), ); } /** * Compose and send. Returns true when WooCommerce sent it. */ function dayslot_email_send( $id, $to, $order = null, $plain = array(), $html = array() ) { $mail = dayslot_email_compose( $id, $order, $plain, $html ); if ( ! $mail || ! $to || ! function_exists( 'WC' ) ) { return false; } $mailer = WC()->mailer(); $body = $mail['html'] ? $mail['html'] : $mailer->wrap_message( $mail['heading'], $mail['body'] ); return (bool) $mailer->send( $to, $mail['subject'], $body ); } add_filter( 'dayslot_default_settings', function ( $d ) { $d['emails'] = array(); return $d; } );