Last active
April 19, 2018 16:57
-
-
Save quimo/d6efe02d6a414add2ce2a1cb12832368 to your computer and use it in GitHub Desktop.
Rendere i commenti WordPress GDPR compliance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* aggiungo il checkbox privacy in coda di campi di default del form dei commenti | |
* https://www.smashingmagazine.com/2012/05/adding-custom-fields-in-wordpress-comment-form/ | |
* https://codex.wordpress.org/Function_Reference/comment_form | |
*/ | |
add_action( 'comment_form_logged_in_after', 'comment_form_additional_fields' ); | |
add_action( 'comment_form_after_fields', 'comment_form_additional_fields' ); | |
function comment_form_additional_fields() { | |
$commenter = wp_get_current_commenter(); | |
$req = get_option( 'require_name_email' ); | |
$aria_req = ( $req ? " aria-required='true'" : '' ); | |
echo '<p class="privacy-container"><input id="privacy" name="privacy" type="checkbox"' . $aria_req . ' value="si"><small> <a href="' . get_theme_mod('cabi_privacy') . '">' . __('I have read the privacy policy</a> and I accept the treatment of my personal data in compliance with the Italian Legislative Decree 196/2003.') . '</small></p>'; | |
} | |
//salvo il valore del campo privacy come meta del commento | |
add_action( 'comment_post', 'save_comment_form_additional_fields' ); | |
function save_comment_form_additional_fields($comment_id) { | |
if (isset($_POST['privacy']) && $_POST['privacy'] == 'si') { | |
$privacy = wp_filter_nohtml_kses($_POST['privacy']); | |
add_comment_meta($comment_id, 'privacy', $privacy); | |
} | |
} | |
//impedisco il salvataggio del commento se non è stato flaggato il checkbox privacy | |
//questa funzione è un fallback se non è attivo javascript | |
add_filter( 'preprocess_comment', 'verify_comment_form_data' ); | |
function verify_comment_form_data($commentdata) { | |
if (!isset($_POST['privacy']) || $_POST['privacy'] != 'si') | |
wp_die( __( '<strong>Error</strong>: You have to accept the privacy policy to proceed. Hit the <em>Back</em> button on your Web browser and resubmit your comment with privacy policy flag checked.' ) ); | |
return $commentdata; | |
} | |
//impedisco l'invio del form dei commenti se non si flagga la checkbox privacy | |
add_action('wp_footer','validate_privacy_comment_form_javascript'); | |
function validate_privacy_comment_form_javascript(){ | |
if (is_single() && comments_open()){ | |
?> | |
<script type="text/javascript"> | |
jQuery(document).ready(function($){ | |
$(".comment-form #submit").on('click', function(e){ | |
if (!$('#privacy').is(':checked')){ | |
e.preventDefault(); | |
return false; | |
} | |
}); | |
}); | |
</script> | |
<?php | |
} | |
} | |
//aggiugo un metabox al template dei commenti nel backend | |
add_action( 'add_meta_boxes_comment', 'extend_comment_add_meta_box' ); | |
function extend_comment_add_meta_box() { | |
add_meta_box( 'comment_meta_box', __( 'Additional fields' ), 'extend_comment_meta_box', 'comment', 'normal', 'high' ); | |
} | |
//recupero i dati e li mostro nel metabox | |
function extend_comment_meta_box($comment) { | |
$privacy = get_comment_meta( $comment->comment_ID, 'privacy', true ); | |
//aggiungo al metabox un campo nascosto con valore random | |
wp_nonce_field( 'extend_comment_update', 'extend_comment_update', false ); | |
?> | |
<p> | |
<?php _e('Agreed with privacy policy') ?> | |
<input type="checkbox" id="privacy" name="privacy" <?php if ($privacy && $privacy == 'si') echo "checked=\"checked\" value=\"{$privacy}\"" ?>> | |
</p> | |
<?php | |
} | |
//salvo i dati aggiuntivi del metabox | |
add_action( 'edit_comment', 'save_extended_comment_data' ); | |
function save_extended_comment_data($comment_id) { | |
//se non ricevo il campo nascosto di sicurezza non eseguo l'aggiornamento | |
if(!isset($_POST['extend_comment_update']) || !wp_verify_nonce($_POST['extend_comment_update'], 'extend_comment_update')) return; | |
if (isset($_POST['privacy']) && $_POST['privacy'] == 'on') { | |
//$privacy = wp_filter_nohtml_kses($_POST['privacy']); | |
update_comment_meta( $comment_id, 'privacy', 'si'); | |
} else { | |
update_comment_meta( $comment_id, 'privacy', 'no'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment