*/ public static function get_links() { $links = get_option( self::OPTION_LINKS, array() ); $out = array(); if ( ! is_array( $links ) ) { return $out; } foreach ( $links as $link ) { if ( ! is_array( $link ) ) { continue; } $label = isset( $link['label'] ) ? sanitize_text_field( (string) $link['label'] ) : ''; $url = isset( $link['url'] ) ? esc_url_raw( (string) $link['url'] ) : ''; if ( $url === '' ) { continue; } if ( $label === '' ) { $label = $url; } $out[] = array( 'label' => $label, 'url' => $url, ); if ( count( $out ) >= self::MAX_LINKS ) { break; } } return $out; } /** * Entry point on template_redirect. Decides whether to withhold the page. */ public function maybe_render_gate() { if ( is_admin() || wp_doing_ajax() || wp_doing_cron() ) { return; } if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { return; } if ( defined( 'WP_CLI' ) && WP_CLI ) { return; } if ( ! self::is_active() ) { return; } if ( $this->is_request_allowed() ) { return; } $age = self::get_entrance_age(); if ( AgeOnce_API::is_verified( true, $age, AgeOnce_API::SURFACE_CONTENT ) ) { return; } $this->render_gate_and_exit( $age ); } /** * Whether the current request is allowed through without verification. * * @return bool */ private function is_request_allowed() { // Logged-in editors can preview the site while building it. if ( 'no' !== get_option( self::OPTION_BYPASS_LOGGED_IN, 'yes' ) && current_user_can( 'edit_posts' ) ) { return true; } // Optional SEO bypass for known crawlers. if ( 'yes' === get_option( self::OPTION_ALLOW_BOTS, 'no' ) && $this->is_search_bot() ) { return true; } // Allowlisted pages (gate landing, legal, contact). $queried_id = (int) get_queried_object_id(); if ( $queried_id > 0 && ( is_page() || is_singular() ) ) { if ( in_array( $queried_id, self::get_allowed_pages(), true ) ) { return true; } } return false; } /** * Match a small allowlist of well-known search engine user agents. * * @return bool */ private function is_search_bot() { if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) { return false; } $ua = strtolower( sanitize_text_field( wp_unslash( (string) $_SERVER['HTTP_USER_AGENT'] ) ) ); if ( $ua === '' ) { return false; } $bots = array( 'googlebot', 'bingbot', 'slurp', 'duckduckbot', 'baiduspider', 'yandex', 'applebot' ); foreach ( $bots as $bot ) { if ( strpos( $ua, $bot ) !== false ) { return true; } } return false; } /** * Current full URL, so the visitor returns here after verifying. * * @return string */ private function get_current_url() { $request = isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( (string) $_SERVER['REQUEST_URI'] ) : '/'; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Passed through esc_url_raw below. $url = esc_url_raw( home_url( $request ) ); return $url !== '' ? $url : home_url( '/' ); } /** * Render the self-contained gate page and stop WordPress. * * @param int $age Minimum age required. */ private function render_gate_and_exit( $age ) { nocache_headers(); if ( ! headers_sent() ) { header( 'X-Robots-Tag: noindex, nofollow', true ); } status_header( 200 ); $current_url = $this->get_current_url(); $ageonce_gate = array( 'age' => absint( $age ), 'verify_url' => AgeOnce_API::get_verify_age_url( $current_url, 'content', absint( $age ) ), 'links' => self::get_links(), 'heading' => sanitize_text_field( (string) get_option( self::OPTION_HEADING, '' ) ), 'message' => sanitize_textarea_field( (string) get_option( self::OPTION_MESSAGE, '' ) ), 'site_name' => get_bloginfo( 'name' ), 'logo_url' => AgeOnce_Appearance::get_logo_url(), 'logo_width' => AgeOnce_Appearance::get_logo_width(), 'bg_image_url' => AgeOnce_Appearance::get_bg_image_url(), 'error_code' => $this->get_front_error_code(), 'appearance_css' => AgeOnce_Appearance::get_frontend_css(), ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Template escapes its output. echo AgeOnce_Templates::get( 'entrance-gate', array( 'ageonce_gate' => $ageonce_gate ) ); exit; } /** * Whitelisted OAuth error code from the return URL, if any. * * @return string */ private function get_front_error_code() { if ( empty( $_GET['ageonce_err'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only public flash param. return ''; } $code = sanitize_key( wp_unslash( $_GET['ageonce_err'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only public flash param. $allowed = array( 'denied', 'csrf', 'connect', 'session', 'config', 'api', 'unknown' ); return in_array( $code, $allowed, true ) ? $code : ''; } }