WC_Email_New_Order::trigger()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
eloc 10
nc 4
nop 1
1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit;
5
}
6
7
if ( ! class_exists( 'WC_Email_New_Order' ) ) :
8
9
/**
10
 * New Order Email.
11
 *
12
 * An email sent to the admin when a new order is received/paid for.
13
 *
14
 * @class       WC_Email_New_Order
15
 * @version     2.0.0
16
 * @package     WooCommerce/Classes/Emails
17
 * @author      WooThemes
18
 * @extends     WC_Email
19
 */
20
class WC_Email_New_Order extends WC_Email {
21
22
	/**
23
	 * Constructor.
24
	 */
25
	public function __construct() {
26
		$this->id               = 'new_order';
27
		$this->title            = __( 'New order', 'woocommerce' );
28
		$this->description      = __( 'New order emails are sent to chosen recipient(s) when a new order is received.', 'woocommerce' );
29
		$this->heading          = __( 'New customer order', 'woocommerce' );
30
		$this->subject          = __( '[{site_title}] New customer order ({order_number}) - {order_date}', 'woocommerce' );
31
		$this->template_html    = 'emails/admin-new-order.php';
32
		$this->template_plain   = 'emails/plain/admin-new-order.php';
33
34
		// Triggers for this email
35
		add_action( 'woocommerce_order_status_pending_to_processing_notification', array( $this, 'trigger' ) );
36
		add_action( 'woocommerce_order_status_pending_to_completed_notification', array( $this, 'trigger' ) );
37
		add_action( 'woocommerce_order_status_pending_to_on-hold_notification', array( $this, 'trigger' ) );
38
		add_action( 'woocommerce_order_status_failed_to_processing_notification', array( $this, 'trigger' ) );
39
		add_action( 'woocommerce_order_status_failed_to_completed_notification', array( $this, 'trigger' ) );
40
		add_action( 'woocommerce_order_status_failed_to_on-hold_notification', array( $this, 'trigger' ) );
41
42
		// Call parent constructor
43
		parent::__construct();
44
45
		// Other settings
46
		$this->recipient = $this->get_option( 'recipient', get_option( 'admin_email' ) );
47
	}
48
49
	/**
50
	 * Trigger.
51
	 *
52
	 * @param int $order_id
53
	 */
54
	public function trigger( $order_id ) {
55
		if ( $order_id ) {
56
			$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...
57
			$this->find['order-date']      = '{order_date}';
58
			$this->find['order-number']    = '{order_number}';
59
			$this->replace['order-date']   = date_i18n( wc_date_format(), strtotime( $this->object->order_date ) );
60
			$this->replace['order-number'] = $this->object->get_order_number();
61
		}
62
63
		if ( ! $this->is_enabled() || ! $this->get_recipient() ) {
64
			return;
65
		}
66
67
		$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
68
	}
69
70
	/**
71
	 * Get content html.
72
	 *
73
	 * @access public
74
	 * @return string
75
	 */
76
	public function get_content_html() {
77
		return wc_get_template_html( $this->template_html, array(
78
			'order'         => $this->object,
79
			'email_heading' => $this->get_heading(),
80
			'sent_to_admin' => true,
81
			'plain_text'    => false,
82
			'email'			=> $this
83
		) );
84
	}
85
86
	/**
87
	 * Get content plain.
88
	 *
89
	 * @access public
90
	 * @return string
91
	 */
92
	public function get_content_plain() {
93
		return wc_get_template_html( $this->template_plain, array(
94
			'order'         => $this->object,
95
			'email_heading' => $this->get_heading(),
96
			'sent_to_admin' => true,
97
			'plain_text'    => true,
98
			'email'			=> $this
99
		) );
100
	}
101
102
	/**
103
	 * Initialise settings form fields.
104
	 */
105
	public function init_form_fields() {
106
		$this->form_fields = array(
107
			'enabled' => array(
108
				'title'         => __( 'Enable/Disable', 'woocommerce' ),
109
				'type'          => 'checkbox',
110
				'label'         => __( 'Enable this email notification', 'woocommerce' ),
111
				'default'       => 'yes'
112
			),
113
			'recipient' => array(
114
				'title'         => __( 'Recipient(s)', 'woocommerce' ),
115
				'type'          => 'text',
116
				'description'   => sprintf( __( 'Enter recipients (comma separated) for this email. Defaults to <code>%s</code>.', 'woocommerce' ), esc_attr( get_option('admin_email') ) ),
117
				'placeholder'   => '',
118
				'default'       => '',
119
				'desc_tip'      => true
120
			),
121
			'subject' => array(
122
				'title'         => __( 'Subject', 'woocommerce' ),
123
				'type'          => 'text',
124
				'description'   => sprintf( __( 'This controls the email subject line. Leave blank to use the default subject: <code>%s</code>.', 'woocommerce' ), $this->subject ),
125
				'placeholder'   => '',
126
				'default'       => '',
127
				'desc_tip'      => true
128
			),
129
			'heading' => array(
130
				'title'         => __( 'Email Heading', 'woocommerce' ),
131
				'type'          => 'text',
132
				'description'   => sprintf( __( 'This controls the main heading contained within the email notification. Leave blank to use the default heading: <code>%s</code>.', 'woocommerce' ), $this->heading ),
133
				'placeholder'   => '',
134
				'default'       => '',
135
				'desc_tip'      => true
136
			),
137
			'email_type' => array(
138
				'title'         => __( 'Email type', 'woocommerce' ),
139
				'type'          => 'select',
140
				'description'   => __( 'Choose which format of email to send.', 'woocommerce' ),
141
				'default'       => 'html',
142
				'class'         => 'email_type wc-enhanced-select',
143
				'options'       => $this->get_email_type_options(),
144
				'desc_tip'      => true
145
			)
146
		);
147
	}
148
}
149
150
endif;
151
152
return new WC_Email_New_Order();
153