_twilio_sid = get_option( 'ywsn_twilio_sid' ); $this->_twilio_auth_token = get_option( 'ywsn_twilio_auth_token' ); parent::__construct(); } /** * Send SMS * * @since 1.0.0 * * @param $to_phone * @param $message * @param $country_code * * @return array * @throws Exception for WP HTTP API error, no response, HTTP status code is not 201 or if HTTP status code not set * @author Alberto Ruggiero */ public function send( $to_phone, $message, $country_code ) { $to_phone = ( '+' != substr( $to_phone, 0, 1 ) ? '+' . $to_phone : $to_phone ); if ( '' != $this->_from_asid && '' != $country_code && $this->country_support_asid( $country_code ) ) { $from = $this->_from_asid; } else { $from = ( '+' != substr( $this->_from_number, 0, 1 ) ? '+' . $this->_from_number : $this->_from_number ); } $wp_remote_http_args = array( 'method' => 'POST', 'timeout' => '10', 'redirection' => 0, 'httpversion' => '1.0', 'sslverify' => true, 'blocking' => true, 'headers' => array( 'Authorization' => sprintf( 'Basic %s', base64_encode( $this->_twilio_sid . ':' . $this->_twilio_auth_token ) ) ), 'body' => http_build_query( array( 'From' => $from, 'To' => $to_phone, 'Body' => $message, ) ), 'cookies' => array() ); $endpoint = str_replace( '{sid}', $this->_twilio_sid, 'https://api.twilio.com/2010-04-01/Accounts/{sid}/Messages.json' ); // perform HTTP request with endpoint / args $response = wp_safe_remote_request( esc_url_raw( $endpoint ), $wp_remote_http_args ); // WP HTTP API error like network timeout, etc if ( is_wp_error( $response ) ) { throw new Exception( $response->get_error_message() ); } $this->_log[] = $response; // Check for proper response / body if ( ! isset( $response['response'] ) || ! isset( $response['body'] ) ) { throw new Exception( __( 'No answer', 'yith-woocommerce-sms-notifications' ) ); } if ( isset( $response['response']['code'] ) ) { if ( $response['response']['code'] != 201 && $response['response']['code'] != 200 ) { $response = json_decode( $response['body'], true ); throw new Exception( ( isset( $response['message'] ) ) ? $response['message'] : __( 'An error has occurred while sending the sms', 'yith-woocommerce-sms-notifications' ) ); } $response = json_decode( $response['body'], true ); } else { throw new Exception( __( 'No answer code', 'yith-woocommerce-sms-notifications' ) ); } return $response['date_created']; } /** * Check if customer country supports Alphanumeric Sender ID * * @since 1.0.0 * * @param $country_code * * @return boolean * @author Alberto Ruggiero */ private function country_support_asid( $country_code ) { $allowed_countries = array( 'AC', 'AD', 'AG', 'AI', 'AL', 'AM', 'AO', 'AQ', 'AS', 'AT', 'AU', 'AW', 'AX', 'BA', 'BB', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BL', 'BM', 'BN', 'BO', 'BQ', 'BS', 'BT', 'BW', 'BY', 'BZ', 'CC', 'CF', 'CH', 'CI', 'CK', 'CM', 'CV', 'CW', 'CX', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'EE', 'EH', 'ER', 'ES', 'ET', 'EU', 'FI', 'FJ', 'FK', 'FM', 'FO', 'FR', 'GA', 'GB', 'GD', 'GE', 'GG', 'GI', 'GL', 'GM', 'GN', 'GP', 'GQ', 'GR', 'GW', 'GY', 'HK', 'HT', 'IE', 'IL', 'IM', 'IO', 'IS', 'IT', 'JE', 'JM', 'JO', 'KH', 'KI', 'KM', 'KN', 'KP', 'KY', 'LB', 'LC', 'LI', 'LR', 'LS', 'LT', 'LU', 'LV', 'LY', 'MD', 'ME', 'MF', 'MG', 'MH', 'MK', 'MN', 'MO', 'MP', 'MQ', 'MR', 'MS', 'MT', 'MU', 'MV', 'MW', 'NC', 'NE', 'NF', 'NG', 'NL', 'NO', 'NU', 'OM', 'PE', 'PF', 'PG', 'PH', 'PL', 'PM', 'PS', 'PT', 'PW', 'PY', 'QN', 'QS', 'QY', 'RE', 'RW', 'SB', 'SC', 'SD', 'SE', 'SG', 'SH', 'SI', 'SJ', 'SK', 'SL', 'SM', 'SN', 'SO', 'SR', 'SS', 'ST', 'SX', 'SZ', 'TA', 'TC', 'TD', 'TG', 'TJ', 'TK', 'TL', 'TM', 'TO', 'TT', 'TV', 'TZ', 'UA', 'UG', 'UK', 'UZ', 'VA', 'VC', 'VG', 'VI', 'VU', 'WF', 'WS', 'XC', 'XD', 'XG', 'XL', 'XN', 'XP', 'XR', 'XS', 'XT', 'XV', 'YE', 'YT', 'ZM', 'ZW', ); return in_array( $country_code, $allowed_countries ); } } }