'asbfg_submission_blocker', 'label' => 'Submission Blocker', 'icon' => 'dashicons-shield-alt', // Icon for the menu item ]; return $menu_items; } public function asbfg_register_settings() { // Only process settings if the form has been submitted and nonce is valid. if ( isset( $_POST['asbfg_submission_blocker_nonce'] ) ) { check_admin_referer( 'asbfg_submission_blocker_action', 'asbfg_submission_blocker_nonce' ); } $settings = [ 'asbfg_blocked_ips' => [ 'sanitize_callback' => [ $this, 'sanitize_textarea_lines' ], 'default' => [] ], 'asbfg_blocked_emails' => [ 'sanitize_callback' => [ $this, 'sanitize_textarea_lines' ], 'default' => [] ], 'asbfg_blocked_domains' => [ 'sanitize_callback' => [ $this, 'sanitize_textarea_lines' ], 'default' => [] ], 'asbfg_blocked_countries' => [ 'sanitize_callback' => [ $this, 'sanitize_textarea_lines' ], 'default' => [] ], 'asbfg_custom_ip_message' => [ 'sanitize_callback' => 'sanitize_text_field', 'default' => 'Submissions from your IP address are not allowed.' ], 'asbfg_custom_email_message' => [ 'sanitize_callback' => 'sanitize_text_field', 'default' => 'Submissions from this email are not allowed.' ], 'asbfg_custom_domain_message' => [ 'sanitize_callback' => 'sanitize_text_field', 'default' => 'Submissions from this email domain are not allowed.' ], 'asbfg_custom_country_message' => [ 'sanitize_callback' => 'sanitize_text_field', 'default' => 'Submissions from your country are not allowed.' ], 'asbfg_blocked_text' => [ 'sanitize_callback' => [ $this, 'sanitize_textarea_lines' ], 'default' => [] ], 'asbfg_custom_text_message' => [ 'sanitize_callback' => 'sanitize_text_field', 'default' => 'Submissions containing blocked text are not allowed.' ], 'asbfg_log_retention_days' => [ 'sanitize_callback' => 'absint', 'default' => 90 ], ]; foreach ( $settings as $key => $args ) { register_setting( 'asbfg_submission_blocker', $key, $args ); } // Add settings success message if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] == 'true' ) { add_settings_error( 'asbfg_submission_blocker', 'settings_updated', 'Settings saved successfully.', 'updated' ); } // Add import success message if ( isset( $_GET['message'] ) && $_GET['message'] === 'imported' ) { add_settings_error( 'asbfg_submission_blocker', 'settings_imported', 'Settings imported successfully.', 'updated' ); } // Add import error messages if ( isset( $_GET['message'] ) && $_GET['message'] === 'import_error' ) { add_settings_error( 'asbfg_submission_blocker', 'import_error', 'Failed to import settings. Please check the file.', 'error' ); } if ( isset( $_GET['message'] ) && $_GET['message'] === 'invalid_json' ) { add_settings_error( 'asbfg_submission_blocker', 'invalid_json', 'Invalid JSON file. Please upload a valid settings file.', 'error' ); } } public function sanitize_textarea_lines( $input ) { if ( is_array( $input ) ) { return array_map( 'sanitize_text_field', $input ); } if ( is_string( $input ) ) { $lines = explode( "\n", $input ); $lines = array_map( 'trim', $lines ); $lines = array_map( 'sanitize_text_field', $lines ); return array_filter( $lines ); } return []; } public function asbfg_render_settings_page() { ?>
render_log_retention_section(); $this->render_ip_blocking_section(); $this->render_email_blocking_section(); $this->render_domain_blocking_section(); $this->render_country_blocking_section(); $this->render_text_blocking_section(); ?>



