'POST', 'callback' => array( self::class, 'tally' ), // Public by design (see the class): the only thing it can do is add 1. 'permission_callback' => '__return_true', 'args' => array( 'outcome' => array( 'type' => 'string', 'required' => true, 'enum' => Tally::OUTCOMES, 'validate_callback' => 'rest_validate_request_arg', ), 'age' => array( 'type' => 'integer', 'minimum' => Settings::AGE_MIN, 'maximum' => Settings::AGE_MAX, 'validate_callback' => 'rest_validate_request_arg', ), ), ) ); return true; } /** * POST tally — add 1 to today's count of one outcome of the gate. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public static function tally( \WP_REST_Request $request ) { $age = $request['age']; if ( 'pass' === $request['outcome'] && Settings::is_age( $age ) && ! headers_sent() ) { $cookie = self::pass_cookie( (int) $age, Settings::get()['days'], is_ssl(), time() ); setcookie( $cookie['name'], $cookie['value'], $cookie['options'] ); } $result = Tally::report( $request['outcome'] ); if ( 'limit' === $result ) { $res = rest_convert_error_to_response( Security::error( 'agevow_slow_down', __( 'Too many reports this minute; this one was not counted.', 'agevow-age-verification-age-gate' ), 429 ) ); } elseif ( 'counted' !== $result ) { $res = rest_convert_error_to_response( Security::error( 'agevow_not_counted', __( 'This report was not counted.', 'agevow-age-verification-age-gate' ), 'bad' === $result ? 400 : 503 ) ); } else { $res = new \WP_REST_Response( array( 'counted' => true ), 202 ); } // A REST answer, never a page: no cache keeps it and no search engine lists it. $res->header( 'Cache-Control', 'no-store, private' ); $res->header( 'X-Robots-Tag', 'noindex' ); return $res; } /** * The "passed" cookie the answer to a pass sets — the same mark the visitor's * script writes (public/js/gate.js): `.`, the day counted in * whole days since 1 January 1970 (UTC), for the owner's number of days; the * whole site; sent on links from other sites (Lax); over HTTPS only when the site * is on HTTPS; readable by the page's script (the gate reads it). * * @param int $age The minimum age passed, 18–25. * @param int $days How many days it is remembered. * @param bool $secure Whether the site is on HTTPS. * @param int $now The time (Unix seconds). * @return array{name: string, value: string, options: array} */ public static function pass_cookie( $age, $days, $secure, $now ) { $days = max( 1, min( Settings::MAX_DAYS, (int) $days ) ); return array( 'name' => 'agevow_ok', 'value' => (int) $age . '.' . (int) floor( (int) $now / DAY_IN_SECONDS ), 'options' => array( 'expires' => (int) $now + $days * DAY_IN_SECONDS, 'path' => '/', 'domain' => '', 'secure' => (bool) $secure, 'httponly' => false, 'samesite' => 'Lax', ), ); } }