strtolower( (string) $code ) ); if ( '' !== (string) $email ) { $payload['e'] = (string) $email; } if ( (int) $grant_id > 0 ) { $payload['g'] = (int) $grant_id; } $ceiling = time() + self::MAX_AGE * DAY_IN_SECONDS; $exp = ( (int) $exp_ts > 0 ) ? (int) $exp_ts : ( time() + 30 * DAY_IN_SECONDS ); $payload['exp'] = min( $exp, $ceiling ); $token = self::sign( $payload ); $cart = function_exists( 'wc_get_cart_url' ) ? wc_get_cart_url() : home_url( '/cart/' ); return add_query_arg( self::ARG, $token, $cart ); } public static function sign( array $payload ) { $payload['v'] = self::VERSION; $payload['iat'] = time(); $b64 = self::b64e( wp_json_encode( $payload ) ); $sig = self::b64e( hash_hmac( 'sha256', $b64, self::secret(), true ) ); return $b64 . '.' . $sig; } public static function verify( $token ) { $token = (string) $token; $parts = explode( '.', $token, 2 ); if ( count( $parts ) !== 2 ) { return new WP_Error( 'offer_format', 'malformed token' ); } list( $b64, $sig ) = $parts; $expected = self::b64e( hash_hmac( 'sha256', $b64, self::secret(), true ) ); if ( ! hash_equals( $expected, (string) $sig ) ) { return new WP_Error( 'offer_sig', 'bad signature' ); } $p = json_decode( self::b64d( $b64 ), true ); if ( ! is_array( $p ) || (int) ( $p['v'] ?? 0 ) !== self::VERSION ) { return new WP_Error( 'offer_payload', 'bad payload' ); } if ( empty( $p['exp'] ) || time() > (int) $p['exp'] ) { return new WP_Error( 'offer_expired', 'expired' ); } if ( ! empty( $p['iat'] ) && ( time() - (int) $p['iat'] ) > self::MAX_AGE * DAY_IN_SECONDS ) { return new WP_Error( 'offer_stale', 'stale token' ); } return $p; } public static function handle() { if ( is_admin() || ! function_exists( 'WC' ) || empty( $_GET[ self::ARG ] ) ) { return; } $p = self::verify( (string) wp_unslash( $_GET[ self::ARG ] ) ); if ( is_wp_error( $p ) ) { if ( function_exists( 'wc_add_notice' ) && WC()->session ) { wc_add_notice( __( 'This offer link is no longer valid.', 'ai-eshop-optimizer' ), 'notice' ); } return; } $code = strtolower( sanitize_text_field( (string) ( $p['c'] ?? '' ) ) ); if ( '' === $code ) { return; } if ( ! is_user_logged_in() && WC()->session && method_exists( WC()->session, 'set_customer_session_cookie' ) ) { WC()->session->set_customer_session_cookie( true ); } $cart = WC()->cart; if ( $cart && ! $cart->has_discount( $code ) ) { $cart->apply_coupon( $code ); } if ( ! empty( $p['e'] ) && is_email( $p['e'] ) && ! is_user_logged_in() && WC()->customer && '' === (string) WC()->customer->get_billing_email() ) { WC()->customer->set_billing_email( sanitize_email( (string) $p['e'] ) ); WC()->customer->save(); } $gid = (int) ( $p['g'] ?? 0 ); if ( $gid > 0 && class_exists( 'AIEO_Coupon_Minter' ) ) { AIEO_Coupon_Minter::mark_clicked( $gid ); } $cart_url = function_exists( 'wc_get_cart_url' ) ? wc_get_cart_url() : home_url( '/cart/' ); $forward = array(); foreach ( array( 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'aieo_src', 'gclid', 'fbclid', 'msclkid', 'ttclid' ) as $k ) { if ( isset( $_GET[ $k ] ) && '' !== $_GET[ $k ] ) { $forward[ $k ] = sanitize_text_field( wp_unslash( $_GET[ $k ] ) ); } } if ( ! empty( $forward ) ) { $cart_url = add_query_arg( $forward, $cart_url ); } wp_safe_redirect( $cart_url ); exit; } private static function secret() { return hash_hmac( 'sha256', 'aieo_offer_link_v1', wp_salt( 'auth' ) ); } private static function b64e( $s ) { return rtrim( strtr( base64_encode( (string) $s ), '+/', '-_' ), '=' ); } private static function b64d( $s ) { $s = strtr( (string) $s, '-_', '+/' ); $pad = strlen( $s ) % 4; if ( $pad ) { $s .= str_repeat( '=', 4 - $pad ); } $out = base64_decode( $s, true ); return false === $out ? '' : $out; } } }