Passed
Push — master ( 258987...abdd42 )
by Brian
11:44 queued 05:35
created

GetPaid_Notification_Email::is_admin_email()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Contains the notification email class.
4
 *
5
 */
6
7
defined( 'ABSPATH' ) || exit;
8
9
/**
10
 * Represents a single email type.
11
 *
12
 */
13
class GetPaid_Notification_Email {
14
15
    /**
16
	 * Contains the type of this notification email.
17
	 *
18
	 * @var string
19
	 */
20
    public $id;
21
22
    /**
23
	 * Contains any object to use in filters.
24
	 *
25
	 * @var false|WPInv_Invoice|WPInv_Item|WPInv_Subscription
26
	 */
27
    public $object;
28
29
    /**
30
	 * Class constructor.
31
	 *
32
     * @param string $id Email Type.
33
     * @param mixed $object Optional. Associated object.
34
	 */
35
	public function __construct( $id, $object = false ) {
36
        $this->id     = $id;
37
        $this->object = $object;
38
    }
39
40
    /**
41
	 * Retrieves the email body.
42
	 *
43
     * @return string
44
	 */
45
	public function get_body() {
46
        $body = wpinv_get_option( "email_{$this->id}_body" );
47
        return apply_filters( 'getpaid_get_email_body', $body, $this->id, $this->object );
0 ignored issues
show
Bug Best Practice introduced by
The expression return apply_filters('ge...his->id, $this->object) also could return the type array which is incompatible with the documented return type string.
Loading history...
48
    }
49
50
    /**
51
	 * Retrieves the email subject.
52
	 *
53
     * @return string
54
	 */
55
	public function get_subject() {
56
        $subject = wpinv_get_option( "email_{$this->id}_subject" );
57
        return apply_filters( 'getpaid_get_email_subject', $subject, $this->id, $this->object );
0 ignored issues
show
Bug Best Practice introduced by
The expression return apply_filters('ge...his->id, $this->object) also could return the type array which is incompatible with the documented return type string.
Loading history...
58
    }
59
60
    /**
61
	 * Retrieves the email heading.
62
	 *
63
     * @return string
64
	 */
65
	public function get_heading() {
66
        $heading = wpinv_get_option( "email_{$this->id}_heading" );
67
        return apply_filters( 'getpaid_get_email_heading', $heading, $this->id, $this->object );
0 ignored issues
show
Bug Best Practice introduced by
The expression return apply_filters('ge...his->id, $this->object) also could return the type array which is incompatible with the documented return type string.
Loading history...
68
    }
69
70
    /**
71
	 * Checks if an email is active.
72
	 *
73
     * @return bool
74
	 */
75
	public function is_active() {
76
        $is_active = (bool) wpinv_get_option( "email_{$this->id}_active", false );
77
        return apply_filters( 'getpaid_email_type_is_active', $is_active, $this->id, $this->object );
78
    }
79
80
    /**
81
	 * Checks if the site's admin should receive email notifications.
82
	 *
83
     * @return bool
84
	 */
85
	public function include_admin_bcc() {
86
        $include_admin_bcc = (bool) wpinv_get_option( "email_{$this->id}_admin_bcc", false );
87
        return apply_filters( 'getpaid_email_type_include_admin_bcc', $include_admin_bcc, $this->id, $this->object );
88
    }
89
90
    /**
91
	 * Checks whether this email should be sent to the customer or admin.
92
	 *
93
     * @return bool
94
	 */
95
	public function is_admin_email() {
96
        $is_admin_email = in_array( $this->id, array( 'new_invoice', 'cancelled_invoice', 'failed_invoice' ) );
97
        return apply_filters( 'getpaid_email_type_is_admin_email', $is_admin_email, $this->id, $this->object );
98
    }
99
100
    /**
101
	 * Returns email attachments.
102
	 *
103
     * @return array
104
	 */
105
	public function get_attachments() {
106
        return apply_filters( 'getpaid_get_email_attachments', array(), $this->id, $this->object );
107
    }
108
109
    /**
110
	 * Returns an array of merge tags.
111
	 *
112
     * @return array
113
	 */
114
	public function get_merge_tags() {
115
116
        $merge_tags = array(
117
            '{site_title}' => wpinv_get_blogname(),
118
            '{date}'       => date_i18n( get_option( 'date_format' ), current_time( 'timestamp' ) ),
0 ignored issues
show
Bug introduced by
It seems like get_option('date_format') can also be of type false; however, parameter $format of date_i18n() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

118
            '{date}'       => date_i18n( /** @scrutinizer ignore-type */ get_option( 'date_format' ), current_time( 'timestamp' ) ),
Loading history...
119
        );
120
121
        return apply_filters( 'getpaid_get_email_merge_tags', $merge_tags, $this->id, $this->object );
122
    }
123
124
    /**
125
	 * Adds merge tags to a text.
126
	 *
127
     * @param string string $text
128
     * @param array $merge_tags
129
     * @return string
130
	 */
131
	public function add_merge_tags( $text, $merge_tags = array() ) {
132
133
        foreach ( $merge_tags as $key => $value ) {
134
            $text = str_replace( $key, $value, $text );
135
        }
136
137
        return wptexturize( $text );
138
    }
139
140
    /**
141
	 * Returns the email content
142
	 *
143
     * @param array $merge_tags
144
     * @param array $extra_args Extra template args
145
     * @return string
146
	 */
147
	public function get_content( $merge_tags = array(), $extra_args = array() ) {
148
149
        return wpinv_get_template_html(
150
            "emails/wpinv-email-{$this->id}.php",
151
            array_merge(
152
                $extra_args,
153
                array(
154
                    'invoice'       => $this->object, // Backwards compat.
155
                    'object'        => $this->object,
156
                    'email_type'    => $this->id,
157
                    'email_heading' => $this->add_merge_tags( $this->get_heading(), $merge_tags ),
158
                    'sent_to_admin' => $this->is_admin_email(),
159
                    'plain_text'    => false,
160
                    'message_body'  => wpautop( $this->add_merge_tags( $this->get_body(), $merge_tags ) ),
161
                )
162
            )
163
        );
164
165
    }
166
167
}
168