Completed
Push — develop ( 0cc879...eb6668 )
by Paul
02:07
created
src/Services/Email.php 1 patch
Spacing   +63 added lines, -63 removed lines patch added patch discarded remove patch
@@ -36,7 +36,7 @@  discard block
 block discarded – undo
36 36
 	 */
37 37
 	protected $to;
38 38
 
39
-	public function __construct( Template $template )
39
+	public function __construct(Template $template)
40 40
 	{
41 41
 		$this->template = $template;
42 42
 	}
@@ -44,19 +44,19 @@  discard block
 block discarded – undo
44 44
 	/**
45 45
 	 * @return Email
46 46
 	 */
47
-	public function compose( array $email )
47
+	public function compose(array $email)
48 48
 	{
49
-		$email = $this->normalize( $email );
49
+		$email = $this->normalize($email);
50 50
 
51 51
 		$this->attachments = $email['attachments'];
52
-		$this->headers     = $this->buildHeaders( $email );
53
-		$this->message     = $this->buildHtmlMessage( $email );
52
+		$this->headers     = $this->buildHeaders($email);
53
+		$this->message     = $this->buildHtmlMessage($email);
54 54
 		$this->subject     = $email['subject'];
55 55
 		$this->to          = $email['to'];
56 56
 
57
-		add_action( 'phpmailer_init', function( PHPMailer $phpmailer ) {
58
-			if( $phpmailer->ContentType === 'text/plain' || !empty( $phpmailer->AltBody ))return;
59
-			$phpmailer->AltBody = $this->buildPlainTextMessage( $phpmailer->Body );
57
+		add_action('phpmailer_init', function(PHPMailer $phpmailer) {
58
+			if ($phpmailer->ContentType === 'text/plain' || !empty($phpmailer->AltBody))return;
59
+			$phpmailer->AltBody = $this->buildPlainTextMessage($phpmailer->Body);
60 60
 		});
61 61
 
62 62
 		return $this;
@@ -67,10 +67,10 @@  discard block
 block discarded – undo
67 67
 	 *
68 68
 	 * @return string|null
69 69
 	 */
70
-	public function read( $plaintext = false )
70
+	public function read($plaintext = false)
71 71
 	{
72 72
 		return $plaintext
73
-			? $this->buildPlainTextMessage( $this->message )
73
+			? $this->buildPlainTextMessage($this->message)
74 74
 			: $this->message;
75 75
 	}
76 76
 
@@ -79,7 +79,7 @@  discard block
 block discarded – undo
79 79
 	 */
80 80
 	public function send()
81 81
 	{
82
-		if( !$this->message || !$this->subject || !$this->to )return;
82
+		if (!$this->message || !$this->subject || !$this->to)return;
83 83
 
84 84
 		$sent = wp_mail(
85 85
 			$this->to,
@@ -97,7 +97,7 @@  discard block
 block discarded – undo
97 97
 	/**
98 98
 	 * @return array
99 99
 	 */
100
-	protected function buildHeaders( array $email )
100
+	protected function buildHeaders(array $email)
101 101
 	{
102 102
 		$allowed = [
103 103
 			'bcc',
@@ -106,34 +106,34 @@  discard block
 block discarded – undo
106 106
 			'reply-to',
107 107
 		];
108 108
 
109
-		$headers = array_intersect_key( $email, array_flip( $allowed ));
110
-		$headers = array_filter( $headers );
109
+		$headers = array_intersect_key($email, array_flip($allowed));
110
+		$headers = array_filter($headers);
111 111
 
112
-		foreach( $headers as $key => $value ) {
113
-			unset( $headers[ $key ] );
114
-			$headers[] = sprintf( '%s: %s', $key, $value );
112
+		foreach ($headers as $key => $value) {
113
+			unset($headers[$key]);
114
+			$headers[] = sprintf('%s: %s', $key, $value);
115 115
 		}
116 116
 
117 117
 		$headers[] = 'Content-Type: text/html';
118 118
 
119
-		return apply_filters( 'castor/email/headers', $headers, $this );
119
+		return apply_filters('castor/email/headers', $headers, $this);
120 120
 	}
121 121
 
122 122
 	/**
123 123
 	 * @return string
124 124
 	 */
125
-	protected function buildHtmlMessage( array $email )
125
+	protected function buildHtmlMessage(array $email)
126 126
 	{
127
-		$body = $this->renderTemplate( 'email' );
127
+		$body = $this->renderTemplate('email');
128 128
 
129
-		$message = !empty( $email['template'] )
130
-			? $this->renderTemplate( $email['template'], $email['template-tags'] )
129
+		$message = !empty($email['template'])
130
+			? $this->renderTemplate($email['template'], $email['template-tags'])
131 131
 			: $email['message'];
132 132
 
133
-		$message = $this->filterHtml( $email['before'] . $message . $email['after'] );
134
-		$message = str_replace( '{message}', $message, $body );
133
+		$message = $this->filterHtml($email['before'] . $message . $email['after']);
134
+		$message = str_replace('{message}', $message, $body);
135 135
 
136
-		return apply_filters( 'castor/email/message', $message, 'html', $this );
136
+		return apply_filters('castor/email/message', $message, 'html', $this);
137 137
 	}
138 138
 
139 139
 	/**
@@ -141,9 +141,9 @@  discard block
 block discarded – undo
141 141
 	 *
142 142
 	 * @return string
143 143
 	 */
144
-	protected function buildPlainTextMessage( $message )
144
+	protected function buildPlainTextMessage($message)
145 145
 	{
146
-		return apply_filters( 'castor/email/message', $this->stripHtmlTags( $message ), 'text', $this );
146
+		return apply_filters('castor/email/message', $this->stripHtmlTags($message), 'text', $this);
147 147
 	}
148 148
 
149 149
 	/**
@@ -151,13 +151,13 @@  discard block
 block discarded – undo
151 151
 	 *
152 152
 	 * @return string
153 153
 	 */
154
-	protected function filterHtml( $message )
154
+	protected function filterHtml($message)
155 155
 	{
156
-		$message = strip_shortcodes( $message );
157
-		$message = wptexturize( $message );
158
-		$message = wpautop( $message );
159
-		$message = str_replace( ['<> ', ']]>'], ['', ']]>'], $message );
160
-		$message = stripslashes( $message );
156
+		$message = strip_shortcodes($message);
157
+		$message = wptexturize($message);
158
+		$message = wpautop($message);
159
+		$message = str_replace(['<> ', ']]>'], ['', ']]>'], $message);
160
+		$message = stripslashes($message);
161 161
 		return $message;
162 162
 	}
163 163
 
@@ -166,33 +166,33 @@  discard block
 block discarded – undo
166 166
 	 */
167 167
 	protected function getDefaultFrom()
168 168
 	{
169
-		return sprintf( '%s <%s>',
170
-			wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ),
171
-			get_option( 'admin_email' )
169
+		return sprintf('%s <%s>',
170
+			wp_specialchars_decode(get_option('blogname'), ENT_QUOTES),
171
+			get_option('admin_email')
172 172
 		);
173 173
 	}
174 174
 
175 175
 	/**
176 176
 	 * @return array
177 177
 	 */
178
-	protected function normalize( $email )
178
+	protected function normalize($email)
179 179
 	{
180 180
 		$defaults = array_fill_keys([
181 181
 			'after', 'attachments', 'bcc', 'before', 'cc', 'from', 'message', 'reply-to', 'subject',
182 182
 			'template', 'template-tags', 'to',
183
-		], '' );
183
+		], '');
184 184
 		$defaults['from'] = $this->getDefaultFrom();
185 185
 
186
-		$email = shortcode_atts( $defaults, $email );
186
+		$email = shortcode_atts($defaults, $email);
187 187
 
188
-		foreach( ['attachments', 'template-tags'] as $key ) {
189
-			$email[$key] = array_filter( (array) $email[$key] );
188
+		foreach (['attachments', 'template-tags'] as $key) {
189
+			$email[$key] = array_filter((array) $email[$key]);
190 190
 		}
191
-		if( empty( $email['reply-to'] )) {
191
+		if (empty($email['reply-to'])) {
192 192
 			$email['reply-to'] = $email['from'];
193 193
 		}
194 194
 
195
-		return apply_filters( 'castor/email/compose', $email, $this );
195
+		return apply_filters('castor/email/compose', $email, $this);
196 196
 	}
197 197
 
198 198
 	/**
@@ -202,20 +202,20 @@  discard block
 block discarded – undo
202 202
 	 *
203 203
 	 * @return void|string
204 204
 	 */
205
-	protected function renderTemplate( $templatePath, array $args = [] )
205
+	protected function renderTemplate($templatePath, array $args = [])
206 206
 	{
207
-		$file = $this->template->get( sprintf( 'castor/%s', $templatePath ));
207
+		$file = $this->template->get(sprintf('castor/%s', $templatePath));
208 208
 
209
-		if( !file_exists( $file )) {
210
-			$file = sprintf( '%s/templates/%s.php', dirname( __DIR__ ), $templatePath );
209
+		if (!file_exists($file)) {
210
+			$file = sprintf('%s/templates/%s.php', dirname(__DIR__), $templatePath);
211 211
 		}
212
-		if( !file_exists( $file ))return;
212
+		if (!file_exists($file))return;
213 213
 
214 214
 		ob_start();
215 215
 		include $file;
216 216
 		$template = ob_get_clean();
217 217
 
218
-		return $this->renderTemplateString( $template, $args );
218
+		return $this->renderTemplateString($template, $args);
219 219
 	}
220 220
 
221 221
 	/**
@@ -223,12 +223,12 @@  discard block
 block discarded – undo
223 223
 	 *
224 224
 	 * @return string
225 225
 	 */
226
-	protected function renderTemplateString( $template, array $args = [] )
226
+	protected function renderTemplateString($template, array $args = [])
227 227
 	{
228
-		foreach( $args as $key => $value ) {
229
-			$template = str_replace( sprintf( '{%s}', $key ), $value, $template );
228
+		foreach ($args as $key => $value) {
229
+			$template = str_replace(sprintf('{%s}', $key), $value, $template);
230 230
 		}
231
-		return trim( $template );
231
+		return trim($template);
232 232
 	}
233 233
 
234 234
 	/**
@@ -251,16 +251,16 @@  discard block
 block discarded – undo
251 251
 	 * - strip all remaining HTML tags
252 252
 	 * @return string
253 253
 	 */
254
-	protected function stripHtmlTags( $string )
254
+	protected function stripHtmlTags($string)
255 255
 	{
256
-		$string = preg_replace( '@<(embed|head|noembed|noscript|object|script|style)[^>]*?>.*?</\\1>@siu', '', $string );
257
-		$string = preg_replace( '@</(div|h[1-9]|p|pre|tr)@iu', "\r\n\$0", $string );
258
-		$string = preg_replace( '@</(td|th)@iu', " \$0", $string );
259
-		$string = preg_replace( '@<(li)[^>]*?>@siu', "\$0-o-^-o-", $string );
260
-		$string = wp_strip_all_tags( $string );
261
-		$string = wp_specialchars_decode( $string, ENT_QUOTES );
262
-		$string = preg_replace( '/\v(?:[\v\h]+){2,}/', "\r\n\r\n", $string );
263
-		$string = str_replace( '-o-^-o-', ' - ', $string );
264
-		return html_entity_decode( $string, ENT_QUOTES, 'UTF-8' );
256
+		$string = preg_replace('@<(embed|head|noembed|noscript|object|script|style)[^>]*?>.*?</\\1>@siu', '', $string);
257
+		$string = preg_replace('@</(div|h[1-9]|p|pre|tr)@iu', "\r\n\$0", $string);
258
+		$string = preg_replace('@</(td|th)@iu', " \$0", $string);
259
+		$string = preg_replace('@<(li)[^>]*?>@siu', "\$0-o-^-o-", $string);
260
+		$string = wp_strip_all_tags($string);
261
+		$string = wp_specialchars_decode($string, ENT_QUOTES);
262
+		$string = preg_replace('/\v(?:[\v\h]+){2,}/', "\r\n\r\n", $string);
263
+		$string = str_replace('-o-^-o-', ' - ', $string);
264
+		return html_entity_decode($string, ENT_QUOTES, 'UTF-8');
265 265
 	}
266 266
 }
Please login to merge, or discard this patch.