get_settings();
if ($s['enabled'] !== 'yes') { return; }
add_filter('wc_order_attribution_origin_formatted_source', array($this, 'origin_phone_label'), 10, 2);
if ('yes' === $s['admin_checkbox']) {
add_action('woocommerce_admin_order_data_after_billing_address', array($this, 'render_admin_checkbox'));
add_action('woocommerce_process_shop_order_meta', array($this, 'save_admin_checkbox'));
add_action('admin_enqueue_scripts', array($this, 'admin_styles'));
}
if ('yes' === $s['user_search_by_phone']) {
add_action('pre_user_query', array($this, 'allow_user_search_by_phone'));
}
}
public static function is_phone_order($order) {
if (!is_a($order, 'WC_Order')) { return false; }
if ('yes' === (string) $order->get_meta('_phone_order')) { return true; }
if ('admin' === (string) $order->get_meta('_wc_order_attribution_source_type')) { return true; }
return false;
}
public function origin_phone_label($formatted, $source) {
if ('Web admin' === $source || $source === __('Web admin', 'woocommerce')) {
return 'Phone Orders';
}
return $formatted;
}
public function get_settings() {
$stored = get_option(self::OPTION_KEY, array());
if (!is_array($stored)) { $stored = array(); }
return array_merge(array(
'enabled' => 'yes',
'admin_checkbox' => 'yes',
'user_search_by_phone' => 'yes',
), $stored);
}
public function ajax_save() {
AIEO_DMM::verify_ajax_request();
update_option(self::OPTION_KEY, array(
'enabled' => !empty($_POST['enabled']) ? 'yes' : 'no',
'admin_checkbox' => !empty($_POST['admin_checkbox']) ? 'yes' : 'no',
'user_search_by_phone' => !empty($_POST['user_search_by_phone']) ? 'yes' : 'no',
), false);
wp_send_json_success(array('message' => __('Phone orders settings saved.', 'ai-eshop-optimizer')));
}
public function render_admin_checkbox($order) {
if (!is_a($order, 'WC_Order')) { return; }
$val = $order->get_meta('_phone_order');
if (function_exists('rooster_admin_table_assets')) { rooster_admin_table_assets(); }
echo '
';
echo \Rooster\Admin\Ui::toggle('phone_order', 'yes' === $val, array('value' => 'yes', 'hidden' => false, 'id' => 'phone_order', 'label' => __('Phone Order', 'ai-eshop-optimizer')));
echo '
';
}
public function save_admin_checkbox($order_id) {
$order = wc_get_order($order_id);
if (!$order) { return; }
$val = isset($_POST['phone_order']) ? 'yes' : 'no';
$order->update_meta_data('_phone_order', $val);
$order->save();
}
public function admin_styles($hook) {
$screen = function_exists('get_current_screen') ? get_current_screen() : null;
if ($screen && in_array($screen->id, array('shop_order', 'woocommerce_page_wc-orders'), true)) {
wp_add_inline_style('woocommerce_admin_styles', '#phone_order{width:20px;height:20px;}');
}
}
public function allow_user_search_by_phone($user_query) {
if (empty($_REQUEST['phone'])) { return; }
$phone = sanitize_text_field(wp_unslash($_REQUEST['phone']));
if ($phone === '') { return; }
$user_query->meta_query->relation = 'OR';
$user_query->meta_query->queries[] = array(
'key' => 'billing_phone',
'value' => $phone,
'compare' => 'LIKE',
);
}
public function render_subtab() {
$s = $this->get_settings();
list($aieo_po_open, $aieo_po_close) = AIEO_DMM_UI::card(array(
'anchor' => 'phone-orders-settings',
'icon' => 'settings',
'title' => __('Settings', 'ai-eshop-optimizer'),
'description' => __('Two small admin conveniences: a "Phone Order" checkbox on the order edit page (saved to `_phone_order` order meta), and the ability to search WP users by their billing phone number.', 'ai-eshop-optimizer'),
'footer' => \Rooster\Admin\States::save_button('aieo-dmm-po-form', array('label' => __('Save', 'ai-eshop-optimizer'))),
));
echo $aieo_po_open;
$aieo_po_ui = '\Rooster\Admin\Ui';
$aieo_po_dep = \Rooster\Admin\States::depends_attrs('aieo-po-enabled', 'yes' === $s['enabled']);
$aieo_po_sw = static function ($name, $on) use ($aieo_po_ui) {
return $aieo_po_ui::toggle($name, $on, array('id' => 'aieo-po-' . str_replace('_', '-', $name), 'hidden' => false, 'label' => __('On', 'ai-eshop-optimizer'), 'label_hidden' => true));
};
$aieo_po_rows = $aieo_po_ui::field_row(array(
'id' => 'aieo-po-enabled',
'label' => __('Enable', 'ai-eshop-optimizer'),
'control' => $aieo_po_ui::toggle('enabled', 'yes' === $s['enabled'], array('id' => 'aieo-po-enabled', 'hidden' => false, 'label' => __('On', 'ai-eshop-optimizer'), 'label_hidden' => true)),
))
. $aieo_po_ui::field_row(array(
'id' => 'aieo-po-admin-checkbox',
'label' => __('Phone-order checkbox on admin order page', 'ai-eshop-optimizer'),
'attrs' => $aieo_po_dep,
'control' => $aieo_po_sw('admin_checkbox', 'yes' === $s['admin_checkbox']),
))
. $aieo_po_ui::field_row(array(
'id' => 'aieo-po-user-search-by-phone',
'label' => __('User search by billing phone', 'ai-eshop-optimizer'),
'help' => __('Adds &?phone=… filter to wp-admin/users.php queries', 'ai-eshop-optimizer'),
'attrs' => $aieo_po_dep,
'control' => $aieo_po_sw('user_search_by_phone', 'yes' === $s['user_search_by_phone']),
));
$aieo_po_form = AIEO_DMM_UI::card_form('aieo_dmm_phone_orders_save', 'phone_orders', 'aieo-dmm-po-form', '' . $aieo_po_rows . '
');
echo $aieo_po_form;
echo $aieo_po_close;
}
}