400 ] ); } } public function block_bad_useragents() { if ( ! GUARDFORGE_Options::get( 'block_bad_useragents' ) ) { return; } $ua = strtolower( sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ?? '' ) ) ); if ( $ua === '' ) { return; } foreach ( $this->effective_blocklist() as $needle ) { if ( $needle !== '' && strpos( $ua, $needle ) !== false ) { $this->deny(); } } } public function effective_blocklist() { $extra = GUARDFORGE_Options::get( 'bad_useragents_extra', '' ); $extra_arr = []; if ( is_string( $extra ) && $extra !== '' ) { foreach ( preg_split( '/[\r\n,]+/', strtolower( $extra ) ) as $line ) { $line = trim( $line ); if ( $line !== '' && $line[0] !== '#' ) { $extra_arr[] = $line; } } } $list = array_merge( self::DEFAULT_BAD_UAS, $extra_arr ); /** * Filter the effective bad-UA blocklist. * * @param string $list lower-cased substrings. */ return apply_filters( 'guardforge/bad_useragents', $list ); } private function deny() { status_header( 403 ); nocache_headers(); wp_die( 'Forbidden.', 'Forbidden', [ 'response' => 403 ] ); } }