* @license GPL3 https://www.gnu.org/licenses/gpl-3.0.en.html * @since 1.0.0 */ namespace Optemiz\AWO; if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly. } /** * Settings class * * @class Settings The class that manages all about Settings. * * @category Admin * @package Optemiz\AWO * @author Nazrul Islam Nayan * @license GPL3 https://www.gnu.org/licenses/gpl-3.0.en.html * * @property null|object $_instance Instance of the class */ class Settings { public function __construct() { add_action( 'admin_menu', [ $this, 'add_settings_submenu' ] ); add_action( 'admin_init', [ $this, 'register_settings' ] ); add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_product_search_script' ] ); add_action( 'admin_footer', [ $this, 'admin_footer_style' ], 999 ); add_action( 'wp_ajax_hawo_test_order', [ $this, 'ajax_test_order' ] ); add_action( 'wp_ajax_hawo_preview_old_orders', [ $this, 'ajax_preview_old_orders' ] ); add_action( 'wp_ajax_hawo_complete_old_orders', [ $this, 'ajax_complete_old_orders' ] ); add_action( 'wp_ajax_hawo_export_rules', [ $this, 'ajax_export_rules' ] ); add_action( 'wp_ajax_hawo_import_rules', [ $this, 'ajax_import_rules' ] ); } public function admin_footer_style() { if(!isset($_GET['page'])) return; if($_GET['page'] !== 'hawo-autocomplete-orders') { return; } ob_start(); ?> plugin_url() . '/assets/css/admin.css' ); } public function add_settings_submenu() { add_submenu_page( 'woocommerce', __( 'Advanced Autocomplete Orders', 'advanced-autocomplete-orders-for-woocommerce' ), __( 'Autocomplete Orders', 'advanced-autocomplete-orders-for-woocommerce' ), 'manage_woocommerce', 'hawo-autocomplete-orders', [ $this, 'settings_page_callback' ] ); } public function settings_page_callback() { $active_tab = $_GET['tab'] ?? 'settings'; // External URLs $external_tabs = [ 'support' => 'https://happydevs.net/support/', 'documentation' => 'https://happydevs.net/docs/advanced-autocomplete-orders-for-woocommerce/getting-started/installation-activation/', 'upgrade' => 'https://happydevs.net/advanced-autocomplete-orders-for-woocommerce-premium', ]; // Redirect if current tab is external if ( isset( $external_tabs[ $active_tab ] ) ) { wp_redirect( $external_tabs[ $active_tab ] ); exit; } echo '
'; echo '

' . esc_html__( 'Advanced Autocomplete Orders', 'advanced-autocomplete-orders-for-woocommerce' ) . '

'; echo ''; switch ( $active_tab ) { case 'free_vs_pro': echo $this->free_vs_pro_content(); break; case 'settings': default: echo '
'; settings_fields( 'hawo_settings_group' ); do_settings_sections( 'hawo-autocomplete-orders' ); submit_button(); echo '
'; $this->render_phase4_tools(); break; } echo '
'; } public function free_vs_pro_content() { ob_start(); ?>

✅ ✅
✅ ✅
✅ ✅
✅ ✅
❌ ✅
❌ ✅
❌ ✅
❌ ✅
❌ ✅
❌ ✅
❌ ✅
'hawo_sanitize_settings', ) ); add_settings_section( 'hawo_general_section', __( 'General Settings', 'advanced-autocomplete-orders-for-woocommerce' ), '__return_false', 'hawo-autocomplete-orders' ); // Enable Checkbox add_settings_field( 'hawo_enable', __( 'Enable', 'advanced-autocomplete-orders-for-woocommerce' ), function () { $options = get_option( 'hawo_settings' ); ?> /> get_setting('enable', ''); ?> /> get_setting('order_type', ''); $types = [ 'all_orders' => __( 'All Orders', 'advanced-autocomplete-orders-for-woocommerce' ), 'only_virtual_all' => __( "Order's All Products are Virtual Products", 'advanced-autocomplete-orders-for-woocommerce' ), 'has_virtual' => __( 'Order has any Virtual Product', 'advanced-autocomplete-orders-for-woocommerce' ), 'has_downloadable' => __( 'Order has any Downloadable Product', 'advanced-autocomplete-orders-for-woocommerce' ), 'only_downloadable_all' => __( 'Every product is downloadable', 'advanced-autocomplete-orders-for-woocommerce' ), 'mixed_cart' => __( 'The cart has both virtual and physical products', 'advanced-autocomplete-orders-for-woocommerce' ), ]; ?>

