Enter the IP addresses or ranges to block, one per line.
', 'access-guard' ) ); } public function access_guard_custom_ban_messages_callback() { $custom_ban_messages = get_option( 'access_guard_custom_ban_messages' ); echo ''; echo wp_kses_post( __( 'Enter the custom ban message to display to blocked users.
', 'access-guard' ) ); } public function access_guard_access_control_rules_callback() { $banned_user_roles = get_option( 'access_guard_access_control_rules', array() ); $banned_user_roles = ! empty( $banned_user_roles ) ? $banned_user_roles : array(); $user_roles = wp_roles()->get_names(); foreach ( $user_roles as $role_value => $role_label ) { $checked = in_array( $role_value, $banned_user_roles, true ) ? 'checked' : ''; echo 'Select the user roles to ban.
'; } public function check_access() { if ( $this->check_ip_ban() ) { $this->display_ban_message(); exit; } if ( $this->is_restricted_content() ) { if ( ! $this->has_access() ) { $this->display_ban_message(); exit; } } if ( $this->is_role_banned() ) { $this->display_ban_message(); exit; } } public function check_ip_ban() { $user_ip = $this->get_user_ip(); $ip_block_list = explode( "\n", get_option( 'access_guard_ip_block_list' ) ); foreach ( $ip_block_list as $ip ) { $ip = trim( $ip ); if ( $ip && $this->is_ip_in_range( $user_ip, $ip ) ) { return true; } } } public function is_role_banned() { if ( is_user_logged_in() ) { $user = wp_get_current_user(); $roles = (array) $user->roles; $banned_user_roles = get_option( 'access_guard_access_control_rules', array() ); return array_intersect( $banned_user_roles, $roles ) ? true : false; // This will returns an array } } private function is_ip_in_range( $user_ip, $ip ) { if ( ! strpos( $ip, '-' ) ) { if ( $user_ip === $ip ) { return true; } } else { $ip_range = explode( '-', $ip ); $start_ip = ip2long( $ip_range[0] ); $end_ip = ip2long( $ip_range[1] ); $user_ip = ip2long( $user_ip ); if ( $user_ip >= $start_ip && $user_ip <= $end_ip ) { return true; } } return false; } public function restrict_content( $content ) { global $post; $restricted = get_post_meta( $post->ID, 'access_guard_restricted', true ); if ( $restricted && ! current_user_can( 'read' ) ) { return __( 'This content is restricted. Please contact the administrator for access.', 'access-guard' ); } return $content; } private function is_restricted_content() { global $post; $restricted = get_post_meta( $post->ID, 'access_guard_restricted', true ); return $restricted ? true : false; } private function get_user_ip() { if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) { //ip from share internet $ip = sanitize_text_field( $_SERVER['HTTP_CLIENT_IP'] ); } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { //phpcs:ignore //ip pass from proxy $ip = sanitize_text_field( $_SERVER['HTTP_X_FORWARDED_FOR'] ); //phpcs:ignore } else { $ip = sanitize_text_field( $_SERVER['REMOTE_ADDR'] ); //phpcs:ignore } return $ip; } private function display_ban_message() { $custom_ban_messages = get_option( 'access_guard_custom_ban_messages' ); $tailwind_css = plugins_url( '/assets/css/tailwind.min.css', __FILE__ ); ?>