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' ] ); //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_360Messenger_to_buyer', esc_html('ارسال پیام واتساپ به مشتری'), [ $this, 'orderMetaboxHtml' ], 'shop_order', 'side', 'high' ); } else { add_meta_box('send_360Messenger_to_buyer', esc_html('Send whatsapp message to customer'), [ $this, 'orderMetaboxHtml' ], 'shop_order', 'side', 'high' ); } } if ( $post_type == 'product' && ( $this->enable_notification || $this->enable_product_admin_360Messenger ) ) { if (get_locale() == 'fa_IR'){ add_meta_box( 'send_360Messenger_to_buyer', esc_html('ارسال پیام واتساپ به مشترکین این محصول'), [ $this, 'productMetaboxHtml' ], 'product', 'side', 'high' ); } else { add_meta_box('send_360Messenger_to_buyer', esc_html('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' => esc_html('خطای ایجکس رخ داده است.') ] ); else wp_send_json_error( [ 'message' => esc_html('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' => esc_html('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' => sanitize_text_field($order_id), 'type' => 3, 'mobile' => sanitize_text_field($mobile), 'message' => sanitize_textarea_field($message), ]; if ( ( $result = WooNotify()->Send360Messenger( $data ) ) === true ) { if (get_locale() == 'fa_IR') $order->add_order_note( esc_html( sprintf( 'پیام با موفقیت به مشتری با شماره واتساپ %s ارسال شد.
متن پیام: %s', $mobile, $message ) ) ); else $order->add_order_note( esc_html(sprintf( 'Message successfully sent to customer with whatsapp number %s.
Message text: %s', sanitize_text_field($mobile), sanitize_text_field($message) )) ); if (get_locale() == 'fa_IR'){ wp_send_json_success( [ 'message' => esc_html('پیام واتساپ با موفقیت ارسال شد.'), 'order_note' => WooNotify()->orderNoteMetaBox( $order_id ), ] ); } else { wp_send_json_success( [ 'message' => esc_html('whatsapp message has been sent successfully.'), 'order_note' => WooNotify()->orderNoteMetaBox( $order_id ), ] ); } } else { if (get_locale() == 'fa_IR') { $order->add_order_note( esc_html(sprintf( 'پیام به مشتری با شماره واتساپ %s ارسال نشد.
متن پیام: %s
پاسخ وبسرویس: %s', sanitize_text_field($mobile), sanitize_text_field($message), sanitize_text_field($result) )) ); wp_send_json_error( [ 'message' => esc_html(sprintf( 'ارسال پیام واتساپ با خطا مواجه شد. %s', sanitize_text_field($result) )), 'order_note' => WooNotify()->orderNoteMetaBox( $order_id ), ] ); } else { $order->add_order_note( esc_html(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', sanitize_text_field($mobile), sanitize_text_field($message), sanitize_text_field($result) )) ); wp_send_json_error( [ 'message' => esc_html(sprintf( 'There was an error sending whatsapp message. %s', sanitize_text_field($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' => esc_html('یک گروه برای دریافت پیام واتساپ انتخاب کنید.') ] ); else wp_send_json_error( [ 'message' => esc_html('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' => sanitize_text_field($product_id), 'type' => sanitize_text_field($type), 'mobile' => sanitize_text_field($mobiles), 'message' => sanitize_textarea_field($message), ]; if ( ( $result = WooNotify()->Send360Messenger( $data ) ) === true ) { if (get_locale() == 'fa_IR') wp_send_json_success( [ 'message' => esc_html(sprintf( 'پیام با موفقیت به %s شماره واتساپ ارسال شد.', count( $mobiles ) )) ] ); else wp_send_json_success( [ 'message' => esc_html(sprintf( 'Message successfully sent to %s whatsapp number.', count( $mobiles ) )) ] ); } else { if (get_locale() == 'fa_IR') wp_send_json_error( [ 'message' => esc_html(sprintf( 'ارسال پیام واتساپ با خطا مواجه شد. %s', $result )) ] ); else wp_send_json_error( [ 'message' => esc_html(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 '

'.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', esc_html(sprintf( '

ارسال پیام واتساپ به شماره %s

', $mobile )) ); else $this->metaBoxHtml( $order_id, 'shop_order', esc_html(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-360Messenger-pro&tab=contacts&product_id=' . $product_id ); if (get_locale() == 'fa_IR') $html_below = '

'.esc_html('مشاهده مشترکین اطلاع رسانی این محصول').'

'; else $html_below = '

'.esc_html('View notification subscribers of this product').'

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