PluginProbe
Texty – SMS Notification for WordPress, WooCommerce, Dokan and more / 1.1
Texty – SMS Notification for WordPress, WooCommerce, Dokan and more v1.1
2.0.2 trunk 0.2 1.0 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 2.0.0 2.0.1
← All changes | includes/Gateways.php +13 -135 trunk1.1 View file →
@@ -10,34 +10,8 @@
10 10 */
11 11 class Gateways {
12 12
13 13 /**
14 - * Registered gateway classes
15 - *
16 - * @var array
17 - */
18 - private $gateways = [];
19 -
20 - /**
21 - * Whether built-in gateways have been registered
22 - *
23 - * @var bool
24 - */
25 - private $registered = false;
26 -
27 - /**
28 - * Register a gateway
29 - *
30 - * @param string $key Gateway identifier
31 - * @param string $classname Fully qualified class name
32 - *
33 - * @return void
34 - */
35 - public function register( $key, $classname ) {
36 - $this->gateways[ $key ] = $classname;
37 - }
38 -
39 - /**
40 14 * Send the message from the active gateway
41 15 *
42 16 * @param string $to The TO phone number
43 17 * @param string $message The message to send
@@ -46,90 +20,13 @@
46 20 */
47 21 public function send( $to, $message ) {
48 22 $gateway = $this->active_gateway();
49 23
50 - if ( ! ( $gateway instanceof GatewayInterface ) ) {
51 - return false;
24 + if ( $gateway instanceof GatewayInterface ) {
25 + return $gateway->send( $to, $message );
52 26 }
53 27
54 - /**
55 - * Filter the recipient phone number.
56 - *
57 - * @param string $to The recipient phone number
58 - * @param string $message The message body
59 - * @param GatewayInterface $gateway The active gateway instance
60 - */
61 - $to = apply_filters( 'texty_sms_to', $to, $message, $gateway );
62 -
63 - // Bail if there's still no recipient after the filter — the filter
64 - // runs first so extensions keep their chance to supply the number.
65 - // Returning a WP_Error keeps `texty_after_send_sms` consumers (the
66 - // SMS-stat logger included) on their is_wp_error(...) failure path
67 - // instead of triggering type errors downstream.
68 - if ( ! is_string( $to ) || '' === trim( $to ) ) {
69 - return new WP_Error(
70 - 'texty_missing_recipient',
71 - __( 'No recipient phone number was provided.', 'texty' )
72 - );
73 - }
74 -
75 - /**
76 - * Filter the SMS message body.
77 - *
78 - * @param string $message The message body
79 - * @param string $to The recipient phone number
80 - * @param GatewayInterface $gateway The active gateway instance
81 - */
82 - $message = apply_filters( 'texty_sms_message', $message, $to, $gateway );
83 -
84 - /**
85 - * Short-circuit SMS sending. Return non-null to skip sending.
86 - *
87 - * @param null|mixed $pre_send Return non-null to short-circuit
88 - * @param string $to The recipient phone number
89 - * @param string $message The message body
90 - * @param GatewayInterface $gateway The active gateway instance
91 - */
92 - $pre_send = apply_filters( 'texty_pre_send_sms', null, $to, $message, $gateway );
93 -
94 - if ( null !== $pre_send ) {
95 - return $pre_send;
96 - }
97 -
98 - /**
99 - * Fires before each SMS is sent.
100 - *
101 - * @param string $to The recipient phone number
102 - * @param string $message The message body
103 - * @param GatewayInterface $gateway The active gateway instance
104 - */
105 - do_action( 'texty_before_send_sms', $to, $message, $gateway );
106 -
107 - $result = $gateway->send( $to, $message );
108 -
109 - /**
110 - * Fires after each SMS is sent.
111 - *
112 - * @param bool|WP_Error $result The send result
113 - * @param string $to The recipient phone number
114 - * @param string $message The message body
115 - * @param GatewayInterface $gateway The active gateway instance
116 - */
117 - do_action( 'texty_after_send_sms', $result, $to, $message, $gateway );
118 -
119 - if ( is_wp_error( $result ) ) {
120 - /**
121 - * Fires when SMS sending fails.
122 - *
123 - * @param WP_Error $result The error object
124 - * @param string $to The recipient phone number
125 - * @param string $message The message body
126 - * @param GatewayInterface $gateway The active gateway instance
127 - */
128 - do_action( 'texty_send_sms_failed', $result, $to, $message, $gateway );
129 - }
130 -
131 - return $result;
28 + return false;
132 29 }
133 30
134 31 /**
135 32 * Get the active gateway
@@ -140,17 +37,9 @@
140 37 $gateways = $this->all();
141 38 $gateway = texty()->settings()->gateway();
142 39
143 40 if ( $gateway && array_key_exists( $gateway, $gateways ) ) {
144 - $instance = new $gateways[ $gateway ]();
145 -
146 - /**
147 - * Filter the gateway instance after creation.
148 - *
149 - * @param GatewayInterface $instance The gateway instance
150 - * @param string $gateway The gateway identifier
151 - */
152 - return apply_filters( 'texty_gateway_instance', $instance, $gateway );
41 + return new $gateways[ $gateway ]();
153 42 }
154 43
155 44 return false;
156 45 }
@@ -160,29 +49,18 @@
160 49 *
161 50 * @return array
162 51 */
163 52 public function all() {
164 - if ( ! $this->registered ) {
165 - $this->gateways = [
166 - 'twilio' => __NAMESPACE__ . '\Gateways\Twilio',
167 - 'vonage' => __NAMESPACE__ . '\Gateways\Vonage',
168 - 'clickatell' => __NAMESPACE__ . '\Gateways\Clickatell',
169 - 'plivo' => __NAMESPACE__ . '\Gateways\Plivo',
170 - ];
53 + $gateways = [
54 + 'twilio' => __NAMESPACE__ . '\Gateways\Twilio',
55 + 'vonage' => __NAMESPACE__ . '\Gateways\Vonage',
56 + 'clickatell' => __NAMESPACE__ . '\Gateways\Clickatell',
57 + 'plivo' => __NAMESPACE__ . '\Gateways\Plivo',
58 + ];
171 59
172 - if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
173 - $this->gateways['fake'] = __NAMESPACE__ . '\Gateways\Fake';
174 - }
175 -
176 - /**
177 - * Fires to allow registration of custom gateways.
178 - *
179 - * @param Gateways $manager The gateway manager instance
180 - */
181 - do_action( 'texty_register_gateways', $this );
182 -
183 - $this->registered = true;
60 + if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
61 + $gateways['fake'] = __NAMESPACE__ . '\Gateways\Fake';
184 62 }
185 63
186 - return apply_filters( 'texty_available_gateways', $this->gateways );
64 + return apply_filters( 'texty_available_gateways', $gateways );
187 65 }
188 66 }