[1-9][0-9]{0,18})'; $day = array( 'type' => 'string', 'pattern' => '^([0-9]{4}-[0-9]{2}-[0-9]{2})?$', 'validate_callback' => array( self::class, 'valid_day' ), ); $paged = array( 'page' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => 100000, 'default' => 1, 'validate_callback' => 'rest_validate_request_arg', 'sanitize_callback' => 'absint', ), 'per_page' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => 100, 'default' => 20, 'validate_callback' => 'rest_validate_request_arg', 'sanitize_callback' => 'absint', ), 'from' => $day, 'to' => $day, ); $mark = array( 'age' => array( 'description' => __( '0 to clear the mark, or a minimum age from 18 to 25.', 'agevow-age-verification-age-gate' ), 'type' => 'integer', 'required' => true, 'validate_callback' => array( self::class, 'valid_mark' ), ), ); register_rest_route( self::NS, '/settings', array( array( 'methods' => 'GET', 'callback' => array( self::class, 'get_settings' ), 'permission_callback' => array( self::class, 'can_settings' ), ), array( 'methods' => 'POST', 'callback' => array( self::class, 'save_settings' ), 'permission_callback' => array( self::class, 'can_settings' ), 'args' => self::settings_args(), ), ) ); $scope_args = array_intersect_key( self::settings_args(), array_flip( array( 'scope', 'pages', 'post_categories', 'never', 'min_age' ) ) ); register_rest_route( self::NS, '/scope/preview', array( 'methods' => 'GET', 'callback' => array( self::class, 'scope_preview' ), 'permission_callback' => array( self::class, 'can_settings' ), 'args' => $scope_args, ) ); register_rest_route( self::NS, '/checks', array( 'methods' => 'GET', 'callback' => array( self::class, 'checks' ), 'permission_callback' => array( self::class, 'can_records' ), 'args' => array_merge( $paged, array( 'result' => array( 'type' => 'string', 'enum' => array_merge( array( '' ), Checks::RESULTS ), 'validate_callback' => 'rest_validate_request_arg', ), 'method' => array( 'type' => 'string', 'enum' => array_merge( array( '' ), Checks::METHODS ), 'validate_callback' => 'rest_validate_request_arg', ), 'source' => array( 'type' => 'string', 'enum' => array_merge( array( '' ), Checks::SOURCES ), 'validate_callback' => 'rest_validate_request_arg', ), 'order_id' => array( 'type' => 'integer', 'minimum' => 1, 'validate_callback' => 'rest_validate_request_arg', ), ) ), ) ); register_rest_route( self::NS, '/tally', array( 'methods' => 'GET', 'callback' => array( self::class, 'tally' ), 'permission_callback' => array( self::class, 'can_records' ), 'args' => array( 'from' => $day, 'to' => $day, ), ) ); register_rest_route( self::NS, '/history', array( 'methods' => 'GET', 'callback' => array( self::class, 'history' ), 'permission_callback' => array( self::class, 'can_records' ), 'args' => array_merge( $paged, array( 'what' => array( 'type' => 'string', 'enum' => array_merge( array( '' ), History::WHATS ), 'validate_callback' => 'rest_validate_request_arg', ), ) ), ) ); register_rest_route( self::NS, '/products/' . $id . '/restricted', array( array( 'methods' => 'GET', 'callback' => array( self::class, 'get_product' ), 'permission_callback' => array( self::class, 'can_product' ), ), array( 'methods' => 'POST', 'callback' => array( self::class, 'set_product' ), 'permission_callback' => array( self::class, 'can_product' ), 'args' => $mark, ), ) ); register_rest_route( self::NS, '/terms/' . $id . '/restricted', array( array( 'methods' => 'GET', 'callback' => array( self::class, 'get_term' ), 'permission_callback' => array( self::class, 'can_term' ), ), array( 'methods' => 'POST', 'callback' => array( self::class, 'set_term' ), 'permission_callback' => array( self::class, 'can_term' ), 'args' => $mark, ), ) ); } /** * The schema of a settings change: every key optional, each of its own shape. * * @return array> */ public static function settings_args() { $bool = array( 'type' => 'boolean', 'validate_callback' => 'rest_validate_request_arg', ); $ids = array( 'type' => 'array', 'items' => array( 'type' => 'integer', 'minimum' => 1, ), 'maxItems' => Settings::MAX_IDS, 'validate_callback' => 'rest_validate_request_arg', ); $text = array( 'type' => 'string', 'maxLength' => Settings::MAX_TEXT, 'validate_callback' => 'rest_validate_request_arg', ); $color = array( 'type' => 'string', 'pattern' => '^(#[0-9a-fA-F]{3}|#[0-9a-fA-F]{6})?$', 'validate_callback' => 'rest_validate_request_arg', ); return array( 'gate' => $bool, 'scope' => array( 'type' => 'string', 'enum' => Settings::SCOPES, 'validate_callback' => 'rest_validate_request_arg', ), 'pages' => $ids, 'post_categories' => $ids, 'never' => $ids, 'skip_logged_in' => $bool, 'min_age' => array( 'type' => 'integer', 'minimum' => Settings::AGE_MIN, 'maximum' => Settings::AGE_MAX, 'validate_callback' => 'rest_validate_request_arg', ), 'method' => array( 'type' => 'string', 'enum' => Settings::METHODS, 'validate_callback' => 'rest_validate_request_arg', ), 'days' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => Settings::MAX_DAYS, 'validate_callback' => 'rest_validate_request_arg', ), 'checkout' => $bool, 'checkout_when' => array( 'type' => 'string', 'enum' => Settings::CHECKOUT_WHEN, 'validate_callback' => 'rest_validate_request_arg', ), 'checkout_method' => array( 'type' => 'string', 'enum' => Settings::METHODS, 'validate_callback' => 'rest_validate_request_arg', ), 'checkout_months' => array( 'type' => 'integer', 'minimum' => 0, 'maximum' => Remember::MAX_MONTHS, 'validate_callback' => 'rest_validate_request_arg', ), 'leave_url' => array( 'type' => 'string', 'maxLength' => 2000, 'validate_callback' => array( self::class, 'valid_url' ), ), 'heading' => $text, 'message' => $text, 'under_message' => $text, 'logo' => array( 'type' => 'integer', 'minimum' => 0, 'validate_callback' => array( self::class, 'valid_logo' ), ), 'color_bg' => $color, 'color_text' => $color, 'color_button' => $color, 'background' => array( 'type' => 'string', 'enum' => Settings::BACKGROUNDS, 'validate_callback' => 'rest_validate_request_arg', ), 'product_note' => $bool, ); } // ── who may ────────────────────────────────────────────────────────────── /** * The checks every route shares: logged in, the capability, the nonce. * * @param \WP_REST_Request $request Request. * @param bool $allowed Whether the user holds what this route needs. * @return true|\WP_Error */ private static function gate( \WP_REST_Request $request, $allowed ) { if ( 0 === get_current_user_id() ) { return Security::error( 'agevow_logged_out', __( 'You are not logged in. Log in again, then reload this page.', 'agevow-age-verification-age-gate' ), 401 ); } if ( ! $allowed ) { return Security::error( 'agevow_forbidden', __( 'Your account is not allowed to do this.', 'agevow-age-verification-age-gate' ), 403 ); } if ( ! Security::nonce_ok( $request ) ) { return Security::error( 'agevow_bad_nonce', __( 'Your session has expired. Reload this page and try again.', 'agevow-age-verification-age-gate' ), 403 ); } return true; } /** * Settings, and turning the gate on or off. * * @param \WP_REST_Request $request Request. * @return true|\WP_Error */ public static function can_settings( \WP_REST_Request $request ) { return self::gate( $request, current_user_can( Security::capability( 'settings' ) ) ); } /** * The record: the checks, the counts, the history. * * @param \WP_REST_Request $request Request. * @return true|\WP_Error */ public static function can_records( \WP_REST_Request $request ) { $gate = self::gate( $request, current_user_can( Security::capability( 'records' ) ) ); if ( true !== $gate ) { return $gate; } // An order named in the request must be an order WooCommerce knows, and one // this user may manage. $order_id = $request->get_param( 'order_id' ); if ( null !== $order_id && ( ! Checks::order_exists( (int) $order_id ) || ! current_user_can( 'edit_shop_orders' ) ) ) { return self::not_found(); } return true; } /** * One product: the id must be a product, not in the trash, that THIS user may edit. * * @param \WP_REST_Request $request Request. * @return true|\WP_Error */ public static function can_product( \WP_REST_Request $request ) { if ( 0 !== get_current_user_id() && ! Security::can_edit_product( (int) $request['id'] ) ) { return self::not_found(); } return self::gate( $request, true ); } /** * One category: the id must be a product or post category THIS user may edit. * * @param \WP_REST_Request $request Request. * @return true|\WP_Error */ public static function can_term( \WP_REST_Request $request ) { if ( 0 !== get_current_user_id() && ! Security::can_edit_term( (int) $request['id'] ) ) { return self::not_found(); } return self::gate( $request, true ); } /** * "Not found" — the same words for an id that does not exist and one that is * not this user's to see. * * @return \WP_Error */ private static function not_found() { return Security::error( 'agevow_not_found', __( 'That was not found, or your account may not change it.', 'agevow-age-verification-age-gate' ), 404 ); } // ── the inputs ─────────────────────────────────────────────────────────── /** * A day filter: empty, or a real calendar day. * * @param mixed $value Value. * @param \WP_REST_Request $request Request. * @param string $param Name. * @return true|\WP_Error */ public static function valid_day( $value, $request, $param ) { $valid = rest_validate_request_arg( $value, $request, $param ); if ( true !== $valid ) { return $valid; } return '' === $value || Dates::is_day( $value ) ? true : Security::error( 'agevow_bad_input', __( 'That is not a date. Use the form 2026-10-31.', 'agevow-age-verification-age-gate' ), 400 ); } /** * A mark: 0 or a minimum age from 18 to 25. * * @param mixed $value Value. * @param \WP_REST_Request $request Request. * @param string $param Name. * @return true|\WP_Error */ public static function valid_mark( $value, $request, $param ) { $valid = rest_validate_request_arg( $value, $request, $param ); if ( true !== $valid ) { return $valid; } return Restrictions::is_mark( $value ) ? true : Security::error( 'agevow_bad_input', __( 'The minimum age must be from 18 to 25, or 0 to clear it.', 'agevow-age-verification-age-gate' ), 400 ); } /** * The "leave" address: empty, or an http(s) address. * * @param mixed $value Value. * @param \WP_REST_Request $request Request. * @param string $param Name. * @return true|\WP_Error */ public static function valid_url( $value, $request, $param ) { $valid = rest_validate_request_arg( $value, $request, $param ); if ( true !== $valid ) { return $valid; } if ( '' === trim( (string) $value ) ) { return true; } $clean = Settings::sanitize( array( 'leave_url' => $value ), Settings::defaults() ); return '' !== $clean['leave_url'] ? true : Security::error( 'agevow_bad_input', __( 'The leave address must start with https:// or http://.', 'agevow-age-verification-age-gate' ), 400 ); } /** * The logo: 0, or an image in the media library. * * @param mixed $value Value. * @param \WP_REST_Request $request Request. * @param string $param Name. * @return true|\WP_Error */ public static function valid_logo( $value, $request, $param ) { $valid = rest_validate_request_arg( $value, $request, $param ); if ( true !== $valid ) { return $valid; } return 0 === (int) $value || wp_attachment_is_image( (int) $value ) ? true : Security::error( 'agevow_bad_input', __( 'The logo must be an image from the media library.', 'agevow-age-verification-age-gate' ), 400 ); } // ── the routes ─────────────────────────────────────────────────────────── /** * GET settings. * * @return \WP_REST_Response */ public static function get_settings() { return rest_ensure_response( Settings::get() ); } /** * POST settings — a partial change; every changed group goes to the history. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public static function save_settings( \WP_REST_Request $request ) { $changes = array(); $params = $request->get_params(); foreach ( array_keys( Settings::defaults() ) as $key ) { if ( array_key_exists( $key, $params ) ) { $changes[ $key ] = $request->get_param( $key ); } } if ( array() === $changes ) { return Security::error( 'agevow_bad_input', __( 'Nothing to save.', 'agevow-age-verification-age-gate' ), 400 ); } return rest_ensure_response( Settings::update( $changes ) ); } /** * GET scope/preview — what the gate would cover: with the saved settings, or with * the scope, pages, categories, never-list and age given (not saved). Every page * and category id is held to what it must be (Settings::sanitize(): a page that * is a page and not in the trash, a post category that exists). * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response */ public static function scope_preview( \WP_REST_Request $request ) { $changes = array(); $params = $request->get_params(); foreach ( array( 'scope', 'pages', 'post_categories', 'never', 'min_age' ) as $key ) { if ( array_key_exists( $key, $params ) ) { $changes[ $key ] = $request->get_param( $key ); } } return rest_ensure_response( Scope::preview( Settings::sanitize( $changes, Settings::get() ) ) ); } /** * GET checks — a page of checkout checks. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response */ public static function checks( \WP_REST_Request $request ) { $args = array(); foreach ( array( 'page', 'per_page', 'from', 'to', 'result', 'method', 'source', 'order_id' ) as $key ) { $args[ $key ] = $request->get_param( $key ); } $list = Checks::query( $args ); $res = rest_ensure_response( array_map( array( self::class, 'check_words' ), $list['items'] ) ); $res->header( 'X-WP-Total', (string) $list['total'] ); return $res; } /** * A check as the Checks screen shows it: the row, plus the site's own date and * time and the words for where, how and what came of it (P9). * * @param array $item Checks::decode(). * @return array */ public static function check_words( array $item ) { return array_merge( $item, array( 'local' => Record::local( $item['checked_at'] ), 'where' => Record::where( $item ), 'how' => Record::method_word( $item['method'] ), 'answer' => Record::result_word( $item['result'] ), ) ); } /** * A history entry as the Checks screen shows it: plus the site's own time and the * change in words (P9). * * @param array $item History::decode(). * @return array */ public static function history_words( array $item ) { return array_merge( $item, array( 'local' => Record::local( $item['changed_at'] ), 'words' => Record::describe( $item ), ) ); } /** * GET tally — the daily counts of a range (default: the last 30 days). * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public static function tally( \WP_REST_Request $request ) { $to = (string) $request->get_param( 'to' ); $from = (string) $request->get_param( 'from' ); $to = '' === $to ? Dates::today() : $to; $from = '' === $from ? Dates::days_ago( 29 ) : $from; if ( $from > $to ) { return Security::error( 'agevow_bad_input', __( 'The first day comes after the last one.', 'agevow-age-verification-age-gate' ), 400 ); } return rest_ensure_response( array( 'from' => $from, 'to' => $to, 'days' => Tally::read( $from, $to ), ) ); } /** * GET history — a page of the settings history. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response */ public static function history( \WP_REST_Request $request ) { $args = array(); foreach ( array( 'page', 'per_page', 'from', 'to', 'what' ) as $key ) { $args[ $key ] = $request->get_param( $key ); } $list = History::query( $args ); $res = rest_ensure_response( array_map( array( self::class, 'history_words' ), $list['items'] ) ); $res->header( 'X-WP-Total', (string) $list['total'] ); return $res; } /** * GET products//restricted. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response */ public static function get_product( \WP_REST_Request $request ) { $id = (int) $request['id']; return rest_ensure_response( array( 'id' => $id, 'age' => Restrictions::product( $id ), ) ); } /** * POST products//restricted — mark (18–25) or clear (0). * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public static function set_product( \WP_REST_Request $request ) { $id = (int) $request['id']; if ( ! Restrictions::set_product( $id, (int) $request['age'] ) ) { return Security::error( 'agevow_bad_input', __( 'The minimum age must be from 18 to 25, or 0 to clear it.', 'agevow-age-verification-age-gate' ), 400 ); } return self::get_product( $request ); } /** * GET terms//restricted. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response */ public static function get_term( \WP_REST_Request $request ) { $id = (int) $request['id']; return rest_ensure_response( array( 'id' => $id, 'age' => Restrictions::term( $id ), ) ); } /** * POST terms//restricted — mark (18–25) or clear (0). * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public static function set_term( \WP_REST_Request $request ) { $id = (int) $request['id']; if ( ! Restrictions::set_term( $id, (int) $request['age'] ) ) { return Security::error( 'agevow_bad_input', __( 'The minimum age must be from 18 to 25, or 0 to clear it.', 'agevow-age-verification-age-gate' ), 400 ); } return self::get_term( $request ); } }