get_customer_id() !== get_current_user_id()) { return $actions; } $eval = AIEO_Withdrawal::evaluate($order); if ($eval['tier'] === AIEO_Withdrawal::TIER_NONE) { return $actions; } $actions['aieo-withdraw'] = array( 'url' => self::panel_url($order), 'name' => ($eval['tier'] === AIEO_Withdrawal::TIER_AUTO_CANCEL) ? __('Cancel order', 'ai-eshop-optimizer') : __('Withdraw', 'ai-eshop-optimizer'), ); return $actions; } public static function add_label_button($actions, $order) { if (!$order instanceof WC_Order) { return $actions; } if (!is_user_logged_in()) { return $actions; } if ((int) $order->get_customer_id() !== get_current_user_id()) { return $actions; } $row = self::label_row($order); if (!$row) { return $actions; } $actions['aieo-return-label'] = array( 'url' => self::label_url($row), 'name' => __('Return label', 'ai-eshop-optimizer'), ); return $actions; } public static function label_row($order) { if (!class_exists('AIEO_Withdrawal_Returns')) { return null; } return AIEO_Withdrawal_Returns::label_row_for_order((int) $order->get_id()); } public static function label_url($row) { return wp_nonce_url( add_query_arg(self::LABEL_ARG, (int) $row->id, wc_get_page_permalink('myaccount')), self::NONCE_LABEL . '_' . (int) $row->id ); } public static function signed_label_url($row, $order) { $exp = time() + self::LABEL_TTL; $tok = (int) $row->id . '.' . $exp . '.' . self::label_signature($row, $order, $exp); return add_query_arg( array( self::LABEL_ARG => (int) $row->id, self::LABEL_TOKEN => rawurlencode($tok), ), wc_get_page_permalink('myaccount') ); } private static function label_signature($row, $order, $exp) { return hash_hmac( 'sha256', implode('|', array( (int) $row->id, (int) $order->get_id(), strtolower((string) $order->get_billing_email()), (int) $exp, )), wp_salt('auth') ); } public static function maybe_stream_label() { if (empty($_GET[self::LABEL_ARG])) { return; } if (!class_exists('AIEO_Withdrawal_Returns')) { return; } $id = absint(wp_unslash($_GET[self::LABEL_ARG])); $row = $id ? AIEO_Withdrawal_Returns::declaration($id) : null; if (!$row) { wp_die(esc_html__('That return label could not be found.', 'ai-eshop-optimizer'), '', array('response' => 404)); } $order = wc_get_order((int) $row->order_id); if (!$order instanceof WC_Order) { wp_die(esc_html__('That return label could not be found.', 'ai-eshop-optimizer'), '', array('response' => 404)); } if (empty($row->approved_at_gmt)) { wp_die( esc_html__('That return label is not ready yet. We will email it to you as soon as it is.', 'ai-eshop-optimizer'), '', array('response' => 404) ); } if (!self::may_download_label($row, $order)) { wp_die( esc_html__('That link is no longer valid. Sign in to your account to download the label, or ask us for a fresh link.', 'ai-eshop-optimizer'), '', array('response' => 403) ); } $label = AIEO_Withdrawal_Returns::label_pdf($row); if (empty($label['ok'])) { wp_die(esc_html((string) $label['error']), '', array('response' => 409)); } nocache_headers(); header('Content-Type: ' . (!empty($label['mime']) ? $label['mime'] : 'application/pdf')); header('Content-Disposition: attachment; filename="' . $label['filename'] . '"'); header('Content-Length: ' . strlen($label['pdf'])); echo $label['pdf']; exit; } private static function may_download_label($row, $order) { if (is_user_logged_in() && (int) $order->get_customer_id() === get_current_user_id() && (int) $order->get_customer_id() > 0) { $nonce = isset($_GET['_wpnonce']) ? sanitize_text_field(wp_unslash($_GET['_wpnonce'])) : ''; if (wp_verify_nonce($nonce, self::NONCE_LABEL . '_' . (int) $row->id)) { return true; } } $token = isset($_GET[self::LABEL_TOKEN]) ? sanitize_text_field(wp_unslash($_GET[self::LABEL_TOKEN])) : ''; if ($token === '') { return false; } $parts = explode('.', $token); if (count($parts) !== 3) { return false; } list($tok_id, $exp, $sig) = $parts; if ((int) $tok_id !== (int) $row->id) { return false; } if ((int) $exp < time()) { return false; } return hash_equals(self::label_signature($row, $order, (int) $exp), (string) $sig); } public static function panel_url($order) { $base = wc_get_endpoint_url('view-order', $order->get_id(), wc_get_page_permalink('myaccount')); return wp_nonce_url( add_query_arg(self::QUERY_ARG, 1, $base), self::NONCE_VIEW . '_' . $order->get_id() ); } public static function maybe_render_panel($order) { if (!AIEO_Withdrawal::is_enabled()) { return; } if (!$order instanceof WC_Order) { return; } if (empty($_GET[self::QUERY_ARG])) { return; } if (!is_user_logged_in()) { return; } if ((int) $order->get_customer_id() !== get_current_user_id()) { return; } $nonce = isset($_GET['_wpnonce']) ? sanitize_text_field(wp_unslash($_GET['_wpnonce'])) : ''; if (!wp_verify_nonce($nonce, self::NONCE_VIEW . '_' . $order->get_id())) { return; } $eval = AIEO_Withdrawal::evaluate($order); self::styles(); echo '
'; echo '
' . self::icon() . '

' . esc_html__('Right of withdrawal', 'ai-eshop-optimizer') . '

'; if ($eval['tier'] === AIEO_Withdrawal::TIER_NONE) { echo '

' . esc_html($eval['message']) . '

'; echo self::contact_block(); echo '
'; return; } echo '

'; echo esc_html( $eval['tier'] === AIEO_Withdrawal::TIER_AUTO_CANCEL ? __('This order has not been dispatched yet, so it will be cancelled immediately.', 'ai-eshop-optimizer') : __('We will register your withdrawal and email you a confirmation with the date and time it was submitted, then arrange the return and refund.', 'ai-eshop-optimizer') ); echo '

'; if (!empty($eval['deadline_gmt'])) { echo '

' . esc_html__('Available until', 'ai-eshop-optimizer') . ' ' . esc_html(wp_date( get_option('date_format') . ', ' . get_option('time_format'), strtotime($eval['deadline_gmt'] . ' UTC') )) . '

'; } echo '
'; wp_nonce_field(self::NONCE_SUBMIT . '_' . $order->get_id(), 'aieo_withdraw_nonce'); $items = $eval['items']; if (count($items) > 1) { echo '
'; echo '' . esc_html__('Which items?', 'ai-eshop-optimizer') . ''; echo '

' . esc_html__('Leave all unticked to withdraw from the entire order.', 'ai-eshop-optimizer') . '

'; foreach ($items as $row) { echo self::item_row($row); } echo '
'; } elseif (!empty($items) && (int) $items[0]['qty'] > 1) { echo '
'; echo '' . esc_html__('How many?', 'ai-eshop-optimizer') . ''; echo '

' . esc_html__('You can return part of this line if you ordered more than you need.', 'ai-eshop-optimizer') . '

'; echo self::item_row($items[0], true); echo '
'; } echo self::reason_field(); echo '
'; echo ''; echo ''; echo '
'; echo '
'; echo ''; echo '' . esc_html__('Back to my orders', 'ai-eshop-optimizer') . ''; echo '
'; echo '
'; echo ''; } public static function render_label_notice($order) { if (!$order instanceof WC_Order) { return; } if (!is_user_logged_in()) { return; } if ((int) $order->get_customer_id() !== get_current_user_id()) { return; } $row = self::label_row($order); if (!$row) { return; } self::styles(); echo '
'; echo '
' . self::icon() . '

' . esc_html__('Your return label', 'ai-eshop-optimizer') . '

'; echo '

' . esc_html__('Print this and attach it to the parcel. The courier details are already filled in.', 'ai-eshop-optimizer') . '

'; echo '

' . esc_html__('Voucher', 'ai-eshop-optimizer') . ' ' . esc_html((string) $row->return_voucher_no) . '

'; echo '
' . esc_html__('Download the label', 'ai-eshop-optimizer') . '
'; echo '
'; } public static function render_status_notice($order) { if (!$order instanceof WC_Order) { return; } if (!is_user_logged_in()) { return; } if ((int) $order->get_customer_id() !== get_current_user_id()) { return; } $row = AIEO_Withdrawal::latest_declaration((int) $order->get_id()); if (!$row) { return; } echo self::status_html($order, $row, false); } public static function status_html($order, $row, $label_link = false) { self::styles(); $st = AIEO_Withdrawal::customer_status($row); $status = (string) $row->status; $fmt = get_option('date_format') . ', ' . get_option('time_format'); $at = static function ($gmt) use ($fmt) { $gmt = (string) $gmt; if ($gmt === '' || $gmt === '0000-00-00 00:00:00') { return ''; } $ts = strtotime($gmt . ' UTC'); return $ts ? '' : ''; }; $refused = AIEO_Withdrawal::STATUS_REJECTED === $status; $cancelled = AIEO_Withdrawal::STATUS_CANCELLED === $status; $ended = $refused || $cancelled || AIEO_Withdrawal::STATUS_REFUNDED === $status; $tone = $refused ? 'error' : ($cancelled ? 'info' : 'success'); if ($refused || $cancelled) { $steps = array( array(__('Received', 'ai-eshop-optimizer'), $row->declared_at_gmt ?? ''), $refused ? array(__('Not accepted', 'ai-eshop-optimizer'), $row->rejected_at_gmt ?? '') : array(__('Order cancelled', 'ai-eshop-optimizer'), ($row->processed_at_gmt ?? '') ?: ($row->declared_at_gmt ?? '')), ); } else { $steps = array( array(__('Received', 'ai-eshop-optimizer'), $row->declared_at_gmt ?? ''), array(__('Approved', 'ai-eshop-optimizer'), $row->approved_at_gmt ?? ''), array(__('Items back', 'ai-eshop-optimizer'), $row->received_at_gmt ?? ''), array(__('Refunded', 'ai-eshop-optimizer'), $row->refunded_at_gmt ?? ''), ); } $html = '
'; $html .= '
' . self::icon() . '

' . esc_html__('Your withdrawal', 'ai-eshop-optimizer') . '

'; $html .= '

' . esc_html($st['title']) . '

'; $html .= '
    '; foreach ($steps as $i => $step) { $n = $i + 1; $cls = 'aieo-wd__step'; $aria = ''; if ($ended || $n < (int) $st['step']) { $cls .= ' is-done'; } elseif ($n === (int) $st['step']) { $cls .= ' is-current'; $aria = ' aria-current="step"'; } $when = $at($step[1]); $html .= '
  1. ' . esc_html($step[0]) . ($when !== '' ? ' ' . $when : '') . '
  2. '; } $html .= '
'; $html .= '

' . esc_html($st['detail']) . '

'; if ($label_link && AIEO_Withdrawal::STATUS_PENDING === $status && class_exists('AIEO_Withdrawal_Returns') && AIEO_Withdrawal_Returns::has_label($row)) { $url = self::signed_label_url($row, $order); if ($url) { $html .= '

' . esc_html__('Download the return label', 'ai-eshop-optimizer') . '

'; } } $html .= self::contact_block(); $html .= '
'; return $html; } public static function refund_reason_labels($totals, $order = null) { if (!is_array($totals) || !class_exists('AIEO_DMM_Cancellation_Reasons_Data')) { return $totals; } foreach ($totals as $key => $row) { if (strpos((string) $key, 'refund_') !== 0 || !is_array($row) || !isset($row['value'])) { continue; } $value = (string) $row['value']; if (!preg_match('#\s*([^<]*)\s*$#', $value, $m)) { continue; } $text = trim(html_entity_decode($m[1], ENT_QUOTES, 'UTF-8')); if ($text === '' || $text !== sanitize_key($text)) { continue; } $reason = AIEO_DMM_Cancellation_Reasons_Data::get($text); if (!$reason) { continue; } $words = (!empty($reason['is_customer_visible']) && trim((string) $reason['label']) !== '') ? '
' . esc_html(trim((string) $reason['label'])) . '' : ''; $totals[$key]['value'] = substr($value, 0, -strlen($m[0])) . $words; } return $totals; } public static function maybe_handle_submit() { if (!AIEO_Withdrawal::is_enabled()) { return; } if (!isset($_POST['aieo_withdraw_nonce'])) { return; } if (!is_user_logged_in()) { return; } $order_id = absint(get_query_var('view-order')); if (!$order_id && isset($_GET['order_id'])) { $order_id = absint($_GET['order_id']); } if (!$order_id) { return; } $nonce = sanitize_text_field(wp_unslash($_POST['aieo_withdraw_nonce'])); if (!wp_verify_nonce($nonce, self::NONCE_SUBMIT . '_' . $order_id)) { wc_add_notice(__('That link expired. Please try again.', 'ai-eshop-optimizer'), 'error'); return; } $order = wc_get_order($order_id); if (!$order || (int) $order->get_customer_id() !== get_current_user_id()) { wc_add_notice(__('Order not found.', 'ai-eshop-optimizer'), 'error'); return; } $item_ids = isset($_POST['aieo_withdraw_items']) && is_array($_POST['aieo_withdraw_items']) ? array_map('absint', wp_unslash($_POST['aieo_withdraw_items'])) : array(); $note = isset($_POST['aieo_withdraw_note']) ? sanitize_textarea_field(wp_unslash($_POST['aieo_withdraw_note'])) : ''; $item_qty = array(); if (isset($_POST['aieo_withdraw_qty']) && is_array($_POST['aieo_withdraw_qty'])) { foreach (wp_unslash($_POST['aieo_withdraw_qty']) as $k => $v) { $item_qty[(int) $k] = (int) $v; } } $result = AIEO_Withdrawal::execute($order, array( 'item_ids' => $item_ids, 'item_qty' => $item_qty, 'channel' => 'account', 'note' => $note, 'actor_id' => get_current_user_id(), )); if (is_wp_error($result)) { wc_add_notice($result->get_error_message(), 'error'); return; } wc_add_notice( $result['cancelled'] ? __('Your order has been cancelled. A confirmation is on its way to your email.', 'ai-eshop-optimizer') : __('Your withdrawal has been registered. We have emailed you a confirmation with the date and time.', 'ai-eshop-optimizer'), 'success' ); wp_safe_redirect(wc_get_endpoint_url('view-order', $order_id, wc_get_page_permalink('myaccount'))); exit; } public static function styles() { static $done = false; if ($done) { return; } $done = true; if (class_exists('AIEO_Form_Fields')) { echo AIEO_Form_Fields::style_here(); } ?> '; $html .= ''; $html .= '' . esc_html($row['name']) . ''; if ($qty > 1) { $html .= ''; $html .= ''; $html .= ''; $html .= '' . esc_html(sprintf( __('of %d', 'ai-eshop-optimizer'), $qty )) . ''; $html .= ''; } else { $html .= '×1'; } $html .= ''; return $html; } public static function reason_field() { if (!class_exists('AIEO_DMM_Cancellation_Reasons_Data')) { return ''; } $rows = array(); foreach ((array) AIEO_DMM_Cancellation_Reasons_Data::enabled() as $row) { $row = (array) $row; if (empty($row['is_customer_visible'])) { continue; } $slug = isset($row['slug']) ? (string) $row['slug'] : ''; $label = isset($row['label']) ? (string) $row['label'] : $slug; if ($slug === '') { continue; } $rows[$slug] = $label; } if (empty($rows)) { return ''; } $html = '
'; $html .= ''; $html .= ''; $html .= '

' . esc_html__('You never have to give a reason — it just helps us improve.', 'ai-eshop-optimizer') . '

'; $html .= '
'; return $html; } public static function account_offer_hint($magic_link) { if ($magic_link) { return __('Your orders — this one and your previous ones — will live in your account, so next time you can cancel or withdraw from individual items in one click. No password to set: we email you a sign-in link whenever you want in.', 'ai-eshop-optimizer'); } return __('We will link this and your previous orders to it, so next time you can cancel or withdraw from individual items in one click. We will email you a link to set a password.', 'ai-eshop-optimizer'); } public static function account_created_lines($linked, $magic_link) { $linked = max(1, (int) $linked); $lines = array( sprintf( _n( 'Your account is ready and %d order now lives in it.', 'Your account is ready and your %d orders now live in it.', $linked, 'ai-eshop-optimizer' ), $linked ), ); $lines[] = $magic_link ? __('Sign in any time with the link we have just emailed you — there is no password to remember, and we will send a fresh link whenever you need one.', 'ai-eshop-optimizer') : __('Check your email for a link to set a password.', 'ai-eshop-optimizer'); return $lines; } private static function icon() { return ''; } public static function contact_block() { $email = ''; if (function_exists('WC') && WC()->mailer()) { $email = (string) WC()->mailer()->get_from_address(); } if ($email === '') { $email = (string) get_option('woocommerce_email_from_address', ''); } $rows = array(); if (class_exists('AIEO_Google_Reviews_Locations')) { foreach ((array) AIEO_Google_Reviews_Locations::list_all(true) as $loc) { $loc = (array) $loc; $label = trim((string) ($loc['label'] ?? $loc['slug'] ?? '')); $parts = array_filter(array( trim((string) ($loc['address'] ?? '')), trim((string) ($loc['phone'] ?? '')), )); if ($label === '' && empty($parts)) { continue; } $rows[] = array('label' => $label, 'parts' => $parts); } } if ($email === '' && empty($rows)) { return ''; } $html = '
'; $html .= '

' . esc_html__('Questions? We are here to help.', 'ai-eshop-optimizer') . '

'; if ($email !== '') { $html .= '

' . esc_html($email) . '

'; } if (!empty($rows)) { $html .= ''; } $html .= '
'; return $html; } } }