'include', 'value' => [ 'start' => '', 'end' => '' ] ]; $mode = isset( $value['mode'] ) ? $value['mode'] : 'include'; $start = isset( $value['value']['start'] ) ? $value['value']['start'] : ''; $end = isset( $value['value']['end'] ) ? $value['value']['end'] : ''; ?>

'include', 'value' => [ 'type' => 'min', 'amount' => '' ] ]; $mode = isset( $value['mode'] ) ? $value['mode'] : 'include'; $type = isset( $value['value']['type'] ) ? $value['value']['type'] : 'min'; $amount = isset( $value['value']['amount'] ) ? $value['value']['amount'] : ''; $has_range = isset( $value['value'] ) && is_array( $value['value'] ) && ( array_key_exists( 'min', $value['value'] ) || array_key_exists( 'max', $value['value'] ) ); $min = $has_range ? ( $value['value']['min'] ?? '' ) : ( 'max' === $type ? '' : $amount ); $max = $has_range ? ( $value['value']['max'] ?? '' ) : ( 'max' === $type ? $amount : '' ); ?>

get_setting( 'complete_when_paid', '' ); ?> /> get_setting( 'paid_orders_only', '' ); ?> /> 'include', 'value' => [] ]; $mode = isset( $value['mode'] ) ? $value['mode'] : 'include'; $saved = isset( $value['value'] ) ? (array) $value['value'] : []; // Get all shipping methods. $shipping_methods = WC()->shipping()->load_shipping_methods(); ?>

get_setting('payment_methods.mode', 'include'); $values = (array) $this->get_setting('payment_methods.value', []); $gateways = []; $gateways = WC()->payment_gateways()->payment_gateways(); ?>

get_setting('products.mode', 'include'); $values = (array) $this->get_setting('products.value', []); ?>

get_setting('categories.mode', 'include'); $values = (array) $this->get_setting('categories.value', []); $categories = []; if(hawo_is_pro_activated()) { $categories = get_terms( [ 'taxonomy' => 'product_cat', 'hide_empty' => false ] ); } ?>

get_setting('tags.mode', 'include'); $values = (array) $this->get_setting('tags.value', []); $tags = []; if(hawo_is_pro_activated()) { $tags = get_terms([ 'taxonomy' => 'product_tag', 'hide_empty' => false, ]); } ?>

get_setting('brands.mode', 'include'); $values = (array) $this->get_setting('brands.value', []); $brands = []; if(hawo_is_pro_activated()) { $brands = get_terms([ 'taxonomy' => 'product_brand', 'hide_empty' => false, ]); } ?>

get_setting('types.mode', 'include'); $values = (array) $this->get_setting('types.value', []); $product_types = []; if(hawo_is_pro_activated()) { $product_types = wc_get_product_types(); } ?>

get_setting('user_roles.mode', 'include'); $values = (array) $this->get_setting('user_roles.value', []); $roles = []; if(hawo_is_pro_activated()) { global $wp_roles; $roles = $wp_roles->roles; } ?>

