get_row( $wpdb->prepare( "SELECT * FROM %i WHERE token_hash = %s AND expires_at > %d", ACPW_Store::events(), hash( 'sha256', $token ), time()) ); if ( ! $event ) { return false; } $cart = ACPW_Store::cart( $event->cart_uuid ); if ( ! $cart || in_array( $cart->status, array( 'ordered', 'empty', 'superseded', 'erased' ), true ) ) { return false; } $entry = json_decode( $event->payload, true ); return is_array( $entry ) && ( ! array_key_exists( 'recovery_enabled', $entry ) || ! empty( $entry['recovery_enabled'] ) ) ? array( 'entry' => $entry, 'cart_uuid' => $event->cart_uuid, 'event_uuid' => $event->event_uuid ) : false; } public static function handle() { $param = isset( $_GET['acpw_restore'] ) ? 'acpw_restore' : 'oacw_restore'; if ( ! isset( $_GET[ $param ] ) ) { return; } if ( ! defined( 'DONOTCACHEPAGE' ) ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- This is the established cache-plugin interoperability constant. define( 'DONOTCACHEPAGE', true ); } nocache_headers(); header( 'Referrer-Policy: no-referrer' ); header( 'X-Robots-Tag: noindex, nofollow' ); header( "Content-Security-Policy: default-src 'none'; style-src 'unsafe-inline'; form-action 'self'; base-uri 'none'; frame-ancestors 'none'" ); if ( ! is_string( $_GET[ $param ] ) ) { self::page( __( 'Recovery link unavailable', 'abandoned-cart-webhooks-for-woocommerce' ), '

' . esc_html__( 'This recovery link is invalid.', 'abandoned-cart-webhooks-for-woocommerce' ) . '

', 400 ); } if ( ! WC()->cart || ! WC()->session ) { self::page( __( 'Recovery unavailable', 'abandoned-cart-webhooks-for-woocommerce' ), '

' . esc_html__( 'The shopping cart is temporarily unavailable. Please try again.', 'abandoned-cart-webhooks-for-woocommerce' ) . '

', 503 ); } $token = sanitize_text_field( wp_unslash( $_GET[ $param ] ) ); $found = self::lookup( $token ); if ( ! $found ) { self::page( __( 'Recovery link unavailable', 'abandoned-cart-webhooks-for-woocommerce' ), '

' . esc_html__( 'This link has expired, is invalid, or the cart has already been checked out.', 'abandoned-cart-webhooks-for-woocommerce' ) . '

' . esc_html__( 'Visit your cart', 'abandoned-cart-webhooks-for-woocommerce' ) . '

', 410 ); } if ( 'POST' === ( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ?? '' ) ) ) ) { $nonce = isset( $_POST['_wpnonce'] ) && is_string( $_POST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ) : ''; if ( ! wp_verify_nonce( $nonce, 'acpw_restore_' . hash( 'sha256', $token ) ) ) { self::page( __( 'Please reopen your recovery link', 'abandoned-cart-webhooks-for-woocommerce' ), '

' . esc_html__( 'The confirmation expired. Open the link again and retry.', 'abandoned-cart-webhooks-for-woocommerce' ) . '

', 403 ); } $mode = isset( $_POST['mode'] ) && 'replace' === $_POST['mode'] ? 'replace' : 'merge'; $result = self::restore( $found['entry'], $mode ); if ( is_wp_error( $result ) ) { self::page( __( 'Cart could not be restored', 'abandoned-cart-webhooks-for-woocommerce' ), '

' . esc_html( $result->get_error_message() ) . '

' . esc_html__( 'Visit your cart', 'abandoned-cart-webhooks-for-woocommerce' ) . '

', 409 ); } WC()->session->set_customer_session_cookie( true ); WC()->session->set( 'acpw_recovery_event', $found['event_uuid'] ); // A recovery token authorises items, never ownership of the sender's tracking identity. // Keep this browser's own identity, or generate a fresh one. Do not copy source contact data. ACPW_Capture::queue(); ACPW_Capture::flush(); WC()->cart->set_session(); WC()->session->save_data(); wp_safe_redirect( wc_get_checkout_url(), 303 ); exit; } $body = '

' . esc_html__( 'This link restores saved items. It does not sign you in or copy contact details. Anyone holding this link can use it.', 'abandoned-cart-webhooks-for-woocommerce' ) . '

' . esc_html__( 'Restore the available items below using today\'s prices and stock availability. Your account and saved addresses will not be changed.', 'abandoned-cart-webhooks-for-woocommerce' ) . '

'; $body .= wp_nonce_field( 'acpw_restore_' . hash( 'sha256', $token ), '_wpnonce', true, false ); if ( ! WC()->cart->is_empty() ) { $body .= '

' . esc_html__( 'Your current cart already contains items.', 'abandoned-cart-webhooks-for-woocommerce' ) . '

'; } else { $body .= ''; } $body .= '
'; self::page( __( 'Restore your cart', 'abandoned-cart-webhooks-for-woocommerce' ), $body ); } /** Restore products, variations, quantities and coupons; do not replay old prices or customer accounts. */ public static function restore( array $entry, $mode = 'merge' ) { $available = array(); $skipped = 0; foreach ( (array) ( $entry['cart'] ?? array() ) as $line ) { // This flag exists in the supplied payloads. Leave automatic gifts to the store's rules. if ( ! empty( $line['wdr_free_product'] ) ) { continue; } $pid = (int) ( $line['product_id'] ?? 0 ); $vid = (int) ( $line['variation_id'] ?? 0 ); $qty = wc_stock_amount( $line['quantity'] ?? 1 ); $product = wc_get_product( $vid ?: $pid ); if ( ! $product || $qty <= 0 || ! $product->is_purchasable() || ! $product->is_in_stock() || ! $product->has_enough_stock( $qty ) || ( $vid && (int) $product->get_parent_id() !== $pid ) ) { ++$skipped; continue; } $available[] = array( $pid, $qty, $vid, (array) ( $line['variation'] ?? array() ), $line ); } if ( ! $available ) { return new WP_Error( 'acpw_no_items', __( 'The saved items are no longer available. Your current cart has been kept.', 'abandoned-cart-webhooks-for-woocommerce' ) ); } ACPW_Capture::$restoring = true; $before = WC()->cart->get_cart(); $coupons = WC()->cart->get_applied_coupons(); $added = 0; try { if ( 'replace' === $mode ) { WC()->cart->empty_cart( false ); } foreach ( $available as list( $pid, $qty, $vid, $variation, $line ) ) { $custom = (array) apply_filters( 'acpw_restore_cart_item_data', array(), $line ); if ( WC()->cart->add_to_cart( $pid, $qty, $vid, $variation, $custom ) ) { ++$added; } else { ++$skipped; } } if ( ! $added ) { WC()->cart->set_cart_contents( $before ); WC()->cart->set_applied_coupons( $coupons ); WC()->cart->calculate_totals(); return new WP_Error( 'acpw_validation', __( 'The saved items could not be added. Your current cart has been kept.', 'abandoned-cart-webhooks-for-woocommerce' ) ); } foreach ( (array) ( $entry['coupons'] ?? array() ) as $coupon ) { if ( is_string( $coupon ) && ! WC()->cart->has_discount( $coupon ) ) { WC()->cart->apply_coupon( $coupon ); } } WC()->cart->calculate_totals(); } catch ( Throwable $error ) { WC()->cart->set_cart_contents( $before ); WC()->cart->set_applied_coupons( $coupons ); WC()->cart->calculate_totals(); return new WP_Error( 'acpw_restore_error', __( 'The cart could not be restored. Your current cart has been kept.', 'abandoned-cart-webhooks-for-woocommerce' ) ); } finally { ACPW_Capture::$restoring = false; } if ( $skipped ) { wc_add_notice( __( 'Some saved items could not be restored because they are no longer available.', 'abandoned-cart-webhooks-for-woocommerce' ), 'notice' ); } return true; } private static function page( $title, $body, $status = 200 ) { status_header( $status ); header( 'Content-Type: text/html; charset=' . get_bloginfo( 'charset' ) ); echo '' . esc_html( $title ) . '

' . esc_html( $title ) . '

' . wp_kses( $body, array( 'p' => array(), 'a' => array( 'href' => true ), 'ul' => array(), 'li' => array(), 'form' => array( 'method' => true, 'action' => true ), 'input' => array( 'type' => true, 'name' => true, 'value' => true, 'checked' => true ), 'label' => array(), 'button' => array( 'type' => true ) ) ) . '
'; exit; } }