PluginProbe
Gianism / trunk
Gianism vtrunk
4.3.0 4.3.1 4.3.2 4.3.3 4.3.4 4.4.0 5.0.0 5.0.1 5.0.2 5.1.0 5.2.1 5.2.2 5.3.0 6.0.0 6.0.1 trunk 1.0 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 All 65 releases
gianism / app / Gianism / Service / NoMailService.php

NoMailService.php in Gianism trunk, at app/Gianism/Service/NoMailService.php

126 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Gianism\Service;
4
5 use Gianism\Helper\PseudoPhpMailer;
6
7
8 /**
9 * Service which doesn't provide email address
10 *
11 * @package Gianism
12 * @since 2.0.0
13 * @author Takahashi Fumiki
14 */
15 abstract class NoMailService extends AbstractService {
16
17 /**
18 * Pseudo domain
19 * @var string
20 */
21 protected $pseudo_domain = '';
22
23 /**
24 * Register pseudo mail action
25 *
26 */
27 protected function init_action() {
28 add_filter( 'wp_mail', array( $this, 'mail_handler' ) );
29 }
30
31 /**
32 * Returns if given mail address is pseudo.
33 *
34 * @param string $mail
35 *
36 * @return boolean
37 */
38 public function is_pseudo_mail( $mail ) {
39 return ! empty( $this->pseudo_domain ) && ( false !== strpos( $mail, '@' . $this->pseudo_domain ) );
40 }
41
42 /**
43 * Alternative wp_mail
44 *
45 * @param int $user_id
46 * @param string $subject
47 * @param string $message
48 * @param array|string $headers
49 * @param string $attachment
50 *
51 * @return void
52 */
53 protected function wp_mail( $user_id, $subject, $message, $headers = '', $attachment = '' ) {
54 /**
55 * Fallback action for no mail
56 *
57 * @param int $user_id
58 * @param string $subject
59 * @param string $message
60 * @param string $headers
61 * @param string $attachment
62 *
63 * @since 3.0.0
64 * @action gianism_mail_fallback
65 */
66 do_action( 'gianism_mail_fallback', $user_id, $subject, $message, $headers, $attachment );
67 }
68
69 /**
70 * Override default wp_mail
71 *
72 * @param array $args
73 *
74 * @return array
75 */
76 final public function mail_handler( $args ) {
77 $tos = [];
78 foreach ( is_array( $args['to'] ) ? $args['to'] : [ $args['to'] ] as $to ) {
79 $user_id = email_exists( $to );
80 if ( $this->is_pseudo_mail( $to ) && $user_id ) {
81 // Send mail
82 $this->wp_mail( $user_id, $args['subject'], $args['message'], $args['headers'], $args['attachments'] );
83 } else {
84 $tos[] = $to;
85 }
86 }
87 if ( empty( $tos ) ) {
88 add_action( 'phpmailer_init', [ $this, 'hijack_php_mailer' ] );
89 } else {
90 $args['to'] = $tos;
91 }
92 return $args;
93 }
94
95 /**
96 * Hijack php mailer to pseudo
97 *
98 * @param \PHPMailer $php_mailer
99 */
100 public function hijack_php_mailer( &$php_mailer ) {
101 $instance = new PseudoPhpMailer( true );
102 /**
103 * You can replace pseudo mail instance
104 *
105 * @filter gianism_pseudo_mailer_instance
106 *
107 * @param PseudoPhpMailer $instance
108 *
109 * @return \Gianism\Pattern\DummyPhpMailer
110 */
111 $php_mailer = apply_filters( 'gianism_pseudo_mailer_instance', $instance );
112 remove_action( 'phpmailer_init', [ $this, 'hijack_php_mailer' ] );
113 }
114
115 /**
116 * Get pseudo email.
117 *
118 * @param mixed $prefix
119 *
120 * @return string
121 */
122 protected function create_pseudo_email( $prefix ) {
123 return sprintf( '%s@%s', $prefix, $this->pseudo_domain );
124 }
125 }
126