array() ); private $enabled_buyers = false; private $enable_super_admin_360Messenger = false; private $enable_product_admin_360Messenger = false; public function __construct() { $this->enabled_buyers = WooNotify()->Options( 'enable_buyer' ); $this->enable_super_admin_360Messenger = WooNotify()->Options( 'enable_super_admin_360Messenger' ); $this->enable_product_admin_360Messenger = WooNotify()->Options( 'enable_product_admin_360Messenger' ); if ( $this->enabled_buyers || $this->enable_super_admin_360Messenger || $this->enable_product_admin_360Messenger ) { add_filter( 'woocommerce_checkout_fields', [ $this, 'mobileLabel' ], 0 ); add_filter( 'woocommerce_billing_fields', [ $this, 'mobileLabel' ] ); add_action( 'wp_enqueue_scripts', [ $this, 'checkoutScript' ] ); add_action( 'woocommerce_after_order_notes', [ $this, 'checkoutFields' ] ); add_action( 'woocommerce_checkout_process', [ $this, 'checkoutFieldsValidation' ] ); add_action( 'woocommerce_checkout_update_order_meta', [ $this, 'save360MessengerOrderMeta' ] ); /*بعد از تغییر وضعیت سفارش*/ add_action( 'woocommerce_order_status_changed', [ $this, 'sendOrder360Messenger' ], 99, 3 ); /*بعد از ثبت سفارش*/ add_action( 'woocommerce_checkout_order_processed', [ $this, 'sendOrder360Messenger' ], 99, 1 ); add_action( 'woocommerce_process_shop_order_meta', [ $this, 'sendOrder360Messenger' ], 999, 1 ); /*جلوگیری از ارسال بعد از ثبت مجدد سفارش از صفحه تسویه حساب*/ add_action( 'woocommerce_resume_order', function () { remove_action( 'woocommerce_checkout_order_processed', [ $this, 'sendOrder360Messenger' ], 99 ); } ); add_filter( 'woocommerce_form_field_WooNotify_multiselect', [ 'WooNotify_360Messenger_Helper', 'multiSelectAndCheckbox', ], 11, 4 ); add_filter( 'woocommerce_form_field_WooNotify_multicheckbox', [ 'WooNotify_360Messenger_Helper', 'multiSelectAndCheckbox', ], 11, 4 ); if ( is_admin() ) { add_action( 'woocommerce_admin_order_data_after_billing_address', [ $this, 'buyer360MessengerDetails', ], 10, 1 ); add_action( 'woocommerce_admin_order_data_after_order_details', [ $this, 'change360MessengerTextJS' ] ); add_action( 'wp_ajax_change_360Messenger_text', [ $this, 'change360MessengerTextCallback' ] ); //add_action( 'wp_ajax_nopriv_change_360Messenger_text', array( $this, 'change360MessengerTextCallback' ) ); } } } public function mobileLabel( $fields ) { $mobile_meta = WooNotify()->buyerMobileMeta(); if ( ! empty( $fields[ $mobile_meta ]['label'] ) ) { $fields[ $mobile_meta ]['label'] = WooNotify()->Options( 'buyer_phone_label', $fields[ $mobile_meta ]['label'] ); } if ( ! empty( $fields['billing'][ $mobile_meta ]['label'] ) ) { $fields['billing'][ $mobile_meta ]['label'] = WooNotify()->Options( 'buyer_phone_label', $fields['billing'][ $mobile_meta ]['label'] ); } return $fields; } public function checkoutScript() { if ( ! function_exists( 'is_checkout' ) || ! function_exists( 'wc_enqueue_js' ) ) { return; } if ( WooNotify()->Options( 'allow_buyer_select_status' ) && is_checkout() ) { wp_register_script( 'WooNotify-frontend-js', WooNotify_URL . '/assets/js/multi-select.js', [ 'jquery' ], WooNotify_VERSION, true ); if (get_locale() == 'fa_IR') { wp_localize_script( 'WooNotify-frontend-js', 'WooNotify', [ 'ajax_url' => admin_url( 'admin-ajax.php' ), 'chosen_placeholder_single' => 'گزینه مورد نظر را انتخاب نمایید.', 'chosen_placeholder_multi' => 'گزینه های مورد نظر را انتخاب نمایید.', 'chosen_no_results_text' => 'هیچ گزینه ای وجود ندارد.', ] ); } else { wp_localize_script( 'WooNotify-frontend-js', 'WooNotify', [ 'ajax_url' => admin_url( 'admin-ajax.php' ), 'chosen_placeholder_single' => 'Choose the desired option.', 'chosen_placeholder_multi' => 'Choose the desired options.', 'chosen_no_results_text' => 'There is no choice.', ] ); } wp_enqueue_script( 'WooNotify-frontend-js' ); if ( ! WooNotify()->Options( 'force_enable_buyer' ) ) { wc_enqueue_js( " jQuery( '#buyer_360Messenger_status_field' ).hide(); jQuery( 'input[name=buyer_360Messenger_notify]' ).change( function () { if ( jQuery( this ).is( ':checked' ) ) jQuery( '#buyer_360Messenger_status_field' ).show(); else jQuery( '#buyer_360Messenger_status_field' ).hide(); } ).change(); " ); } } } public function checkoutFields( $checkout ) { if ( ! $this->enabled_buyers || count( WooNotify()->GetBuyerAllowedStatuses() ) < 0 ) { return; } echo '
'; if (get_locale() == 'fa_IR') $checkbox_text = WooNotify()->Options( 'buyer_checkbox_text', 'میخواهم از وضعیت سفارش از طریق پیام واتساپ آگاه شوم.' ); else $checkbox_text = WooNotify()->Options( 'buyer_checkbox_text', 'I want to be informed about the status of the order via 360Messenger message.' ); $required = WooNotify()->Options( 'force_enable_buyer' ); if ( ! $required ) { woocommerce_form_field( 'buyer_360Messenger_notify', [ 'type' => 'checkbox', 'class' => [ 'buyer-360Messenger-notify form-row-wide' ], 'label' => $checkbox_text, 'label_class' => '', 'required' => false, ], $checkout->get_value( 'buyer_360Messenger_notify' ) ); } if ( WooNotify()->Options( 'allow_buyer_select_status' ) ) { $multiselect_text = WooNotify()->Options( 'buyer_select_status_text_top' ); $multiselect_text_bellow = WooNotify()->Options( 'buyer_select_status_text_bellow' ); $required = WooNotify()->Options( 'force_buyer_select_status' ); $mode = WooNotify()->Options( 'buyer_status_mode', 'selector' ) == 'selector' ? 'WooNotify_multiselect' : 'WooNotify_multicheckbox'; woocommerce_form_field( 'buyer_360Messenger_status', [ 'type' => $mode ? $mode : '', 'class' => [ 'buyer-360Messenger-status form-row-wide wc-enhanced-select' ], 'label' => $multiselect_text, 'options' => WooNotify()->GetBuyerAllowedStatuses( true ), 'required' => $required, 'description' => $multiselect_text_bellow, ], $checkout->get_value( 'buyer_360Messenger_status' ) ); } echo '
'; } public function checkoutFieldsValidation() { $mobile_meta = WooNotify()->buyerMobileMeta(); $_POST[ $mobile_meta ] = WooNotify()->modifyMobile(esc_attr (sanitize_text_field( $_POST[ $mobile_meta ] ?? null ) )); if ( ! $this->enabled_buyers || count( WooNotify()->GetBuyerAllowedStatuses() ) < 0 ) { return; } $force_buyer = WooNotify()->Options( 'force_enable_buyer' ); if ( ! $force_buyer && ! empty( $_POST['buyer_360Messenger_notify'] ) && empty( $_POST[ $mobile_meta ] ) ) { if (get_locale() == 'fa_IR') wc_add_notice( 'برای دریافت پیام می بایست شماره واتساپ را وارد نمایید.', 'error' ); else wc_add_notice( 'You must enter the 360Messenger number to receive the message.', 'error'); } $buyer_selected = $force_buyer || ( ! $force_buyer && ! empty( $_POST['buyer_360Messenger_notify'] ) ); //if ( $buyer_selected && ! WooNotify()->validateMobile( $_POST[ $mobile_meta ] ?? null ) ) { if ( $buyer_selected && ! WooNotify()->validateMobile(esc_attr(sanitize_text_field( $_POST[ $mobile_meta ] ?? '' )) ) ) { if (get_locale() == 'fa_IR') wc_add_notice( 'شماره واتساپ معتبر نیست.', 'error' ); else wc_add_notice( 'The 360Messenger number is not valid.', 'error' ); } if ( $buyer_selected && empty( $_POST['buyer_360Messenger_status'] ) && WooNotify()->Options( 'allow_buyer_select_status' ) && WooNotify()->Options( 'force_buyer_select_status' ) ) { if (get_locale() == 'fa_IR') wc_add_notice( 'انتخاب حداقل یکی از وضعیت های سفارش دریافت پیام واتساپ الزامی است.', 'error' ); else wc_add_notice( 'Choosing at least one of the order status to receive 360Messenger messages is required.', 'error' ); } } public function save360MessengerOrderMeta( $order_id ) { if ( ! $this->enabled_buyers || count( WooNotify()->GetBuyerAllowedStatuses() ) <= 0 ) { return; } update_post_meta( $order_id, '_force_enable_buyer', WooNotify()->Options( 'force_enable_buyer', '__' ) ); update_post_meta( $order_id, '_allow_buyer_select_status', WooNotify()->Options( 'allow_buyer_select_status', '__' ) ); if ( ! empty( $_POST['buyer_360Messenger_notify'] ) || WooNotify()->Options( 'force_enable_buyer' ) ) { update_post_meta( $order_id, '_buyer_360Messenger_notify', 'yes' ); } else { delete_post_meta( $order_id, '_buyer_360Messenger_notify' ); } if ( ! empty( $_POST['buyer_360Messenger_status'] ) ) { $statuses = is_array( $_POST['buyer_360Messenger_status'] ) ? array_map( 'sanitize_text_field', $_POST['buyer_360Messenger_status'] ) : esc_attr(sanitize_text_field( $_POST['buyer_360Messenger_status'] )); update_post_meta( $order_id, '_buyer_360Messenger_status', $statuses ); } else { delete_post_meta( $order_id, '_buyer_360Messenger_status' ); } } public function buyer360MessengerDetails( WC_Order $order ) { if ( ! $this->enabled_buyers || count( WooNotify()->GetBuyerAllowedStatuses() ) < 0 ) { return; } $mobile = WooNotify()->buyerMobile( $order->get_id() ); if ( empty( $mobile ) ) { return; } if ( ! WooNotify()->validateMobile( $mobile ) ) { if (get_locale() == 'fa_IR') echo '

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

'; else echo '

The customers 360Messenger number is not valid.

'; return; } if ( WooNotify()->maybeBool( get_post_meta( $order->get_id(), '_force_enable_buyer', true ) ) ) { if (get_locale() == 'fa_IR') echo '

مشتری حق انتخاب دریافت یا عدم دریافت پیام واتساپ را ندارد.

'; else echo '

The customer does not have the right to choose whether or not to receive 360Messenger messages.

'; } else { $want_360Messenger = get_post_meta( $order->get_id(), '_buyer_360Messenger_notify', true ); if (get_locale() == 'fa_IR') echo '

آیا مشتری مایل به دریافت پیام واتساپ هست : ' . ( WooNotify()->maybeBool( $want_360Messenger ) ? 'بله' : 'خیر' ) . '

'; else echo '

Does the customer want to receive 360Messenger messages: '. ( WooNotify()->maybeBool( $want_360Messenger ) ? 'Yes' : 'No' ) . '

'; } echo '

'; if ( WooNotify()->maybeBool( get_post_meta( $order->get_id(), '_allow_buyer_select_status', true ) ) ) { $buyer_360Messenger_status = (array) get_post_meta( $order->get_id(), '_buyer_360Messenger_status', true ); $buyer_360Messenger_status = array_filter( $buyer_360Messenger_status ); if (get_locale() == 'fa_IR') echo 'وضعیت های انتخابی توسط مشتری برای دریافت پیام واتساپ : '; else echo 'situations selected by the customer to receive 360Messenger messages: '; if ( ! empty( $buyer_360Messenger_status ) ) { $statuses = []; foreach ( $buyer_360Messenger_status as $status ) { $statuses[] = WooNotify()->statusName( $status ); } echo esc_html( implode( ' - ', $statuses ) ); } else { if (get_locale() == 'fa_IR') echo 'وضعیتی انتخاب نشده است.'; else echo 'A status is not selected.'; } } else { if (get_locale() == 'fa_IR') echo 'مشتری حق انتخاب وضعیت های دریافت پیام واتساپ را ندارد و از تنظیمات افزونه پیروی میکند.'; else echo 'The customer does not have the right to choose the status of receiving 360Messenger messages and follows the settings of the plugin.'; /* $allowed_status = WooNotify()->GetBuyerAllowedStatuses(); if ( ! empty( $allowed_status ) ) { echo ' وضعیت مجاز برای دریافت پیامک با توجه به تنظیمات: ' . '
'; echo esc_html( implode( ' - ', array_values( $allowed_status ) ) ); } */ } echo '

'; } public function sendOrder360Messenger( int $order_id, $old_status = '', $new_status = 'created' ) { if ( current_action() == 'woocommerce_process_shop_order_meta' ) { if ( ! is_admin() ) { return; } } else { remove_action( 'woocommerce_process_shop_order_meta', [ $this, 'sendOrder360Messenger' ], 999 ); } $new_status = WooNotify()->modifyStatus( $new_status ); if ( ! $order_id ) { return; } $order = new WC_Order( $order_id ); // Customer $order_page = ( $_POST['is_shop_order'] ?? null ) == 'true'; if ( ( $order_page && ! empty( $_POST['360Messenger_order_send'] ) ) || ( ! $order_page && $this->buyerCanGet360Messenger( $order_id, $new_status ) ) ) { $mobile = WooNotify()->buyerMobile( $order_id ); $message = isset( $_POST['360Messenger_order_text'] ) ? sanitize_textarea_field( $_POST['360Messenger_order_text'] ) : WooNotify()->Options( '360Messenger_body_' . $new_status ); $data = [ 'post_id' => $order_id, 'type' => 2, 'mobile' => $mobile, 'message' => WooNotify()->ReplaceShortCodes( $message, $new_status, $order ), ]; if ( ( $result = WooNotify()->Send360Messenger( $data ) ) === true ) { if (get_locale() == 'fa_IR') $order->add_order_note( sprintf( 'پیام با موفقیت به مشتری با شماره واتساپ %s ارسال گردید.', $mobile ) ); else $order->add_order_note( sprintf( 'The message was successfully sent to the customer with the 360Messenger number %s.', $mobile ) ); } else { if (get_locale() == 'fa_IR') $order->add_order_note( sprintf( 'پیام بخاطر خطا به مشتری با شماره واتساپ %s ارسال نشد.
پاسخ وبسرویس: %s', $mobile, $result ) ); else $order->add_order_note( sprintf( 'The message could not be sent to the customer with 360Messenger number %s due to an error.
Web service response: %s', $mobile, $result ) ); } } //superAdmin if ( $this->enable_super_admin_360Messenger && in_array( $new_status, (array) WooNotify()->Options( 'super_admin_order_status' ) ) ) { $mobile = WooNotify()->Options( 'super_admin_phone' ); $message = WooNotify()->Options( 'super_admin_360Messenger_body_' . $new_status ); $data = [ 'post_id' => $order_id, 'type' => 4, 'mobile' => $mobile, 'message' => WooNotify()->ReplaceShortCodes( $message, $new_status, $order ), ]; if ( ( $result = WooNotify()->Send360Messenger( $data ) ) === true ) { if (get_locale() == 'fa_IR') $order->add_order_note( sprintf( 'پیام با موفقیت به مدیر کل با شماره واتساپ %s ارسال گردید.', $mobile ) ); else $order->add_order_note( sprintf( 'The message was successfully sent to the general manager with the 360Messenger number %s.', $mobile ) ); } else { $order->add_order_note( sprintf( 'پیامک بخاطر خطا به مدیر کل با شماره %s ارسال نشد.
پاسخ وبسرویس: %s', $mobile, $result ) ); } } //productAdmin if ( $this->enable_product_admin_360Messenger ) { $order_products = WooNotify()->GetProdcutLists( $order, 'product_id' ); $mobiles = WooNotify()->ProductAdminMobiles( $order_products['product_id'], $new_status ); foreach ( (array) $mobiles as $mobile => $product_ids ) { $vendor_items = WooNotify()->ProductAdminItems( $order_products, $product_ids ); $message = WooNotify()->Options( 'product_admin_360Messenger_body_' . $new_status ); $data = [ 'post_id' => $order_id, 'type' => 5, 'mobile' => $mobile, 'message' => WooNotify()->ReplaceShortCodes( $message, $new_status, $order, $vendor_items ), ]; if ( ( $result = WooNotify()->Send360Messenger( $data ) ) === true ) { if (get_locale() == 'fa_IR') $order->add_order_note( sprintf( 'پیام با موفقیت به مدیر محصول با شماره واتساپ %s ارسال گردید.', $mobile ) ); else $order->add_order_note( sprintf( 'The message was successfully sent to the product manager with the 360Messenger number %s.', $mobile ) ); } else { if (get_locale() == 'fa_IR') $order->add_order_note( sprintf( 'پیام بخاطر خطا به مدیر محصول با شماره واتساپ %s ارسال نشد.
پاسخ وبسرویس: %s', $mobile, $result ) ); else $order->add_order_note( sprintf( 'The message could not be sent to the product manager with the 360Messenger number %s due to an error.
Response from the web service: %s', $mobile, $result ) ); } } } } public function buyerCanGet360Messenger( int $order_id, string $new_status ): bool { if ( ! $this->enabled_buyers ) { return false; } if ( ! $order_id ) { return false; } $order = new WC_Order( $order_id ); $allowed_status = array_keys( WooNotify()->GetBuyerAllowedStatuses() ); if ( is_admin() ) { $status = WooNotify()->OrderProp( $order, 'status' ); $created_via = WooNotify()->OrderProp( $order, 'created_via' ); if ( $created_via == 'admin' || ! in_array( $status, array_keys( WooNotify()->GetAllStatuses() ) ) ) { update_post_meta( $order_id, '_force_enable_buyer', WooNotify()->Options( 'force_enable_buyer', '__' ) ); update_post_meta( $order_id, '_allow_buyer_select_status', WooNotify()->Options( 'allow_buyer_select_status', '__' ) ); update_post_meta( $order_id, '_buyer_360Messenger_notify', 'yes' ); update_post_meta( $order_id, '_buyer_360Messenger_status', $allowed_status ); } } if ( ! WooNotify()->validateMobile( WooNotify()->buyerMobile( $order_id ) ) ) { return false; } if ( in_array( $new_status, $allowed_status ) && WooNotify()->maybeBool( get_post_meta( $order_id, '_buyer_360Messenger_notify', true ) ) ) { $buyer_360Messenger_status = (array) get_post_meta( $order_id, '_buyer_360Messenger_status', true ); $allow_select_status = WooNotify()->maybeBool( get_post_meta( $order_id, '_allow_buyer_select_status', true ) ); if ( ! $allow_select_status || ( $allow_select_status && in_array( $new_status, $buyer_360Messenger_status ) ) ) { return true; } } return false; } public function change360MessengerTextJS( WC_Order $order ) { if ( $this->enabled_buyers && WooNotify()->validateMobile( WooNotify()->buyerMobile( $order->get_id() ) ) ) { ?>

modifyStatus( $_order_status ); } $order = new WC_Order( $order_id ); $message = WooNotify()->Options( '360Messenger_body_' . $new_status ); $message = WooNotify()->ReplaceShortCodes( $message, $new_status, $order ); echo ''; echo ''; if ( $this->buyerCanGet360Messenger( $order_id, $new_status ) ) { $woonotify_360messenger_checked = 'checked="checked"'; if (get_locale() == 'fa_IR') $description = 'با توجه به تنظیمات و انتخاب ها، مشتری باید این پیام را دریافت کند. ولی میتوانید ارسال پیام واتساپ به او را از طریق این چک باکس غیرفعال نمایید.'; else $description = 'According to the settings and choices, the client should receive this message. But you can disable sending 360Messenger messages to him through this check box.'; } else { $woonotify_360messenger_checked = ''; if (get_locale() == 'fa_IR') $description = 'با توجه به تنظیمات و انتخاب ها، مشتری نباید این پیام را دریافت کند. ولی میتوانید ارسال پیام واتساپ به او را از طریق این چک باکس فعال نمایید.'; else $description = 'Due to settings and choices, the client should not receive this message. But you can send 360Messenger messages to him through this check box.'; } echo ' ' . esc_attr( $description ) . ''; die(); } } new WooNotify_360Messenger_Orders();