Skip to content

Commit

Permalink
Import/export function ui
Browse files Browse the repository at this point in the history
  • Loading branch information
erikyo committed Jan 27, 2024
1 parent 4f17497 commit 3627e63
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 22 deletions.
49 changes: 30 additions & 19 deletions admin/CF7_AntiSpam_Admin_Customizations.php
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,13 @@ public function cf7a_print_user_agent() {
public function cf7a_print_dnsbl() {
printf( '<p>%s</p>', esc_html__( 'Check sender ip on DNS Blacklists, DNSBL are real-time lists of proven/recognised spam addresses. These may include lists of addresses of zombie computers or other machines used to send spam, Internet Service Providers (ISPs) that voluntarily host spammers, BUT they could also be users behind a proxy and that is why the method is no longer 100 per cent reliable. Add a DSNBL server url each line ', 'cf7-antispam' ) );
/** Translators: %s%s%s - a spam score of xyz will be added */
printf( '<p><span class="cf7a-option-notice">%s%s%s</span></p>', esc_html__( '⚠️ Use the servers you trust as safe, and consider that for each server reporting the ip a spam score of ', 'cf7-antispam' ), floatval( $this->options['score']['_dnsbl'] ), esc_html__( ' will be added.', 'cf7-antispam' ) );
printf( '<p><span class="cf7a-option-notice">%s%s%s</span></p>', esc_html__( '⚠️ Use FEW servers, those you tested reliable, and consider that for each server reporting the ip a spam score of ', 'cf7-antispam' ), floatval( $this->options['score']['_dnsbl'] ), esc_html__( ' will be added to the spam rating, 1 equal spam.', 'cf7-antispam' ) );
printf(
'<p>%s<a href="%s" target="_blank">%s</a></p>',
esc_html__( 'Here a you can find a list of servers: ', 'cf7-antispam' ),
esc_url( 'https://gist.github.com/search?q=dnsbl+list&ref=searchresults' ),
esc_url_raw( 'gist.github.com/search?q=dnsbl+list' )
);
}

/** It prints the honeypot info text */
Expand Down Expand Up @@ -1049,24 +1055,29 @@ private function cf7a_clean_recursive( $json_data ) {
*/
public function cf7a_sanitize_options( $input ) {
/* get the import options */
$new_input = $this->options;
$import_data = isset( $_POST['to-import'] ) ? sanitize_text_field( $_POST['to-import'] ) : false;
if ( ! empty( $import_data ) ) {
$json_data = json_decode( wp_unslash( $import_data ) );
$input = $this->cf7a_clean_recursive( $json_data );
// monkey pathing arrays that needs to be imploded
$input['bad_ip_list'] = implode( ',', $input['bad_ip_list'] );
$input['ip_whitelist'] = implode( ',', $input['ip_whitelist'] );
$input['bad_email_strings_list'] = implode( ',', $input['bad_email_strings_list'] );
$input['bad_user_agent_list'] = implode( ',', $input['bad_user_agent_list'] );
$input['dnsbl_list'] = implode( ',', $input['dnsbl_list'] );
$input['honeypot_input_names'] = implode( ',', $input['honeypot_input_names'] );
$input['bad_words_list'] = implode( ',', $input['bad_words_list'] );
$input['languages_locales']['allowed'] = implode( ',', $input['languages_locales']['allowed'] );
$input['languages_locales']['disallowed'] = implode( ',', $input['languages_locales']['disallowed'] );
$input['cf7a_enabled'] = 1;
$input['cf7a_enable'] = 1;
$input['cf7a_version'] = CF7ANTISPAM_VERSION;
$new_input = $this->options;
if ( isset( $_POST['to-import'] ) ) {
$json_data = json_decode( stripslashes( $_POST['to-import'] ) );
if ( ! empty( $json_data ) && is_object( $json_data ) ) {
$input = $this->cf7a_clean_recursive( $json_data );
// monkey pathing arrays that needs to be imploded
$input['bad_ip_list'] = implode( ',', $input['bad_ip_list'] );
$input['ip_whitelist'] = implode( ',', $input['ip_whitelist'] );
$input['bad_email_strings_list'] = implode( ',', $input['bad_email_strings_list'] );
$input['bad_user_agent_list'] = implode( ',', $input['bad_user_agent_list'] );
$input['dnsbl_list'] = implode( ',', $input['dnsbl_list'] );
$input['honeypot_input_names'] = implode( ',', $input['honeypot_input_names'] );
$input['bad_words_list'] = implode( ',', $input['bad_words_list'] );
$input['languages_locales']['allowed'] = implode( ',', $input['languages_locales']['allowed'] );
$input['languages_locales']['disallowed'] = implode( ',', $input['languages_locales']['disallowed'] );
$input['cf7a_enabled'] = 1;
$input['cf7a_enable'] = 1;
$input['cf7a_version'] = CF7ANTISPAM_VERSION;
} else {
cf7a_log( print_r( $_POST['to-import'], true ) );
cf7a_log( 'CF7 AntiSpam: The import data is invalid' );
return $this->options;
}
}

$new_input['cf7a_enabled'] = isset( $input['cf7a_enabled'] ) ? 1 : 0;
Expand Down
23 changes: 20 additions & 3 deletions src/settings/importExport.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
const loader = () => {
// create an element to show loading with a svg loader
const i = document.createElement('div');
i.innerHTML = `<svg viewBox="0 0 50 50" class="circular-loader">
<circle cx="25" cy="25" r="20" fill="none" stroke-linecap="round" stroke="#222" stroke-width="6" stroke-dasharray="140,250" stroke-dashoffset="360" >
<animateTransform attributeType="xml" attributeName="transform" type="rotate" from="0 25 25" to="360 25 25" dur="3s" additive="sum" repeatCount="indefinite" />
</circle></svg>`;
i.className = 'cf7a-loader';
return i;
};

window.onload = function () {
// Example for download button
document
Expand Down Expand Up @@ -38,12 +49,19 @@ function importExportOptions(e) {
alert('Invalid JSON. Please check your file and try again.');
return;
}

/**
* Get the submit form data and append the cf7-ntispam options to the form data options
* @type {FormData}
*/
const data = new FormData(e.target);
data.append('to-import', encodeURIComponent(JSON.stringify(cf7aOptions)));
data.append('to-import', JSON.stringify(cf7aOptions));

// append after the form data options a spinning loader
const loaderElement = loader();
e.target
.querySelector('#cf7a_import_button')
.insertAdjacentElement('afterend', loaderElement);

// Make an AJAX request to save the merged options
fetch(e.target.getAttribute('action'), {
Expand All @@ -53,8 +71,6 @@ function importExportOptions(e) {
.then((response) => response)
.then((response) => {
// Handle the response
// eslint-disable-next-line no-console
console.log(response);
if (response.status === 200) {
// emulate the php non async behavior
window.location.href = response.url;
Expand All @@ -64,6 +80,7 @@ function importExportOptions(e) {
// Handle the error
// eslint-disable-next-line no-console
console.error(error);
loaderElement.innerHTML = null;
});
}

Expand Down
10 changes: 10 additions & 0 deletions src/settings/settings.scss
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ $color__info: $color__blue;
max-width: 900px;
}

.cf7a-loader {
display: inline-block;
height: 25px;
vertical-align: middle;
width: 25px;
margin: 3px 12px;
opacity: 0.5;
}

form {
// input style
h2 {
Expand Down Expand Up @@ -142,6 +151,7 @@ $color__info: $color__blue;
.cf7a-option-notice {
padding: 4px 8px;
border: 2px solid $color__yellow;
display: block;
border-radius: 8px;
}

Expand Down

0 comments on commit 3627e63

Please sign in to comment.