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 ' ' . esc_html($eval['message']) . '' . esc_html__('Right of withdrawal', 'ai-eshop-optimizer') . '
'; 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 ''; } 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 '' . esc_html__('Print this and attach it to the parcel. The courier details are already filled in.', 'ai-eshop-optimizer') . '
'; echo ''; echo ''; echo '' . esc_html($st['title']) . '
'; $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 .= '' . esc_html__('You never have to give a reason — it just helps us improve.', 'ai-eshop-optimizer') . '
'; $html .= '' . esc_html__('Questions? We are here to help.', 'ai-eshop-optimizer') . '
'; if ($email !== '') { $html .= ''; } if (!empty($rows)) { $html .= '