get_setting('order_status.mode', 'include'); $values = (array) $this->get_setting('order_status.value', []); $statuses = []; if(hawo_is_pro_activated()) { // Get all WC order statuses except 'completed' $all_statuses = wc_get_order_statuses(); // Filter out 'completed' $statuses = array_filter( $all_statuses, function($key) { return 'wc-completed' !== $key; }, ARRAY_FILTER_USE_KEY ); } ?>

get_setting( 'product_match', 'any' ); if ( ! in_array( $selected, array( 'any', 'every', 'only' ), true ) ) { $selected = 'any'; } $choices = array( 'any' => __( 'Any product in the order', 'advanced-autocomplete-orders-for-woocommerce' ), 'every' => __( 'Every product in the order', 'advanced-autocomplete-orders-for-woocommerce' ), 'only' => __( 'Only these products', 'advanced-autocomplete-orders-for-woocommerce' ), ); ?>

get_setting( 'customers.mode', 'include' ); $guests = $this->get_setting( 'customers.guests', '' ); $values = (array) $this->get_setting( 'customers.value', array() ); ?>
render_mode_select( 'hawo_settings[customers][mode]', $mode ); ?>

get_setting( 'coupons.mode', 'include' ); $values = (array) $this->get_setting( 'coupons.value', array() ); ?>
render_mode_select( 'hawo_settings[coupons][mode]', $mode ); ?>
get_setting( 'shipping_instances.mode', 'include' ); $values = (array) $this->get_setting( 'shipping_instances.value', array() ); $choices = hawo_can_edit_pro_settings() ? hawo_shipping_instance_choices() : array(); ?>
render_mode_select( 'hawo_settings[shipping_instances][mode]', $mode ); ?>

render_location_fields( 'billing' ); } public function render_shipping_location_field() { $this->render_location_fields( 'shipping' ); } protected function render_location_fields( $which ) { $countries = ( hawo_can_edit_pro_settings() && function_exists( 'WC' ) && WC()->countries ) ? WC()->countries->get_countries() : array(); $selected_countries = (array) $this->get_setting( $which . '_countries.value', array() ); $states = implode( ', ', (array) $this->get_setting( $which . '_states.value', array() ) ); $postcodes = implode( ', ', (array) $this->get_setting( $which . '_postcodes.value', array() ) ); ?>
render_mode_select( 'hawo_settings[' . $which . '_countries][mode]', $this->get_setting( $which . '_countries.mode', 'include' ) ); ?> render_mode_select( 'hawo_settings[' . $which . '_states][mode]', $this->get_setting( $which . '_states.mode', 'include' ) ); ?> render_mode_select( 'hawo_settings[' . $which . '_postcodes][mode]', $this->get_setting( $which . '_postcodes.mode', 'include' ) ); ?>

$rule ) : ?> render_extra_rule_box( (int) $index, is_array( $rule ) ? $rule : array() ); ?>




render_mode_select( $prefix . '[products][mode]', $rule['products']['mode'] ?? 'include' ); ?>


render_mode_select( $prefix . '[coupons][mode]', $rule['coupons']['mode'] ?? 'include' ); ?>


render_mode_select( $prefix . '[customers][mode]', $rule['customers']['mode'] ?? 'include' ); ?>


render_mode_select( $prefix . '[billing_countries][mode]', $rule['billing_countries']['mode'] ?? 'include' ); ?>


render_mode_select( $prefix . '[shipping_instances][mode]', $rule['shipping_instances']['mode'] ?? 'include' ); ?>

render_phase3_inputs( $prefix, $rule ); ?> render_after_match_inputs( $prefix, $rule ); ?> render_other_apps_inputs( $prefix, $rule ); ?>

render_phase3_inputs( 'hawo_settings', $settings ); ?>

__( 'Sunday', 'advanced-autocomplete-orders-for-woocommerce' ), '1' => __( 'Monday', 'advanced-autocomplete-orders-for-woocommerce' ), '2' => __( 'Tuesday', 'advanced-autocomplete-orders-for-woocommerce' ), '3' => __( 'Wednesday', 'advanced-autocomplete-orders-for-woocommerce' ), '4' => __( 'Thursday', 'advanced-autocomplete-orders-for-woocommerce' ), '5' => __( 'Friday', 'advanced-autocomplete-orders-for-woocommerce' ), '6' => __( 'Saturday', 'advanced-autocomplete-orders-for-woocommerce' ), ); ?>


render_mode_select( $prefix . '[attributes][mode]', $source['attributes']['mode'] ?? 'include' ); ?>

