Passed
Pull Request — master (#410)
by Brian
04:58
created

GetPaid_Notification_Email::get_attachments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 2
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 an option
42
	 *
43
     * @return mixed
44
	 */
45
	public function get_option( $key ) {
46
47
        $key   = "email_{$this->id}_$key";
48
        $value = wpinv_get_option( $key, null );
49
50
        if ( is_null( $value ) ) {
0 ignored issues
show
introduced by
The condition is_null($value) is always false.
Loading history...
51
            $options = wpinv_get_emails();
52
53
            if ( ! isset( $options[ $this->id ] ) || ! isset( $options[ $this->id ][ $key ] ) ) {
54
                return '';
55
            }
56
57
            $value = isset( $options[ $this->id ][ $key ]['std'] ) ? $options[ $this->id ][ $key ]['std'] : '';
58
        }
59
60
        return $value;
61
    }
62
63
    /**
64
	 * Retrieves the email body.
65
	 *
66
     * @return string
67
	 */
68
	public function get_body() {
69
        $body = $this->get_option( 'body' );
70
        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...
71
    }
72
73
    /**
74
	 * Retrieves the email subject.
75
	 *
76
     * @return string
77
	 */
78
	public function get_subject() {
79
        $subject = $this->get_option( 'subject' );
80
        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...
81
    }
82
83
    /**
84
	 * Retrieves the email heading.
85
	 *
86
     * @return string
87
	 */
88
	public function get_heading() {
89
        $heading = $this->get_option( 'heading' );
90
        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...
91
    }
92
93
    /**
94
	 * Checks if an email is active.
95
	 *
96
     * @return bool
97
	 */
98
	public function is_active() {
99
        $is_active = ! empty( $this->get_option( 'is_active' ) );
100
        return apply_filters( 'getpaid_email_type_is_active', $is_active, $this->id, $this->object );
101
    }
102
103
    /**
104
	 * Checks if the site's admin should receive email notifications.
105
	 *
106
     * @return bool
107
	 */
108
	public function include_admin_bcc() {
109
        $include_admin_bcc = ! empty( $this->get_option( 'admin_bcc' ) );
110
        return apply_filters( 'getpaid_email_type_include_admin_bcc', $include_admin_bcc, $this->id, $this->object );
111
    }
112
113
    /**
114
	 * Checks whether this email should be sent to the customer or admin.
115
	 *
116
     * @return bool
117
	 */
118
	public function is_admin_email() {
119
        $is_admin_email = in_array( $this->id, array( 'new_invoice', 'cancelled_invoice', 'failed_invoice' ) );
120
        return apply_filters( 'getpaid_email_type_is_admin_email', $is_admin_email, $this->id, $this->object );
121
    }
122
123
    /**
124
	 * Returns email attachments.
125
	 *
126
     * @return array
127
	 */
128
	public function get_attachments() {
129
        return apply_filters( 'getpaid_get_email_attachments', array(), $this->id, $this->object );
130
    }
131
132
    /**
133
	 * Returns an array of merge tags.
134
	 *
135
     * @return array
136
	 */
137
	public function get_merge_tags() {
138
139
        $merge_tags = array(
140
            '{site_title}' => wpinv_get_blogname(),
141
            '{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

141
            '{date}'       => date_i18n( /** @scrutinizer ignore-type */ get_option( 'date_format' ), current_time( 'timestamp' ) ),
Loading history...
142
        );
143
144
        return apply_filters( 'getpaid_get_email_merge_tags', $merge_tags, $this->id, $this->object );
145
    }
146
147
    /**
148
	 * Adds merge tags to a text.
149
	 *
150
     * @param string string $text
151
     * @param array $merge_tags
152
     * @return string
153
	 */
154
	public function add_merge_tags( $text, $merge_tags = array() ) {
155
156
        foreach ( $merge_tags as $key => $value ) {
157
            $text = str_replace( $key, $value, $text );
158
        }
159
160
        return wptexturize( $text );
161
    }
162
163
    /**
164
	 * Returns the email content
165
	 *
166
     * @param array $merge_tags
167
     * @param array $extra_args Extra template args
168
     * @return string
169
	 */
170
	public function get_content( $merge_tags = array(), $extra_args = array() ) {
171
172
        $content = wpinv_get_template_html(
173
            "emails/wpinv-email-{$this->id}.php",
174
            array_merge(
175
                $extra_args,
176
                array(
177
                    'invoice'       => $this->object, // Backwards compat.
178
                    'object'        => $this->object,
179
                    'email_type'    => $this->id,
180
                    'email_heading' => $this->add_merge_tags( $this->get_heading(), $merge_tags ),
181
                    'sent_to_admin' => $this->is_admin_email(),
182
                    'plain_text'    => false,
183
                    'message_body'  => wpautop( $this->add_merge_tags( $this->get_body(), $merge_tags ) ),
184
                )
185
            )
186
        );
187
188
        return wpinv_email_style_body( $content );
189
    }
190
191
}
192