diff --git a/admin/CF7_AntiSpam_Admin_Customizations.php b/admin/CF7_AntiSpam_Admin_Customizations.php index 5f1e8da..7a51ca5 100644 --- a/admin/CF7_AntiSpam_Admin_Customizations.php +++ b/admin/CF7_AntiSpam_Admin_Customizations.php @@ -4,6 +4,8 @@ use CF7_AntiSpam\Core\CF7_AntiSpam; use CF7_AntiSpam\Core\CF7_Antispam_Geoip; +use WP_Query; + /** * The plugin settings. * @@ -459,6 +461,15 @@ public function cf7a_options_init() { 'cf7a_honeyform' ); + /* Honeyform excluded pages */ + add_settings_field( + 'honeyform_excluded_pages', + __( 'Add pages you wish shouldn\'t have a Honeyform', 'cf7-antispam' ), + array( $this, 'cf7a_honeyform_excluded_pages_callback' ), + 'cf7a-settings', + 'cf7a_honeyform' + ); + /* Identity Protection */ add_settings_section( 'cf7a_identity_protection', @@ -1091,6 +1102,7 @@ public function cf7a_sanitize_options( $input ) { /* honeyform */ $new_input['check_honeyform'] = isset( $input['check_honeyform'] ) ? 1 : 0; $new_input['honeyform_position'] = ! empty( $input['honeyform_position'] ) ? sanitize_title( $input['honeyform_position'] ) : 'wp_body_open'; + $new_input['honeyform_excluded_pages'] = ! empty( $input['honeyform_excluded_pages'] ) ? array_map('number_format', $input['honeyform_excluded_pages']) : ''; /* honeyform */ $new_input['identity_protection_user'] = isset( $input['identity_protection_user'] ) ? 1 : 0; @@ -1462,6 +1474,55 @@ public function cf7a_honeyform_position_callback() { ); } + public function cf7a_honeyform_excluded_pages_callback() { + $args = array( + 'post_type' => 'page', // change this to the post type you're querying + 'fields' => 'ids', // get all posts + ); + $query = new WP_Query($args); + + $options = ''; + + if ($query->have_posts()) { + while ($query->have_posts()) { + $query->the_post(); + $options .= ''; + } + } + + $admin_options = get_option( 'cf7a_options' ); + $excluded = $admin_options['honeyform_excluded_pages']; + $str_excluded = ''; + if ( is_array($excluded) ) { + foreach ($excluded as $entry) { + $str_excluded .= ''; + } + } + wp_reset_postdata(); + printf( + '
+
+
+ +
+
Add >
+
+
+ +
+
< Remove
+
+
+
', + $options, + $str_excluded + ); + } + /** It creates a checkbox with the id of "cf7a_identity_protection_user_callback" */ public function cf7a_identity_protection_user_callback() { printf( diff --git a/core/CF7_AntiSpam_Frontend.php b/core/CF7_AntiSpam_Frontend.php index fd00ff2..3b8bc32 100644 --- a/core/CF7_AntiSpam_Frontend.php +++ b/core/CF7_AntiSpam_Frontend.php @@ -146,13 +146,22 @@ public function cf7a_honeyform( $content ) { * @param array $option an array of pages id where cf7-antispam won't insert the honeyform. */ $excluded_ids = apply_filters( 'cf7a_honeyform_excluded_id', array() ); + $current_id = get_the_ID(); // Check if the current post ID is in the excluded IDs array - if ( in_array( get_the_ID(), $excluded_ids ) ) { + if ( in_array( $current_id, $excluded_ids ) ) { // If the current post ID is excluded, return the original content return $content; } + $cf7a_options = get_option('cf7a_options'); + if ( in_array( $current_id, $cf7a_options['honeyform_excluded_pages'] ) ) { + // If the current post ID is excluded, return the original content + return $content; + } + + + /* $html will store the honeyform html */ $html = ''; diff --git a/engine/CF7_AntiSpam_Activator.php b/engine/CF7_AntiSpam_Activator.php index eea9129..7744c8d 100644 --- a/engine/CF7_AntiSpam_Activator.php +++ b/engine/CF7_AntiSpam_Activator.php @@ -81,6 +81,7 @@ public static function init_vars() { 'bad_user_agent_list' => array(), 'dnsbl_list' => array(), 'honeypot_input_names' => array(), + 'honeyform_excluded_pages' => array(), 'languages' => array( 'allowed' => array(), 'disallowed' => array(), diff --git a/src/settings/settings.js b/src/settings/settings.js index 31ab75a..55c7606 100644 --- a/src/settings/settings.js +++ b/src/settings/settings.js @@ -77,6 +77,60 @@ window.onload = function () { AdvSettingsFormEl.classList.add( 'hidden' ); } }; + + // Honeyform page exlusion + if ( document.body.classList.contains( 'cf7-antispam-admin' ) ) { + const addListButton = document.querySelector('.add-list'); + const addSelect = document.querySelector('.add-select'); + const removeListButton = document.querySelector('.remove-list'); + const removeSelect = document.querySelector('.remove-select'); + + for (const remove of removeSelect) { + for (const add of addSelect) { + if (remove.value === add.value) { + addSelect.removeChild(add); + } + } + } + addListButton.addEventListener('click', () => { + for (const option of addSelect.options) { + if (option.selected) { + const name = option.textContent; + const value = option.value; + + if (!removeSelect.options[value]) { + const newOption = document.createElement('option'); + newOption.setAttribute('selected', true); + newOption.value = value; + newOption.textContent = name; + + removeSelect.appendChild(newOption); + } + option.remove(); + } + } + }); + + removeListButton.addEventListener('click', () => { + + for (const option of removeSelect.options) { + if (option.selected) { + const name = option.textContent; + const value = option.value; + + if (!removeSelect.options[value]) { + const newOption = document.createElement('option'); + newOption.value = value; + newOption.textContent = name; + + addSelect.appendChild(newOption); + } + option.remove(); + } + } + }); + } + /* on click show advanced options */ document .getElementById( 'enable_advanced_settings' ) diff --git a/src/settings/settings.scss b/src/settings/settings.scss index a64878b..ae1d570 100644 --- a/src/settings/settings.scss +++ b/src/settings/settings.scss @@ -88,6 +88,28 @@ $color__info: $color__blue; } } + // Style for honeyform exclude pages + .honeyform-container { + + .row { + display: flex; + flex-wrap: wrap; + + .add, + .remove { + flex: 1; + select { + width: 100%; + min-height: 120px; + } + div { + text-align: center; + border: #8c8f94 solid 1px; + } + } + } + } + .blacklist-table { display: block; max-width: 100%;