options = get_option( 'accessdefender_options' ); } /** * Add options page under Settings menu. */ public function add_plugin_page() { add_options_page( 'Access Defender Settings', 'Access Defender', 'manage_options', 'access-defender', array( $this, 'create_admin_page' ) ); } /** * Create the admin page form for plugin settings. */ public function create_admin_page() { ?>
' . esc_html__( 'Enter the custom warning title (e.g., "Access Denied").', 'access-defender' ) . '
'; } /** * Callback for custom warning message input field using WordPress editor. */ public function warning_message_callback() { $warning_message = isset( $this->options['warning_message'] ) ? wp_kses_post( $this->options['warning_message'] ) : ''; wp_editor( $warning_message, 'accessdefender_warning_message', array( 'textarea_name' => 'accessdefender_options[warning_message]', 'media_buttons' => false, 'textarea_rows' => 10, 'teeny' => true, 'quicktags' => true, ) ); } /** * Check if the user is using VPN or Proxy and show warning message if detected. */ public function check_vpn_proxy() { if ( ! isset( $this->options['enable_vpn_blocking'] ) || '1' !== $this->options['enable_vpn_blocking'] ) { return; // VPN blocking is disabled. } if ( $this->is_vpn_or_proxy() ) { wp_die( wp_kses_post( $this->get_warning_message() ), '', array( 'response' => 403 ) ); } } /** * Retrieve the client's IP address from the HTTP headers. * * First, it checks the HTTP_CLIENT_IP header, then the HTTP_X_FORWARDED_FOR * header, followed by the HTTP_X_FORWARDED and HTTP_FORWARDED_FOR headers. * If none of these headers are present, it defaults to the REMOTE_ADDR header. * * @return string The client's IP address or an empty string if it can't be determined. */ private function get_client_ip() { $ip_headers = array( 'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR', ); foreach ( $ip_headers as $header ) { if ( ! empty( $_SERVER[ $header ] ) ) { $ip_list = sanitize_text_field( wp_unslash( $_SERVER[ $header ] ) ); $ip_array = explode( ',', $ip_list ); $client_ip = trim( $ip_array[0] ); if ( filter_var( $client_ip, FILTER_VALIDATE_IP ) ) { return $client_ip; } } } return ''; } /** * Check if the user is using VPN or Proxy by calling the external API. * * @return bool True if VPN or proxy is detected, false otherwise. */ private function is_vpn_or_proxy() { $ip_address = $this->get_client_ip(); if ( empty( $ip_address ) ) { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log error_log( 'Access Defender: Unable to determine client IP address.' ); return false; // If we can't determine the IP, don't block the user. } $api_url = "http://ip-api.com/json/{$ip_address}?fields=proxy,hosting"; $response = wp_remote_get( $api_url ); if ( is_wp_error( $response ) ) { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log error_log( 'Access Defender API Error: ' . $response->get_error_message() ); return false; // If there's an error, don't block the user. } $body = wp_remote_retrieve_body( $response ); $data = json_decode( $body, true ); // Check if the IP is identified as a proxy or hosting service. return ( isset( $data['proxy'] ) && true === $data['proxy'] ) || ( isset( $data['hosting'] ) && true === $data['hosting'] ); } /** * Get the warning message with the title and the message. * * @return string The formatted warning message. */ private function get_warning_message() { $title_html = '' . $this->options['warning_message'] . '
' ) : '' . wp_kses_post( $default_message ) . '
'; return $title_html . $message; } /** * Get the warning title, either custom or default. * * @return string The warning title. */ private function get_warning_title() { $default_title = __( 'Access Denied', 'access-defender' ); return ! empty( $this->options['warning_title'] ) ? esc_html( $this->options['warning_title'] ) : $default_title; } } new Access_Defender();