151 lines
4.1 KiB
PHP
151 lines
4.1 KiB
PHP
|
|
<?php
|
||
|
|
/**
|
||
|
|
* SMS Manager
|
||
|
|
*
|
||
|
|
* Manages SMS provider selection and message sending
|
||
|
|
*/
|
||
|
|
class TWP_SMS_Manager {
|
||
|
|
|
||
|
|
private static $provider = null;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the active SMS provider instance
|
||
|
|
*
|
||
|
|
* @return TWP_SMS_Provider|null
|
||
|
|
*/
|
||
|
|
public static function get_provider() {
|
||
|
|
if (self::$provider !== null) {
|
||
|
|
return self::$provider;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Load interface and providers
|
||
|
|
require_once dirname(__FILE__) . '/interface-twp-sms-provider.php';
|
||
|
|
require_once dirname(__FILE__) . '/class-twp-sms-provider-twilio.php';
|
||
|
|
require_once dirname(__FILE__) . '/class-twp-sms-provider-sns.php';
|
||
|
|
|
||
|
|
// Get selected provider from settings
|
||
|
|
$selected_provider = get_option('twp_sms_provider', 'twilio');
|
||
|
|
|
||
|
|
switch ($selected_provider) {
|
||
|
|
case 'aws_sns':
|
||
|
|
self::$provider = new TWP_SMS_Provider_SNS();
|
||
|
|
break;
|
||
|
|
|
||
|
|
case 'twilio':
|
||
|
|
default:
|
||
|
|
self::$provider = new TWP_SMS_Provider_Twilio();
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
|
||
|
|
return self::$provider;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Send an SMS message using the configured provider
|
||
|
|
*
|
||
|
|
* @param string $to_number Recipient phone number (E.164 format)
|
||
|
|
* @param string $message Message body
|
||
|
|
* @param string $from_number Sender phone number (E.164 format)
|
||
|
|
* @return array Response array with 'success' and 'data' or 'error'
|
||
|
|
*/
|
||
|
|
public static function send_sms($to_number, $message, $from_number = null) {
|
||
|
|
$provider = self::get_provider();
|
||
|
|
|
||
|
|
if (!$provider) {
|
||
|
|
return [
|
||
|
|
'success' => false,
|
||
|
|
'error' => 'No SMS provider configured'
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
// Validate provider configuration before sending
|
||
|
|
$validation = $provider->validate_configuration();
|
||
|
|
if (!$validation['success']) {
|
||
|
|
error_log('TWP SMS Error: Provider validation failed - ' . $validation['error']);
|
||
|
|
return $validation;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Send SMS
|
||
|
|
$result = $provider->send_sms($to_number, $message, $from_number);
|
||
|
|
|
||
|
|
// Log the result
|
||
|
|
if ($result['success']) {
|
||
|
|
error_log(sprintf(
|
||
|
|
'TWP SMS: Message sent via %s to %s',
|
||
|
|
$provider->get_provider_name(),
|
||
|
|
$to_number
|
||
|
|
));
|
||
|
|
} else {
|
||
|
|
error_log(sprintf(
|
||
|
|
'TWP SMS Error: Failed to send via %s to %s - %s',
|
||
|
|
$provider->get_provider_name(),
|
||
|
|
$to_number,
|
||
|
|
$result['error'] ?? 'Unknown error'
|
||
|
|
));
|
||
|
|
}
|
||
|
|
|
||
|
|
return $result;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get list of available SMS providers
|
||
|
|
*
|
||
|
|
* @return array Array of provider IDs and names
|
||
|
|
*/
|
||
|
|
public static function get_available_providers() {
|
||
|
|
return [
|
||
|
|
'twilio' => 'Twilio',
|
||
|
|
'aws_sns' => 'Amazon SNS'
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Validate current provider configuration
|
||
|
|
*
|
||
|
|
* @return array Response array with 'success' and 'message' or 'error'
|
||
|
|
*/
|
||
|
|
public static function validate_current_provider() {
|
||
|
|
$provider = self::get_provider();
|
||
|
|
|
||
|
|
if (!$provider) {
|
||
|
|
return [
|
||
|
|
'success' => false,
|
||
|
|
'error' => 'No SMS provider configured'
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
return $provider->validate_configuration();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get current provider name
|
||
|
|
*
|
||
|
|
* @return string Provider name
|
||
|
|
*/
|
||
|
|
public static function get_current_provider_name() {
|
||
|
|
$provider = self::get_provider();
|
||
|
|
|
||
|
|
if (!$provider) {
|
||
|
|
return 'None';
|
||
|
|
}
|
||
|
|
|
||
|
|
return $provider->get_provider_name();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Test SMS send
|
||
|
|
*
|
||
|
|
* @param string $to_number Test recipient number
|
||
|
|
* @return array Response array
|
||
|
|
*/
|
||
|
|
public static function send_test_sms($to_number) {
|
||
|
|
$message = sprintf(
|
||
|
|
'This is a test message from Twilio WordPress Plugin using %s provider at %s',
|
||
|
|
self::get_current_provider_name(),
|
||
|
|
current_time('Y-m-d H:i:s')
|
||
|
|
);
|
||
|
|
|
||
|
|
return self::send_sms($to_number, $message);
|
||
|
|
}
|
||
|
|
}
|