Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 98
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Bad_Address_Email
0.00% covered (danger)
0.00%
0 / 98
0.00% covered (danger)
0.00%
0 / 8
182
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 get_default_subject
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_default_heading
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 trigger
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
42
 get_content_html
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 get_content_plain
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 get_default_additional_content
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 init_form_fields
0.00% covered (danger)
0.00%
0 / 51
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3
4namespace BrianHenryIE\WC_Address_Validation\WooCommerce\Email;
5
6use WC_Email;
7use WC_Order;
8use BrianHenryIE\WC_Address_Validation\WooCommerce\Order_Status;
9
10class Bad_Address_Email extends WC_Email {
11
12
13    /**
14     * Create an instance of the class.
15     *
16     * @access public
17     * @return void
18     */
19    public function __construct() {
20
21        $this->id             = 'bad_address_admin';
22        $this->title          = __( 'Bad Address', 'bh-wc-address-validation' );
23        $this->description    = __( 'An email sent to the admin when an order\'s address is not recognized by USPS.', 'bh-wc-address-validation' );
24        $this->template_base  = WP_PLUGIN_DIR . '/bh-wc-address-validation/woocommerce/templates/';
25        $this->template_html  = 'emails/bad-address.php';
26        $this->template_plain = 'emails/plain/bad-address.php';
27        $this->placeholders   = array(
28            '{order_date}'   => '',
29            '{order_number}' => '',
30        );
31
32        // Action to which we hook onto to send the email.
33        add_action( 'woocommerce_order_status_' . Order_Status::BAD_ADDRESS_STATUS, array( $this, 'trigger' ) );
34
35        parent::__construct();
36    }
37
38
39    /**
40     * Get email subject.
41     *
42     * @since  3.1.0
43     * @return string
44     */
45    public function get_default_subject() {
46        return __( '[{site_title}]: Order #{order_number} has failed USPS address verification', 'bh-wc-address-validation' );
47    }
48
49    /**
50     * Get email heading.
51     *
52     * @since  3.1.0
53     * @return string
54     */
55    public function get_default_heading() {
56        return __( 'Bad Address: #{order_number}', 'bh-wc-address-validation' );
57    }
58
59    /**
60     * Trigger the sending of this email.
61     *
62     * @param int            $order_id The order ID.
63     * @param WC_Order|false $order Order object.
64     */
65    public function trigger( $order_id, $order = false ) {
66        $this->setup_locale();
67
68        if ( $order_id && ! is_a( $order, WC_Order::class ) ) {
69            $order = wc_get_order( $order_id );
70        }
71
72        if ( is_a( $order, WC_Order::class ) ) {
73            $this->object                         = $order;
74            $this->placeholders['{order_date}']   = wc_format_datetime( $this->object->get_date_created() );
75            $this->placeholders['{order_number}'] = $this->object->get_order_number();
76        }
77
78        if ( $this->is_enabled() && $this->get_recipient() ) {
79            $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
80        }
81
82        $this->restore_locale();
83    }
84
85    /**
86     * Get content html.
87     *
88     * @return string
89     */
90    public function get_content_html() {
91        return wc_get_template_html(
92            $this->template_html,
93            array(
94                'order'              => $this->object,
95                'email_heading'      => $this->get_heading(),
96                'additional_content' => $this->get_additional_content(),
97                'sent_to_admin'      => true,
98                'plain_text'         => false,
99                'email'              => $this,
100            )
101        );
102    }
103
104    /**
105     * Get content plain.
106     *
107     * @return string
108     */
109    public function get_content_plain() {
110        return wc_get_template_html(
111            $this->template_plain,
112            array(
113                'order'              => $this->object,
114                'email_heading'      => $this->get_heading(),
115                'additional_content' => $this->get_additional_content(),
116                'sent_to_admin'      => true,
117                'plain_text'         => true,
118                'email'              => $this,
119            )
120        );
121    }
122
123    /**
124     * Default content to show below main email content.
125     *
126     * @since 3.7.0
127     * @return string
128     */
129    public function get_default_additional_content() {
130        return __( 'The shipping address for this order needs to be corrected before the order can be processed. This can be done with <a target="_blank" href="https://tools.usps.com/zip-code-lookup.htm?byaddress">USPS Zip Code Lookup</a> or by contacting the customer.', 'bh-wc-address-validation' );
131    }
132
133    /**
134     * Initialise settings form fields.
135     *
136     * @return void
137     */
138    public function init_form_fields() {
139        /* translators: %s: list of placeholders */
140        $placeholder_text  = sprintf( __( 'Available placeholders: %s', 'bh-wc-address-validation' ), '<code>' . esc_html( implode( '</code>, <code>', array_keys( $this->placeholders ) ) ) . '</code>' );
141        $this->form_fields = array(
142            'enabled'            => array(
143                'title'   => __( 'Enable/Disable', 'bh-wc-address-validation' ),
144                'type'    => 'checkbox',
145                'label'   => __( 'Enable this email notification', 'bh-wc-address-validation' ),
146                'default' => 'no',
147            ),
148            'recipient'          => array(
149                'title'       => __( 'Recipient(s)', 'bh-wc-address-validation' ),
150                'type'        => 'text',
151                /* translators: %s: WP admin email */
152                'description' => sprintf( __( 'Enter recipients (comma separated) for this email. Defaults to %s.', 'bh-wc-address-validation' ), '<code>' . esc_attr( get_option( 'admin_email' ) ) . '</code>' ),
153                'placeholder' => '',
154                'default'     => '',
155                'desc_tip'    => true,
156            ),
157            'subject'            => array(
158                'title'       => __( 'Subject', 'bh-wc-address-validation' ),
159                'type'        => 'text',
160                'desc_tip'    => true,
161                'description' => $placeholder_text,
162                'placeholder' => $this->get_default_subject(),
163                'default'     => '',
164            ),
165            'heading'            => array(
166                'title'       => __( 'Email heading', 'bh-wc-address-validation' ),
167                'type'        => 'text',
168                'desc_tip'    => true,
169                'description' => $placeholder_text,
170                'placeholder' => $this->get_default_heading(),
171                'default'     => '',
172            ),
173            'additional_content' => array(
174                'title'       => __( 'Additional content', 'bh-wc-address-validation' ),
175                'description' => __( 'Text to appear below the main email content.', 'bh-wc-address-validation' ) . ' ' . $placeholder_text,
176                'css'         => 'width:400px; height: 75px;',
177                'placeholder' => __( 'N/A', 'bh-wc-address-validation' ),
178                'type'        => 'textarea',
179                'default'     => $this->get_default_additional_content(),
180                'desc_tip'    => true,
181            ),
182            'email_type'         => array(
183                'title'       => __( 'Email type', 'bh-wc-address-validation' ),
184                'type'        => 'select',
185                'description' => __( 'Choose which format of email to send.', 'bh-wc-address-validation' ),
186                'default'     => 'html',
187                'class'       => 'email_type wc-enhanced-select',
188                'options'     => $this->get_email_type_options(),
189                'desc_tip'    => true,
190            ),
191        );
192    }
193}