` (the
* `language_attributes` filter) — the page's own minimum age;
* - the head lock, a tiny inline script at the top of `
` (public/js/lock.js);
* - the stylesheet and the deferred gate script (public/), with the settings the
* script needs — the tally's address, the days, the leave address, the words;
* - the overlay in the footer, hidden until the lock shows it;
* - a `` line saying who the page is for.
* Every byte of it depends on the address and the settings only. No cookie is read
* or set, no user agent is read, no nonce is printed, no header is changed: the page
* is the same for every visitor and every search engine, so a page cache can keep
* it and serve it to anyone (§S-LESSONS rows 15, 38, 39). Who has already answered
* is decided in the visitor's browser (public/js/gate.js).
*/
final class Gate {
/**
* The script and style handle.
*/
const HANDLE = 'agevow-gate';
/**
* The look when the owner chose no colours: a white card, near-black text and
* button — contrast well above 4.5:1 (§S-A11Y).
*/
const DEFAULT_LOOK = array(
'color_bg' => '#ffffff',
'color_text' => '#1d2327',
'color_button' => '#1d2327',
);
/**
* The contrast text needs against its background (WCAG 2.x, normal text).
*/
const MIN_CONTRAST = 4.5;
/**
* Locales that write a date month first; year-first ones; the rest day first.
*/
const MONTH_FIRST = array( 'en_US', 'en_PH', 'en_CA_US' );
const YEAR_FIRST = array( 'ja', 'zh_CN', 'zh_TW', 'zh_HK', 'ko_KR', 'hu_HU', 'lt_LT', 'mn' );
/**
* Whether the `` line was printed at the top of the body.
*
* @var bool
*/
private static $noscript_done = false;
/**
* Hook the public side — only while the gate is on.
*
* @return bool Whether anything was hooked.
*/
public static function init() {
if ( ! Settings::get()['gate'] ) {
return false;
}
add_filter( 'language_attributes', array( self::class, 'html_attributes' ), 20 );
add_action( 'wp_enqueue_scripts', array( self::class, 'enqueue' ), 20 );
add_action( 'wp_head', array( self::class, 'print_lock' ), 1 );
add_action( 'wp_body_open', array( self::class, 'print_noscript' ), 1 );
add_action( 'wp_footer', array( self::class, 'print_overlay' ), 20 );
return true;
}
/**
* ``: the page's minimum age and how many days a pass lasts.
*
* @param string $output The attributes WordPress prints (lang, dir).
* @return string
*/
public static function html_attributes( $output ) {
$age = Scope::current();
if ( $age <= 0 ) {
return $output;
}
return $output . ' ' . self::data_attributes( $age, Settings::get(), Scope::current_context() );
}
/**
* The attributes on ``, as text. A function of the page and the settings.
*
* @param int $age The page's minimum age.
* @param array $settings The settings.
* @param array $ctx The page (Scope::context()).
* @return string
*/
public static function data_attributes( $age, array $settings, array $ctx ) {
$out = sprintf( 'data-agevow-age="%d" data-agevow-days="%d"', (int) $age, (int) $settings['days'] );
// "Skip for logged-in users" is decided in the browser from the body class
// WordPress prints for a logged-in visitor (page caches never keep those pages).
// A preview is gated like the page, so the owner sees what visitors will see.
if ( ! empty( $settings['skip_logged_in'] ) && empty( $ctx['preview'] ) ) {
$out .= ' data-agevow-skip="li"';
}
return $out;
}
/**
* The stylesheet (in the head, so the page never paints uncovered) and the
* deferred gate script, with what it needs.
*
* @return void
*/
public static function enqueue() {
$age = Scope::current();
if ( $age <= 0 ) {
return;
}
self::assets( Settings::get() );
}
/**
* The stylesheet and the gate script with its settings — once a page, whether a
* whole page or only a section of it is behind the gate (Section, B2).
*
* @param array $settings The settings.
* @return bool Whether they were added now (false: already there).
*/
public static function assets( array $settings ) {
if ( wp_script_is( self::HANDLE, 'enqueued' ) ) {
return false;
}
wp_enqueue_style( self::HANDLE, AGEVOW_URL . 'public/css/gate.css', array(), AGEVOW_VERSION );
wp_add_inline_style( self::HANDLE, self::looks_css( $settings ) );
wp_enqueue_script(
self::HANDLE,
AGEVOW_URL . 'public/js/gate.js',
array(),
AGEVOW_VERSION,
array(
'in_footer' => false,
'strategy' => 'defer',
)
);
wp_add_inline_script( self::HANDLE, 'var agevowGate = ' . wp_json_encode( self::config( $settings ) ) . ';', 'before' );
return true;
}
/**
* What the gate script needs — the same for every page of the site.
*
* @param array $settings The settings.
* @return array
*/
public static function config( array $settings ) {
return array(
// The one public route (Rest_Public): it counts, and on a pass it sets the
// same mark from the server, which Safari keeps for the owner's full period.
// It exists only while the gate is on; a section of a page (B2) on a site
// whose gate is off is not counted.
'tally' => empty( $settings['gate'] ) ? '' : esc_url_raw( rest_url( Rest_Public::NS . '/tally' ) ),
'days' => (int) $settings['days'],
// The site's offset from UTC in minutes, so an age is counted on the
// site's calendar day.
'offset' => (int) round( wp_timezone()->getOffset( new \DateTime( 'now', new \DateTimeZone( 'UTC' ) ) ) / 60 ),
'leave' => (string) $settings['leave_url'],
'text' => self::mistakes(),
);
}
/**
* A plain sentence for each mistake in a typed date (the gate's script and the
* checkout's question say the same).
*
* @return array
*/
public static function mistakes() {
return array(
'empty' => __( 'Enter your date of birth.', 'agevow-age-verification-age-gate' ),
'month' => __( 'Enter the month as a number from 1 to 12.', 'agevow-age-verification-age-gate' ),
'day' => __( 'Enter the day as a number from 1 to 31.', 'agevow-age-verification-age-gate' ),
'year' => __( 'Enter the year with four digits.', 'agevow-age-verification-age-gate' ),
'date' => __( 'That date does not exist. Check the day and the month.', 'agevow-age-verification-age-gate' ),
'future' => __( 'That date is in the future.', 'agevow-age-verification-age-gate' ),
);
}
/**
* The head lock, inline at the very top of `` on a gated page.
*
* @return void
*/
public static function print_lock() {
if ( Scope::current() <= 0 ) {
return;
}
wp_print_inline_script_tag( self::lock_script(), array( 'id' => 'agevow-lock' ) );
}
/**
* The lock's code, read from its readable source (public/js/lock.js) with the
* comment lines and the indentation dropped. The same on every page — so a
* site's Content-Security-Policy can allow it by one hash.
*
* @return string
*/
public static function lock_script() {
return self::own_script( 'lock.js' );
}
/**
* One of this plugin's small inline scripts (public/js/), read from its readable
* source with the comment lines and the indentation dropped — once a request.
*
* @param string $file lock.js · section-lock.js.
* @return string
*/
public static function own_script( $file ) {
static $code = array();
$file = in_array( $file, array( 'lock.js', 'section-lock.js' ), true ) ? $file : 'lock.js';
if ( ! isset( $code[ $file ] ) ) {
$raw = (string) file_get_contents( plugin_dir_path( AGEVOW_FILE ) . 'public/js/' . $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- this plugin's own file, read from disk, never a URL.
$code[ $file ] = self::compact( $raw );
}
return $code[ $file ];
}
/**
* Script text without its comment lines, blank lines and indentation.
*
* @param string $js Readable script.
* @return string
*/
public static function compact( $js ) {
$out = array();
foreach ( preg_split( '/\R/', (string) $js ) as $line ) {
$line = trim( $line );
if ( '' === $line || 0 === strpos( $line, '//' ) || 0 === strpos( $line, '/*' ) || 0 === strpos( $line, '*' ) ) {
continue;
}
$out[] = $line;
}
return implode( "\n", $out );
}
/**
* The `` line at the top of the body: without JavaScript the gate never
* locks the page, so the page itself says who it is for.
*
* @return void
*/
public static function print_noscript() {
$age = Scope::current();
if ( $age <= 0 || self::$noscript_done ) {
return;
}
self::$noscript_done = true;
?>
` line.
*
* @param int $age The page's minimum age.
* @return string
*/
public static function noscript_text( $age ) {
/* translators: %d: the minimum age, 18 to 25. */
return sprintf( __( 'This page is for people aged %d or older.', 'agevow-age-verification-age-gate' ), (int) $age );
}
/**
* The overlay, at the end of the body, hidden until the head lock shows it — and
* the `` line, if the theme never opened the body with `wp_body_open`.
*
* @return void
*/
public static function print_overlay() {
$age = Scope::current();
if ( $age <= 0 ) {
return;
}
self::print_noscript();
self::overlay( $age, Settings::get() );
}
/**
* The overlay's markup (a dialog; §S-A11Y): the logo, the heading, the message,
* the question — one tap or the date of birth — and the under-age answer.
*
* @param int $age The page's minimum age.
* @param array $settings The settings.
* @return void
*/
public static function overlay( $age, array $settings ) {
$method = 'dob' === $settings['method'] ? 'dob' : 'confirm';
$look = 'blur' === $settings['background'] ? 'blur' : 'solid';
$heading = self::words( $settings['heading'], 'dob' === $method ? __( 'Please enter your date of birth', 'agevow-age-verification-age-gate' ) : __( 'Are you old enough to enter?', 'agevow-age-verification-age-gate' ), $age );
/* translators: %d: the minimum age, 18 to 25. */
$message = self::words( $settings['message'], __( 'You must be %d or older to see this page.', 'agevow-age-verification-age-gate' ), $age );
/* translators: %d: the minimum age, 18 to 25. */
$under = self::words( $settings['under_message'], __( 'Sorry, you must be %d or older to see this page.', 'agevow-age-verification-age-gate' ), $age );
$logo = (int) $settings['logo'] > 0 ? wp_get_attachment_image(
(int) $settings['logo'],
'medium',
false,
array(
'class' => 'agevow-logo-img',
'alt' => '',
'loading' => 'eager',
'decoding' => 'async',
)
) : '';
?>
*/
public static function field( $part ) {
switch ( $part ) {
case 'day':
return array(
'label' => __( 'Day', 'agevow-age-verification-age-gate' ),
'max' => 2,
/* translators: the placeholder of the day field: two letters standing for the digits. */
'placeholder' => _x( 'DD', 'day placeholder', 'agevow-age-verification-age-gate' ),
);
case 'month':
return array(
'label' => __( 'Month', 'agevow-age-verification-age-gate' ),
'max' => 2,
/* translators: the placeholder of the month field: two letters standing for the digits. */
'placeholder' => _x( 'MM', 'month placeholder', 'agevow-age-verification-age-gate' ),
);
}
return array(
'label' => __( 'Year', 'agevow-age-verification-age-gate' ),
'max' => 4,
/* translators: the placeholder of the year field: four letters standing for the digits. */
'placeholder' => _x( 'YYYY', 'year placeholder', 'agevow-age-verification-age-gate' ),
);
}
/**
* The colours and the background, as CSS custom properties (added to the
* stylesheet inline). The owner's colours where set, the defaults otherwise.
*
* @param array $settings The settings.
* @return string
*/
public static function looks_css( array $settings ) {
$c = self::colors( $settings );
$css = sprintf(
':root{--agevow-bg:%1$s;--agevow-text:%2$s;--agevow-button:%3$s;--agevow-button-text:%4$s;--agevow-cover:%1$s}',
$c['color_bg'],
$c['color_text'],
$c['color_button'],
self::readable_on( $c['color_button'] )
);
if ( 'blur' === $settings['background'] ) {
$rgb = self::rgb( $c['color_bg'] );
$css .= sprintf(
'html.agevow-locked::before{background:rgba(%d,%d,%d,.55);-webkit-backdrop-filter:blur(14px);backdrop-filter:blur(14px)}',
$rgb[0],
$rgb[1],
$rgb[2]
);
}
return $css;
}
/**
* The three colours in force: the owner's, or the default for each one unset.
*
* @param array $settings The settings.
* @return array
*/
public static function colors( array $settings ) {
$out = array();
foreach ( self::DEFAULT_LOOK as $key => $default ) {
$value = isset( $settings[ $key ] ) ? (string) $settings[ $key ] : '';
$color = '' !== $value ? sanitize_hex_color( $value ) : '';
$out[ $key ] = is_string( $color ) && '' !== $color ? strtolower( $color ) : $default;
}
return $out;
}
/**
* What the owner's colours fail, in plain words for the settings screen: text on
* the card and the button's words on the button need 4.5:1 (§S-GATE "Looks").
*
* @param array $settings The settings.
* @return string[] One sentence per problem; empty when the colours read well.
*/
public static function color_warnings( array $settings ) {
$c = self::colors( $settings );
$out = array();
$t = self::contrast( $c['color_text'], $c['color_bg'] );
if ( $t < self::MIN_CONTRAST ) {
/* translators: %s: a contrast ratio such as 2.1. */
$out[] = sprintf( __( 'The text colour is hard to read on the background colour (contrast %s to 1; at least 4.5 to 1 is needed). Pick a darker text or a lighter background, or the other way round.', 'agevow-age-verification-age-gate' ), number_format_i18n( $t, 1 ) );
}
$b = self::contrast( self::readable_on( $c['color_button'] ), $c['color_button'] );
if ( $b < self::MIN_CONTRAST ) {
/* translators: %s: a contrast ratio such as 2.1. */
$out[] = sprintf( __( 'The words on the button are hard to read on the button colour (contrast %s to 1; at least 4.5 to 1 is needed). Pick a darker or a lighter button colour.', 'agevow-age-verification-age-gate' ), number_format_i18n( $b, 1 ) );
}
return $out;
}
/**
* White or near-black — whichever reads better on a colour.
*
* @param string $hex A #rgb or #rrggbb colour.
* @return string
*/
public static function readable_on( $hex ) {
return self::contrast( '#ffffff', $hex ) >= self::contrast( '#111111', $hex ) ? '#ffffff' : '#111111';
}
/**
* The contrast ratio of two colours (WCAG 2.x), from 1 to 21.
*
* @param string $a A #rgb or #rrggbb colour.
* @param string $b Another.
* @return float
*/
public static function contrast( $a, $b ) {
$la = self::luminance( $a );
$lb = self::luminance( $b );
return round( ( max( $la, $lb ) + 0.05 ) / ( min( $la, $lb ) + 0.05 ), 2 );
}
/**
* The relative luminance of a colour (WCAG 2.x).
*
* @param string $hex A #rgb or #rrggbb colour.
* @return float
*/
private static function luminance( $hex ) {
$l = 0.0;
foreach ( self::rgb( $hex ) as $i => $v ) {
$c = $v / 255;
$c = $c <= 0.03928 ? $c / 12.92 : pow( ( $c + 0.055 ) / 1.055, 2.4 );
$l += array( 0.2126, 0.7152, 0.0722 )[ $i ] * $c;
}
return $l;
}
/**
* A colour's red, green and blue, 0–255 (black for anything unreadable).
*
* @param string $hex A #rgb or #rrggbb colour.
* @return int[]
*/
private static function rgb( $hex ) {
$hex = ltrim( (string) $hex, '#' );
if ( 3 === strlen( $hex ) ) {
$hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
}
if ( ! preg_match( '/^[0-9a-fA-F]{6}$/', $hex ) ) {
return array( 0, 0, 0 );
}
return array( (int) hexdec( substr( $hex, 0, 2 ) ), (int) hexdec( substr( $hex, 2, 2 ) ), (int) hexdec( substr( $hex, 4, 2 ) ) );
}
}