Skip to content

Commit

Permalink
closes #72
Browse files Browse the repository at this point in the history
  • Loading branch information
gardenboi committed Nov 25, 2023
1 parent da6b4e7 commit 395f3ef
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 1 deletion.
61 changes: 61 additions & 0 deletions admin/CF7_AntiSpam_Admin_Customizations.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use CF7_AntiSpam\Core\CF7_AntiSpam;
use CF7_AntiSpam\Core\CF7_Antispam_Geoip;
use WP_Query;

/**
* The plugin settings.
*
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 .= '<option value="' . get_the_ID() . '">' . get_the_title() . '</option>';
}
}

$admin_options = get_option( 'cf7a_options' );
$excluded = $admin_options['honeyform_excluded_pages'];
$str_excluded = '';
if ( is_array($excluded) ) {
foreach ($excluded as $entry) {
$str_excluded .= '<option selected="true" value="' . $entry . '">' . get_the_title($entry) . '</option>';
}
}
wp_reset_postdata();
printf(
'<div class="honeyform-container">
<div class="row">
<div class="add">
<select name="add" multiple class="form-control add-select">
%s
</select>
<br/>
<div class="add-list">Add ></div>
</div>
<div class="remove">
<select id="honeyform_excluded_pages" name="cf7a_options[honeyform_excluded_pages][]" multiple="multiple" class="form-control remove-select" >
%s
</select>
<br/>
<div class="remove-list">< Remove</div>
</div>
</div>
</div>',
$options,
$str_excluded
);
}

/** It creates a checkbox with the id of "cf7a_identity_protection_user_callback" */
public function cf7a_identity_protection_user_callback() {
printf(
Expand Down
11 changes: 10 additions & 1 deletion core/CF7_AntiSpam_Frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';

Expand Down
1 change: 1 addition & 0 deletions engine/CF7_AntiSpam_Activator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
54 changes: 54 additions & 0 deletions src/settings/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' )
Expand Down
22 changes: 22 additions & 0 deletions src/settings/settings.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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%;
Expand Down

0 comments on commit 395f3ef

Please sign in to comment.