enable_metabox = WooNotify()->Options( 'enable_metabox' );
$this->enable_notification = WooNotify()->Options( 'enable_notif_360Messenger_main' );
$this->enable_product_admin_360Messenger = WooNotify()->Options( 'enable_product_admin_360Messenger' );
if ( $this->enable_metabox || $this->enable_notification || $this->enable_product_admin_360Messenger ) {
add_action( 'add_meta_boxes', [ $this, 'addMetaBox' ] );
add_action( 'wp_ajax_WooNotify_metabox', [ $this, 'ajaxCallback' ] );
}
}
public function addMetaBox( ) {
if ( $this->enable_metabox ) {
if (get_locale() == 'fa_IR'){
add_meta_box( 'send_360Messenger_to_buyer', ('ارسال پیام واتساپ به مشتری'),
[
$this,
'orderMetaBoxHtml',
], [
'shop_order',
wc_get_page_screen_id( 'shop-order' ),
], 'side', 'high' );
}
else
{
add_meta_box('send_360Messenger_to_buyer', ('Send whatsapp message to customer'),
[
$this,
'orderMetaBoxHtml',
], [
'shop_order',
wc_get_page_screen_id( 'shop-order' ),
], 'side', 'high' );
}
}
if ( $this->enable_notification || $this->enable_product_admin_360Messenger ) {
if (get_locale() == 'fa_IR'){
add_meta_box( 'send_360Messenger_to_buyer', ('ارسال پیام واتساپ به مشترکین این محصول'),
[ $this, 'productMetaboxHtml' ], 'product', 'side', 'high' );
}
else
{
add_meta_box('send_360Messenger_to_buyer', ('Send whatsapp message to subscribers of this product'),
[ $this, 'productMetaboxHtml' ], 'product', 'side', 'high' );
}
}
}
public function ajaxCallback() {
check_ajax_referer( 'WooNotify_metabox', 'security' );
if ( empty( $_POST['post_id'] ) || empty( $_POST['post_type'] ) ) {
if (get_locale() == 'fa_IR')
wp_send_json_error( [ 'message' => ('خطای ایجکس رخ داده است.') ] );
else
wp_send_json_error( [ 'message' => ('An Ajax error has occurred.') ] );
}
$message = esc_attr(sanitize_text_field( $_POST['message'] ?? '' ));
//old code
/*
switch ( $_POST['post_type'] ) {
case 'shop_order':
$this->orderMetaboxResult( intval( $_POST['post_id'] ), $message );
break;
case 'product':
$this->productMetaboxResult( intval( $_POST['post_id'] ), $message, sanitize_text_field( $_POST['group'] ?? '' ) );
break;
default:
wp_send_json_error( [ 'message' => 'An Ajax error has occurred.' ] );
}
*/
//new code
// $post_type = isset( $_POST['post_type'] ) ? sanitize_key( $_POST['post_type'] ) : '';
switch ( esc_attr(sanitize_text_field($_POST['post_type'] ))) {
case 'shop_order':
$this->orderMetaBoxResult( absint( sanitize_text_field($_POST['post_id']) ), $message );
break;
case 'product':
case 'product':
$this->productMetaBoxResult( absint(sanitize_text_field($_POST['post_id']) ), $message, sanitize_text_field( $_POST['group'] ?? '' ) );
break;
default:
wp_send_json_error( [ 'message' => ('An Ajax error has occurred.') ] );
}
}
public function orderMetaBoxResult( $order_id, $message ) {
$order = new WC_Order( $order_id );
$mobile = WooNotify()->buyerMobile( $order_id );
$country = WooNotify()->buyerCountry( $order_id );
$data = [
'post_id' => $order_id,
'type' => 3,
'mobile' => $mobile,
'country' => $country,
'message' => $message,
];
if ( ( $result = WooNotify()->Send360Messenger( $data ) ) === true ) {
if (get_locale() == 'fa_IR')
$order->add_order_note( (
sprintf( 'پیام با موفقیت به مشتری با شماره واتساپ %s ارسال شد.
متن پیام: %s', $mobile, $message )
) );
else
$order->add_order_note( (sprintf( 'Message successfully sent to customer with whatsapp number %s.
Message text: %s', esc_html(sanitize_text_field($mobile)), esc_html(sanitize_text_field($message)) )) );
if (get_locale() == 'fa_IR'){
wp_send_json_success( [
'message' => ('پیام واتساپ با موفقیت ارسال شد.'),
'order_note' => WooNotify()->orderNoteMetaBox( $order ),
] );
}
else
{
wp_send_json_success( [
'message' => ('whatsapp message has been sent successfully.'),
'order_note' => WooNotify()->orderNoteMetaBox( $order ),
] );
}
} else {
if (get_locale() == 'fa_IR')
{
$order->add_order_note( (sprintf( 'پیام به مشتری با شماره واتساپ %s ارسال نشد.
متن پیام: %s
پاسخ وبسرویس: %s', esc_html(sanitize_text_field($mobile)), esc_html(sanitize_text_field($message)), esc_html(sanitize_text_field($result)) )) );
wp_send_json_error( [
'message' => (sprintf( 'ارسال پیام واتساپ با خطا مواجه شد. %s', esc_html(sanitize_text_field($result)) )),
'order_note' => WooNotify()->orderNoteMetaBox( $order ),
] );
}
else
{
$order->add_order_note( (sprintf( 'The message was not sent to the customer with the whatsapp number %s.
Text of the message: %s
Response from the web service: %s', esc_html(sanitize_text_field($mobile)), esc_html(sanitize_text_field($message)), esc_html(sanitize_text_field($result)) )) );
wp_send_json_error( [
'message' => (sprintf( 'There was an error sending whatsapp message. %s', esc_html(sanitize_text_field($result)) )),
'order_note' => WooNotify()->orderNoteMetaBox( $order ),
] );
}
}
}
public function productMetaBoxResult( int $product_id, string $message, string $group ) {
if ( empty( $group ) ) {
if (get_locale() == 'fa_IR')
wp_send_json_error( [ 'message' => ('یک گروه برای دریافت پیام واتساپ انتخاب کنید.') ] );
else
wp_send_json_error( [ 'message' => ('Select a group to receive whatsapp messages.') ] );
}
if ( $group == '_product_admins' ) {
$type = 6;
$mobiles = array_keys( WooNotify()->ProductAdminMobiles( $product_id ) );
} else {
switch ( $group ) {
case '_onsale'://حراج
$type = 10;
break;
case '_in'://موجود شدن
$type = 12;
break;
case '_low'://کم بودن موجودی
$type = 14;
break;
default:
$type = 15;
}
$mobiles = WooNotify_360Messenger_Contacts::getContactsMobiles( $product_id, $group );
}
$data = [
'post_id' => absint(sanitize_text_field($product_id)),
'type' => esc_html(sanitize_text_field($type)),
'mobile' => esc_html(sanitize_text_field($mobiles)),
'message' => esc_html(sanitize_textarea_field($message)),
];
if ( ( $result = WooNotify()->Send360Messenger( $data ) ) === true ) {
if (get_locale() == 'fa_IR')
wp_send_json_success( [ 'message' => (sprintf( 'پیام با موفقیت به %s شماره واتساپ ارسال شد.', count( $mobiles ) )) ] );
else
wp_send_json_success( [ 'message' => (sprintf( 'Message successfully sent to %s whatsapp number.', count( $mobiles ) )) ] );
} else {
if (get_locale() == 'fa_IR')
wp_send_json_error( [ 'message' => (sprintf( 'ارسال پیام واتساپ با خطا مواجه شد. %s', $result )) ] );
else
wp_send_json_error( [ 'message' => (sprintf( 'Error sending whatsapp message. %s', $result )) ] );
}
}
public function orderMetaBoxHtml( $post_or_order_object ) {
$order_id = $post_or_order_object instanceof WC_Order ? $post_or_order_object->get_id() : $post_or_order_object->ID;
$mobile = WooNotify()->buyerMobile( $order_id );
if ( empty( $mobile ) ) {
if (get_locale() == 'fa_IR')
echo '
'.esc_html('شماره واتساپی برای ارسال پیام وجود ندارد.').'
'; else echo ''.esc_html('There is no whatsapp number to send message to.').'
'; return; } if ( ! WooNotify()->validateMobile( $mobile ) ) { if (get_locale() == 'fa_IR') echo ''.esc_html('شماره واتساپ مشتری معتبر نیست.').'
'; else echo ''.esc_html('The customers whatsapp number is not valid.').'
'; return; } if (get_locale() == 'fa_IR') $this->metaBoxHtml( $order_id, 'shop_order', (sprintf( 'ارسال پیام واتساپ به شماره %s
', $mobile )) ); else $this->metaBoxHtml( $order_id, 'shop_order', (sprintf( 'Send whatsapp message to %s
', $mobile )) ); } /*محصول*/ private function metaBoxHtml( int $post_id, $post_type, $html_above = '', $html_below = '' ) { ?> [ 'href' => true, 'title' => true, 'target' => true ], 'p' => [], 'select' => [ 'id' => true, 'class' => true ], 'option' => [ 'value' => true ], 'label' => [ 'for' => true ], 'optgroup' => [ 'label' => true, ], ]; echo wp_kses( $html_above, $safemetabox ); ?>