'yesno',
'avp_min_age' => 18,
'avp_redirect_url' => 'https://www.google.com',
'avp_logo' => '',
'avp_title' => __( 'Please verify your age', 'advanced-age-verification-popup' ),
'avp_subtitle' => __( 'You must be of legal age to enter this site.', 'advanced-age-verification-popup' ),
'avp_enter_text' => __( 'I am 18 or older', 'advanced-age-verification-popup' ),
'avp_exit_text' => __( 'I am under 18', 'advanced-age-verification-popup' ),
'avp_warning_text' => __( 'WARNING: This website contains age-restricted content.', 'advanced-age-verification-popup' ),
'avp_cookie_duration' => 30,
'avp_show_remember_me' => '1',
'avp_bypass_logged_in' => '1',
'avp_bypass_crawlers' => '1',
'avp_pro_test_mode' => '0',
'avp_color_popup_bg' => '#1a1b20',
'avp_color_primary' => '#10b981',
'avp_color_text' => '#ffffff',
'avp_color_text_muted' => '#9ca3af',
'avp_free_stats_on' => '1',
);
}
/** Read one setting with its default. */
function avp_free_get( $key ) {
$defaults = avp_free_defaults();
return get_option( $key, isset( $defaults[ $key ] ) ? $defaults[ $key ] : '' );
}
/** Verification methods available in the free plugin. Pro-only methods stored by Pro fall back to date of birth. */
function avp_free_method() {
$method = (string) get_option( 'avp_verification_method', 'yesno' );
if ( in_array( $method, array( 'yesno', 'dob', 'both' ), true ) ) {
return $method;
}
return 'dob';
}
/** Ready-made colour styles (same values as the Pro colour styles, so Pro recognises them after an upgrade). */
function avp_free_color_styles() {
return array(
array( 'id' => 'emerald', 'name' => __( 'Emerald Night', 'advanced-age-verification-popup' ), 'desc' => __( 'Deep navy & emerald', 'advanced-age-verification-popup' ), 'bg' => '#0f172a', 'pri' => '#10b981', 'txt' => '#ffffff', 'mut' => '#94a3b8' ),
array( 'id' => 'classic', 'name' => __( 'Classic Dark', 'advanced-age-verification-popup' ), 'desc' => __( 'Charcoal & signal red', 'advanced-age-verification-popup' ), 'bg' => '#1a1b20', 'pri' => '#ef4444', 'txt' => '#ffffff', 'mut' => '#9ca3af' ),
array( 'id' => 'royal', 'name' => __( 'Royal', 'advanced-age-verification-popup' ), 'desc' => __( 'Indigo & violet', 'advanced-age-verification-popup' ), 'bg' => '#1e1b4b', 'pri' => '#8b5cf6', 'txt' => '#ffffff', 'mut' => '#c4b5fd' ),
array( 'id' => 'light', 'name' => __( 'Clean Light', 'advanced-age-verification-popup' ), 'desc' => __( 'White & ink black', 'advanced-age-verification-popup' ), 'bg' => '#ffffff', 'pri' => '#0f172a', 'txt' => '#0f172a', 'mut' => '#64748b' ),
array( 'id' => 'gold', 'name' => __( 'Gold', 'advanced-age-verification-popup' ), 'desc' => __( 'Black & warm gold', 'advanced-age-verification-popup' ), 'bg' => '#111111', 'pri' => '#d4a017', 'txt' => '#fafafa', 'mut' => '#a3a3a3' ),
);
}
/**
* Sanitize one submitted value. Used by the AJAX save and by register_setting (plain form submit).
*
* @param string $key Option name.
* @param mixed $value Raw (already unslashed) value.
*/
function avp_free_sanitize( $key, $value ) {
$value = is_scalar( $value ) ? (string) $value : '';
switch ( $key ) {
case 'avp_verification_method':
return in_array( $value, array( 'yesno', 'dob', 'both' ), true ) ? $value : 'yesno';
case 'avp_min_age':
return min( 120, max( 1, absint( $value ) ) );
case 'avp_cookie_duration':
return min( 3650, max( 1, absint( $value ) ) );
case 'avp_redirect_url':
$url = esc_url_raw( trim( $value ), array( 'http', 'https' ) );
return $url ? $url : 'https://www.google.com';
case 'avp_logo':
return esc_url_raw( trim( $value ), array( 'http', 'https' ) );
case 'avp_subtitle':
case 'avp_warning_text':
return avp_free_kses_text( $value );
case 'avp_show_remember_me':
case 'avp_bypass_logged_in':
case 'avp_bypass_crawlers':
case 'avp_pro_test_mode':
case 'avp_free_stats_on':
return $value === '1' ? '1' : '0';
case 'avp_color_popup_bg':
case 'avp_color_primary':
case 'avp_color_text':
case 'avp_color_text_muted':
$hex = sanitize_hex_color( $value );
$defaults = avp_free_defaults();
return $hex ? $hex : $defaults[ $key ];
default:
return sanitize_text_field( $value );
}
}
/* ------------------------------------------------------------------ Links in the popup texts */
/**
* HTML allowed in the subtitle and the warning line: links and simple formatting.
* Lets visitors open the terms / privacy pages they agree to. Filter: avp_free_allowed_html.
*/
function avp_free_allowed_html() {
return apply_filters(
'avp_free_allowed_html',
array(
'a' => array(
'href' => true,
'title' => true,
'target' => true,
'rel' => true,
'class' => true,
),
'strong' => array(),
'b' => array(),
'em' => array(),
'i' => array(),
'u' => array(),
'br' => array(),
'span' => array( 'class' => true ),
)
);
}
/** Sanitize a popup text: keeps line breaks, links and simple formatting, strips everything else. */
function avp_free_kses_text( $value ) {
$value = str_replace( array( "\r\n", "\r" ), "\n", (string) $value );
return trim( wp_kses( $value, avp_free_allowed_html() ) );
}
/**
* Smart links for the popup texts: {terms} and {privacy}, optionally with a label: {terms|Read our terms}.
* Terms: the WooCommerce terms page, otherwise a published page with a usual terms slug.
* Privacy: the privacy policy page chosen in Settings > Privacy.
*
* @return array Token => array( 'id' => int, 'url' => string, 'label' => string ).
*/
function avp_free_link_tokens() {
static $tokens = null;
if ( null !== $tokens ) {
return $tokens;
}
$terms_id = absint( get_option( 'woocommerce_terms_page_id' ) );
if ( $terms_id && 'publish' !== get_post_status( $terms_id ) ) {
$terms_id = 0;
}
if ( ! $terms_id ) {
foreach ( array( 'terms-and-conditions', 'terms-conditions', 'terms-of-service', 'terms-of-use', 'terms' ) as $slug ) {
$page = get_page_by_path( $slug );
if ( $page && 'publish' === $page->post_status ) {
$terms_id = (int) $page->ID;
break;
}
}
}
$privacy_id = absint( get_option( 'wp_page_for_privacy_policy' ) );
if ( $privacy_id && 'publish' !== get_post_status( $privacy_id ) ) {
$privacy_id = 0;
}
$tokens = array(
'terms' => array(
'id' => $terms_id,
'url' => $terms_id ? (string) get_permalink( $terms_id ) : '',
'label' => $terms_id ? wp_strip_all_tags( get_the_title( $terms_id ) ) : __( 'Terms & Conditions', 'advanced-age-verification-popup' ),
),
'privacy' => array(
'id' => $privacy_id,
'url' => $privacy_id ? (string) get_permalink( $privacy_id ) : '',
'label' => $privacy_id ? wp_strip_all_tags( get_the_title( $privacy_id ) ) : __( 'Privacy Policy', 'advanced-age-verification-popup' ),
),
);
$tokens = (array) apply_filters( 'avp_free_link_tokens', $tokens );
return $tokens;
}
/**
* Turn a saved popup text into safe HTML: smart links, line breaks, allowed tags only.
* Links open in a new tab, so the popup is still there when the visitor comes back.
*
* @param string $text Saved subtitle or warning line.
* @return string
*/
function avp_free_format_text( $text ) {
$text = (string) $text;
if ( '' === $text ) {
return '';
}
if ( false !== strpos( $text, '{' ) ) {
$tokens = avp_free_link_tokens();
$text = preg_replace_callback(
'/\{([a-z_]+)(?:\|([^{}]*))?\}/i',
function ( $m ) use ( $tokens ) {
$key = strtolower( $m[1] );
if ( ! isset( $tokens[ $key ] ) ) {
return $m[0];
}
$t = $tokens[ $key ];
$label = ( isset( $m[2] ) && '' !== trim( $m[2] ) ) ? trim( $m[2] ) : $t['label'];
if ( empty( $t['url'] ) ) {
return esc_html( $label );
}
return '' . esc_html( $label ) . '';
},
$text
);
}
$html = wp_kses( nl2br( $text, false ), avp_free_allowed_html() );
$html = preg_replace_callback(
'/]*>/i',
function ( $m ) {
$tag = rtrim( substr( $m[0], 0, -1 ), ' /' );
if ( ! preg_match( '/\starget\s*=/i', $tag ) ) {
$tag .= ' target="_blank"';
}
if ( ! preg_match( '/\srel\s*=/i', $tag ) ) {
$tag .= ' rel="noopener"';
}
return $tag . '>';
},
$html
);
return (string) apply_filters( 'avp_free_format_text', $html, $text );
}
/** IDs of this site's pages linked from the popup texts. */
function avp_free_linked_page_ids() {
static $ids = null;
if ( null !== $ids ) {
return $ids;
}
$ids = array();
$html = avp_free_format_text( avp_free_get( 'avp_subtitle' ) ) . avp_free_format_text( avp_free_get( 'avp_warning_text' ) );
if ( false !== stripos( $html, 'href' ) && preg_match_all( '/href="([^"]+)"/i', $html, $m ) ) {
foreach ( $m[1] as $url ) {
$url = html_entity_decode( $url, ENT_QUOTES );
if ( 0 === strpos( $url, '/' ) && 0 !== strpos( $url, '//' ) ) {
$url = home_url( $url );
}
$id = url_to_postid( $url );
if ( $id ) {
$ids[] = (int) $id;
}
}
}
$ids = array_values( array_unique( $ids ) );
return $ids;
}
/**
* Pages linked from the popup (terms, privacy…) never show the gate, so visitors can read
* what they agree to. Turn off with: add_filter( 'avp_free_exempt_linked_pages', '__return_false' );
*/
function avp_free_is_linked_page( $post_id ) {
if ( ! $post_id || ! apply_filters( 'avp_free_exempt_linked_pages', true ) ) {
return false;
}
return in_array( (int) $post_id, avp_free_linked_page_ids(), true );
}
/** Keys saved by the free settings screen (never Pro-only keys, so a downgrade can't wipe Pro settings). */
function avp_free_setting_keys() {
return array_keys( avp_free_defaults() );
}
function avp_free_checkbox_keys() {
return array( 'avp_show_remember_me', 'avp_bypass_logged_in', 'avp_bypass_crawlers', 'avp_pro_test_mode', 'avp_free_stats_on' );
}
/**
* Pages where the gate never shows. Same post meta as the Pro version ("Never show the age gate on this page"),
* so these pages stay exempt after an upgrade.
*/
define( 'AVP_FREE_EXEMPT_META', '_avp_gate_exempt' );
function avp_free_is_exempt( $post_id ) {
return $post_id && get_post_meta( $post_id, AVP_FREE_EXEMPT_META, true ) === '1';
}
/* ------------------------------------------------------------------ Verification counter */
/**
* Anonymous daily totals of passed and under-age checks, stored in this site's own database.
* No personal data, no cookies of its own and nothing is sent to other services.
*
* @param int $days Number of days to return (newest last).
* @return array{pass:int,fail:int,days:array}
*/
function avp_free_stats( $days = 30 ) {
$raw = get_option( 'avp_free_stats', array() );
$raw = is_array( $raw ) ? $raw : array();
$out = array( 'pass' => 0, 'fail' => 0, 'days' => array() );
$now = current_time( 'timestamp' ); // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested -- local day buckets.
for ( $i = $days - 1; $i >= 0; $i-- ) {
$d = gmdate( 'Y-m-d', $now - $i * DAY_IN_SECONDS );
$p = isset( $raw[ $d ]['p'] ) ? absint( $raw[ $d ]['p'] ) : 0;
$f = isset( $raw[ $d ]['f'] ) ? absint( $raw[ $d ]['f'] ) : 0;
$out['pass'] += $p;
$out['fail'] += $f;
$out['days'][] = array( 'd' => $d, 'p' => $p, 'f' => $f );
}
return $out;
}
/** True when the request comes from a page of this site (Origin or Referer header; "www." is ignored). */
function avp_free_same_site_request() {
$norm = function ( $url ) {
$host = strtolower( (string) wp_parse_url( (string) $url, PHP_URL_HOST ) );
return 0 === strpos( $host, 'www.' ) ? substr( $host, 4 ) : $host;
};
$home = $norm( home_url() );
$from = '';
if ( ! empty( $_SERVER['HTTP_ORIGIN'] ) ) {
$from = esc_url_raw( wp_unslash( $_SERVER['HTTP_ORIGIN'] ) );
} else {
$from = (string) wp_get_raw_referer();
}
return $home && $from && $norm( $from ) === $home;
}
add_action( 'wp_ajax_avp_free_stat', 'avp_free_ajax_stat' );
add_action( 'wp_ajax_nopriv_avp_free_stat', 'avp_free_ajax_stat' );
function avp_free_ajax_stat() {
// No nonce on purpose: the popup is served from page caches, where nonces expire. The endpoint only adds 1 to a
// daily counter, accepts two fixed values and only requests coming from this site.
if ( get_option( 'avp_free_stats_on', '1' ) !== '1' ) {
wp_die( '', '', array( 'response' => 204 ) );
}
if ( ! avp_free_same_site_request() ) {
wp_die( '', '', array( 'response' => 204 ) );
}
$r = isset( $_POST['r'] ) ? sanitize_key( wp_unslash( $_POST['r'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- see above.
if ( 'pass' !== $r && 'fail' !== $r ) {
wp_die( '', '', array( 'response' => 204 ) );
}
$raw = get_option( 'avp_free_stats', array() );
$raw = is_array( $raw ) ? $raw : array();
$day = gmdate( 'Y-m-d', current_time( 'timestamp' ) ); // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested
$k = 'pass' === $r ? 'p' : 'f';
if ( ! isset( $raw[ $day ] ) ) {
$raw[ $day ] = array( 'p' => 0, 'f' => 0 );
ksort( $raw );
$raw = array_slice( $raw, -60, null, true ); // Keep 60 days.
}
$raw[ $day ][ $k ] = absint( $raw[ $day ][ $k ] ) + 1;
update_option( 'avp_free_stats', $raw, false );
wp_die( '', '', array( 'response' => 204 ) );
}