'waiting' ); if ( null !== self::email_matches( (string) $row['code'] ) ) { $standing = self::standing( (int) $row['id'], (int) $row['offer_id'], $offer ); } else { $would = self::standing( (int) $row['id'], (int) $row['offer_id'], $offer ); $standing = array( 'state' => 'waiting', 'condition' => $would['condition'] ?? null, 'promise' => 'applied' === $would['state'], 'outbid' => ! empty( $would['outbid'] ), ); } $state = (string) $standing['state']; if ( 'applied' === $state && function_exists( 'is_checkout' ) && is_checkout() ) { return null; } return array( 'key' => 'pc-' . (int) $row['id'] . '-' . $state, 'html' => AIEO_DMM_Offer_Band_Words::personal_code( $state, array( 'code' => (string) $row['code'], 'value' => self::value_of( $row, $offer ), 'scope' => AIEO_DMM_Offer_Band_Words::scope_words( $offer, AIEO_DMM_Offer_Band_Words::orders_words( self::orders_left( (int) $row['coupon_id'] ) ), ! empty( $row['combines'] ) ), 'ends' => self::ends( self::end_of( $row, $offer ) ), 'condition' => $standing['condition'] ?? null, 'outbid' => ! empty( $standing['outbid'] ), 'promise' => $standing['promise'] ?? true, ) ), ); } return self::lapsed( $held ); } private static function standing( int $grant, int $offer_id, ?array $offer ): array { $keys = self::bought_keys(); if ( ! $keys ) { $need = self::unmet( $offer_id, $offer, array() ); if ( null !== $need ) { return array( 'state' => 'condition', 'condition' => $need ); } return array( 'state' => self::holds_for_the_visitor( $offer_id, $offer ) ? 'applied' : 'saved' ); } $mine = array( $grant => true ); foreach ( AIEO_DMM_Person_Key::visit_grants() as $held ) { if ( is_array( $held ) && (int) ( $held['offer_id'] ?? 0 ) === $offer_id ) { $mine[ (int) ( $held['id'] ?? 0 ) ] = true; } } $outbid = 0; foreach ( $keys as $key ) { $roles = array(); $line = AIEO_DMM_Cart_Pass::line( $key ); foreach ( is_array( $line ) ? (array) ( $line['offers'] ?? array() ) : array() as $one ) { if ( (int) ( $one['id'] ?? 0 ) === $offer_id && isset( $mine[ (int) ( $one['grant'] ?? 0 ) ] ) ) { $roles[] = (string) ( $one['role'] ?? '' ); } } if ( in_array( 'applied', $roles, true ) ) { return array( 'state' => 'applied' ); } $outbid += in_array( 'outbid', $roles, true ) ? 1 : 0; } $need = self::unmet( $offer_id, $offer, $keys ); if ( null !== $need ) { return array( 'state' => 'condition', 'condition' => $need ); } return array( 'state' => 'saved', 'outbid' => count( $keys ) === $outbid, ); } private static function typed_condition( int $offer_id ): ?array { return self::unmet( $offer_id, self::offer_row( $offer_id ), self::bought_keys() ); } private static function bought_keys(): array { if ( ! class_exists( 'AIEO_DMM_Cart_Pass' ) || ! function_exists( 'WC' ) || ! WC() || ! WC()->cart instanceof WC_Cart ) { return array(); } $keys = array(); foreach ( WC()->cart->get_cart() as $key => $item ) { if ( empty( $item['_aieo_dmm_gift_offer_id'] ) ) { $keys[] = (string) $key; } } return $keys; } private static function unmet( int $offer_id, ?array $offer, array $keys ): ?array { $when = self::when_of( $offer ); if ( null === $when ) { return null; } $matched = null; return self::unmet_in( $when, $offer_id, $offer, $keys, $matched ); } private static function unmet_in( array $tree, int $offer_id, ?array $offer, array $keys, ?array &$matched ): ?array { if ( isset( $tree['p'] ) ) { if ( ! in_array( (string) $tree['p'], array( 'cart.subtotal', 'cart.quantity' ), true ) || (float) ( $tree['min'] ?? 0 ) <= 0 ) { return null; } $m = array( 'p' => (string) $tree['p'], 'min' => (float) $tree['min'], 'of' => 'cart' === ( $tree['of'] ?? 'matched' ) ? 'cart' : 'matched', ); $done = self::done( $m, $offer_id, $offer, $keys, $matched ); return $done < $m['min'] ? array( 'p' => $m['p'], 'min' => $m['min'], 'left' => max( 0.0, $m['min'] - $done ), 'names' => 'matched' === $m['of'] ? AIEO_DMM_Offer_Band_Words::target_names( $offer ) : array(), ) : null; } if ( isset( $tree['any'] ) && is_array( $tree['any'] ) ) { $first = null; foreach ( $tree['any'] as $child ) { $found = is_array( $child ) ? self::unmet_in( $child, $offer_id, $offer, $keys, $matched ) : null; if ( null === $found ) { return null; } $first = $first ?? $found; } return $first; } foreach ( isset( $tree['all'] ) && is_array( $tree['all'] ) ? $tree['all'] : array() as $child ) { $found = is_array( $child ) ? self::unmet_in( $child, $offer_id, $offer, $keys, $matched ) : null; if ( null !== $found ) { return $found; } } return null; } private static function done( array $m, int $offer_id, ?array $offer, array $keys, ?array &$matched ): float { $p = AIEO_DMM_Cart_Pass::progress( $offer_id ); if ( $p && (string) ( $p['p'] ?? '' ) === $m['p'] && (string) ( $p['of'] ?? '' ) === $m['of'] && abs( (float) ( $p['min'] ?? 0 ) - $m['min'] ) < 0.00001 ) { return (float) ( $p['done'] ?? 0 ); } if ( 'matched' === $m['of'] ) { $matched = $matched ?? self::matched_keys( $offer_id, $keys ); $keys = $matched; } $figure = 'cart.quantity' === $m['p'] ? 'qty' : ( in_array( (string) ( $offer['type'] ?? '' ), array( 'gift', 'free_shipping' ), true ) ? 'amount' : 'pre' ); $done = 0.0; foreach ( $keys as $key ) { $line = AIEO_DMM_Cart_Pass::line( $key ); $done += (float) ( $line[ $figure ] ?? 0 ); } return round( $done, 6 ); } private static function matched_keys( int $offer_id, array $keys ): array { $term = null; foreach ( $keys && class_exists( 'AIEO_DMM_Offers_Now' ) ? AIEO_DMM_Offers_Now::cart_offers() : array() as $one ) { if ( (int) ( $one['id'] ?? 0 ) === $offer_id ) { $term = $one; break; } } if ( null === $term || ! class_exists( 'AIEO_DMM_Offer_Matcher' ) ) { return array(); } $cart = WC()->cart->get_cart(); $out = array(); foreach ( $keys as $key ) { $line = array_merge( (array) ( $cart[ $key ] ?? array() ), array( 'line_subtotal' => 1 ) ); try { foreach ( AIEO_DMM_Offer_Matcher::for_cart( array( $key => $line ), array( $term ) ) as $answer ) { if ( (float) ( $answer['_targeted_subtotal'] ?? 0 ) > 0 ) { $out[] = $key; } } } catch ( \Throwable $e ) { continue; } } return $out; } private static function holds_for_the_visitor( int $offer_id, ?array $offer ): bool { $when = self::when_of( $offer ); if ( null === $when ) { return null !== $offer; } if ( ! class_exists( 'AIEO_DMM_Offer_Conditions', false ) && is_readable( __DIR__ . '/class-aieo-dmm-offer-conditions.php' ) ) { require_once __DIR__ . '/class-aieo-dmm-offer-conditions.php'; } if ( ! class_exists( 'AIEO_DMM_Offer_Conditions', false ) || ! class_exists( 'AIEO_DMM_Cart_Pass' ) || ! class_exists( 'AIEO_DMM_Offer_Matcher' ) ) { return false; } try { return AIEO_DMM_Offer_Conditions::matches( self::cart_taken_as_met( $when ), array( 'product' => array(), 'lines' => array(), 'matched' => array(), 'customer' => AIEO_DMM_Cart_Pass::who(), 'now' => gmdate( 'Y-m-d H:i:s' ), 'grants' => AIEO_DMM_Person_Key::visit_grants(), 'offer_id' => $offer_id, ) ); } catch ( \Throwable $e ) { return false; } } private static function cart_taken_as_met( array $tree ): array { if ( isset( $tree['p'] ) ) { return 0 === strpos( (string) $tree['p'], 'cart.' ) ? array( 'all' => array() ) : $tree; } if ( isset( $tree['not'] ) ) { $needs = AIEO_DMM_Offer_Matcher::needs_of( array( 'when' => $tree['not'] ) ); return ! empty( $needs['cart'] ) ? array( 'all' => array() ) : $tree; } foreach ( array( 'all', 'any' ) as $list ) { if ( isset( $tree[ $list ] ) && is_array( $tree[ $list ] ) ) { $tree[ $list ] = array_map( static function ( $child ) { return is_array( $child ) ? self::cart_taken_as_met( $child ) : $child; }, $tree[ $list ] ); } } return $tree; } private static function when_of( ?array $offer ): ?array { $definition = $offer && class_exists( 'AIEO_DMM_Offer_Definition' ) ? AIEO_DMM_Offer_Definition::for_row( $offer ) : null; return is_array( $definition ) && isset( $definition['when'] ) && is_array( $definition['when'] ) ? $definition['when'] : null; } private static function value_of( array $row, ?array $offer ): string { $definition = $offer && class_exists( 'AIEO_DMM_Offer_Definition' ) ? AIEO_DMM_Offer_Definition::for_row( $offer ) : null; $effect = is_array( $definition ) && isset( $definition['effect'] ) && is_array( $definition['effect'] ) ? $definition['effect'] : array(); $kind = (string) ( $effect['e'] ?? '' ); if ( 'percent' === $kind && isset( $effect['value'] ) ) { return AIEO_DMM_Offer_Band_Words::value_words( 'percent', (float) $effect['value'] ); } if ( in_array( $kind, array( 'amount', 'fixed' ), true ) && isset( $effect['value'] ) ) { return AIEO_DMM_Offer_Band_Words::value_words( 'fixed_cart', (float) $effect['value'] ); } return AIEO_DMM_Offer_Band_Words::value_words( (string) $row['offer_type'], (float) $row['offer_value'] ); } private static function lapsed( array $held ): ?array { if ( ! class_exists( 'AIEO_DMM_Visit_Grants' ) ) { return null; } $gone = array_values( array_diff( AIEO_DMM_Visit_Grants::cookie_grant_ids(), $held ) ); if ( ! $gone ) { return null; } $row = self::personal_grant( 'g.id IN (' . implode( ',', array_map( 'intval', $gone ) ) . ')' ); if ( ! $row ) { return null; } $reason = self::reason( $row ); if ( ! in_array( $reason, AIEO_DMM_Offer_Band_Words::REFUSALS, true ) ) { return null; } if ( in_array( $reason, self::FINAL, true ) ) { AIEO_DMM_Visit_Grants::withdraw( (int) $row['id'] ); } if ( ! self::tell_once( (int) $row['id'], (string) $row['code'], $reason ) ) { return null; } $once = self::take_once(); return $once ? self::refused( (int) $once['grant'], (string) $once['code'], (string) $once['reason'] ) : null; } private static function refused( int $grant, string $code, string $reason ): array { return array( 'key' => 'pc-' . $grant . '-' . str_replace( '_', '-', $reason ), 'html' => AIEO_DMM_Offer_Band_Words::personal_code( $reason, array( 'code' => $code ) ), 'once' => true, ); } private static function tell_once( int $grant, string $code, string $reason ): bool { $session = self::session(); if ( ! $session ) { return false; } $kept = $session->get( self::SESSION ); $kept = is_array( $kept ) ? $kept : array(); $told = isset( $kept['told'] ) && is_array( $kept['told'] ) ? $kept['told'] : array(); $key = $grant . ':' . $reason; if ( in_array( $key, $told, true ) ) { return false; } $told[] = $key; $session->set( self::SESSION, array( 'once' => array( 'grant' => $grant, 'code' => $code, 'reason' => $reason, ), 'told' => array_slice( $told, -20 ), ) ); return true; } private static function take_once(): ?array { $session = self::session(); if ( ! $session ) { return null; } $kept = $session->get( self::SESSION ); if ( ! is_array( $kept ) || empty( $kept['once'] ) || ! is_array( $kept['once'] ) ) { return null; } $once = $kept['once']; $kept['once'] = null; $session->set( self::SESSION, $kept ); return in_array( (string) ( $once['reason'] ?? '' ), AIEO_DMM_Offer_Band_Words::REFUSALS, true ) ? $once : null; } private static function session() { return function_exists( 'WC' ) && WC() && isset( WC()->session ) && WC()->session ? WC()->session : null; } public static function coupon_words( $err, $err_code = 0, $coupon = null ) { if ( ! $coupon instanceof WC_Coupon || ! self::speaks_for( $coupon ) ) { return $err; } if ( self::$prechecking ) { return ''; } $code = (string) $coupon->get_code(); $row = self::row_by_code( $code ); $reason = self::reason( $row ); $typed = 'auto' !== self::context(); if ( WC_Coupon::E_WC_COUPON_NOT_YOURS_REMOVED === (int) $err_code && 'ok' === $reason ) { $reason = 'other_email'; } if ( 'ok' === $reason ) { $again = in_array( (int) $err_code, array( WC_Coupon::E_WC_COUPON_ALREADY_APPLIED, WC_Coupon::E_WC_COUPON_ALREADY_APPLIED_INDIV_USE_ONLY ), true ); if ( $typed && ! $again && in_array( (int) $err_code, array( WC_Coupon::E_WC_COUPON_NOT_APPLICABLE, WC_Coupon::E_WC_COUPON_MIN_SPEND_LIMIT_NOT_MET ), true ) ) { $need = self::typed_condition( (int) $row['offer_id'] ); return null !== $need ? AIEO_DMM_Offer_Band_Words::coupon_text( 'condition', $code, $need ) : AIEO_DMM_Offer_Band_Words::coupon_text( 'not_here', $code ); } return $typed || $again ? $err : ''; } if ( 'other_email' === $reason ) { return $typed ? AIEO_DMM_Offer_Band_Words::coupon_text( 'not_yours', $code ) : ''; } if ( 'waiting' === $reason ) { return $typed ? AIEO_DMM_Offer_Band_Words::coupon_text( 'waiting', $code ) : ''; } if ( $typed ) { return AIEO_DMM_Offer_Band_Words::coupon_text( $reason, $code ); } self::tell_once( (int) $row['id'], $code, $reason ); return ''; } private static function context(): string { if ( null !== self::$store_route ) { list( $method, $route ) = self::$store_route; if ( 'POST' === $method && 1 === preg_match( '#/checkout/?$#', $route ) ) { return 'checkout'; } if ( 'POST' === $method && 1 === preg_match( '#/cart/(apply-coupon|coupons)/?$#', $route ) ) { return 'field'; } return 'auto'; } if ( doing_action( 'wc_ajax_checkout' ) || doing_action( 'woocommerce_after_checkout_validation' ) || isset( $_POST['woocommerce-process-checkout-nonce'] ) ) { return 'checkout'; } if ( doing_action( 'wc_ajax_apply_coupon' ) || isset( $_POST['apply_coupon'] ) || doing_action( 'wp_ajax_aieo_side_cart_apply_coupon' ) || doing_action( 'wp_ajax_nopriv_aieo_side_cart_apply_coupon' ) ) { return 'field'; } return 'auto'; } public static function precheck(): void { if ( self::$prechecking || 'checkout' === self::context() || ( is_admin() && ! wp_doing_ajax() ) ) { return; } if ( doing_action( 'template_redirect' ) && ! ( ( function_exists( 'is_cart' ) && is_cart() ) || ( function_exists( 'is_checkout' ) && is_checkout() ) || AIEO_DMM_Offer_Band::uncacheable() ) ) { return; } if ( ! class_exists( 'AIEO_DMM_Offer_Access' ) || ! AIEO_DMM_Offer_Access::is_on() || ! function_exists( 'WC' ) || ! WC() || ! WC()->cart instanceof WC_Cart ) { return; } $ours = array(); foreach ( WC()->cart->get_applied_coupons() as $code ) { $coupon = new WC_Coupon( (string) $code ); if ( self::speaks_for( $coupon ) ) { $ours[] = $coupon; } } if ( ! $ours ) { return; } self::$prechecking = true; $muted = class_exists( 'AIEO_Signal_Facts', false ); if ( $muted ) { AIEO_Signal_Facts::mute(); } $removed = false; try { if ( doing_action( 'template_redirect' ) ) { WC()->cart->calculate_totals(); } foreach ( $ours as $coupon ) { if ( $coupon->is_valid() ) { continue; } $code = (string) $coupon->get_code(); $row = self::row_by_code( $code ); $reason = self::reason( $row ); WC()->cart->remove_coupon( $code ); $removed = true; if ( $row && in_array( $reason, AIEO_DMM_Offer_Band_Words::REFUSALS, true ) ) { self::tell_once( (int) $row['id'], $code, $reason ); } } if ( $removed && ! doing_action( 'woocommerce_after_calculate_totals' ) ) { WC()->cart->calculate_totals(); } } finally { if ( $muted ) { AIEO_Signal_Facts::unmute(); } self::$prechecking = false; } if ( $removed && doing_action( 'woocommerce_after_calculate_totals' ) ) { WC()->cart->calculate_totals(); } } public static function precheck_store_api(): void { static $running = false; if ( $running || null === self::$store_route || self::$prechecking ) { return; } $running = true; try { self::precheck(); } finally { $running = false; } } public static function speaks_for( WC_Coupon $coupon ): bool { if ( '' === (string) $coupon->get_meta( '_aieo_coupon', true ) || ! class_exists( 'AIEO_DMM_Offer_Access' ) || (int) $coupon->get_meta( AIEO_DMM_Offer_Access::COUPON_ACCESS, true ) <= 0 || ! AIEO_DMM_Offer_Access::is_on() ) { return false; } return null !== self::row_by_code( (string) $coupon->get_code() ); } private static function reason( ?array $row ): string { if ( ! $row ) { return 'ended'; } $status = (string) $row['status']; if ( ! in_array( $status, AIEO_DMM_Visit_Grants::HOLDING, true ) ) { return 'ended'; } if ( ! self::use_left( (int) $row['id'] ) ) { return 'used'; } $coupon = class_exists( 'WC_Coupon' ) && (int) $row['coupon_id'] > 0 ? new WC_Coupon( (int) $row['coupon_id'] ) : null; $now = time(); if ( ! empty( $row['expires_at'] ) && 0 !== strpos( (string) $row['expires_at'], '0000-' ) && (int) strtotime( (string) $row['expires_at'] . ' UTC' ) <= $now ) { return 'expired'; } if ( $coupon && $coupon->get_id() && $coupon->get_date_expires() && $coupon->get_date_expires()->getTimestamp() <= $now ) { return 'expired'; } if ( 'retired' === (string) $row['access_state'] ) { return 'ended'; } $offer = self::offer_row( (int) $row['offer_id'] ); $runs = $offer ? AIEO_DMM_Offer_Access::offer_runs( $offer ) : 'ended'; if ( 'ended' === $runs ) { return 'ended'; } if ( 'runs' !== $runs || 'active' !== (string) $row['access_state'] ) { return 'paused'; } $user = (int) get_current_user_id(); if ( (int) $row['wp_user_id'] > 0 && (int) $row['wp_user_id'] !== $user ) { return 'not_yours'; } if ( (int) $row['max_uses'] > 0 || (int) $row['max_uses_per_customer'] > 0 ) { $spent = AIEO_DMM_Offer_Access::spent_among( array( (int) $row['access_id'] ), $user, array(), AIEO_DMM_Offer_Access::buyer_emails() ); if ( in_array( (int) $row['access_id'], array_map( 'intval', $spent ), true ) ) { return 'used'; } } $email = self::email_matches( (string) $row['code'] ); if ( null === $email ) { return 'waiting'; } return false === $email ? 'other_email' : 'ok'; } public static function on_applied_coupon( $code ): void { self::$displacing = false; $grant = self::grant_by_code( (string) $code, true ); if ( ! $grant || ! class_exists( 'AIEO_DMM_Visit_Grants' ) || true !== self::email_matches( (string) $code ) ) { return; } AIEO_DMM_Visit_Grants::give( (int) $grant['id'] ); self::forget_request(); } public static function on_removed_coupon( $code ): void { $grant = self::grant_by_code( (string) $code, false ); if ( ! $grant || ! class_exists( 'AIEO_DMM_Visit_Grants' ) ) { return; } if ( ! self::removed_by_the_visitor() && ! self::no_longer_theirs( (string) $code ) ) { return; } AIEO_DMM_Visit_Grants::withdraw( (int) $grant['id'] ); self::forget_request(); } private static function no_longer_theirs( string $code ): bool { if ( ! self::grant_by_code( $code, true ) ) { return true; } if ( class_exists( 'WC_Coupon' ) ) { $coupon = new WC_Coupon( $code ); if ( $coupon->get_id() ) { $expires = $coupon->get_date_expires(); if ( $expires && $expires->getTimestamp() <= time() ) { return true; } foreach ( array( (int) $coupon->get_usage_limit(), (int) $coupon->get_usage_limit_per_user() ) as $limit ) { if ( $limit > 0 && (int) $coupon->get_usage_count() >= $limit ) { return true; } } } } return false === self::email_matches( $code ); } private static function removed_by_the_visitor(): bool { if ( self::$prechecking ) { return false; } if ( self::$displacing ) { return true; } if ( doing_action( 'woocommerce_check_cart_items' ) || doing_action( 'woocommerce_after_checkout_validation' ) ) { return false; } if ( null === self::$store_route ) { return true; } list( $method, $route ) = self::$store_route; return ( 'POST' === $method && 1 === preg_match( '#/cart/remove-coupon/?$#', $route ) ) || ( 'DELETE' === $method && 1 === preg_match( '#/cart/coupons/[^/]+/?$#', $route ) ); } public static function note_displacing( $keep ) { self::$displacing = true; return $keep; } public static function note_store_route( $response, $handler, $request ) { if ( $request instanceof WP_REST_Request && 0 === strpos( (string) $request->get_route(), '/wc/store' ) ) { self::$store_route = array( strtoupper( (string) $request->get_method() ), (string) $request->get_route() ); } return $response; } public static function forget_store_route( $response ) { self::$store_route = null; return $response; } private static function grant_by_code( string $code, bool $live ): ?array { $row = self::row_by_code( $code ); if ( ! $row || ! $live ) { return $row; } $ends = ! empty( $row['expires_at'] ) && 0 !== strpos( (string) $row['expires_at'], '0000-' ) ? (int) strtotime( (string) $row['expires_at'] . ' UTC' ) : 0; return in_array( (string) $row['status'], AIEO_DMM_Visit_Grants::HOLDING, true ) && ( 0 === $ends || $ends > time() ) && self::use_left( (int) $row['id'] ) ? $row : null; } private static function use_left( int $grant_id ): bool { global $wpdb; if ( $grant_id <= 0 ) { return false; } if ( ! array_key_exists( $grant_id, self::$uses_left ) ) { $grants = function_exists( 'aieo_coupon_grants_table' ) ? aieo_coupon_grants_table() : $wpdb->prefix . 'aieo_coupon_grants'; list( $left, $args ) = AIEO_DMM_Visit_Grants::use_left_sql(); self::$uses_left[ $grant_id ] = '1' === (string) $wpdb->get_var( $wpdb->prepare( "SELECT 1 FROM `{$grants}` g WHERE g.id = %d AND {$left}", array_merge( array( $grant_id ), $args ) ) ); } return self::$uses_left[ $grant_id ]; } private static function row_by_code( string $code ): ?array { global $wpdb; $code = strtolower( trim( $code ) ); if ( '' === $code || ! class_exists( 'AIEO_DMM_Offer_Access' ) || ! AIEO_DMM_Offer_Access::is_on() ) { return null; } if ( ! array_key_exists( $code, self::$rows ) ) { self::$rows[ $code ] = self::personal_grant( $wpdb->prepare( 'g.code = %s', $code ) ); } return self::$rows[ $code ]; } private static function personal_grant( string $where ): ?array { global $wpdb; if ( ! class_exists( 'AIEO_DMM_Offer_Access' ) ) { return null; } $grants = function_exists( 'aieo_coupon_grants_table' ) ? aieo_coupon_grants_table() : $wpdb->prefix . 'aieo_coupon_grants'; $access = AIEO_DMM_Offer_Access::table(); $row = $wpdb->get_row( "SELECT g.id, g.code, g.coupon_id, g.offer_id, g.offer_type, g.offer_value, g.expires_at, g.status, g.wp_user_id, g.access_id, a.state AS access_state, a.max_uses, a.max_uses_per_customer, a.combines FROM `{$grants}` g JOIN `{$access}` a ON a.id = g.access_id WHERE {$where} AND g.kind = 'code' AND a.kind = 'personal' AND g.offer_id > 0 AND g.code <> '' ORDER BY g.id DESC LIMIT 1", ARRAY_A ); return is_array( $row ) ? $row : null; } private static function offer_row( int $offer_id ): ?array { global $wpdb; if ( $offer_id <= 0 ) { return null; } if ( ! array_key_exists( $offer_id, self::$offers ) ) { $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}aieo_dmm_offers WHERE id = %d", $offer_id ), ARRAY_A ); self::$offers[ $offer_id ] = is_array( $row ) ? $row : null; } return self::$offers[ $offer_id ]; } private static function email_matches( string $code ): ?bool { if ( ! class_exists( 'WC_Coupon' ) ) { return null; } $allowed = array_filter( array_map( 'strval', (array) ( new WC_Coupon( $code ) )->get_email_restrictions() ) ); if ( ! $allowed ) { return true; } $emails = array(); if ( is_user_logged_in() ) { $emails[] = (string) wp_get_current_user()->user_email; } if ( function_exists( 'WC' ) && WC()->customer && is_callable( array( WC()->customer, 'get_billing_email' ) ) ) { $emails[] = (string) WC()->customer->get_billing_email(); } $emails = array_values( array_unique( array_filter( array_map( static function ( $e ) { return strtolower( trim( (string) $e ) ); }, $emails ) ) ) ); if ( ! $emails ) { return null; } if ( class_exists( '\Automattic\WooCommerce\Utilities\DiscountsUtil' ) ) { return (bool) \Automattic\WooCommerce\Utilities\DiscountsUtil::is_coupon_emails_allowed( $emails, $allowed ); } return (bool) array_intersect( $emails, array_map( 'strtolower', $allowed ) ); } private static function orders_left( int $coupon_id ): int { if ( $coupon_id <= 0 || ! class_exists( 'WC_Coupon' ) ) { return 1; } $coupon = new WC_Coupon( $coupon_id ); if ( ! $coupon->get_id() ) { return 1; } $limits = array_filter( array( (int) $coupon->get_usage_limit(), (int) $coupon->get_usage_limit_per_user() ) ); if ( ! $limits ) { return 0; } return max( 1, (int) min( $limits ) - (int) $coupon->get_usage_count() ); } private static function end_of( array $grant, ?array $offer ): int { $ends = array(); if ( ! empty( $grant['expires_at'] ) && 0 !== strpos( (string) $grant['expires_at'], '0000-' ) ) { $ends[] = (int) strtotime( (string) $grant['expires_at'] . ' UTC' ); } if ( $offer ) { if ( ! empty( $offer['ends_at'] ) && 0 !== strpos( (string) $offer['ends_at'], '0000-' ) ) { $ends[] = (int) strtotime( (string) $offer['ends_at'] . ' UTC' ); } elseif ( ! empty( $offer['end_date'] ) && class_exists( 'AIEO_DMM_Schedule' ) ) { $bounds = AIEO_DMM_Schedule::day_bounds( (string) $offer['end_date'] ); if ( is_array( $bounds ) ) { $ends[] = (int) $bounds[1]; } } } $ends = array_filter( $ends ); return $ends ? (int) min( $ends ) : 0; } private static function ends( int $end ): ?array { return $end > 0 ? AIEO_DMM_Quantity_Front::ends_words( gmdate( 'Y-m-d H:i:s', $end ) ) : null; } public static function forget_request(): void { self::$rows = array(); self::$offers = array(); self::$uses_left = array(); } } }