get_incomplete_count();
$badge = $incomplete_count > 0 ? sprintf(' %d ', $incomplete_count, $incomplete_count) : '';
// 1. Submenu directly under WooCommerce menu (right below Orders)
add_submenu_page(
'woocommerce',
__('Incomplete Orders', 'advance-fake-order-protector'),
__('Incomplete Orders', 'advance-fake-order-protector') . $badge,
'manage_woocommerce',
'afop-incomplete-orders',
array($this, 'render_incomplete_orders_page')
);
}
// 2. Main Plugin Top-Level Menu
add_menu_page(
__('Sarkar IT Fake Order Protector', 'advance-fake-order-protector'),
__('SARKAR IT Protection', 'advance-fake-order-protector'),
'manage_woocommerce',
'afop-settings',
array($this, 'render_settings_page'),
'dashicons-shield-alt',
56
);
add_submenu_page(
'afop-settings',
__('Settings & Dashboard - SARKAR IT', 'advance-fake-order-protector'),
__('Settings', 'advance-fake-order-protector'),
'manage_woocommerce',
'afop-settings',
array($this, 'render_settings_page')
);
if ($capture_enabled) {
add_submenu_page(
'afop-settings',
__('Incomplete Orders', 'advance-fake-order-protector'),
__('Incomplete Orders', 'advance-fake-order-protector') . $badge,
'manage_woocommerce',
'afop-incomplete-orders',
array($this, 'render_incomplete_orders_page')
);
}
add_submenu_page(
'afop-settings',
__('Blocklist Manager', 'advance-fake-order-protector'),
__('Blocklist Manager', 'advance-fake-order-protector'),
'manage_woocommerce',
'afop-blocklist',
array($this, 'render_blocklist_page')
);
}
/**
* Get pending incomplete orders count
*/
private function get_incomplete_count() {
global $wpdb;
$table = $wpdb->prefix . 'afop_incomplete_orders';
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Prepared operations on plugin-owned recovery/blocklist tables require current records and counts after admin or checkout writes.
$count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(id) FROM %i WHERE status = 'incomplete'", $table));
return intval($count);
}
/**
* Enqueue Admin Scripts and Styles
*/
public function enqueue_admin_assets($hook) {
$screen = get_current_screen();
$is_order_screen = $screen && (
$screen->id === 'edit-shop_order' ||
$screen->id === 'shop_order' ||
$screen->id === 'woocommerce_page_wc-orders' ||
strpos($screen->id, 'afop') !== false
);
if (!$is_order_screen) {
return;
}
// FontAwesome 6
wp_enqueue_style(
'afop-icons',
AFOP_PLUGIN_URL . 'assets/css/afop-icons.css',
array(),
AFOP_VERSION
);
wp_enqueue_style(
'afop-admin-css',
AFOP_PLUGIN_URL . 'assets/css/afop-admin.css',
array('afop-icons'),
AFOP_VERSION
);
wp_enqueue_script(
'afop-admin-js',
AFOP_PLUGIN_URL . 'assets/js/afop-admin.js',
array('jquery'),
AFOP_VERSION,
true
);
wp_localize_script('afop-admin-js', 'afop_admin_data', array(
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('afop_admin_nonce'),
'i18n' => array(
'loading' => __('তথ্য লোড হচ্ছে...', 'advance-fake-order-protector'),
'block' => __('Block', 'advance-fake-order-protector'),
'unblock' => __('Unblock', 'advance-fake-order-protector'),
'confirm_delete' => __('আপনি কি নিশ্চিত এটি ডিলিট করতে চান?', 'advance-fake-order-protector'),
'confirm_bulk_del' => __('নির্বাচিত রেকর্ডগুলো ডিলিট করতে চান?', 'advance-fake-order-protector'),
'confirm_recovered'=> __('আপনি কি এটিকে Recovered হিসেবে মার্ক করতে চান?', 'advance-fake-order-protector')
)
));
}
/**
* Add Custom Columns to WooCommerce Orders Table
*/
/**
* Add Custom Columns to WooCommerce Orders Table
*/
public function add_orders_column($columns) {
$new_columns = array();
foreach ($columns as $key => $title) {
$new_columns[$key] = $title;
if ($key === 'order_number' || $key === 'order_status') {
$new_columns['afop_order_products'] = ' ' . __('Products', 'advance-fake-order-protector');
$new_columns['afop_courier_ratio'] = ' ' . __('Courier Ratio', 'advance-fake-order-protector');
$new_columns['afop_security_actions'] = ' ' . __('Security Actions', 'advance-fake-order-protector');
}
}
if (!isset($new_columns['afop_order_products'])) {
$new_columns['afop_order_products'] = ' ' . __('Products', 'advance-fake-order-protector');
}
if (!isset($new_columns['afop_courier_ratio'])) {
$new_columns['afop_courier_ratio'] = ' ' . __('Courier Ratio', 'advance-fake-order-protector');
}
if (!isset($new_columns['afop_security_actions'])) {
$new_columns['afop_security_actions'] = ' ' . __('Security Actions', 'advance-fake-order-protector');
}
return $new_columns;
}
/**
* Render Custom Columns for Classic CPT Orders
*/
public function render_orders_column_content($column, $post_id) {
$order = wc_get_order($post_id);
if (!$order) {
return;
}
if ($column === 'afop_order_products') {
$this->render_order_products_column($order);
} elseif ($column === 'afop_courier_ratio') {
$this->render_courier_ratio_column($order);
} elseif ($column === 'afop_security_actions') {
$this->render_security_action_buttons($order);
}
}
/**
* Render Custom Columns for HPOS Orders
*/
public function render_hpos_orders_column_content($column, $order) {
if (!$order) {
return;
}
if ($column === 'afop_order_products') {
$this->render_order_products_column($order);
} elseif ($column === 'afop_courier_ratio') {
$this->render_courier_ratio_column($order);
} elseif ($column === 'afop_security_actions') {
$this->render_security_action_buttons($order);
}
}
/**
* Render Ordered Products with Image, Link & Quantity in Orders Table
*/
public function render_order_products_column($order) {
$items = $order->get_items();
if (empty($items)) {
echo '' . esc_html__('No items', 'advance-fake-order-protector') . ' ';
return;
}
?>
get_product();
$qty = $item->get_quantity();
$product_name = $item->get_name();
$product_id = $item->get_product_id();
// Image
$img_url = '';
if ($product) {
$img_id = $product->get_image_id();
if ($img_id) {
$img_url = wp_get_attachment_image_url($img_id, 'thumbnail');
}
}
if (empty($img_url)) {
$img_url = wc_placeholder_img_src('thumbnail');
}
// Link to edit product in admin or frontend permalink
$product_link = '';
if ($product_id) {
$product_link = get_edit_post_link($product_id);
if (empty($product_link)) {
$product_link = get_permalink($product_id);
}
}
?>
get_billing_phone();
$normalized_phone = AFOP_Validator::normalize_phone($phone);
$customer_name = $order->get_formatted_billing_full_name() ?: 'Customer';
if (empty($normalized_phone)) {
echo '' . esc_html__('No Phone', 'advance-fake-order-protector') . ' ';
return;
}
$active_provider = get_option('afop_courier_provider', 'bdcourier');
$has_api_configured = AFOP_Courier_Checker::is_provider_configured($active_provider) || AFOP_Courier_Checker::has_any_api_configured();
if (!$has_api_configured) {
?>
= 75) {
$risk_level = 'safe';
} elseif ($rate_num >= 50) {
$risk_level = 'medium';
} else {
$risk_level = 'high';
}
if ($risk_level === 'neutral') {
$risk_label = __('Insufficient History', 'advance-fake-order-protector');
} elseif ($risk_level === 'safe') {
$risk_label = __('Safe', 'advance-fake-order-protector');
} elseif ($risk_level === 'medium') {
$risk_label = __('Medium', 'advance-fake-order-protector');
} else {
$risk_label = __('Risky', 'advance-fake-order-protector');
}
if ($has_rating) {
$risk_label = $courier_stat['rating_label'] ?: __('Customer Rating', 'advance-fake-order-protector');
}
}
?>
get_billing_phone();
$normalized_phone = AFOP_Validator::normalize_phone($phone);
$client_ip = $order->get_customer_ip_address();
$customer_name = $order->get_formatted_billing_full_name() ?: 'Customer';
$phone_blocked = !empty($normalized_phone) ? AFOP_Blocklist::is_blocked('phone', $normalized_phone) : false;
$ip_blocked = !empty($client_ip) ? AFOP_Blocklist::is_blocked('ip', $client_ip) : false;
$customer_orders = !empty($normalized_phone) ? AFOP_Repeat_Order::get_orders_by_phone($normalized_phone) : array();
$order_count = count($customer_orders);
// WhatsApp direct message link
$wa_url = '';
if (!empty($normalized_phone)) {
$clean_phone = preg_replace('/[^0-9]/', '', $normalized_phone);
if (strpos($clean_phone, '88') !== 0 && strlen($clean_phone) === 11) {
$wa_num = '88' . $clean_phone;
} else {
$wa_num = $clean_phone;
}
/* translators: Numeric placeholders are counts; string placeholders identify the provider, customer or blocklist item. */
$wa_msg = rawurlencode(sprintf(__('Hello %1$s, regarding your order #%2$s.', 'advance-fake-order-protector'), $customer_name, $order->get_order_number()));
$wa_url = 'https://wa.me/' . esc_attr($wa_num) . '?text=' . $wa_msg;
}
?>
ID);
if (!$order) {
return;
}
$phone = $order->get_billing_phone();
$normalized_phone = AFOP_Validator::normalize_phone($phone);
$client_ip = $order->get_customer_ip_address();
$customer_name = $order->get_formatted_billing_full_name() ?: 'Customer';
$phone_blocked = !empty($normalized_phone) ? AFOP_Blocklist::is_blocked('phone', $normalized_phone) : false;
$ip_blocked = !empty($client_ip) ? AFOP_Blocklist::is_blocked('ip', $client_ip) : false;
$has_any_api = AFOP_Courier_Checker::has_any_api_configured();
$courier_stat = ($has_any_api && !empty($normalized_phone)) ? AFOP_Courier_Checker::get_quick_courier_stat($normalized_phone) : null;
$has_data = !empty($courier_stat['success']) && !empty($courier_stat['has_api']) && (isset($courier_stat['delivery_rate']) || !empty($courier_stat['customer_rating']));
$has_rate = $has_data && isset($courier_stat['delivery_rate']);
$has_rating = $has_data && !empty($courier_stat['customer_rating']);
$rate_num = $has_rate ? round(floatval($courier_stat['delivery_rate'])) : 0;
$total_orders = $has_data && isset($courier_stat['total_orders']) ? (string) $courier_stat['total_orders'] : ($courier_stat['volume_range'] ?? '—');
$delivered = $has_data && isset($courier_stat['delivered']) ? (string) $courier_stat['delivered'] : '—';
$returned = $has_data && isset($courier_stat['returned']) ? (string) $courier_stat['returned'] : '—';
$risk_level = 'neutral';
$risk_label = __('Check Ratio', 'advance-fake-order-protector');
if ($has_data) {
if (isset($courier_stat['risk_level'])) {
$risk_level = $courier_stat['risk_level'] === 'unknown' ? 'neutral' : $courier_stat['risk_level'];
} elseif ($rate_num >= 75) {
$risk_level = 'safe';
} elseif ($rate_num >= 50) {
$risk_level = 'medium';
} else {
$risk_level = 'high';
}
if ($risk_level === 'neutral') {
$risk_label = __('Insufficient History', 'advance-fake-order-protector');
} elseif ($risk_level === 'safe') {
$risk_label = __('Safe Customer', 'advance-fake-order-protector');
} elseif ($risk_level === 'medium') {
$risk_label = __('Medium Risk', 'advance-fake-order-protector');
} else {
$risk_label = __('High Risk', 'advance-fake-order-protector');
}
if ($has_rating) {
$risk_label = $courier_stat['rating_label'] ?: __('Customer Rating', 'advance-fake-order-protector');
}
}
?>
BD Courier
Steadfast
Pathao
0
Delivered Parcels
0
Returned / Cancelled
0
Total Parcels
0%
Return Rate
0
Total Orders Placed
৳ 0
Total Spent Amount
Order #
Date
Status
Products Purchased
Total
Action
'yes_no',
'afop_strict_fake_patterns' => 'yes_no',
'afop_invalid_phone_title' => 'text',
'afop_invalid_phone_msg' => 'textarea',
'afop_enable_blocklist' => 'yes_no',
'afop_blocked_title' => 'text',
'afop_blocked_msg' => 'textarea',
'afop_whatsapp_number' => 'text',
'afop_whatsapp_message' => 'textarea',
'afop_whatsapp_btn_text' => 'text',
'afop_enable_repeat_check' => 'yes_no',
'afop_repeat_time_limit' => 'int',
'afop_repeat_order_title' => 'text',
'afop_repeat_order_msg' => 'textarea',
'afop_repeat_whatsapp_btn' => 'text',
'afop_repeat_whatsapp_msg' => 'textarea',
'afop_enable_incomplete_capture'=> 'yes_no',
'afop_incomplete_retention_days'=> 'int',
'afop_courier_consent' => 'yes_no',
'afop_courier_provider' => 'text',
'afop_bdcourier_api_key' => 'text',
'afop_steadfast_api_key' => 'text',
'afop_steadfast_secret_key' => 'text',
'afop_pathao_base_url' => 'text',
'afop_pathao_client_id' => 'text',
'afop_pathao_client_secret' => 'text',
'afop_pathao_username' => 'text',
'afop_pathao_password' => 'text',
'afop_courier_cache_hours' => 'int'
);
$tab = isset($_POST['afop_settings_tab']) ? sanitize_key(wp_unslash($_POST['afop_settings_tab'])) : '';
$tabs = array(
'general' => array('afop_whatsapp_number', 'afop_whatsapp_message', 'afop_whatsapp_btn_text', 'afop_enable_blocklist', 'afop_blocked_title', 'afop_blocked_msg', 'afop_enable_incomplete_capture', 'afop_incomplete_retention_days'),
'validation' => array('afop_enable_bd_validation', 'afop_strict_fake_patterns', 'afop_invalid_phone_title', 'afop_invalid_phone_msg'),
'repeat' => array('afop_enable_repeat_check', 'afop_repeat_time_limit', 'afop_repeat_order_title', 'afop_repeat_order_msg', 'afop_repeat_whatsapp_btn', 'afop_repeat_whatsapp_msg'),
'courier' => array('afop_courier_consent', 'afop_courier_provider', 'afop_courier_cache_hours', 'afop_bdcourier_api_key', 'afop_steadfast_api_key', 'afop_steadfast_secret_key', 'afop_pathao_base_url', 'afop_pathao_client_id', 'afop_pathao_client_secret', 'afop_pathao_username', 'afop_pathao_password'),
);
if (!isset($tabs[$tab])) { return; }
foreach ($fields as $field => $type) {
if (!in_array($field, $tabs[$tab], true)) { continue; }
if ($field === 'afop_pathao_base_url') {
$url = isset($_POST[$field]) ? esc_url_raw(wp_unslash($_POST[$field])) : '';
if (in_array($url, array('https://api-hermes.pathao.com', 'https://courier-api-sandbox.pathao.com'), true)) { update_option($field, $url); }
delete_option('afop_pathao_token_data');
continue;
}
if (strpos($field, 'afop_pathao_') === 0) { delete_option('afop_pathao_token_data'); }
if ($field === 'afop_courier_provider' && (!isset($_POST[$field]) || !in_array($_POST[$field], array('bdcourier', 'steadfast', 'pathao'), true))) { continue; }
if ($type === 'yes_no') {
$val = isset($_POST[$field]) ? 'yes' : 'no';
update_option($field, $val);
} elseif ($type === 'int') {
$limits = array('afop_repeat_time_limit' => 1440, 'afop_courier_cache_hours' => 168, 'afop_incomplete_retention_days' => 365);
$val = max(1, min($limits[$field], isset($_POST[$field]) ? absint($_POST[$field]) : 1));
update_option($field, $val);
} elseif ($type === 'textarea') {
$val = isset($_POST[$field]) ? sanitize_textarea_field(wp_unslash($_POST[$field])) : '';
update_option($field, $val);
} else {
$val = isset($_POST[$field]) ? sanitize_text_field(wp_unslash($_POST[$field])) : '';
update_option($field, $val);
}
}
add_settings_error('afop_messages', 'afop_message', __('Settings saved successfully!', 'advance-fake-order-protector'), 'updated');
}
/**
* Admin AJAX to fetch customer store orders history by phone
*/
public function ajax_get_customer_orders() {
check_ajax_referer('afop_admin_nonce', 'nonce');
if (!current_user_can('manage_woocommerce')) {
wp_send_json_error(array('message' => 'Unauthorized.'));
}
$phone = isset($_POST['phone']) ? sanitize_text_field(wp_unslash($_POST['phone'])) : '';
if (empty($phone)) {
wp_send_json_error(array('message' => 'Phone number is required.'));
}
$orders = AFOP_Repeat_Order::get_orders_by_phone($phone);
$total_spent = 0;
foreach ($orders as $o) {
$total_spent += floatval($o['raw_total']);
}
wp_send_json_success(array(
'phone' => AFOP_Validator::normalize_phone($phone),
'order_count' => count($orders),
'formatted_spent' => wp_strip_all_tags(wc_price($total_spent)),
'orders' => $orders
));
}
/**
* Render Settings Page
*/
public function render_settings_page() {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only admin navigation/search; menu capability controls access, mutations have nonces.
$active_tab = isset($_GET['tab']) ? sanitize_text_field(wp_unslash($_GET['tab'])) : 'general';
settings_errors('afop_messages');
?>
Flat No: 4A, House, 9 Main Rd, Dhaka 1207
prefix . 'afop_incomplete_orders';
// Stats
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Prepared operations on plugin-owned recovery/blocklist tables require current records and counts after admin or checkout writes.
$total_leads = $wpdb->get_var($wpdb->prepare("SELECT COUNT(id) FROM %i WHERE status = 'incomplete'", $table));
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Prepared operations on plugin-owned recovery/blocklist tables require current records and counts after admin or checkout writes.
$total_converted = $wpdb->get_var($wpdb->prepare("SELECT COUNT(id) FROM %i WHERE status = 'converted'", $table));
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Prepared operations on plugin-owned recovery/blocklist tables require current records and counts after admin or checkout writes.
$total_recovered = $wpdb->get_var($wpdb->prepare("SELECT COUNT(id) FROM %i WHERE status = 'recovered'", $table));
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Prepared operations on plugin-owned recovery/blocklist tables require current records and counts after admin or checkout writes.
$lost_revenue = $wpdb->get_var($wpdb->prepare("SELECT SUM(cart_total) FROM %i WHERE status = 'incomplete'", $table)) ?: 0.00;
// Fetch active incomplete items
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only admin navigation/search; menu capability controls access, mutations have nonces.
$search = isset($_GET['s']) ? sanitize_text_field(wp_unslash($_GET['s'])) : '';
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only admin navigation/search; menu capability controls access, mutations have nonces.
$filter_status = isset($_GET['status_filter']) ? sanitize_text_field(wp_unslash($_GET['status_filter'])) : 'incomplete';
$like = '%' . $wpdb->esc_like($search) . '%';
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Prepared operations on plugin-owned recovery/blocklist tables require current records and counts after admin or checkout writes.
$items = $wpdb->get_results($wpdb->prepare(
"SELECT * FROM %i WHERE (%s = 'all' OR status = %s) AND (%s = '' OR phone LIKE %s OR name LIKE %s OR address LIKE %s) ORDER BY updated_at DESC LIMIT 100", $table,
$filter_status, $filter_status, $search, $like, $like, $like
));
?>
Pending Incomplete
Potential Lost Value
Converted to Orders
Manually Recovered
Customer & Contact
Location & IP
Cart Items
Total
Time
Status
Actions
phone);
if (substr($clean_phone, 0, 2) !== '88' && strlen($clean_phone) === 11) {
$wa_num = '88' . $clean_phone;
} else {
$wa_num = $clean_phone;
}
$wa_msg = rawurlencode(sprintf('Hello %s, we noticed you started an order on our store. Do you need any assistance?', $item->name ?: 'Customer'));
$cart_items = json_decode($item->cart_data, true) ?: array();
?>
name ?: 'Unknown Name'); ?>
phone); ?>
email)): ?>
email); ?>
address)): ?>
address)); ?>
city)): ?>
City: city); ?>
IP: ip_address); ?>
×
Cart empty
cart_total)); ?>
updated_at), current_time('timestamp'))); ?> ago
status)); ?>
Ratio
status === 'incomplete'): ?>
prefix . 'afop_blocklist';
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only admin navigation/search; menu capability controls access, mutations have nonces.
$search = isset($_GET['s']) ? sanitize_text_field(wp_unslash($_GET['s'])) : '';
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only admin navigation/search; menu capability controls access, mutations have nonces.
$type_filter = isset($_GET['type_filter']) ? sanitize_text_field(wp_unslash($_GET['type_filter'])) : 'all';
$like = '%' . $wpdb->esc_like($search) . '%';
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Prepared operations on plugin-owned recovery/blocklist tables require current records and counts after admin or checkout writes.
$items = $wpdb->get_results($wpdb->prepare(
"SELECT * FROM %i WHERE (%s = 'all' OR type = %s) AND (%s = '' OR value LIKE %s OR reason LIKE %s) ORDER BY updated_at DESC LIMIT 150", $table,
$type_filter, $type_filter, $search, $like, $like
));
$export_url = wp_nonce_url(admin_url('admin-post.php?action=afop_export_blocklist_csv'), 'afop_export_blocklist_nonce');
?>