render_range_inputs( $prefix . '[item_count]', $source['item_count'] ?? array(), __( 'How many items', 'advanced-autocomplete-orders-for-woocommerce' ) ); $this->render_range_inputs( $prefix . '[line_count]', $source['line_count'] ?? array(), __( 'How many lines', 'advanced-autocomplete-orders-for-woocommerce' ) ); $this->render_range_inputs( $prefix . '[product_count]', $source['product_count'] ?? array(), __( 'How many different products', 'advanced-autocomplete-orders-for-woocommerce' ) ); ?>



render_range_inputs( $prefix . '[past_orders]', $source['past_orders'] ?? array(), __( 'Past orders', 'advanced-autocomplete-orders-for-woocommerce' ) ); $this->render_range_inputs( $prefix . '[amount_spent]', $source['amount_spent'] ?? array(), __( 'Money spent', 'advanced-autocomplete-orders-for-woocommerce' ), '0.01' ); ?>


$label ) : ?>


$row ) : ?> render_currency_row( $prefix, $index, is_array( $row ) ? $row : array() ); ?>


render_after_match_inputs( 'hawo_settings', $settings ); ?>




render_other_apps_inputs( 'hawo_settings', $settings ); ?>

__( 'Subscription renewals', 'advanced-autocomplete-orders-for-woocommerce' ), 'booking' => __( 'Bookings', 'advanced-autocomplete-orders-for-woocommerce' ), 'preorder' => __( 'Pre-orders', 'advanced-autocomplete-orders-for-woocommerce' ), 'deposit' => __( 'Part payments', 'advanced-autocomplete-orders-for-woocommerce' ), ); $fraud = isset( $source['fraud_hold'] ) ? (string) $source['fraud_hold'] : ''; $tracking = isset( $source['shipment_tracking'] ) ? (string) $source['shipment_tracking'] : ''; $also = hawo_also_complete_list( is_array( $source ) ? $source : array() ); ?>


render_mode_select( $prefix . '[special_orders][mode]', $source['special_orders']['mode'] ?? 'include' ); ?>





