-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration.php
More file actions
83 lines (70 loc) · 2.19 KB
/
integration.php
File metadata and controls
83 lines (70 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/**
* Integration with WP Send SMS plugin.
*
* @package Meloniq\WpSendSms\Integration
*/
/**
* Action hook to integrate with the plugin.
*
* @param string $phone_number The phone number to send the SMS to.
* @param string $message The message to send.
* @param string $country_code The country code to use for the phone number.
*
* do_action( 'wp_send_sms_action', $phone_number, $message, $country_code );
*/
add_action( 'wp_send_sms_action', 'wp_send_sms', 10, 3 );
/**
* Send an SMS message using the WP Send SMS plugin.
*
* @param string $phone_number The phone number to send the SMS to.
* @param string $message The message to send.
* @param string $country_code The country code to use for the phone number.
*
* @return bool
*/
function wp_send_sms( $phone_number, $message, $country_code = '' ) {
$provider_name = get_option( 'wpss_provider' );
if ( empty( $provider_name ) ) {
return false;
}
$provider_class = "Meloniq\WpSendSms\Providers\\$provider_name";
if ( ! class_exists( $provider_class ) ) {
return false;
}
// Get the default country code if not provided.
if ( empty( $country_code ) ) {
$country_code = get_option( 'wpss_country_code' );
}
// Validate the country code.
$country_code_details = Meloniq\WpSendSms\CountryCodes::get_country_code_details( $country_code );
if ( empty( $country_code_details ) ) {
return false;
}
// Format the phone number.
$phone_number = Meloniq\WpSendSms\Utils::standardize_phone_number( $phone_number, $country_code );
if ( empty( $phone_number ) ) {
return false;
}
// Check if the phone number is valid for the given country code.
$phone_number_length = strlen( $phone_number );
if ( $phone_number_length !== $country_code_details['length'] ) {
return false;
}
// Add the country code to the phone number.
$phone_number = $country_code . $phone_number;
// todo: validate message.
$provider = new $provider_class();
$result = $provider->send( $phone_number, $message );
if ( ! $result['success'] && ! empty( $result['error'] ) ) {
set_transient(
'wpss_send_sms_last_error',
array(
'success' => false,
'message' => $result['error'],
),
60
);
}
return $result['success'];
}