Skip to content

Commit

Permalink
adds a box that enables the import and export of the plugin options wip
Browse files Browse the repository at this point in the history
  • Loading branch information
erikyo committed Jan 23, 2024
1 parent 833f2ed commit 69dd99c
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions admin/CF7_AntiSpam_Admin_Display.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ public function cf7a_get_debug_info() {
);
}

$this->cf7a_export_options();

/* output the options */
$this->cf7a_get_debug_info_options();

Expand Down Expand Up @@ -330,6 +332,80 @@ private function cf7a_get_debug_info_options() {
);
}

private function cf7a_export_options() {
printf( '<hr/><h3>%s</h3>', esc_html__( 'Export/Import Options', 'cf7-antispam' ) );
?>

<form method="post" action="">
<input type="hidden" name="cf7a_action" value="import">
<input type="hidden" name="nonce" value="<?php echo wp_create_nonce( 'cf7a-nonce' ); ?>">

<!-- Form field -->
<label for="export_options"><?php esc_html__( 'Copy or paste here the settings to import it or export it', 'cf7-antispam' ); ?></label>
<textarea id="cf7a_options_area" name="export_options" rows="5"><?php echo wp_json_encode( $this->options, JSON_PRETTY_PRINT ); ?></textarea>

<!-- buttons -->
<div class="cf7a_buttons cf7a_buttons_export_import">
<button type="submit" id="cf7a_import_button">Import</button>
<button type="button" id="cf7a_download_button">Download</button>
</div>
</form>

<script>
document.addEventListener('DOMContentLoaded', function () {

// Example for download button
document.getElementById('cf7a_download_button').addEventListener('click', function () {
const optionsContent = document.getElementById('cf7a_options_area').value;
const blob = new Blob([optionsContent], { type: 'application/json' });
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = 'cf7a-' + (new Date().getTime() / 1000) + '.json';
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
alert('Your file has downloaded!');
});

// Example for import button
document.getElementById('cf7a_import_button').addEventListener('click', function () {
const confirmImport = confirm('Are you sure you want to import options? This will overwrite your current settings.');
if (confirmImport) {
const optionsContent = document.getElementById('cf7a_options_area').value;
let cf7aOptions = null;
try {
cf7aOptions = JSON.parse(optionsContent);
} catch (e) {
alert('Invalid JSON. Please check your file and try again.');
}
if (cf7aOptions) {
const postData = new FormData();
postData.append('cf7a', cf7aOptions);

// Make an AJAX request to save the merged options
fetch('.', {
method: 'POST',
body: postData
})
.then(response => response.json())
.then(data => {
// Handle the response
console.log(data);
})
.catch(error => {
// Handle the error
console.error(error);
});
}
}
});
});
</script>
<?php
}

/**
* It checks if the GeoIP database is enabled, and if so, it checks the next update date and displays it
*/
Expand Down

0 comments on commit 69dd99c

Please sign in to comment.