phase4_request_ok() ) { wp_send_json_error( array( 'message' => __( 'You cannot copy rules.', 'advanced-autocomplete-orders-for-woocommerce' ) ) ); } $settings = get_option( 'hawo_settings', array() ); $json = function_exists( 'wp_json_encode' ) ? wp_json_encode( $settings ) : json_encode( $settings ); wp_send_json_success( array( 'json' => $json ) ); } public function ajax_import_rules() { if ( ! $this->phase4_request_ok() ) { wp_send_json_error( array( 'message' => __( 'You cannot copy rules.', 'advanced-autocomplete-orders-for-woocommerce' ) ) ); } $raw = isset( $_POST['json'] ) ? wp_unslash( $_POST['json'] ) : ''; $decoded = json_decode( is_string( $raw ) ? $raw : '', true ); if ( ! is_array( $decoded ) ) { wp_send_json_error( array( 'message' => __( 'Those rules could not be read.', 'advanced-autocomplete-orders-for-woocommerce' ) ) ); } update_option( 'hawo_settings', hawo_sanitize_settings( $decoded ) ); wp_send_json_success( array( 'message' => __( 'Rules copied in. Reload this page to see them.', 'advanced-autocomplete-orders-for-woocommerce' ) ) ); } public function render_phase4_tools() { $can = hawo_can_edit_pro_settings() && current_user_can( 'manage_woocommerce' ); $log = get_option( 'hawo_log', array() ); if ( ! is_array( $log ) ) { $log = array(); } ?>

/>

phase4_request_ok() ) { wp_send_json_error( array( 'message' => __( 'You cannot check orders.', 'advanced-autocomplete-orders-for-woocommerce' ) ) ); } $order = $this->find_order_for_test( isset( $_POST['order_number'] ) ? wp_unslash( $_POST['order_number'] ) : '' ); if ( ! $order ) { wp_send_json_error( array( 'message' => __( 'That order was not found.', 'advanced-autocomplete-orders-for-woocommerce' ) ) ); } $report = hawo_explain_order( get_option( 'hawo_settings', array() ), $order, hawo_trigger_for_order( $order ) ); wp_send_json_success( array( 'message' => $report['summary'] ) ); } public function ajax_preview_old_orders() { if ( ! $this->phase4_request_ok() ) { wp_send_json_error( array( 'message' => __( 'You cannot look at old orders.', 'advanced-autocomplete-orders-for-woocommerce' ) ) ); } $settings = get_option( 'hawo_settings', array() ); $ids = hawo_preview_old_order_ids( is_array( $settings ) ? $settings : array() ); set_transient( 'hawo_bulk_preview_' . get_current_user_id(), $ids, 15 * MINUTE_IN_SECONDS ); if ( empty( $ids ) ) { wp_send_json_success( array( 'message' => __( 'No old orders matched. Nothing was changed.', 'advanced-autocomplete-orders-for-woocommerce' ) ) ); } wp_send_json_success( array( 'message' => sprintf( /* translators: %d: number of orders */ __( '%d old orders matched. Nothing was changed yet. Press the complete button to update them.', 'advanced-autocomplete-orders-for-woocommerce' ), count( $ids ) ), ) ); } public function ajax_complete_old_orders() { if ( ! $this->phase4_request_ok() ) { wp_send_json_error( array( 'message' => __( 'You cannot complete old orders.', 'advanced-autocomplete-orders-for-woocommerce' ) ) ); } $ids = get_transient( 'hawo_bulk_preview_' . get_current_user_id() ); if ( ! is_array( $ids ) ) { wp_send_json_error( array( 'message' => __( 'Look at old orders first.', 'advanced-autocomplete-orders-for-woocommerce' ) ) ); } $settings = get_option( 'hawo_settings', array() ); $result = hawo_complete_listed_orders( $ids, is_array( $settings ) ? $settings : array() ); $done = (int) $result['done']; $skipped = (int) $result['skipped']; delete_transient( 'hawo_bulk_preview_' . get_current_user_id() ); wp_send_json_success( array( 'message' => sprintf( /* translators: 1: updated orders, 2: skipped orders */ __( 'Updated %1$d orders. Skipped %2$d. A wait you saved is still used.', 'advanced-autocomplete-orders-for-woocommerce' ), $done, $skipped ), ) ); } protected function phase4_request_ok() { if ( ! hawo_can_edit_pro_settings() || ! current_user_can( 'manage_woocommerce' ) ) { return false; } return (bool) check_ajax_referer( 'hawo_phase4', 'nonce', false ); } protected function old_order_query() { $statuses = array( 'pending', 'processing', 'on-hold', 'failed' ); if ( function_exists( 'wc_get_order_statuses' ) ) { $statuses = array(); foreach ( array_keys( wc_get_order_statuses() ) as $key ) { if ( 'wc-completed' === $key ) { continue; } $statuses[] = $key; } } return array( 'limit' => 20, 'orderby' => 'date', 'order' => 'DESC', 'status' => $statuses, 'return' => 'objects', ); } protected function find_order_for_test( $number ) { $number = trim( sanitize_text_field( (string) $number ) ); if ( '' === $number || ! function_exists( 'wc_get_order' ) ) { return null; } if ( ctype_digit( $number ) ) { $order = wc_get_order( (int) $number ); if ( is_object( $order ) ) { return $order; } } if ( ! function_exists( 'wc_get_orders' ) ) { return null; } $orders = wc_get_orders( array( 'limit' => 50, 'orderby' => 'date', 'order' => 'DESC', 'status' => 'any', 'return' => 'objects', ) ); foreach ( (array) $orders as $order ) { if ( ! is_object( $order ) || ! is_callable( array( $order, 'get_order_number' ) ) ) { continue; } if ( (string) $order->get_order_number() === $number || (string) $order->get_id() === $number ) { return $order; } } return null; } protected function render_mode_select( $name, $mode ) { ?>