PluginProbe
Texty – SMS Notification for WordPress, WooCommerce, Dokan and more / trunk
Texty – SMS Notification for WordPress, WooCommerce, Dokan and more vtrunk
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
texty / includes / Gateways.php

Gateways.php in Texty – SMS Notification for WordPress, WooCommerce, Dokan and more trunk, at includes/Gateways.php

189 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Texty;
4
5 use Texty\Gateways\GatewayInterface;
6 use WP_Error;
7
8 /**
9 * Manager Class
10 */
11 class Gateways {
12
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 * Send the message from the active gateway
41 *
42 * @param string $to The TO phone number
43 * @param string $message The message to send
44 *
45 * @return bool|WP_Error
46 */
47 public function send( $to, $message ) {
48 $gateway = $this->active_gateway();
49
50 if ( ! ( $gateway instanceof GatewayInterface ) ) {
51 return false;
52 }
53
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;
132 }
133
134 /**
135 * Get the active gateway
136 *
137 * @return Gateways\GatewayInterface|false
138 */
139 public function active_gateway() {
140 $gateways = $this->all();
141 $gateway = texty()->settings()->gateway();
142
143 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 );
153 }
154
155 return false;
156 }
157
158 /**
159 * Get all the available gateways
160 *
161 * @return array
162 */
163 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 ];
171
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;
184 }
185
186 return apply_filters( 'texty_available_gateways', $this->gateways );
187 }
188 }
189