__( 'Checkout', 'agevow-age-verification-age-gate' ), 'block' => __( 'Checkout (blocks)', 'agevow-age-verification-age-gate' ), 'pay' => __( 'Paid later (pay for order)', 'agevow-age-verification-age-gate' ), 'express' => __( 'Express payment', 'agevow-age-verification-age-gate' ), 'remembered' => __( 'Remembered customer', 'agevow-age-verification-age-gate' ), 'service' => __( 'Checking service', 'agevow-age-verification-age-gate' ), ); return isset( $words[ $source ] ) ? $words[ $source ] : (string) $source; } /** * Where a check was asked, for one row: a remembered customer's row says which * check it rests on — "Remembered from the check of 2026-10-14 13:32" (B1). * * @param array $row A check: source, and ref (the remembered check's UTC time). * @return string */ public static function where( array $row ) { $source = isset( $row['source'] ) ? (string) $row['source'] : ''; $ref = isset( $row['ref'] ) ? $row['ref'] : null; if ( 'remembered' === $source && Remember::is_time( $ref ) ) { /* translators: %s: the date and time (site time) of the check the customer was remembered from. */ return sprintf( __( 'Remembered from the check of %s', 'agevow-age-verification-age-gate' ), self::local( $ref ) ); } return self::source_word( $source ); } /** * How it was answered. * * @param string $method One of Checks::METHODS. * @return string */ public static function method_word( $method ) { $words = array( 'confirm' => __( 'Confirmed (tick box)', 'agevow-age-verification-age-gate' ), 'dob' => __( 'Date of birth', 'agevow-age-verification-age-gate' ), 'service' => __( 'Checking service', 'agevow-age-verification-age-gate' ), ); return isset( $words[ $method ] ) ? $words[ $method ] : (string) $method; } /** * What came of it. * * @param string $result One of Checks::RESULTS. * @return string */ public static function result_word( $result ) { $words = array( 'pass' => __( 'Passed', 'agevow-age-verification-age-gate' ), 'under' => __( 'Refused: under the minimum age', 'agevow-age-verification-age-gate' ), 'missing' => __( 'Not answered', 'agevow-age-verification-age-gate' ), ); return isset( $words[ $result ] ) ? $words[ $result ] : (string) $result; } /** * A UTC time as the site's own date and time (Y-m-d H:i). * * @param string $utc Y-m-d H:i:s, UTC. * @return string */ public static function local( $utc ) { return get_date_from_gmt( (string) $utc, 'Y-m-d H:i' ); } /** * One setting's value in words. * * @param string $key A key of Settings::defaults(). * @param mixed $value Its value. * @return string */ public static function value_word( $key, $value ) { if ( null === $value ) { return __( 'none', 'agevow-age-verification-age-gate' ); } switch ( $key ) { case 'gate': case 'checkout': return $value ? __( 'on', 'agevow-age-verification-age-gate' ) : __( 'off', 'agevow-age-verification-age-gate' ); case 'skip_logged_in': case 'product_note': return $value ? __( 'yes', 'agevow-age-verification-age-gate' ) : __( 'no', 'agevow-age-verification-age-gate' ); case 'scope': return self::where_word( (string) $value ); case 'min_age': /* translators: %d: a minimum age, 18 to 25. */ return sprintf( __( '%d+', 'agevow-age-verification-age-gate' ), (int) $value ); case 'method': return 'dob' === $value ? __( 'date of birth', 'agevow-age-verification-age-gate' ) : __( 'one tap', 'agevow-age-verification-age-gate' ); case 'checkout_method': return 'dob' === $value ? __( 'date of birth', 'agevow-age-verification-age-gate' ) : __( 'tick box', 'agevow-age-verification-age-gate' ); case 'checkout_when': return 'always' === $value ? __( 'every order', 'agevow-age-verification-age-gate' ) : __( 'orders with an age-restricted product', 'agevow-age-verification-age-gate' ); case 'days': $days = (int) $value; // A plural's count is a plain variable (wordpress.org reads it). /* translators: %d: a number of days. */ return sprintf( _n( '%d day', '%d days', $days, 'agevow-age-verification-age-gate' ), $days ); case 'checkout_months': if ( (int) $value < 1 ) { return __( 'not remembered (asked at every order)', 'agevow-age-verification-age-gate' ); } $months = (int) $value; /* translators: %d: a number of months. */ return sprintf( _n( '%d month', '%d months', $months, 'agevow-age-verification-age-gate' ), $months ); case 'pages': case 'post_categories': case 'never': $n = is_array( $value ) ? count( $value ) : 0; /* translators: %d: how many pages or categories. */ return sprintf( _n( '%d chosen', '%d chosen', $n, 'agevow-age-verification-age-gate' ), $n ); } return is_scalar( $value ) && '' !== (string) $value ? (string) $value : __( 'none', 'agevow-age-verification-age-gate' ); } /** * Where the gate shows, in words. * * @param string $scope site · restricted · chosen. * @return string */ public static function where_word( $scope ) { if ( 'restricted' === $scope ) { return __( 'age-restricted products and their categories', 'agevow-age-verification-age-gate' ); } if ( 'chosen' === $scope ) { return __( 'the chosen pages and categories', 'agevow-age-verification-age-gate' ); } return __( 'the whole site', 'agevow-age-verification-age-gate' ); } /** * A setting's name. * * @param string $key A key of Settings::defaults(). * @return string */ public static function key_word( $key ) { $words = array( 'gate' => __( 'Age gate', 'agevow-age-verification-age-gate' ), 'scope' => __( 'Where the gate shows', 'agevow-age-verification-age-gate' ), 'pages' => __( 'Chosen pages', 'agevow-age-verification-age-gate' ), 'post_categories' => __( 'Chosen post categories', 'agevow-age-verification-age-gate' ), 'never' => __( 'Pages never gated', 'agevow-age-verification-age-gate' ), 'skip_logged_in' => __( 'Logged-in users skip the gate', 'agevow-age-verification-age-gate' ), 'min_age' => __( 'Minimum age', 'agevow-age-verification-age-gate' ), 'method' => __( 'How visitors answer', 'agevow-age-verification-age-gate' ), 'days' => __( 'A pass is remembered for', 'agevow-age-verification-age-gate' ), 'checkout' => __( 'Age asked again at checkout', 'agevow-age-verification-age-gate' ), 'checkout_when' => __( 'Checkout asks for', 'agevow-age-verification-age-gate' ), 'checkout_method' => __( 'Checkout asks with', 'agevow-age-verification-age-gate' ), 'checkout_months' => __( 'A logged-in customer\'s check is remembered for', 'agevow-age-verification-age-gate' ), 'product_note' => __( 'Line on restricted products', 'agevow-age-verification-age-gate' ), ); return isset( $words[ $key ] ) ? $words[ $key ] : (string) $key; } /** * One history entry in words: "Minimum age: 18+ → 21+". * * @param array $entry History::decode(). * @return string */ public static function describe( array $entry ) { $before = is_array( $entry['before'] ) ? $entry['before'] : array(); $after = is_array( $entry['after'] ) ? $entry['after'] : array(); if ( ! empty( $before['truncated'] ) || ! empty( $after['truncated'] ) ) { return __( 'Changed (too long to show)', 'agevow-age-verification-age-gate' ); } if ( 'product' === $entry['what'] || 'term' === $entry['what'] ) { $id = isset( $after['id'] ) ? (int) $after['id'] : 0; $name = 'product' === $entry['what'] ? get_the_title( $id ) : self::term_name( $id ); /* translators: 1: a product or category name, 2: its id. */ $what = sprintf( __( '%1$s (#%2$d)', 'agevow-age-verification-age-gate' ), '' !== (string) $name ? $name : __( 'a removed item', 'agevow-age-verification-age-gate' ), $id ); /* translators: 1: a product or category, 2: its mark before, 3: its mark after. */ return sprintf( __( 'Age-restricted: %1$s: %2$s → %3$s', 'agevow-age-verification-age-gate' ), $what, self::mark_word( isset( $before['age'] ) ? $before['age'] : 0 ), self::mark_word( isset( $after['age'] ) ? $after['age'] : 0 ) ); } if ( 'looks' === $entry['what'] ) { return __( 'The gate\'s words or look changed', 'agevow-age-verification-age-gate' ); } $parts = array(); foreach ( $after as $key => $value ) { if ( ! is_string( $key ) || ! array_key_exists( $key, Settings::defaults() ) ) { continue; } /* translators: 1: a setting's name, 2: its value before, 3: its value after. */ $parts[] = sprintf( __( '%1$s: %2$s → %3$s', 'agevow-age-verification-age-gate' ), self::key_word( $key ), self::value_word( $key, array_key_exists( $key, $before ) ? $before[ $key ] : null ), self::value_word( $key, $value ) ); } return array() === $parts ? __( 'Changed', 'agevow-age-verification-age-gate' ) : implode( '; ', $parts ); } /** * A product's or category's mark in words. * * @param mixed $age 0 or a minimum age. * @return string */ private static function mark_word( $age ) { /* translators: %d: a minimum age, 18 to 25. */ return (int) $age > 0 ? sprintf( __( '%d+', 'agevow-age-verification-age-gate' ), (int) $age ) : __( 'not restricted', 'agevow-age-verification-age-gate' ); } /** * A category's name, or ''. * * @param int $id Term id. * @return string */ private static function term_name( $id ) { $term = $id > 0 ? get_term( $id ) : null; return $term && ! is_wp_error( $term ) ? (string) $term->name : ''; } // ── the summary of a range ─────────────────────────────────────────────── /** * The settings in force at a moment: today's, with every change made since undone. * * @param string $utc Y-m-d H:i:s, UTC. * @return array */ public static function settings_at( $utc ) { global $wpdb; $state = Settings::get(); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- this plugin's own table, read once for a printed summary. $rows = (array) $wpdb->get_results( $wpdb->prepare( "SELECT before_value FROM %i WHERE changed_at >= %s AND what NOT IN ( 'product', 'term' ) ORDER BY id DESC", Schema::table( 'history' ), $utc ), ARRAY_A ); foreach ( $rows as $row ) { $before = json_decode( (string) $row['before_value'], true ); if ( ! is_array( $before ) ) { continue; } foreach ( $before as $key => $value ) { if ( is_string( $key ) && array_key_exists( $key, $state ) ) { $state[ $key ] = $value; } } } return Settings::sanitize( $state, Settings::defaults() ); } /** * The settings that matter to an inspector, in one line: "Age gate on for the whole * site at 21+, one tap; checkout asks orders with an age-restricted product, by * date of birth". * * @param array $s Settings. * @return string */ public static function in_force( array $s ) { if ( $s['gate'] ) { /* translators: 1: where (e.g. "the whole site"), 2: the minimum age, 3: how visitors answer (e.g. "one tap"). */ $gate = sprintf( __( 'Age gate on for %1$s at %2$d+, %3$s', 'agevow-age-verification-age-gate' ), self::where_word( $s['scope'] ), (int) $s['min_age'], self::value_word( 'method', $s['method'] ) ); } else { $gate = __( 'Age gate off', 'agevow-age-verification-age-gate' ); } if ( $s['checkout'] ) { /* translators: 1: which orders (e.g. "every order"), 2: how (e.g. "date of birth"). */ $checkout = sprintf( __( 'checkout asks %1$s, by %2$s', 'agevow-age-verification-age-gate' ), self::value_word( 'checkout_when', $s['checkout_when'] ), self::value_word( 'checkout_method', $s['checkout_method'] ) ); if ( (int) $s['checkout_months'] > 0 ) { /* translators: 1: what the checkout asks (e.g. "checkout asks every order, by date of birth"), 2: how long (e.g. "12 months"). */ $checkout = sprintf( __( '%1$s, a logged-in customer\'s check remembered for %2$s', 'agevow-age-verification-age-gate' ), $checkout, self::value_word( 'checkout_months', $s['checkout_months'] ) ); } } else { $checkout = __( 'checkout question off', 'agevow-age-verification-age-gate' ); } return $gate . '; ' . $checkout; } /** * Everything the printable summary shows, for a range of the site's days. * * @param string $from First day (Y-m-d). * @param string $to Last day (Y-m-d). * @return array|null Null when the range is not one. */ public static function summary( $from, $to ) { global $wpdb; if ( ! Dates::is_day( $from ) || ! Dates::is_day( $to ) || $from > $to ) { return null; } list( $start, $end ) = Dates::utc_range( $from, $to ); $changes = array(); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- this plugin's own table, read once for a printed summary. $rows = (array) $wpdb->get_results( $wpdb->prepare( 'SELECT id, changed_at, user_id, what, before_value, after_value FROM %i WHERE changed_at >= %s AND changed_at <= %s ORDER BY id ASC LIMIT 500', Schema::table( 'history' ), $start, $end ), ARRAY_A ); foreach ( $rows as $row ) { $entry = History::decode( $row ); $changes[] = array( 'when' => self::local( $entry['changed_at'] ), 'who' => $entry['user'], 'words' => self::describe( $entry ), ); } $gate = array_fill_keys( Tally::OUTCOMES, 0 ); $cout = array_fill_keys( Tally::OUTCOMES, 0 ); foreach ( Tally::read( $from, $to ) as $day ) { foreach ( Tally::OUTCOMES as $o ) { $gate[ $o ] += (int) $day['gate'][ $o ]; $cout[ $o ] += (int) $day['checkout'][ $o ]; } } $by = array(); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- this plugin's own table, counted once for a printed summary. $counts = (array) $wpdb->get_results( $wpdb->prepare( 'SELECT method, result, COUNT(*) AS n FROM %i WHERE checked_at >= %s AND checked_at <= %s GROUP BY method, result ORDER BY method, result', Schema::table( 'checks' ), $start, $end ), ARRAY_A ); foreach ( $counts as $c ) { $m = (string) $c['method']; if ( ! isset( $by[ $m ] ) ) { $by[ $m ] = array_fill_keys( Checks::RESULTS, 0 ); } if ( in_array( $c['result'], Checks::RESULTS, true ) ) { $by[ $m ][ $c['result'] ] = (int) $c['n']; } } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- this plugin's own table, counted once for a printed summary. $orders = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT order_id) FROM %i WHERE checked_at >= %s AND checked_at <= %s AND result = 'pass' AND order_id IS NOT NULL", Schema::table( 'checks' ), $start, $end ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- this plugin's own table, counted once for a printed summary. $refused = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE checked_at >= %s AND checked_at <= %s AND result = 'under'", Schema::table( 'checks' ), $start, $end ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- this plugin's own table, read once for a printed summary. $versions = (array) $wpdb->get_col( $wpdb->prepare( 'SELECT DISTINCT version FROM %i WHERE checked_at >= %s AND checked_at <= %s ORDER BY version', Schema::table( 'checks' ), $start, $end ) ); $first = self::settings_at( $start ); return array( 'from' => $from, 'to' => $to, 'first' => $first, 'last' => self::settings_at( $end ), 'changes' => $changes, 'gate' => $gate, 'checkout' => $cout, 'by' => $by, 'orders' => $orders, 'refused' => $refused, 'versions' => array_values( array_filter( array_map( 'strval', $versions ) ) ), 'version' => AGEVOW_VERSION, 'sentence' => self::sentence( $from, $to, $first, count( $changes ), $orders, $refused ), ); } /** * The summary's first sentence: "Between 1 October 2026 and 31 October 2026 the * age gate was on for the whole site at 21+; 214 orders were checked at checkout, * 3 refused." * * @param string $from First day. * @param string $to Last day. * @param array $first The settings in force when the range began. * @param int $changes How many changes the range holds. * @param int $orders Orders checked at checkout. * @param int $refused Refusals under the minimum age. * @return string */ public static function sentence( $from, $to, array $first, $changes, $orders, $refused ) { $zone = wp_timezone(); $a = wp_date( get_option( 'date_format', 'j F Y' ), ( new \DateTimeImmutable( $from . ' 12:00:00', $zone ) )->getTimestamp() ); $b = wp_date( get_option( 'date_format', 'j F Y' ), ( new \DateTimeImmutable( $to . ' 12:00:00', $zone ) )->getTimestamp() ); if ( $changes > 0 ) { /* translators: %d: how many changes. */ $gate = sprintf( _n( 'the settings changed %d time (listed below)', 'the settings changed %d times (listed below)', $changes, 'agevow-age-verification-age-gate' ), $changes ); } elseif ( $first['gate'] ) { /* translators: 1: where (e.g. "the whole site"), 2: the minimum age. */ $gate = sprintf( __( 'the age gate was on for %1$s at %2$d+', 'agevow-age-verification-age-gate' ), self::where_word( $first['scope'] ), (int) $first['min_age'] ); } else { $gate = __( 'the age gate was off', 'agevow-age-verification-age-gate' ); } /* translators: %d: how many orders. */ $checked = sprintf( _n( '%d order was checked at checkout', '%d orders were checked at checkout', $orders, 'agevow-age-verification-age-gate' ), $orders ); /* translators: %d: how many refusals. */ $no = sprintf( _n( '%d refused', '%d refused', $refused, 'agevow-age-verification-age-gate' ), $refused ); /* translators: 1: the first day, 2: the last day, 3: what the gate did, 4: how many orders were checked, 5: how many were refused. */ return sprintf( __( 'Between %1$s and %2$s %3$s; %4$s, %5$s.', 'agevow-age-verification-age-gate' ), $a, $b, $gate, $checked, $no ); } }