WC_Email_Customer_Completed_Order::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
eloc 13
nc 1
nop 0
1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit; // Exit if accessed directly
5
}
6
7
if ( ! class_exists( 'WC_Email_Customer_Completed_Order' ) ) :
8
9
/**
10
 * Customer Completed Order Email.
11
 *
12
 * Order complete emails are sent to the customer when the order is marked complete and usual indicates that the order has been shipped.
13
 *
14
 * @class       WC_Email_Customer_Completed_Order
15
 * @version     2.0.0
16
 * @package     WooCommerce/Classes/Emails
17
 * @author      WooThemes
18
 * @extends     WC_Email
19
 */
20
class WC_Email_Customer_Completed_Order extends WC_Email {
21
22
	/**
23
	 * Constructor.
24
	 */
25
	public function __construct() {
26
27
		$this->id             = 'customer_completed_order';
28
		$this->customer_email = true;
29
		$this->title          = __( 'Completed order', 'woocommerce' );
30
		$this->description    = __( 'Order complete emails are sent to customers when their orders are marked completed and usually indicate that their orders have been shipped.', 'woocommerce' );
31
32
		$this->heading        = __( 'Your order is complete', 'woocommerce' );
33
		$this->subject        = __( 'Your {site_title} order from {order_date} is complete', 'woocommerce' );
34
35
		$this->template_html  = 'emails/customer-completed-order.php';
36
		$this->template_plain = 'emails/plain/customer-completed-order.php';
37
38
		// Triggers for this email
39
		add_action( 'woocommerce_order_status_completed_notification', array( $this, 'trigger' ) );
40
41
		// Other settings
42
		$this->heading_downloadable = $this->get_option( 'heading_downloadable', __( 'Your order is complete - download your files', 'woocommerce' ) );
43
		$this->subject_downloadable = $this->get_option( 'subject_downloadable', __( 'Your {site_title} order from {order_date} is complete - download your files', 'woocommerce' ) );
44
45
		// Call parent constuctor
46
		parent::__construct();
47
	}
48
49
	/**
50
	 * Trigger.
51
	 *
52
	 * @param int $order_id
53
	 */
54
	public function trigger( $order_id ) {
55
56
		if ( $order_id ) {
57
			$this->object                  = wc_get_order( $order_id );
0 ignored issues
show
Documentation Bug introduced by
It seems like wc_get_order($order_id) can also be of type false. However, the property $object is declared as type object. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
58
			$this->recipient               = $this->object->billing_email;
59
60
			$this->find['order-date']      = '{order_date}';
61
			$this->find['order-number']    = '{order_number}';
62
63
			$this->replace['order-date']   = date_i18n( wc_date_format(), strtotime( $this->object->order_date ) );
64
			$this->replace['order-number'] = $this->object->get_order_number();
65
		}
66
67
		if ( ! $this->is_enabled() || ! $this->get_recipient() ) {
68
			return;
69
		}
70
71
		$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
72
	}
73
74
	/**
75
	 * Get email subject.
76
	 *
77
	 * @access public
78
	 * @return string
79
	 */
80 View Code Duplication
	public function get_subject() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
		if ( ! empty( $this->object ) && $this->object->has_downloadable_item() ) {
82
			return apply_filters( 'woocommerce_email_subject_customer_completed_order', $this->format_string( $this->subject_downloadable ), $this->object );
83
		} else {
84
			return apply_filters( 'woocommerce_email_subject_customer_completed_order', $this->format_string( $this->subject ), $this->object );
85
		}
86
	}
87
88
	/**
89
	 * Get email heading.
90
	 *
91
	 * @access public
92
	 * @return string
93
	 */
94 View Code Duplication
	public function get_heading() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
		if ( ! empty( $this->object ) && $this->object->has_downloadable_item() ) {
96
			return apply_filters( 'woocommerce_email_heading_customer_completed_order', $this->format_string( $this->heading_downloadable ), $this->object );
97
		} else {
98
			return apply_filters( 'woocommerce_email_heading_customer_completed_order', $this->format_string( $this->heading ), $this->object );
99
		}
100
	}
101
102
	/**
103
	 * Get content html.
104
	 *
105
	 * @access public
106
	 * @return string
107
	 */
108
	public function get_content_html() {
109
		return wc_get_template_html( $this->template_html, array(
110
			'order'         => $this->object,
111
			'email_heading' => $this->get_heading(),
112
			'sent_to_admin' => false,
113
			'plain_text'    => false,
114
			'email'			=> $this
115
		) );
116
	}
117
118
	/**
119
	 * Get content plain.
120
	 *
121
	 * @return string
122
	 */
123
	public function get_content_plain() {
124
		return wc_get_template_html( $this->template_plain, array(
125
			'order'         => $this->object,
126
			'email_heading' => $this->get_heading(),
127
			'sent_to_admin' => false,
128
			'plain_text'    => true,
129
			'email'			=> $this
130
		) );
131
	}
132
133
	/**
134
	 * Initialise settings form fields.
135
	 */
136
	public function init_form_fields() {
137
		$this->form_fields = array(
138
			'enabled' => array(
139
				'title'         => __( 'Enable/Disable', 'woocommerce' ),
140
				'type'          => 'checkbox',
141
				'label'         => __( 'Enable this email notification', 'woocommerce' ),
142
				'default'       => 'yes'
143
			),
144
			'subject' => array(
145
				'title'         => __( 'Subject', 'woocommerce' ),
146
				'type'          => 'text',
147
				'description'   => sprintf( __( 'Defaults to <code>%s</code>', 'woocommerce' ), $this->subject ),
148
				'placeholder'   => '',
149
				'default'       => '',
150
				'desc_tip'      => true
151
			),
152
			'heading' => array(
153
				'title'         => __( 'Email Heading', 'woocommerce' ),
154
				'type'          => 'text',
155
				'description'   => sprintf( __( 'Defaults to <code>%s</code>', 'woocommerce' ), $this->heading ),
156
				'placeholder'   => '',
157
				'default'       => '',
158
				'desc_tip'      => true
159
			),
160
			'subject_downloadable' => array(
161
				'title'         => __( 'Subject (downloadable)', 'woocommerce' ),
162
				'type'          => 'text',
163
				'description'   => sprintf( __( 'Defaults to <code>%s</code>', 'woocommerce' ), $this->subject_downloadable ),
164
				'placeholder'   => '',
165
				'default'       => '',
166
				'desc_tip'      => true
167
			),
168
			'heading_downloadable' => array(
169
				'title'         => __( 'Email Heading (downloadable)', 'woocommerce' ),
170
				'type'          => 'text',
171
				'description'   => sprintf( __( 'Defaults to <code>%s</code>', 'woocommerce' ), $this->heading_downloadable ),
172
				'placeholder'   => '',
173
				'default'       => '',
174
				'desc_tip'      => true
175
			),
176
			'email_type' => array(
177
				'title'         => __( 'Email type', 'woocommerce' ),
178
				'type'          => 'select',
179
				'description'   => __( 'Choose which format of email to send.', 'woocommerce' ),
180
				'default'       => 'html',
181
				'class'         => 'email_type wc-enhanced-select',
182
				'options'       => $this->get_email_type_options(),
183
				'desc_tip'      => true
184
			)
185
		);
186
	}
187
}
188
189
endif;
190
191
return new WC_Email_Customer_Completed_Order();
192