40) { return $data; } // The store's own coupon wins if (function_exists('wc_get_coupon_id_by_code') && wc_get_coupon_id_by_code($code)) { return $data; } $check = self::check($code); if (!$check || empty($check['valid'])) { return $data; } return array( 'discount_type' => 'percent', 'amount' => (float) $check['discount_percent'], 'individual_use' => false, 'usage_limit' => 0, 'usage_limit_per_user' => 0, 'free_shipping' => false, 'exclude_sale_items' => false, 'description' => 'ADLY', ); } /** The cart at regular prices, before discounts, without shipping (what ADLY's check takes). */ private static function cart_total() { if (!function_exists('WC') || !WC()->cart) { return null; } return Adly_Shop_Api::money((float) WC()->cart->get_subtotal() + (float) WC()->cart->get_subtotal_tax()); } /** ADLY's answer for a code, once per request and per cart total; kept in the session for the order. */ public static function check($code) { $total = self::cart_total(); $key = $code . '|' . ($total === null ? '' : $total); if (isset(self::$memo[$key])) { return self::$memo[$key]; } $session = (function_exists('WC') && WC()->session) ? WC()->session : null; $stored = $session ? (array) $session->get(self::SESSION_KEY, array()) : array(); // A fresh answer for the same cart is reused for ten minutes if (isset($stored[$code]) && $stored[$code]['cart'] === $total && time() - (int) $stored[$code]['at'] < 600) { return self::$memo[$key] = $stored[$code]; } $body = array('code' => $code); if ($total !== null) { $body['cart_total'] = $total; } // Five seconds: a silent ADLY must not hold the checkout (no answer means no discount) $res = Adly_Shop_Api::post('/validate', $body, 'secret', 5); if (!$res['ok']) { return self::$memo[$key] = null; } $answer = array( 'valid' => !empty($res['body']['valid']), 'discount_percent' => isset($res['body']['discount_percent']) ? (float) $res['body']['discount_percent'] : 0, 'check_id' => isset($res['body']['check_id']) ? (string) $res['body']['check_id'] : '', 'cart' => $total, 'at' => time(), ); if ($session) { $stored[$code] = $answer; $session->set(self::SESSION_KEY, $stored); } return self::$memo[$key] = $answer; } /** The order keeps the code and check_id of a creator's code it used. */ public static function stamp_order($order) { if (!$order instanceof WC_Order || !function_exists('WC') || !WC()->session) { return; } $stored = (array) WC()->session->get(self::SESSION_KEY, array()); foreach ($order->get_coupon_codes() as $code) { $code = strtoupper($code); if (!empty($stored[$code]['valid']) && !empty($stored[$code]['check_id'])) { $order->update_meta_data('_adly_code', $code); $order->update_meta_data('_adly_check_id', $stored[$code]['check_id']); $order->save(); break; } } } // A buyer arriving from a creator's link: the utm parameters are only read, sanitized, and kept as a cookie. // phpcs:disable WordPress.Security.NonceVerification.Recommended public static function remember_link_code() { if (is_admin() || empty($_GET['utm_source']) || empty($_GET['utm_content'])) { return; } if (strtolower(sanitize_text_field(wp_unslash($_GET['utm_source']))) !== 'adly') { return; } $code = strtoupper(preg_replace('/[^A-Za-z0-9_-]/', '', sanitize_text_field(wp_unslash($_GET['utm_content'])))); if ($code !== '' && strlen($code) <= 40) { setcookie(self::COOKIE, $code, time() + 30 * DAY_IN_SECONDS, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN, is_ssl(), true); $_COOKIE[self::COOKIE] = $code; } } // phpcs:enable WordPress.Security.NonceVerification.Recommended /** The link's code on the cart, once per session (a buyer who removes it is not overruled). */ public static function apply_link_code() { if (empty($_COOKIE[self::COOKIE]) || !function_exists('WC') || !WC()->cart || WC()->cart->is_empty() || !WC()->session) { return; } $code = strtoupper(sanitize_text_field(wp_unslash($_COOKIE[self::COOKIE]))); if (WC()->session->get('adly_shop_link_applied') === $code || WC()->cart->has_discount($code)) { return; } // Not connected through the plugin (a store on WooCommerce keys): only the store's own coupons exist, // so a code that is not one of them is left alone instead of showing the buyer an error if (!Adly_Shop_Settings::connected() && !(function_exists('wc_get_coupon_id_by_code') && wc_get_coupon_id_by_code($code))) { return; } WC()->session->set('adly_shop_link_applied', $code); WC()->cart->apply_coupon($code); } }