2018-11-16 17:14:15 +01:00
< ? php
class Wiaas_Support_Api {
/**
* Endpoint namespace .
*
* @ var string
*/
private static $namespace = 'wiaas' ;
private static $rest_base = 'support' ;
public static function register_routes () {
2018-11-19 12:35:04 +01:00
register_rest_route ( self :: $namespace , self :: $rest_base . '/send-support-email' , array (
2018-11-16 17:14:15 +01:00
'methods' => 'POST' ,
'callback' => array ( __CLASS__ , 'send_support_email' ),
'permission_callback' => 'is_user_logged_in' ,
'args' => array (
'id' => array (
'description' => __ ( 'Order ID.' , 'wiaas' ),
'type' => 'integer' ,
'required' => true ,
'sanitize_callback' => 'absint' ,
),
2018-11-19 12:35:04 +01:00
'support_text' => array (
2018-11-16 17:14:15 +01:00
'description' => __ ( 'Email text.' , 'wiaas' ),
'type' => 'string' ,
'required' => true
)
)
));
}
/**
* Send support email and save massage to order notes
*
* @ param WP_REST_Request $request Request data .
*
* @ return WP_REST_Response
*/
public static function send_support_email ( $request ) {
$order_id = $request [ 'id' ];
2018-11-19 12:35:04 +01:00
$message = $request [ 'support_text' ];
$order = wc_get_order ( $order_id );
$customer_id = $order -> get_customer_id ();
$customer = get_user_by ( 'id' , $customer_id );
2018-11-16 17:14:15 +01:00
$mailer = WC () -> mailer ();
$recipient = WIAAS_SUPPORT_EMAIL ;
2018-11-19 12:35:04 +01:00
$subject = __ ( 'Customer: ' . $customer -> get ( 'first_name' ) . ', ' . '' . $customer -> get ( 'last_name' ) . ' needs support for order number: ' . $order -> get_order_number ());
2018-11-16 17:14:15 +01:00
$headers = array ();
$success = $mailer -> send ( $recipient , $subject , $message , $headers );
if ( $success ) {
2018-11-19 12:35:04 +01:00
wc_create_order_note ( $order_id , $message , true );
2018-11-16 17:14:15 +01:00
return wiaas_api_notice ( 'EMAIL_SENT' , 'success' );
}
return wiaas_api_notice ( 'EMAIL_NOT_SENT' , 'failed' );
}
}