enable_metabox = WooNotify()->Options( 'enable_metabox' );//سفارش - مشتری $this->enable_notification = WooNotify()->Options( 'enable_notif_whatsApp_main' );//اطلاع رسانی $this->enable_product_admin_whatsApp = WooNotify()->Options( 'enable_product_admin_whatsApp' );//مدیر محصول if ( $this->enable_metabox || $this->enable_notification || $this->enable_product_admin_whatsApp ) { add_action( 'add_meta_boxes', [ $this, 'addMetabox' ] ); add_action( 'wp_ajax_WooNotify_metabox', [ $this, 'ajaxCallback' ] ); //add_action( 'wp_ajax_nopriv_WooNotify_metabox', array( $this, 'ajaxCallback' ) ); } } public function addMetabox( string $post_type ) { if ( $post_type == 'shop_order' && $this->enable_metabox ) { if (get_locale() == 'fa_IR'){ add_meta_box( 'send_whatsApp_to_buyer', 'ارسال پیام واتساپ به مشتری', [ $this, 'orderMetaboxHtml' ], 'shop_order', 'side', 'high' ); } else { add_meta_box('send_whatsApp_to_buyer', 'Send WhatsApp message to customer', [ $this, 'orderMetaboxHtml' ], 'shop_order', 'side', 'high' ); } } if ( $post_type == 'product' && ( $this->enable_notification || $this->enable_product_admin_whatsApp ) ) { if (get_locale() == 'fa_IR'){ add_meta_box( 'send_whatsApp_to_buyer', 'ارسال پیام واتساپ به مشترکین این محصول', [ $this, 'productMetaboxHtml' ], 'product', 'side', 'high' ); } else { add_meta_box('send_whatsApp_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 = sanitize_text_field( $_POST['message'] ?? '' ); 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.' ] ); } } public function orderMetaboxResult( $order_id, $message ) { $order = new WC_Order( $order_id ); $mobile = WooNotify()->buyerMobile( $order_id ); $data = [ 'post_id' => $order_id, 'type' => 3, 'mobile' => $mobile, 'message' => $message, ]; if ( ( $result = WooNotify()->SendwhatsApp( $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', $mobile, $message ) ); if (get_locale() == 'fa_IR'){ wp_send_json_success( [ 'message' => 'پیام واتساپ با موفقیت ارسال شد.', 'order_note' => WooNotify()->orderNoteMetaBox( $order_id ), ] ); } else { wp_send_json_success( [ 'message' => 'WhatsApp message has been sent successfully.', 'order_note' => WooNotify()->orderNoteMetaBox( $order_id ), ] ); } } else { if (get_locale() == 'fa_IR') { $order->add_order_note( sprintf( 'پیام به مشتری با شماره واتساپ %s ارسال نشد.
متن پیام: %s
پاسخ وبسرویس: %s', $mobile, $message, $result ) ); wp_send_json_error( [ 'message' => sprintf( 'ارسال پیام واتساپ با خطا مواجه شد. %s', $result ), 'order_note' => WooNotify()->orderNoteMetaBox( $order_id ), ] ); } 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', $mobile, $message, $result ) ); wp_send_json_error( [ 'message' => sprintf( 'There was an error sending WhatsApp message. %s', $result ), 'order_note' => WooNotify()->orderNoteMetaBox( $order_id ), ] ); } } } /*سفارش*/ 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 = WaMessenger_whatsApp_Contacts::getContactsMobiles( $product_id, $group ); } $data = [ 'post_id' => $product_id, 'type' => $type, 'mobile' => $mobiles, 'message' => $message, ]; if ( ( $result = WooNotify()->SendwhatsApp( $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 ) { $order_id = $post->ID; $mobile = WooNotify()->buyerMobile( $order_id ); if ( empty( $mobile ) ) { if (get_locale() == 'fa_IR') echo '

شماره واتساپی برای ارسال پیام وجود ندارد.

'; else echo '

There is no WhatsApp number to send message to.

'; return; } if ( ! WooNotify()->validateMobile( $mobile ) ) { if (get_locale() == 'fa_IR') echo '

شماره واتساپ مشتری معتبر نیست.

'; else echo '

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 = '' ) { ?>
array('href'=>true , 'title' =>true ,'target'=>true) , 'p' => array(), 'select' => ['id' =>true, 'class' =>true], 'option' => array('value'=>true), 'label' => array('for'=>true), 'optgroup' => array( 'label' => true ), ); echo wp_kses($html_above, $safemetabox); ?>

ID; ob_start(); ?>


enable_notification ) { $contact_url = admin_url( 'admin.php?page=wooNotify-woocommerece-whatsApp-pro&tab=contacts&product_id=' . $product_id ); if (get_locale() == 'fa_IR') $html_below = '

مشاهده مشترکین اطلاع رسانی این محصول

'; else $html_below = '

View notification subscribers of this product

'; } $this->metaBoxHtml( $product_id, 'product', $html_above, $html_below ); } } new WaMessenger_whatsApp_Metabox();