@@ -4,82 +4,82 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Development |
| 6 | 6 | { |
| 7 | - public $templatePaths = []; |
|
| 7 | + public $templatePaths = []; |
|
| 8 | 8 | |
| 9 | - protected $utility; |
|
| 9 | + protected $utility; |
|
| 10 | 10 | |
| 11 | - public function __construct(Utility $utility) |
|
| 12 | - { |
|
| 13 | - $this->utility = $utility; |
|
| 14 | - } |
|
| 11 | + public function __construct(Utility $utility) |
|
| 12 | + { |
|
| 13 | + $this->utility = $utility; |
|
| 14 | + } |
|
| 15 | 15 | |
| 16 | - public function capture() |
|
| 17 | - { |
|
| 18 | - ob_start(); |
|
| 19 | - call_user_func_array([$this, 'printF'], func_get_args()); |
|
| 20 | - return ob_get_clean(); |
|
| 21 | - } |
|
| 16 | + public function capture() |
|
| 17 | + { |
|
| 18 | + ob_start(); |
|
| 19 | + call_user_func_array([$this, 'printF'], func_get_args()); |
|
| 20 | + return ob_get_clean(); |
|
| 21 | + } |
|
| 22 | 22 | |
| 23 | - public function className($override = 'dev') |
|
| 24 | - { |
|
| 25 | - return $this->isDev() ? $override : ''; |
|
| 26 | - } |
|
| 23 | + public function className($override = 'dev') |
|
| 24 | + { |
|
| 25 | + return $this->isDev() ? $override : ''; |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - public function debug() |
|
| 29 | - { |
|
| 30 | - call_user_func_array([$this, 'printF'], func_get_args()); |
|
| 31 | - } |
|
| 28 | + public function debug() |
|
| 29 | + { |
|
| 30 | + call_user_func_array([$this, 'printF'], func_get_args()); |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - public function isDev() |
|
| 34 | - { |
|
| 35 | - return defined('DEV') && (bool) DEV && WP_ENV == 'development'; |
|
| 36 | - } |
|
| 33 | + public function isDev() |
|
| 34 | + { |
|
| 35 | + return defined('DEV') && (bool) DEV && WP_ENV == 'development'; |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - public function isProduction() |
|
| 39 | - { |
|
| 40 | - return WP_ENV == 'production'; |
|
| 41 | - } |
|
| 38 | + public function isProduction() |
|
| 39 | + { |
|
| 40 | + return WP_ENV == 'production'; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - public function printFiltersFor($hook = '') |
|
| 44 | - { |
|
| 45 | - global $wp_filter; |
|
| 46 | - if (empty($hook) || !isset($wp_filter[$hook])) { |
|
| 47 | - return; |
|
| 48 | - } |
|
| 49 | - $this->printF($wp_filter[$hook]); |
|
| 50 | - } |
|
| 43 | + public function printFiltersFor($hook = '') |
|
| 44 | + { |
|
| 45 | + global $wp_filter; |
|
| 46 | + if (empty($hook) || !isset($wp_filter[$hook])) { |
|
| 47 | + return; |
|
| 48 | + } |
|
| 49 | + $this->printF($wp_filter[$hook]); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - public function printTemplatePaths() |
|
| 53 | - { |
|
| 54 | - $this->printF(implode("\n", $this->templatePaths())); |
|
| 55 | - } |
|
| 52 | + public function printTemplatePaths() |
|
| 53 | + { |
|
| 54 | + $this->printF(implode("\n", $this->templatePaths())); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - public function storeTemplatePath($template) |
|
| 58 | - { |
|
| 59 | - if (is_string($template)) { |
|
| 60 | - $this->templatePaths[] = $this->utility->trimLeft($template, trailingslashit(WP_CONTENT_DIR)); |
|
| 61 | - } |
|
| 62 | - } |
|
| 57 | + public function storeTemplatePath($template) |
|
| 58 | + { |
|
| 59 | + if (is_string($template)) { |
|
| 60 | + $this->templatePaths[] = $this->utility->trimLeft($template, trailingslashit(WP_CONTENT_DIR)); |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - protected function printF() |
|
| 65 | - { |
|
| 66 | - $args = func_num_args(); |
|
| 64 | + protected function printF() |
|
| 65 | + { |
|
| 66 | + $args = func_num_args(); |
|
| 67 | 67 | |
| 68 | - if (1 == $args) { |
|
| 69 | - printf('<div class="print__r"><pre>%s</pre></div>', |
|
| 70 | - htmlspecialchars(print_r(func_get_arg(0), true), ENT_QUOTES, 'UTF-8') |
|
| 71 | - ); |
|
| 72 | - } elseif ($args > 1) { |
|
| 73 | - echo '<div class="print__r_group">'; |
|
| 74 | - foreach (func_get_args() as $value) { |
|
| 75 | - $this->printF($value); |
|
| 76 | - } |
|
| 77 | - echo '</div>'; |
|
| 78 | - } |
|
| 79 | - } |
|
| 68 | + if (1 == $args) { |
|
| 69 | + printf('<div class="print__r"><pre>%s</pre></div>', |
|
| 70 | + htmlspecialchars(print_r(func_get_arg(0), true), ENT_QUOTES, 'UTF-8') |
|
| 71 | + ); |
|
| 72 | + } elseif ($args > 1) { |
|
| 73 | + echo '<div class="print__r_group">'; |
|
| 74 | + foreach (func_get_args() as $value) { |
|
| 75 | + $this->printF($value); |
|
| 76 | + } |
|
| 77 | + echo '</div>'; |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - public function templatePaths() |
|
| 82 | - { |
|
| 83 | - return $this->templatePaths; |
|
| 84 | - } |
|
| 81 | + public function templatePaths() |
|
| 82 | + { |
|
| 83 | + return $this->templatePaths; |
|
| 84 | + } |
|
| 85 | 85 | } |
@@ -8,7 +8,7 @@ discard block |
||
| 8 | 8 | |
| 9 | 9 | protected $utility; |
| 10 | 10 | |
| 11 | - public function __construct(Utility $utility) |
|
| 11 | + public function __construct( Utility $utility ) |
|
| 12 | 12 | { |
| 13 | 13 | $this->utility = $utility; |
| 14 | 14 | } |
@@ -16,23 +16,23 @@ discard block |
||
| 16 | 16 | public function capture() |
| 17 | 17 | { |
| 18 | 18 | ob_start(); |
| 19 | - call_user_func_array([$this, 'printF'], func_get_args()); |
|
| 19 | + call_user_func_array( [$this, 'printF'], func_get_args() ); |
|
| 20 | 20 | return ob_get_clean(); |
| 21 | 21 | } |
| 22 | 22 | |
| 23 | - public function className($override = 'dev') |
|
| 23 | + public function className( $override = 'dev' ) |
|
| 24 | 24 | { |
| 25 | 25 | return $this->isDev() ? $override : ''; |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | public function debug() |
| 29 | 29 | { |
| 30 | - call_user_func_array([$this, 'printF'], func_get_args()); |
|
| 30 | + call_user_func_array( [$this, 'printF'], func_get_args() ); |
|
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | public function isDev() |
| 34 | 34 | { |
| 35 | - return defined('DEV') && (bool) DEV && WP_ENV == 'development'; |
|
| 35 | + return defined( 'DEV' ) && (bool) DEV && WP_ENV == 'development'; |
|
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | public function isProduction() |
@@ -40,24 +40,24 @@ discard block |
||
| 40 | 40 | return WP_ENV == 'production'; |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | - public function printFiltersFor($hook = '') |
|
| 43 | + public function printFiltersFor( $hook = '' ) |
|
| 44 | 44 | { |
| 45 | 45 | global $wp_filter; |
| 46 | - if (empty($hook) || !isset($wp_filter[$hook])) { |
|
| 46 | + if( empty( $hook ) || !isset( $wp_filter[$hook] ) ) { |
|
| 47 | 47 | return; |
| 48 | 48 | } |
| 49 | - $this->printF($wp_filter[$hook]); |
|
| 49 | + $this->printF( $wp_filter[$hook] ); |
|
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | public function printTemplatePaths() |
| 53 | 53 | { |
| 54 | - $this->printF(implode("\n", $this->templatePaths())); |
|
| 54 | + $this->printF( implode( "\n", $this->templatePaths() ) ); |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | - public function storeTemplatePath($template) |
|
| 57 | + public function storeTemplatePath( $template ) |
|
| 58 | 58 | { |
| 59 | - if (is_string($template)) { |
|
| 60 | - $this->templatePaths[] = $this->utility->trimLeft($template, trailingslashit(WP_CONTENT_DIR)); |
|
| 59 | + if( is_string( $template ) ) { |
|
| 60 | + $this->templatePaths[] = $this->utility->trimLeft( $template, trailingslashit( WP_CONTENT_DIR ) ); |
|
| 61 | 61 | } |
| 62 | 62 | } |
| 63 | 63 | |
@@ -65,14 +65,14 @@ discard block |
||
| 65 | 65 | { |
| 66 | 66 | $args = func_num_args(); |
| 67 | 67 | |
| 68 | - if (1 == $args) { |
|
| 69 | - printf('<div class="print__r"><pre>%s</pre></div>', |
|
| 70 | - htmlspecialchars(print_r(func_get_arg(0), true), ENT_QUOTES, 'UTF-8') |
|
| 68 | + if( 1 == $args ) { |
|
| 69 | + printf( '<div class="print__r"><pre>%s</pre></div>', |
|
| 70 | + htmlspecialchars( print_r( func_get_arg( 0 ), true ), ENT_QUOTES, 'UTF-8' ) |
|
| 71 | 71 | ); |
| 72 | - } elseif ($args > 1) { |
|
| 72 | + } elseif( $args > 1 ) { |
|
| 73 | 73 | echo '<div class="print__r_group">'; |
| 74 | - foreach (func_get_args() as $value) { |
|
| 75 | - $this->printF($value); |
|
| 74 | + foreach( func_get_args() as $value ) { |
|
| 75 | + $this->printF( $value ); |
|
| 76 | 76 | } |
| 77 | 77 | echo '</div>'; |
| 78 | 78 | } |
@@ -6,266 +6,266 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Email |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * @var Template |
|
| 11 | - */ |
|
| 12 | - public $template; |
|
| 13 | - |
|
| 14 | - /** |
|
| 15 | - * @var array |
|
| 16 | - */ |
|
| 17 | - protected $attachments; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * @var array |
|
| 21 | - */ |
|
| 22 | - protected $headers; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * @var string |
|
| 26 | - */ |
|
| 27 | - protected $message; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @var string |
|
| 31 | - */ |
|
| 32 | - protected $subject; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @var string |
|
| 36 | - */ |
|
| 37 | - protected $to; |
|
| 38 | - |
|
| 39 | - public function __construct(Template $template) |
|
| 40 | - { |
|
| 41 | - $this->template = $template; |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * @return Email |
|
| 46 | - */ |
|
| 47 | - public function compose(array $email) |
|
| 48 | - { |
|
| 49 | - $email = $this->normalize($email); |
|
| 50 | - |
|
| 51 | - $this->attachments = $email['attachments']; |
|
| 52 | - $this->headers = $this->buildHeaders($email); |
|
| 53 | - $this->message = $this->buildHtmlMessage($email); |
|
| 54 | - $this->subject = $email['subject']; |
|
| 55 | - $this->to = $email['to']; |
|
| 56 | - |
|
| 57 | - add_action('phpmailer_init', function ($phpmailer) { |
|
| 58 | - if ('text/plain' === $phpmailer->ContentType || !empty($phpmailer->AltBody)) { |
|
| 59 | - return; |
|
| 60 | - } |
|
| 61 | - $phpmailer->AltBody = $this->buildPlainTextMessage($phpmailer->Body); |
|
| 62 | - }); |
|
| 63 | - |
|
| 64 | - return $this; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @param bool $plaintext |
|
| 69 | - * |
|
| 70 | - * @return string|null |
|
| 71 | - */ |
|
| 72 | - public function read($plaintext = false) |
|
| 73 | - { |
|
| 74 | - return $plaintext |
|
| 75 | - ? $this->buildPlainTextMessage($this->message) |
|
| 76 | - : $this->message; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * @return bool|null |
|
| 81 | - */ |
|
| 82 | - public function send() |
|
| 83 | - { |
|
| 84 | - if (!$this->message || !$this->subject || !$this->to) { |
|
| 85 | - return; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - $sent = wp_mail( |
|
| 89 | - $this->to, |
|
| 90 | - $this->subject, |
|
| 91 | - $this->message, |
|
| 92 | - $this->headers, |
|
| 93 | - $this->attachments |
|
| 94 | - ); |
|
| 95 | - |
|
| 96 | - $this->reset(); |
|
| 97 | - |
|
| 98 | - return $sent; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * @return array |
|
| 103 | - */ |
|
| 104 | - protected function buildHeaders(array $email) |
|
| 105 | - { |
|
| 106 | - $allowed = [ |
|
| 107 | - 'bcc', |
|
| 108 | - 'cc', |
|
| 109 | - 'from', |
|
| 110 | - 'reply-to', |
|
| 111 | - ]; |
|
| 112 | - |
|
| 113 | - $headers = array_intersect_key($email, array_flip($allowed)); |
|
| 114 | - $headers = array_filter($headers); |
|
| 115 | - |
|
| 116 | - foreach ($headers as $key => $value) { |
|
| 117 | - unset($headers[$key]); |
|
| 118 | - $headers[] = sprintf('%s: %s', $key, $value); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - $headers[] = 'Content-Type: text/html'; |
|
| 122 | - |
|
| 123 | - return apply_filters('castor/email/headers', $headers, $this); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * @return string |
|
| 128 | - */ |
|
| 129 | - protected function buildHtmlMessage(array $email) |
|
| 130 | - { |
|
| 131 | - $body = $this->renderTemplate('index'); |
|
| 132 | - $message = !empty($email['template']) |
|
| 133 | - ? $this->renderTemplate($email['template'], $email['template-tags']) |
|
| 134 | - : $email['message']; |
|
| 135 | - |
|
| 136 | - $message = $this->filterHtml($email['before'].$message.$email['after']); |
|
| 137 | - $message = str_replace('{message}', $message, $body); |
|
| 138 | - |
|
| 139 | - return apply_filters('castor/email/message', $message, 'html', $this); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * @param string $message |
|
| 144 | - * |
|
| 145 | - * @return string |
|
| 146 | - */ |
|
| 147 | - protected function buildPlainTextMessage($message) |
|
| 148 | - { |
|
| 149 | - return apply_filters('castor/email/message', $this->stripHtmlTags($message), 'text', $this); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * @param string $message |
|
| 154 | - * |
|
| 155 | - * @return string |
|
| 156 | - */ |
|
| 157 | - protected function filterHtml($message) |
|
| 158 | - { |
|
| 159 | - $message = strip_shortcodes($message); |
|
| 160 | - $message = wptexturize($message); |
|
| 161 | - $message = wpautop($message); |
|
| 162 | - $message = str_replace(['<> ', ']]>'], ['', ']]>'], $message); |
|
| 163 | - $message = stripslashes($message); |
|
| 164 | - return $message; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * @return string |
|
| 169 | - */ |
|
| 170 | - protected function getDefaultFrom() |
|
| 171 | - { |
|
| 172 | - return sprintf('%s <%s>', |
|
| 173 | - wp_specialchars_decode((string) get_option('blogname'), ENT_QUOTES), |
|
| 174 | - (string) get_option('admin_email') |
|
| 175 | - ); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * @return array |
|
| 180 | - */ |
|
| 181 | - protected function normalize($email) |
|
| 182 | - { |
|
| 183 | - $defaults = array_fill_keys([ |
|
| 184 | - 'after', 'attachments', 'bcc', 'before', 'cc', 'from', 'message', 'reply-to', 'subject', |
|
| 185 | - 'template', 'template-tags', 'to', |
|
| 186 | - ], ''); |
|
| 187 | - $defaults['from'] = $this->getDefaultFrom(); |
|
| 188 | - |
|
| 189 | - $email = shortcode_atts($defaults, $email); |
|
| 190 | - |
|
| 191 | - foreach (['attachments', 'template-tags'] as $key) { |
|
| 192 | - $email[$key] = array_filter((array) $email[$key]); |
|
| 193 | - } |
|
| 194 | - if (empty($email['reply-to'])) { |
|
| 195 | - $email['reply-to'] = $email['from']; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - return apply_filters('castor/email/compose', $email, $this); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * Override by adding the custom template to "templates/castor/" in your theme. |
|
| 203 | - * |
|
| 204 | - * @param string $templatePath |
|
| 205 | - * |
|
| 206 | - * @return void|string |
|
| 207 | - */ |
|
| 208 | - protected function renderTemplate($templatePath, array $args = []) |
|
| 209 | - { |
|
| 210 | - $file = $this->template->get('castor/email/'.$templatePath); |
|
| 211 | - |
|
| 212 | - if (!file_exists($file)) { |
|
| 213 | - $file = sprintf('%s/templates/email/%s.php', dirname(dirname(__DIR__)), $templatePath); |
|
| 214 | - } |
|
| 215 | - if (!file_exists($file)) { |
|
| 216 | - return; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - ob_start(); |
|
| 220 | - include $file; |
|
| 221 | - $template = ob_get_clean(); |
|
| 222 | - |
|
| 223 | - return $this->renderTemplateString($template, $args); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * @param string $template |
|
| 228 | - * |
|
| 229 | - * @return string |
|
| 230 | - */ |
|
| 231 | - protected function renderTemplateString($template, array $args = []) |
|
| 232 | - { |
|
| 233 | - foreach ($args as $key => $value) { |
|
| 234 | - $template = str_replace('{'.$key.'}', $value, $template); |
|
| 235 | - } |
|
| 236 | - return trim($template); |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * @return void |
|
| 241 | - */ |
|
| 242 | - protected function reset() |
|
| 243 | - { |
|
| 244 | - $this->attachments = []; |
|
| 245 | - $this->headers = []; |
|
| 246 | - $this->message = null; |
|
| 247 | - $this->subject = null; |
|
| 248 | - $this->to = null; |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * - remove invisible elements |
|
| 253 | - * - replace certain elements with a line-break |
|
| 254 | - * - replace certain table elements with a space |
|
| 255 | - * - add a placeholder for plain-text bullets to list elements |
|
| 256 | - * - strip all remaining HTML tags. |
|
| 257 | - * @return string |
|
| 258 | - */ |
|
| 259 | - protected function stripHtmlTags($string) |
|
| 260 | - { |
|
| 261 | - $string = preg_replace('@<(embed|head|noembed|noscript|object|script|style)[^>]*?>.*?</\\1>@siu', '', $string); |
|
| 262 | - $string = preg_replace('@</(div|h[1-9]|p|pre|tr)@iu', "\r\n\$0", $string); |
|
| 263 | - $string = preg_replace('@</(td|th)@iu', ' $0', $string); |
|
| 264 | - $string = preg_replace('@<(li)[^>]*?>@siu', '$0-o-^-o-', $string); |
|
| 265 | - $string = wp_strip_all_tags($string); |
|
| 266 | - $string = wp_specialchars_decode($string, ENT_QUOTES); |
|
| 267 | - $string = preg_replace('/\v(?:[\v\h]+){2,}/', "\r\n\r\n", $string); |
|
| 268 | - $string = str_replace('-o-^-o-', ' - ', $string); |
|
| 269 | - return html_entity_decode($string, ENT_QUOTES, 'UTF-8'); |
|
| 270 | - } |
|
| 9 | + /** |
|
| 10 | + * @var Template |
|
| 11 | + */ |
|
| 12 | + public $template; |
|
| 13 | + |
|
| 14 | + /** |
|
| 15 | + * @var array |
|
| 16 | + */ |
|
| 17 | + protected $attachments; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * @var array |
|
| 21 | + */ |
|
| 22 | + protected $headers; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * @var string |
|
| 26 | + */ |
|
| 27 | + protected $message; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @var string |
|
| 31 | + */ |
|
| 32 | + protected $subject; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @var string |
|
| 36 | + */ |
|
| 37 | + protected $to; |
|
| 38 | + |
|
| 39 | + public function __construct(Template $template) |
|
| 40 | + { |
|
| 41 | + $this->template = $template; |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * @return Email |
|
| 46 | + */ |
|
| 47 | + public function compose(array $email) |
|
| 48 | + { |
|
| 49 | + $email = $this->normalize($email); |
|
| 50 | + |
|
| 51 | + $this->attachments = $email['attachments']; |
|
| 52 | + $this->headers = $this->buildHeaders($email); |
|
| 53 | + $this->message = $this->buildHtmlMessage($email); |
|
| 54 | + $this->subject = $email['subject']; |
|
| 55 | + $this->to = $email['to']; |
|
| 56 | + |
|
| 57 | + add_action('phpmailer_init', function ($phpmailer) { |
|
| 58 | + if ('text/plain' === $phpmailer->ContentType || !empty($phpmailer->AltBody)) { |
|
| 59 | + return; |
|
| 60 | + } |
|
| 61 | + $phpmailer->AltBody = $this->buildPlainTextMessage($phpmailer->Body); |
|
| 62 | + }); |
|
| 63 | + |
|
| 64 | + return $this; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @param bool $plaintext |
|
| 69 | + * |
|
| 70 | + * @return string|null |
|
| 71 | + */ |
|
| 72 | + public function read($plaintext = false) |
|
| 73 | + { |
|
| 74 | + return $plaintext |
|
| 75 | + ? $this->buildPlainTextMessage($this->message) |
|
| 76 | + : $this->message; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * @return bool|null |
|
| 81 | + */ |
|
| 82 | + public function send() |
|
| 83 | + { |
|
| 84 | + if (!$this->message || !$this->subject || !$this->to) { |
|
| 85 | + return; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + $sent = wp_mail( |
|
| 89 | + $this->to, |
|
| 90 | + $this->subject, |
|
| 91 | + $this->message, |
|
| 92 | + $this->headers, |
|
| 93 | + $this->attachments |
|
| 94 | + ); |
|
| 95 | + |
|
| 96 | + $this->reset(); |
|
| 97 | + |
|
| 98 | + return $sent; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * @return array |
|
| 103 | + */ |
|
| 104 | + protected function buildHeaders(array $email) |
|
| 105 | + { |
|
| 106 | + $allowed = [ |
|
| 107 | + 'bcc', |
|
| 108 | + 'cc', |
|
| 109 | + 'from', |
|
| 110 | + 'reply-to', |
|
| 111 | + ]; |
|
| 112 | + |
|
| 113 | + $headers = array_intersect_key($email, array_flip($allowed)); |
|
| 114 | + $headers = array_filter($headers); |
|
| 115 | + |
|
| 116 | + foreach ($headers as $key => $value) { |
|
| 117 | + unset($headers[$key]); |
|
| 118 | + $headers[] = sprintf('%s: %s', $key, $value); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + $headers[] = 'Content-Type: text/html'; |
|
| 122 | + |
|
| 123 | + return apply_filters('castor/email/headers', $headers, $this); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * @return string |
|
| 128 | + */ |
|
| 129 | + protected function buildHtmlMessage(array $email) |
|
| 130 | + { |
|
| 131 | + $body = $this->renderTemplate('index'); |
|
| 132 | + $message = !empty($email['template']) |
|
| 133 | + ? $this->renderTemplate($email['template'], $email['template-tags']) |
|
| 134 | + : $email['message']; |
|
| 135 | + |
|
| 136 | + $message = $this->filterHtml($email['before'].$message.$email['after']); |
|
| 137 | + $message = str_replace('{message}', $message, $body); |
|
| 138 | + |
|
| 139 | + return apply_filters('castor/email/message', $message, 'html', $this); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * @param string $message |
|
| 144 | + * |
|
| 145 | + * @return string |
|
| 146 | + */ |
|
| 147 | + protected function buildPlainTextMessage($message) |
|
| 148 | + { |
|
| 149 | + return apply_filters('castor/email/message', $this->stripHtmlTags($message), 'text', $this); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * @param string $message |
|
| 154 | + * |
|
| 155 | + * @return string |
|
| 156 | + */ |
|
| 157 | + protected function filterHtml($message) |
|
| 158 | + { |
|
| 159 | + $message = strip_shortcodes($message); |
|
| 160 | + $message = wptexturize($message); |
|
| 161 | + $message = wpautop($message); |
|
| 162 | + $message = str_replace(['<> ', ']]>'], ['', ']]>'], $message); |
|
| 163 | + $message = stripslashes($message); |
|
| 164 | + return $message; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * @return string |
|
| 169 | + */ |
|
| 170 | + protected function getDefaultFrom() |
|
| 171 | + { |
|
| 172 | + return sprintf('%s <%s>', |
|
| 173 | + wp_specialchars_decode((string) get_option('blogname'), ENT_QUOTES), |
|
| 174 | + (string) get_option('admin_email') |
|
| 175 | + ); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * @return array |
|
| 180 | + */ |
|
| 181 | + protected function normalize($email) |
|
| 182 | + { |
|
| 183 | + $defaults = array_fill_keys([ |
|
| 184 | + 'after', 'attachments', 'bcc', 'before', 'cc', 'from', 'message', 'reply-to', 'subject', |
|
| 185 | + 'template', 'template-tags', 'to', |
|
| 186 | + ], ''); |
|
| 187 | + $defaults['from'] = $this->getDefaultFrom(); |
|
| 188 | + |
|
| 189 | + $email = shortcode_atts($defaults, $email); |
|
| 190 | + |
|
| 191 | + foreach (['attachments', 'template-tags'] as $key) { |
|
| 192 | + $email[$key] = array_filter((array) $email[$key]); |
|
| 193 | + } |
|
| 194 | + if (empty($email['reply-to'])) { |
|
| 195 | + $email['reply-to'] = $email['from']; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + return apply_filters('castor/email/compose', $email, $this); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * Override by adding the custom template to "templates/castor/" in your theme. |
|
| 203 | + * |
|
| 204 | + * @param string $templatePath |
|
| 205 | + * |
|
| 206 | + * @return void|string |
|
| 207 | + */ |
|
| 208 | + protected function renderTemplate($templatePath, array $args = []) |
|
| 209 | + { |
|
| 210 | + $file = $this->template->get('castor/email/'.$templatePath); |
|
| 211 | + |
|
| 212 | + if (!file_exists($file)) { |
|
| 213 | + $file = sprintf('%s/templates/email/%s.php', dirname(dirname(__DIR__)), $templatePath); |
|
| 214 | + } |
|
| 215 | + if (!file_exists($file)) { |
|
| 216 | + return; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + ob_start(); |
|
| 220 | + include $file; |
|
| 221 | + $template = ob_get_clean(); |
|
| 222 | + |
|
| 223 | + return $this->renderTemplateString($template, $args); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * @param string $template |
|
| 228 | + * |
|
| 229 | + * @return string |
|
| 230 | + */ |
|
| 231 | + protected function renderTemplateString($template, array $args = []) |
|
| 232 | + { |
|
| 233 | + foreach ($args as $key => $value) { |
|
| 234 | + $template = str_replace('{'.$key.'}', $value, $template); |
|
| 235 | + } |
|
| 236 | + return trim($template); |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * @return void |
|
| 241 | + */ |
|
| 242 | + protected function reset() |
|
| 243 | + { |
|
| 244 | + $this->attachments = []; |
|
| 245 | + $this->headers = []; |
|
| 246 | + $this->message = null; |
|
| 247 | + $this->subject = null; |
|
| 248 | + $this->to = null; |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * - remove invisible elements |
|
| 253 | + * - replace certain elements with a line-break |
|
| 254 | + * - replace certain table elements with a space |
|
| 255 | + * - add a placeholder for plain-text bullets to list elements |
|
| 256 | + * - strip all remaining HTML tags. |
|
| 257 | + * @return string |
|
| 258 | + */ |
|
| 259 | + protected function stripHtmlTags($string) |
|
| 260 | + { |
|
| 261 | + $string = preg_replace('@<(embed|head|noembed|noscript|object|script|style)[^>]*?>.*?</\\1>@siu', '', $string); |
|
| 262 | + $string = preg_replace('@</(div|h[1-9]|p|pre|tr)@iu', "\r\n\$0", $string); |
|
| 263 | + $string = preg_replace('@</(td|th)@iu', ' $0', $string); |
|
| 264 | + $string = preg_replace('@<(li)[^>]*?>@siu', '$0-o-^-o-', $string); |
|
| 265 | + $string = wp_strip_all_tags($string); |
|
| 266 | + $string = wp_specialchars_decode($string, ENT_QUOTES); |
|
| 267 | + $string = preg_replace('/\v(?:[\v\h]+){2,}/', "\r\n\r\n", $string); |
|
| 268 | + $string = str_replace('-o-^-o-', ' - ', $string); |
|
| 269 | + return html_entity_decode($string, ENT_QUOTES, 'UTF-8'); |
|
| 270 | + } |
|
| 271 | 271 | } |
@@ -36,7 +36,7 @@ discard block |
||
| 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,21 +44,21 @@ discard block |
||
| 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) { |
|
| 58 | - if ('text/plain' === $phpmailer->ContentType || !empty($phpmailer->AltBody)) { |
|
| 57 | + add_action( 'phpmailer_init', function( $phpmailer ) { |
|
| 58 | + if( 'text/plain' === $phpmailer->ContentType || !empty( $phpmailer->AltBody ) ) { |
|
| 59 | 59 | return; |
| 60 | 60 | } |
| 61 | - $phpmailer->AltBody = $this->buildPlainTextMessage($phpmailer->Body); |
|
| 61 | + $phpmailer->AltBody = $this->buildPlainTextMessage( $phpmailer->Body ); |
|
| 62 | 62 | }); |
| 63 | 63 | |
| 64 | 64 | return $this; |
@@ -69,10 +69,10 @@ discard block |
||
| 69 | 69 | * |
| 70 | 70 | * @return string|null |
| 71 | 71 | */ |
| 72 | - public function read($plaintext = false) |
|
| 72 | + public function read( $plaintext = false ) |
|
| 73 | 73 | { |
| 74 | 74 | return $plaintext |
| 75 | - ? $this->buildPlainTextMessage($this->message) |
|
| 75 | + ? $this->buildPlainTextMessage( $this->message ) |
|
| 76 | 76 | : $this->message; |
| 77 | 77 | } |
| 78 | 78 | |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | */ |
| 82 | 82 | public function send() |
| 83 | 83 | { |
| 84 | - if (!$this->message || !$this->subject || !$this->to) { |
|
| 84 | + if( !$this->message || !$this->subject || !$this->to ) { |
|
| 85 | 85 | return; |
| 86 | 86 | } |
| 87 | 87 | |
@@ -101,7 +101,7 @@ discard block |
||
| 101 | 101 | /** |
| 102 | 102 | * @return array |
| 103 | 103 | */ |
| 104 | - protected function buildHeaders(array $email) |
|
| 104 | + protected function buildHeaders( array $email ) |
|
| 105 | 105 | { |
| 106 | 106 | $allowed = [ |
| 107 | 107 | 'bcc', |
@@ -110,33 +110,33 @@ discard block |
||
| 110 | 110 | 'reply-to', |
| 111 | 111 | ]; |
| 112 | 112 | |
| 113 | - $headers = array_intersect_key($email, array_flip($allowed)); |
|
| 114 | - $headers = array_filter($headers); |
|
| 113 | + $headers = array_intersect_key( $email, array_flip( $allowed ) ); |
|
| 114 | + $headers = array_filter( $headers ); |
|
| 115 | 115 | |
| 116 | - foreach ($headers as $key => $value) { |
|
| 117 | - unset($headers[$key]); |
|
| 118 | - $headers[] = sprintf('%s: %s', $key, $value); |
|
| 116 | + foreach( $headers as $key => $value ) { |
|
| 117 | + unset( $headers[$key] ); |
|
| 118 | + $headers[] = sprintf( '%s: %s', $key, $value ); |
|
| 119 | 119 | } |
| 120 | 120 | |
| 121 | 121 | $headers[] = 'Content-Type: text/html'; |
| 122 | 122 | |
| 123 | - return apply_filters('castor/email/headers', $headers, $this); |
|
| 123 | + return apply_filters( 'castor/email/headers', $headers, $this ); |
|
| 124 | 124 | } |
| 125 | 125 | |
| 126 | 126 | /** |
| 127 | 127 | * @return string |
| 128 | 128 | */ |
| 129 | - protected function buildHtmlMessage(array $email) |
|
| 129 | + protected function buildHtmlMessage( array $email ) |
|
| 130 | 130 | { |
| 131 | - $body = $this->renderTemplate('index'); |
|
| 132 | - $message = !empty($email['template']) |
|
| 133 | - ? $this->renderTemplate($email['template'], $email['template-tags']) |
|
| 131 | + $body = $this->renderTemplate( 'index' ); |
|
| 132 | + $message = !empty( $email['template'] ) |
|
| 133 | + ? $this->renderTemplate( $email['template'], $email['template-tags'] ) |
|
| 134 | 134 | : $email['message']; |
| 135 | 135 | |
| 136 | - $message = $this->filterHtml($email['before'].$message.$email['after']); |
|
| 137 | - $message = str_replace('{message}', $message, $body); |
|
| 136 | + $message = $this->filterHtml( $email['before'].$message.$email['after'] ); |
|
| 137 | + $message = str_replace( '{message}', $message, $body ); |
|
| 138 | 138 | |
| 139 | - return apply_filters('castor/email/message', $message, 'html', $this); |
|
| 139 | + return apply_filters( 'castor/email/message', $message, 'html', $this ); |
|
| 140 | 140 | } |
| 141 | 141 | |
| 142 | 142 | /** |
@@ -144,9 +144,9 @@ discard block |
||
| 144 | 144 | * |
| 145 | 145 | * @return string |
| 146 | 146 | */ |
| 147 | - protected function buildPlainTextMessage($message) |
|
| 147 | + protected function buildPlainTextMessage( $message ) |
|
| 148 | 148 | { |
| 149 | - return apply_filters('castor/email/message', $this->stripHtmlTags($message), 'text', $this); |
|
| 149 | + return apply_filters( 'castor/email/message', $this->stripHtmlTags( $message ), 'text', $this ); |
|
| 150 | 150 | } |
| 151 | 151 | |
| 152 | 152 | /** |
@@ -154,13 +154,13 @@ discard block |
||
| 154 | 154 | * |
| 155 | 155 | * @return string |
| 156 | 156 | */ |
| 157 | - protected function filterHtml($message) |
|
| 157 | + protected function filterHtml( $message ) |
|
| 158 | 158 | { |
| 159 | - $message = strip_shortcodes($message); |
|
| 160 | - $message = wptexturize($message); |
|
| 161 | - $message = wpautop($message); |
|
| 162 | - $message = str_replace(['<> ', ']]>'], ['', ']]>'], $message); |
|
| 163 | - $message = stripslashes($message); |
|
| 159 | + $message = strip_shortcodes( $message ); |
|
| 160 | + $message = wptexturize( $message ); |
|
| 161 | + $message = wpautop( $message ); |
|
| 162 | + $message = str_replace( ['<> ', ']]>'], ['', ']]>'], $message ); |
|
| 163 | + $message = stripslashes( $message ); |
|
| 164 | 164 | return $message; |
| 165 | 165 | } |
| 166 | 166 | |
@@ -169,33 +169,33 @@ discard block |
||
| 169 | 169 | */ |
| 170 | 170 | protected function getDefaultFrom() |
| 171 | 171 | { |
| 172 | - return sprintf('%s <%s>', |
|
| 173 | - wp_specialchars_decode((string) get_option('blogname'), ENT_QUOTES), |
|
| 174 | - (string) get_option('admin_email') |
|
| 172 | + return sprintf( '%s <%s>', |
|
| 173 | + wp_specialchars_decode( (string) get_option( 'blogname' ), ENT_QUOTES ), |
|
| 174 | + (string) get_option( 'admin_email' ) |
|
| 175 | 175 | ); |
| 176 | 176 | } |
| 177 | 177 | |
| 178 | 178 | /** |
| 179 | 179 | * @return array |
| 180 | 180 | */ |
| 181 | - protected function normalize($email) |
|
| 181 | + protected function normalize( $email ) |
|
| 182 | 182 | { |
| 183 | - $defaults = array_fill_keys([ |
|
| 183 | + $defaults = array_fill_keys( [ |
|
| 184 | 184 | 'after', 'attachments', 'bcc', 'before', 'cc', 'from', 'message', 'reply-to', 'subject', |
| 185 | 185 | 'template', 'template-tags', 'to', |
| 186 | - ], ''); |
|
| 186 | + ], '' ); |
|
| 187 | 187 | $defaults['from'] = $this->getDefaultFrom(); |
| 188 | 188 | |
| 189 | - $email = shortcode_atts($defaults, $email); |
|
| 189 | + $email = shortcode_atts( $defaults, $email ); |
|
| 190 | 190 | |
| 191 | - foreach (['attachments', 'template-tags'] as $key) { |
|
| 192 | - $email[$key] = array_filter((array) $email[$key]); |
|
| 191 | + foreach( ['attachments', 'template-tags'] as $key ) { |
|
| 192 | + $email[$key] = array_filter( (array) $email[$key] ); |
|
| 193 | 193 | } |
| 194 | - if (empty($email['reply-to'])) { |
|
| 194 | + if( empty( $email['reply-to'] ) ) { |
|
| 195 | 195 | $email['reply-to'] = $email['from']; |
| 196 | 196 | } |
| 197 | 197 | |
| 198 | - return apply_filters('castor/email/compose', $email, $this); |
|
| 198 | + return apply_filters( 'castor/email/compose', $email, $this ); |
|
| 199 | 199 | } |
| 200 | 200 | |
| 201 | 201 | /** |
@@ -205,14 +205,14 @@ discard block |
||
| 205 | 205 | * |
| 206 | 206 | * @return void|string |
| 207 | 207 | */ |
| 208 | - protected function renderTemplate($templatePath, array $args = []) |
|
| 208 | + protected function renderTemplate( $templatePath, array $args = [] ) |
|
| 209 | 209 | { |
| 210 | - $file = $this->template->get('castor/email/'.$templatePath); |
|
| 210 | + $file = $this->template->get( 'castor/email/'.$templatePath ); |
|
| 211 | 211 | |
| 212 | - if (!file_exists($file)) { |
|
| 213 | - $file = sprintf('%s/templates/email/%s.php', dirname(dirname(__DIR__)), $templatePath); |
|
| 212 | + if( !file_exists( $file ) ) { |
|
| 213 | + $file = sprintf( '%s/templates/email/%s.php', dirname( dirname( __DIR__ ) ), $templatePath ); |
|
| 214 | 214 | } |
| 215 | - if (!file_exists($file)) { |
|
| 215 | + if( !file_exists( $file ) ) { |
|
| 216 | 216 | return; |
| 217 | 217 | } |
| 218 | 218 | |
@@ -220,7 +220,7 @@ discard block |
||
| 220 | 220 | include $file; |
| 221 | 221 | $template = ob_get_clean(); |
| 222 | 222 | |
| 223 | - return $this->renderTemplateString($template, $args); |
|
| 223 | + return $this->renderTemplateString( $template, $args ); |
|
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | /** |
@@ -228,12 +228,12 @@ discard block |
||
| 228 | 228 | * |
| 229 | 229 | * @return string |
| 230 | 230 | */ |
| 231 | - protected function renderTemplateString($template, array $args = []) |
|
| 231 | + protected function renderTemplateString( $template, array $args = [] ) |
|
| 232 | 232 | { |
| 233 | - foreach ($args as $key => $value) { |
|
| 234 | - $template = str_replace('{'.$key.'}', $value, $template); |
|
| 233 | + foreach( $args as $key => $value ) { |
|
| 234 | + $template = str_replace( '{'.$key.'}', $value, $template ); |
|
| 235 | 235 | } |
| 236 | - return trim($template); |
|
| 236 | + return trim( $template ); |
|
| 237 | 237 | } |
| 238 | 238 | |
| 239 | 239 | /** |
@@ -256,16 +256,16 @@ discard block |
||
| 256 | 256 | * - strip all remaining HTML tags. |
| 257 | 257 | * @return string |
| 258 | 258 | */ |
| 259 | - protected function stripHtmlTags($string) |
|
| 259 | + protected function stripHtmlTags( $string ) |
|
| 260 | 260 | { |
| 261 | - $string = preg_replace('@<(embed|head|noembed|noscript|object|script|style)[^>]*?>.*?</\\1>@siu', '', $string); |
|
| 262 | - $string = preg_replace('@</(div|h[1-9]|p|pre|tr)@iu', "\r\n\$0", $string); |
|
| 263 | - $string = preg_replace('@</(td|th)@iu', ' $0', $string); |
|
| 264 | - $string = preg_replace('@<(li)[^>]*?>@siu', '$0-o-^-o-', $string); |
|
| 265 | - $string = wp_strip_all_tags($string); |
|
| 266 | - $string = wp_specialchars_decode($string, ENT_QUOTES); |
|
| 267 | - $string = preg_replace('/\v(?:[\v\h]+){2,}/', "\r\n\r\n", $string); |
|
| 268 | - $string = str_replace('-o-^-o-', ' - ', $string); |
|
| 269 | - return html_entity_decode($string, ENT_QUOTES, 'UTF-8'); |
|
| 261 | + $string = preg_replace( '@<(embed|head|noembed|noscript|object|script|style)[^>]*?>.*?</\\1>@siu', '', $string ); |
|
| 262 | + $string = preg_replace( '@</(div|h[1-9]|p|pre|tr)@iu', "\r\n\$0", $string ); |
|
| 263 | + $string = preg_replace( '@</(td|th)@iu', ' $0', $string ); |
|
| 264 | + $string = preg_replace( '@<(li)[^>]*?>@siu', '$0-o-^-o-', $string ); |
|
| 265 | + $string = wp_strip_all_tags( $string ); |
|
| 266 | + $string = wp_specialchars_decode( $string, ENT_QUOTES ); |
|
| 267 | + $string = preg_replace( '/\v(?:[\v\h]+){2,}/', "\r\n\r\n", $string ); |
|
| 268 | + $string = str_replace( '-o-^-o-', ' - ', $string ); |
|
| 269 | + return html_entity_decode( $string, ENT_QUOTES, 'UTF-8' ); |
|
| 270 | 270 | } |
| 271 | 271 | } |
@@ -4,8 +4,7 @@ |
||
| 4 | 4 | |
| 5 | 5 | use GeminiLabs\Castor\Helpers\Template; |
| 6 | 6 | |
| 7 | -class Email |
|
| 8 | -{ |
|
| 7 | +class Email { |
|
| 9 | 8 | /** |
| 10 | 9 | * @var Template |
| 11 | 10 | */ |