asbfg_render_setting_section( 'IP Address Blocking', 'Block form submissions from specific IP addresses.', 'asbfg_blocked_ips', 'Blocked IP Addresses', 'Enter one IP address per line (e.g., 192.168.1.1)', 'asbfg_custom_ip_message', 'Custom Validation Message', 'Message shown when an IP is blocked' ); } private function render_email_blocking_section() { $this->asbfg_render_setting_section( 'Email Address Blocking', 'Block form submissions from specific email addresses.', 'asbfg_blocked_emails', 'Blocked Email Addresses', 'Enter one email address per line (e.g., spam@example.com)', 'asbfg_custom_email_message', 'Custom Validation Message', 'Message shown when an email is blocked' ); } private function render_domain_blocking_section() { $this->asbfg_render_setting_section( 'Domain Blocking', 'Block form submissions from specific email domains.', 'asbfg_blocked_domains', 'Blocked Domains', 'Enter one domain per line (e.g., example.com)', 'asbfg_custom_domain_message', 'Custom Validation Message', 'Message shown when a domain is blocked' ); } private function render_country_blocking_section() { $this->asbfg_render_setting_section( 'Country Blocking', 'Block form submissions from specific countries using ISO country codes. Uses ip-api.com (60 requests/minute, results cached for 24 hours).', 'asbfg_blocked_countries', 'Blocked Countries', 'Enter one country code per line (e.g., US or CN or RU)', 'asbfg_custom_country_message', 'Custom Validation Message', 'Message shown when a country is blocked' ); } private function render_text_blocking_section() { $this->asbfg_render_setting_section( 'Text Content Blocking', 'Block form submissions containing specific words or phrases.', 'asbfg_blocked_text', 'Blocked Text', 'Enter one word or phrase per line', 'asbfg_custom_text_message', 'Custom Validation Message', 'Message shown when blocked text is detected' ); } private function asbfg_render_setting_section( $title, $description, $textarea_name, $textarea_label, $textarea_placeholder, $input_name, $input_label, $input_description ) { $textarea_value = implode( "\n", (array) get_option( $textarea_name, [] ) ); $input_value = esc_attr( get_option( $input_name, '' ) ); $blocked_count = count( (array) get_option( $textarea_name, [] ) ); ?>

blocked

' . esc_html__( 'Block Entry', 'addon-submission-blocker-for-gravityforms' ) . '

'; $message = isset( $_GET['message'] ) ? sanitize_text_field( wp_unslash( $_GET['message'] ) ) : ''; if ( $message === 'success' ) { echo '

' . esc_html__( 'Successfully blocked entry.', 'addon-submission-blocker-for-gravityforms' ) . '

'; } elseif ( $message === 'error' ) { echo '

' . esc_html__( 'Block type and value are required.', 'addon-submission-blocker-for-gravityforms' ) . '

'; } elseif ( $message === 'exists' ) { echo '

' . esc_html__( 'Value is already blocked.', 'addon-submission-blocker-for-gravityforms' ) . '

'; } if ( ! isset( $_GET['entry_id'] ) ) { wp_die( esc_html__( 'Entry ID not specified.', 'addon-submission-blocker-for-gravityforms' ) ); } $entry_id = absint( $_GET['entry_id'] ); $entry = GFAPI::get_entry( $entry_id ); if ( is_wp_error( $entry ) ) { wp_die( esc_html__( 'Entry not found.', 'addon-submission-blocker-for-gravityforms' ) ); } if ( isset( $_POST['asbfg_block_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['asbfg_block_nonce'] ) ), 'asbfg_block_action_' . $entry['id'] ) ) { $this->handle_block_submission( $entry ); } else { $this->display_block_options( $entry ); } echo ''; } private function handle_block_submission( $entry ) { // Verify nonce first if ( ! isset( $_POST['asbfg_block_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['asbfg_block_nonce'] ) ), 'asbfg_block_action_' . $entry['id'] ) ) { wp_die( esc_html__( 'Security check failed.', 'addon-submission-blocker-for-gravityforms' ) ); } // Validate input existence if ( ! isset( $_POST['block_type'], $_POST['value_to_block'] ) || empty( $_POST['block_type'] ) || empty( $_POST['value_to_block'] ) ) { $redirect_url = add_query_arg( [ 'message' => 'error', 'entry_id' => $entry['id'] ], admin_url( 'admin.php?page=asbfg_block_entry' ) ); wp_safe_redirect( $redirect_url ); exit; } // Sanitize inputs $block_type = sanitize_text_field( wp_unslash( $_POST['block_type'] ) ); $value_to_block = sanitize_text_field( wp_unslash( $_POST['value_to_block'] ) ); $option_name = 'asbfg_blocked_' . $block_type; $blocked_items = (array) get_option( $option_name, [] ); if ( ! in_array( $value_to_block, $blocked_items, true ) ) { $blocked_items[] = $value_to_block; update_option( $option_name, $blocked_items ); $redirect_url = add_query_arg( [ 'message' => 'success', 'entry_id' => $entry['id'] ], admin_url( 'admin.php?page=asbfg_block_entry' ) ); } else { $redirect_url = add_query_arg( [ 'message' => 'exists', 'entry_id' => $entry['id'] ], admin_url( 'admin.php?page=asbfg_block_entry' ) ); } wp_safe_redirect( $redirect_url ); exit; } private function display_block_options( $entry ) { $form = GFAPI::get_form( $entry['form_id'] ); echo '

' . esc_html__( 'Entry Details', 'addon-submission-blocker-for-gravityforms' ) . '

'; echo ''; foreach ( $form['fields'] as $field ) { $value = rgar( $entry, (string) $field->id ); if ( ! empty( $value ) ) { echo ''; } } echo ''; echo '
' . esc_html( $field->label ) . '' . esc_html( $value ) . '
' . esc_html__( 'User IP', 'addon-submission-blocker-for-gravityforms' ) . '' . esc_html( $entry['ip'] ) . '
'; echo '

' . esc_html__( 'Block Options', 'addon-submission-blocker-for-gravityforms' ) . '

'; echo '
'; wp_nonce_field( 'asbfg_block_action_' . $entry['id'], 'asbfg_block_nonce' ); echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo '
' . esc_html__( 'Value to Block', 'addon-submission-blocker-for-gravityforms' ) . '
' . esc_html__( 'Block Type', 'addon-submission-blocker-for-gravityforms' ) . ''; echo ''; echo '
'; echo '

'; echo '
'; } public function handle_export() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Unauthorized', 'addon-submission-blocker-for-gravityforms' ) ); } $settings = [ 'blocked_ips' => get_option( 'asbfg_blocked_ips', [] ), 'blocked_emails' => get_option( 'asbfg_blocked_emails', [] ), 'blocked_domains' => get_option( 'asbfg_blocked_domains', [] ), 'blocked_countries' => get_option( 'asbfg_blocked_countries', [] ), 'blocked_text' => get_option( 'asbfg_blocked_text', [] ), 'custom_ip_message' => get_option( 'asbfg_custom_ip_message', '' ), 'custom_email_message' => get_option( 'asbfg_custom_email_message', '' ), 'custom_domain_message' => get_option( 'asbfg_custom_domain_message', '' ), 'custom_country_message' => get_option( 'asbfg_custom_country_message', '' ), 'custom_text_message' => get_option( 'asbfg_custom_text_message', '' ), 'log_retention_days' => get_option( 'asbfg_log_retention_days', 90 ), ]; header( 'Content-Type: application/json' ); header( 'Content-Disposition: attachment; filename="asbfg-settings-' . gmdate( 'Y-m-d' ) . '.json"' ); echo wp_json_encode( $settings, JSON_PRETTY_PRINT ); exit; } public function handle_import() { check_admin_referer( 'asbfg_import_action', 'asbfg_import_nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Unauthorized', 'addon-submission-blocker-for-gravityforms' ) ); } if ( ! isset( $_FILES['import_file'] ) || ! isset( $_FILES['import_file']['error'] ) || $_FILES['import_file']['error'] !== UPLOAD_ERR_OK ) { wp_safe_redirect( add_query_arg( 'message', 'import_error', admin_url( 'admin.php?page=gf_settings&subview=asbfg_submission_blocker' ) ) ); exit; } if ( ! isset( $_FILES['import_file']['tmp_name'] ) ) { wp_safe_redirect( add_query_arg( 'message', 'import_error', admin_url( 'admin.php?page=gf_settings&subview=asbfg_submission_blocker' ) ) ); exit; } // Validate file size (max 1MB) if ( isset( $_FILES['import_file']['size'] ) && $_FILES['import_file']['size'] > 1048576 ) { wp_safe_redirect( add_query_arg( 'message', 'import_error', admin_url( 'admin.php?page=gf_settings&subview=asbfg_submission_blocker' ) ) ); exit; } // Validate file type if ( isset( $_FILES['import_file']['type'] ) && 'application/json' !== $_FILES['import_file']['type'] ) { // Some browsers send different MIME types, so also check extension $filename = isset( $_FILES['import_file']['name'] ) ? sanitize_file_name( wp_unslash( $_FILES['import_file']['name'] ) ) : ''; if ( ! preg_match( '/\.json$/i', $filename ) ) { wp_safe_redirect( add_query_arg( 'message', 'invalid_json', admin_url( 'admin.php?page=gf_settings&subview=asbfg_submission_blocker' ) ) ); exit; } } // Sanitize file path $tmp_name = sanitize_text_field( wp_unslash( $_FILES['import_file']['tmp_name'] ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Reading uploaded file $file_content = file_get_contents( $tmp_name ); if ( false === $file_content ) { wp_safe_redirect( add_query_arg( 'message', 'import_error', admin_url( 'admin.php?page=gf_settings&subview=asbfg_submission_blocker' ) ) ); exit; } $settings = json_decode( $file_content, true ); if ( json_last_error() !== JSON_ERROR_NONE || ! is_array( $settings ) ) { wp_safe_redirect( add_query_arg( 'message', 'invalid_json', admin_url( 'admin.php?page=gf_settings&subview=asbfg_submission_blocker' ) ) ); exit; } $option_map = [ 'blocked_ips' => 'asbfg_blocked_ips', 'blocked_emails' => 'asbfg_blocked_emails', 'blocked_domains' => 'asbfg_blocked_domains', 'blocked_countries' => 'asbfg_blocked_countries', 'blocked_text' => 'asbfg_blocked_text', 'custom_ip_message' => 'asbfg_custom_ip_message', 'custom_email_message' => 'asbfg_custom_email_message', 'custom_domain_message' => 'asbfg_custom_domain_message', 'custom_country_message'=> 'asbfg_custom_country_message', 'custom_text_message' => 'asbfg_custom_text_message', 'log_retention_days' => 'asbfg_log_retention_days', ]; foreach ( $option_map as $key => $option_name ) { if ( isset( $settings[ $key ] ) ) { // Sanitize based on type if ( strpos( $key, 'blocked_' ) === 0 ) { // Array values - sanitize each item $sanitized_value = is_array( $settings[ $key ] ) ? array_map( 'sanitize_text_field', $settings[ $key ] ) : []; update_option( $option_name, $sanitized_value ); } elseif ( 'log_retention_days' === $key ) { // Integer value update_option( $option_name, absint( $settings[ $key ] ) ); } else { // String values - sanitize text update_option( $option_name, sanitize_text_field( $settings[ $key ] ) ); } } } wp_safe_redirect( add_query_arg( 'message', 'imported', admin_url( 'admin.php?page=gf_settings&subview=asbfg_submission_blocker' ) ) ); exit; } } // Initialize admin settings. new ASBFG_Admin_Settings();