Passed
Push — master ( 15c473...1ac4fd )
by Paul
06:19
created
plugin/Modules/Email.php 1 patch
Indentation   +201 added lines, -201 removed lines patch added patch discarded remove patch
@@ -8,205 +8,205 @@
 block discarded – undo
8 8
 
9 9
 class Email
10 10
 {
11
-    /**
12
-     * @var array
13
-     */
14
-    public $attachments;
15
-
16
-    /**
17
-     * @var array
18
-     */
19
-    public $email;
20
-
21
-    /**
22
-     * @var array
23
-     */
24
-    public $headers;
25
-
26
-    /**
27
-     * @var string
28
-     */
29
-    public $message;
30
-
31
-    /**
32
-     * @var string
33
-     */
34
-    public $subject;
35
-
36
-    /**
37
-     * @var string|array
38
-     */
39
-    public $to;
40
-
41
-    /**
42
-     * @return Email
43
-     */
44
-    public function compose(array $email)
45
-    {
46
-        $this->normalize($email);
47
-        $this->attachments = $this->email['attachments'];
48
-        $this->headers = $this->buildHeaders();
49
-        $this->message = $this->buildHtmlMessage();
50
-        $this->subject = $this->email['subject'];
51
-        $this->to = $this->email['to'];
52
-        add_action('phpmailer_init', [$this, 'buildPlainTextMessage']);
53
-        return $this;
54
-    }
55
-
56
-    /**
57
-     * @param string $format
58
-     * @return string|null
59
-     */
60
-    public function read($format = '')
61
-    {
62
-        if ('plaintext' == $format) {
63
-            $message = $this->stripHtmlTags($this->message);
64
-            return apply_filters('site-reviews/email/message', $message, 'text', $this);
65
-        }
66
-        return $this->message;
67
-    }
68
-
69
-    /**
70
-     * @return void|bool
71
-     */
72
-    public function send()
73
-    {
74
-        if (!$this->message || !$this->subject || !$this->to) {
75
-            return;
76
-        }
77
-        add_action('wp_mail_failed', [$this, 'logMailError']);
78
-        $sent = wp_mail(
79
-            $this->to,
80
-            $this->subject,
81
-            $this->message,
82
-            $this->headers,
83
-            $this->attachments
84
-        );
85
-        remove_action('wp_mail_failed', [$this, 'logMailError']);
86
-        $this->reset();
87
-        return $sent;
88
-    }
89
-
90
-    /**
91
-     * @return void
92
-     * @action phpmailer_init
93
-     */
94
-    public function buildPlainTextMessage($phpmailer)
95
-    {
96
-        if (empty($this->email)) {
97
-            return;
98
-        }
99
-        if ('text/plain' === $phpmailer->ContentType || !empty($phpmailer->AltBody)) {
100
-            return;
101
-        }
102
-        $message = $this->stripHtmlTags($phpmailer->Body);
103
-        $phpmailer->AltBody = apply_filters('site-reviews/email/message', $message, 'text', $this);
104
-    }
105
-
106
-    /**
107
-     * @return array
108
-     */
109
-    protected function buildHeaders()
110
-    {
111
-        $allowed = [
112
-            'bcc', 'cc', 'from', 'reply-to',
113
-        ];
114
-        $headers = array_intersect_key($this->email, array_flip($allowed));
115
-        $headers = array_filter($headers);
116
-        foreach ($headers as $key => $value) {
117
-            unset($headers[$key]);
118
-            $headers[] = $key.': '.$value;
119
-        }
120
-        $headers[] = 'Content-Type: text/html';
121
-        return apply_filters('site-reviews/email/headers', $headers, $this);
122
-    }
123
-
124
-    /**
125
-     * @return string
126
-     */
127
-    protected function buildHtmlMessage()
128
-    {
129
-        $template = trim(glsr(OptionManager::class)->get('settings.general.notification_message'));
130
-        if (!empty($template)) {
131
-            $message = glsr(Template::class)->interpolate(
132
-                $template, 
133
-                ['context' => $this->email['template-tags']], 
134
-                $this->email['template']
135
-            );
136
-        } elseif ($this->email['template']) {
137
-            $message = glsr(Template::class)->build('templates/'.$this->email['template'], [
138
-                'context' => $this->email['template-tags'],
139
-            ]);
140
-        }
141
-        if (!isset($message)) {
142
-            $message = $this->email['message'];
143
-        }
144
-        $message = $this->email['before'].$message.$this->email['after'];
145
-        $message = strip_shortcodes($message);
146
-        $message = wptexturize($message);
147
-        $message = wpautop($message);
148
-        $message = str_replace('<> ', '', $message);
149
-        $message = str_replace(']]>', ']]>', $message);
150
-        $message = glsr(Template::class)->build('partials/email/index', [
151
-            'context' => ['message' => $message],
152
-        ]);
153
-        return apply_filters('site-reviews/email/message', stripslashes($message), 'html', $this);
154
-    }
155
-
156
-    /**
157
-     * @param \WP_Error $error
158
-     * @return void
159
-     */
160
-    protected function logMailError($error)
161
-    {
162
-        glsr_log()->error('Email was not sent (wp_mail failed)')
163
-            ->debug($this)
164
-            ->debug($error);
165
-    }
166
-
167
-    /**
168
-     * @return void
169
-     */
170
-    protected function normalize(array $email = [])
171
-    {
172
-        $email = shortcode_atts(glsr(EmailDefaults::class)->defaults(), $email);
173
-        if (empty($email['reply-to'])) {
174
-            $email['reply-to'] = $email['from'];
175
-        }
176
-        $this->email = apply_filters('site-reviews/email/compose', $email, $this);
177
-    }
178
-
179
-    /**
180
-     * @return void
181
-     */
182
-    protected function reset()
183
-    {
184
-        $this->attachments = [];
185
-        $this->email = [];
186
-        $this->headers = [];
187
-        $this->message = null;
188
-        $this->subject = null;
189
-        $this->to = null;
190
-    }
191
-
192
-    /**
193
-     * @return string
194
-     */
195
-    protected function stripHtmlTags($string)
196
-    {
197
-        // remove invisible elements
198
-        $string = preg_replace('@<(embed|head|noembed|noscript|object|script|style)[^>]*?>.*?</\\1>@siu', '', $string);
199
-        // replace certain elements with a line-break
200
-        $string = preg_replace('@</(div|h[1-9]|p|pre|tr)@iu', "\r\n\$0", $string);
201
-        // replace other elements with a space
202
-        $string = preg_replace('@</(td|th)@iu', ' $0', $string);
203
-        // add a placeholder for plain-text bullets to list elements
204
-        $string = preg_replace('@<(li)[^>]*?>@siu', '$0-o-^-o-', $string);
205
-        // strip all remaining HTML tags
206
-        $string = wp_strip_all_tags($string);
207
-        $string = wp_specialchars_decode($string, ENT_QUOTES);
208
-        $string = preg_replace('/\v(?:[\v\h]+){2,}/u', "\r\n\r\n", $string);
209
-        $string = str_replace('-o-^-o-', ' - ', $string);
210
-        return html_entity_decode($string, ENT_QUOTES, 'UTF-8');
211
-    }
11
+	/**
12
+	 * @var array
13
+	 */
14
+	public $attachments;
15
+
16
+	/**
17
+	 * @var array
18
+	 */
19
+	public $email;
20
+
21
+	/**
22
+	 * @var array
23
+	 */
24
+	public $headers;
25
+
26
+	/**
27
+	 * @var string
28
+	 */
29
+	public $message;
30
+
31
+	/**
32
+	 * @var string
33
+	 */
34
+	public $subject;
35
+
36
+	/**
37
+	 * @var string|array
38
+	 */
39
+	public $to;
40
+
41
+	/**
42
+	 * @return Email
43
+	 */
44
+	public function compose(array $email)
45
+	{
46
+		$this->normalize($email);
47
+		$this->attachments = $this->email['attachments'];
48
+		$this->headers = $this->buildHeaders();
49
+		$this->message = $this->buildHtmlMessage();
50
+		$this->subject = $this->email['subject'];
51
+		$this->to = $this->email['to'];
52
+		add_action('phpmailer_init', [$this, 'buildPlainTextMessage']);
53
+		return $this;
54
+	}
55
+
56
+	/**
57
+	 * @param string $format
58
+	 * @return string|null
59
+	 */
60
+	public function read($format = '')
61
+	{
62
+		if ('plaintext' == $format) {
63
+			$message = $this->stripHtmlTags($this->message);
64
+			return apply_filters('site-reviews/email/message', $message, 'text', $this);
65
+		}
66
+		return $this->message;
67
+	}
68
+
69
+	/**
70
+	 * @return void|bool
71
+	 */
72
+	public function send()
73
+	{
74
+		if (!$this->message || !$this->subject || !$this->to) {
75
+			return;
76
+		}
77
+		add_action('wp_mail_failed', [$this, 'logMailError']);
78
+		$sent = wp_mail(
79
+			$this->to,
80
+			$this->subject,
81
+			$this->message,
82
+			$this->headers,
83
+			$this->attachments
84
+		);
85
+		remove_action('wp_mail_failed', [$this, 'logMailError']);
86
+		$this->reset();
87
+		return $sent;
88
+	}
89
+
90
+	/**
91
+	 * @return void
92
+	 * @action phpmailer_init
93
+	 */
94
+	public function buildPlainTextMessage($phpmailer)
95
+	{
96
+		if (empty($this->email)) {
97
+			return;
98
+		}
99
+		if ('text/plain' === $phpmailer->ContentType || !empty($phpmailer->AltBody)) {
100
+			return;
101
+		}
102
+		$message = $this->stripHtmlTags($phpmailer->Body);
103
+		$phpmailer->AltBody = apply_filters('site-reviews/email/message', $message, 'text', $this);
104
+	}
105
+
106
+	/**
107
+	 * @return array
108
+	 */
109
+	protected function buildHeaders()
110
+	{
111
+		$allowed = [
112
+			'bcc', 'cc', 'from', 'reply-to',
113
+		];
114
+		$headers = array_intersect_key($this->email, array_flip($allowed));
115
+		$headers = array_filter($headers);
116
+		foreach ($headers as $key => $value) {
117
+			unset($headers[$key]);
118
+			$headers[] = $key.': '.$value;
119
+		}
120
+		$headers[] = 'Content-Type: text/html';
121
+		return apply_filters('site-reviews/email/headers', $headers, $this);
122
+	}
123
+
124
+	/**
125
+	 * @return string
126
+	 */
127
+	protected function buildHtmlMessage()
128
+	{
129
+		$template = trim(glsr(OptionManager::class)->get('settings.general.notification_message'));
130
+		if (!empty($template)) {
131
+			$message = glsr(Template::class)->interpolate(
132
+				$template, 
133
+				['context' => $this->email['template-tags']], 
134
+				$this->email['template']
135
+			);
136
+		} elseif ($this->email['template']) {
137
+			$message = glsr(Template::class)->build('templates/'.$this->email['template'], [
138
+				'context' => $this->email['template-tags'],
139
+			]);
140
+		}
141
+		if (!isset($message)) {
142
+			$message = $this->email['message'];
143
+		}
144
+		$message = $this->email['before'].$message.$this->email['after'];
145
+		$message = strip_shortcodes($message);
146
+		$message = wptexturize($message);
147
+		$message = wpautop($message);
148
+		$message = str_replace('&lt;&gt; ', '', $message);
149
+		$message = str_replace(']]>', ']]&gt;', $message);
150
+		$message = glsr(Template::class)->build('partials/email/index', [
151
+			'context' => ['message' => $message],
152
+		]);
153
+		return apply_filters('site-reviews/email/message', stripslashes($message), 'html', $this);
154
+	}
155
+
156
+	/**
157
+	 * @param \WP_Error $error
158
+	 * @return void
159
+	 */
160
+	protected function logMailError($error)
161
+	{
162
+		glsr_log()->error('Email was not sent (wp_mail failed)')
163
+			->debug($this)
164
+			->debug($error);
165
+	}
166
+
167
+	/**
168
+	 * @return void
169
+	 */
170
+	protected function normalize(array $email = [])
171
+	{
172
+		$email = shortcode_atts(glsr(EmailDefaults::class)->defaults(), $email);
173
+		if (empty($email['reply-to'])) {
174
+			$email['reply-to'] = $email['from'];
175
+		}
176
+		$this->email = apply_filters('site-reviews/email/compose', $email, $this);
177
+	}
178
+
179
+	/**
180
+	 * @return void
181
+	 */
182
+	protected function reset()
183
+	{
184
+		$this->attachments = [];
185
+		$this->email = [];
186
+		$this->headers = [];
187
+		$this->message = null;
188
+		$this->subject = null;
189
+		$this->to = null;
190
+	}
191
+
192
+	/**
193
+	 * @return string
194
+	 */
195
+	protected function stripHtmlTags($string)
196
+	{
197
+		// remove invisible elements
198
+		$string = preg_replace('@<(embed|head|noembed|noscript|object|script|style)[^>]*?>.*?</\\1>@siu', '', $string);
199
+		// replace certain elements with a line-break
200
+		$string = preg_replace('@</(div|h[1-9]|p|pre|tr)@iu', "\r\n\$0", $string);
201
+		// replace other elements with a space
202
+		$string = preg_replace('@</(td|th)@iu', ' $0', $string);
203
+		// add a placeholder for plain-text bullets to list elements
204
+		$string = preg_replace('@<(li)[^>]*?>@siu', '$0-o-^-o-', $string);
205
+		// strip all remaining HTML tags
206
+		$string = wp_strip_all_tags($string);
207
+		$string = wp_specialchars_decode($string, ENT_QUOTES);
208
+		$string = preg_replace('/\v(?:[\v\h]+){2,}/u', "\r\n\r\n", $string);
209
+		$string = str_replace('-o-^-o-', ' - ', $string);
210
+		return html_entity_decode($string, ENT_QUOTES, 'UTF-8');
211
+	}
212 212
 }
Please login to merge, or discard this patch.