(int) $age, 'method' => $method, 'time' => gmdate( 'Y-m-d H:i:s' ), ); update_user_meta( $user_id, self::META, $mark ); return self::of( $user_id ) === $mark; } /** * After a passed check of an order: remember it on the customer's account, when the * owner chose a number of months and the person who answered is the order's own * logged-in customer. * * @param \WC_Order $order The order. * @param string $method confirm · dob. * @param int $age The minimum age passed. * @return bool Whether the account was marked. */ public static function keep( $order, $method, $age ) { if ( self::months() < 1 || ! self::customer_of( $order ) ) { return false; } return self::mark( get_current_user_id(), $method, $age ); } /** * The mark an account carries, cleaned — or null when there is none, or it is not * one this plugin wrote. * * @param int $user_id The customer. * @return array{min_age: int, method: string, time: string}|null */ public static function of( $user_id ) { $user_id = (int) $user_id; if ( $user_id < 1 ) { return null; } $mark = get_user_meta( $user_id, self::META, true ); if ( ! is_array( $mark ) || ! isset( $mark['min_age'], $mark['method'], $mark['time'] ) ) { return null; } if ( ! Settings::is_age( $mark['min_age'] ) || ! in_array( $mark['method'], self::METHODS, true ) || ! self::is_time( $mark['time'] ) ) { return null; } return array( 'min_age' => (int) $mark['min_age'], 'method' => (string) $mark['method'], 'time' => (string) $mark['time'], ); } /** * The mark that spares this customer the question at this age now: remembering on, * a mark at this age or higher, and younger than the owner's months. Null means ask. * * @param int $user_id The customer (0 = a guest: always null). * @param int $age The minimum age asked now. * @param int|null $now A Unix time (default: now). * @return array{min_age: int, method: string, time: string, until: string}|null */ public static function holds( $user_id, $age, $now = null ) { $months = self::months(); $mark = $months > 0 && (int) $age > 0 ? self::of( $user_id ) : null; if ( null === $mark || $mark['min_age'] < (int) $age ) { return null; } $now = null === $now ? time() : (int) $now; $from = strtotime( $mark['time'] . ' UTC' ); $until = false === $from ? false : strtotime( '+' . $months . ' months', $from ); // A mark from the future is not one this plugin wrote. if ( false === $until || $from > $now + HOUR_IN_SECONDS || $until <= $now ) { return null; } $mark['until'] = gmdate( 'Y-m-d H:i:s', $until ); return $mark; } /** * Whether a remembered check holds for an order: only for its own customer, logged in now. * * @param \WC_Order $order The order. * @param int $age The minimum age it asks. * @return array|null */ public static function for_order( $order, $age ) { return self::customer_of( $order ) ? self::holds( get_current_user_id(), $age ) : null; } /** * Whether the person logged in now is this order's own customer. * * @param mixed $order The order. * @return bool */ public static function customer_of( $order ) { $me = get_current_user_id(); return $me > 0 && $order instanceof \WC_Order && (int) $order->get_customer_id() === $me; } /** * Take the mark off one account. * * @param int $user_id The customer. * @return bool Whether there was one. */ public static function forget( $user_id ) { $user_id = (int) $user_id; if ( $user_id < 1 || '' === get_user_meta( $user_id, self::META, true ) ) { return false; } return delete_user_meta( $user_id, self::META ); } /** * Take the mark off every account (remembering switched off). * * @return void */ public static function forget_all() { delete_metadata( 'user', 0, self::META, '', true ); } /** * The owner's number of months (0 = ask at every order). * * @return int */ public static function months() { return (int) Settings::get()['checkout_months']; } /** * What the checkout tells a remembered customer instead of asking. * * @param array $mark holds(). * @return string */ public static function sentence( array $mark ) { /* translators: %s: the date of the customer's earlier age check, in the site's date format. */ return sprintf( __( 'Your age was checked with an earlier order, on %s, so you are not asked again.', 'agevow-age-verification-age-gate' ), self::day( $mark['time'] ) ); } /** * A UTC time as the site's own date, in its date format. * * @param string $utc Y-m-d H:i:s, UTC. * @return string */ public static function day( $utc ) { $at = strtotime( (string) $utc . ' UTC' ); return false === $at ? '' : (string) wp_date( (string) get_option( 'date_format', 'Y-m-d' ), $at ); } /** * Whether a value is a time as this plugin writes one (Y-m-d H:i:s, a real date). * * @param mixed $value Value. * @return bool */ public static function is_time( $value ) { if ( ! is_string( $value ) || 1 !== preg_match( '/^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/', $value, $m ) ) { return false; } return checkdate( (int) $m[2], (int) $m[3], (int) $m[1] ) && (int) $m[1] >= 2000; } }