Last active
August 3, 2018 08:27
-
-
Save 1naveengiri/f1540539f52ccdf417bf7ff9662f3a95 to your computer and use it in GitHub Desktop.
Add new consent in Awesome Support
This file contains hidden or 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 | |
add_filter('wpas_gdpr_consents', 'add_new_gdpr_consent_item', 10, 1 ); | |
/** | |
* Function to add new consent in AS settings. | |
* | |
* @param array $gdpr_consent_options GDPR consent options | |
*/ | |
function add_new_gdpr_consent_item( $gdpr_consent_options ){ | |
$consent_short_desc = wpas_get_option( 'join_mailing_list_short_note', false ); | |
$gdpr_id = wpas_get_gdpr_data( $consent_short_desc ); | |
$gdpr_consent_options[] = array( | |
'name' => __( 'Join our mailing list', 'awesome-support' ), | |
'type' => 'heading', | |
'desc' => __( 'If personal data will be used in any additional context add that here. It is best to keep this brief.', 'awesome-support' ), | |
); | |
$gdpr_consent_options[] = array( | |
'name' => __( 'GDPR Notice: Short Description', 'awesome-support' ), | |
'id' => 'join_mailing_list_short_note', | |
'type' => 'text', | |
'default' => '', | |
'desc' => __( 'If you fill this in, a mandatory checkbox will be added in the registration form. Users won\'t be able to register if they don\'t tick the checkbox. It is best to keep this brief - eg: Receive Emails? or Join Email List?', 'awesome-support' ), | |
); | |
$gdpr_consent_options[] = array( | |
'name' => __( 'Content', 'awesome-support' ), | |
'id' => 'join_mailing_list_consent', | |
'type' => 'editor', | |
'default' => '', | |
'desc' => __( 'Join our mailing list' ), | |
'settings' => array( 'quicktags' => true, 'textarea_rows' => 7 ) | |
); | |
$gdpr_consent_options[] = array( | |
'name' => __( 'Mandatory', 'awesome-support' ), | |
'id' => "join_mailing_list_medatory_check", | |
'type' => 'checkbox', | |
'default' => false, | |
'desc' => __( 'Does the user need to check this option before being able to register?', 'awesome-support' ) | |
); | |
$gdpr_consent_options[] =array( | |
'name' => __( 'Can Opt Out', 'awesome-support' ), | |
'id' => "gdpr_notice_opt_out_ok_0".$gdpr_id, | |
'type' => 'checkbox', | |
'default' => false, | |
'desc' => __( 'Is this an option that the user can opt-out from after granting consent?', 'awesome-support' ) | |
); | |
return $gdpr_consent_options; | |
} | |
// Add checkbox in registration form. | |
add_action( 'wpas_after_registration_fields', 'add_gdpr_consent_checkbox', 10, 4 ); | |
/** | |
* Add consent checkbox. | |
*/ | |
function add_gdpr_consent_checkbox(){ | |
$consent_short_desc = wpas_get_option( 'join_mailing_list_short_note', false ); | |
$gdpr_id = wpas_get_gdpr_data( $consent_short_desc ); | |
echo 'GDPR_ID: '.$gdpr_id; | |
$gdpr_short_desc = wpas_get_option( 'join_mailing_list_short_note', '' ); | |
$gdpr_long_desc = wpas_get_option( 'join_mailing_list_consent', '' ); | |
$gdpr_opt_out_ok = wpas_get_option('gdpr_notice_opt_out_ok_0'.$gdpr_id, '' ); | |
$gdpr_required = boolval( wpas_get_option( 'join_mailing_list_medatory_check', true ) ); | |
if ( ! empty( $gdpr_short_desc ) || ! empty( $gdpr_short_desc ) ) { | |
$gdpr01 = new WPAS_Custom_Field( $gdpr_id, array( | |
'name' => $gdpr_id, | |
'args' => array( | |
'required' => $gdpr_required, | |
'field_type' => 'checkbox', | |
'sanitize' => 'sanitize_text_field', | |
'options' => array( '1' => $gdpr_short_desc ), | |
'desc' => $gdpr_long_desc, | |
), | |
) ); | |
echo $gdpr01->get_output(); | |
} | |
} | |
add_action('wpas_pre_register_account', 'wpas_save_conset_afterregistration', 10, 3); | |
/** | |
* Validate mandatory check for consent | |
* | |
* @param array $user user data array | |
* @param string $redirect_to redirection url | |
* @param array $data consent data array | |
* @return [type] [description] | |
*/ | |
function wpas_save_conset_afterregistration( $user, $redirect_to, $data ){ | |
$consent_short_desc = wpas_get_option( 'join_mailing_list_short_note', false ); | |
$gdpr_id = wpas_get_gdpr_data( $consent_short_desc ); | |
if ( wpas_get_option( 'join_mailing_list_short_note', false ) && wpas_get_option( 'join_mailing_list_medatory_check', true) && !isset( $data['wpas_'.$gdpr_id] ) ) { | |
wpas_add_error( 'accept_gdpr06_conditions', sprintf( __( 'You must check the <b>%s</b> box in order to register a support account on this site.', 'awesome-support' ), esc_html( wpas_get_option( 'join_mailing_list_short_note', false ) ) ) ); | |
wp_safe_redirect( $redirect_to ); | |
exit; | |
} | |
} | |
add_action('wpas_register_account_after', 'wpas_save_conset_onregistration', 10, 3); | |
/** | |
* Add consent after successful registration. | |
* | |
* @param int $user_id user id | |
* @param array $user user data array | |
* @param array $data consent data array | |
* @return [type] [description] | |
*/ | |
function wpas_save_conset_onregistration( $user_id, $user, $data ){ | |
$consent_short_desc = wpas_get_option( 'join_mailing_list_short_note', false ); | |
$gdpr_id = wpas_get_gdpr_data( $consent_short_desc ); | |
/** | |
* Record GDPR 4 consent | |
*/ | |
if ( wpas_get_option( 'join_mailing_list_short_note', false ) ) { | |
$status = isset( $data['wpas_'.$gdpr_id] ) ? isset( $data['wpas_'.$gdpr_id] ) : ""; | |
$opt_in = ! empty ( $status ) ? strtotime( 'NOW' ) : ""; | |
$opt_out = empty ( $opt_in ) ? strtotime( 'NOW' ) : ""; | |
$args = array( | |
'item' => wpas_get_option( 'join_mailing_list_short_note', false ), | |
'status' => $status, | |
'opt_in' => $opt_in, | |
'opt_out' => '', | |
'is_tor' => false | |
); | |
wpas_track_consent( $args, $user_id ); | |
} | |
} | |
// GDPR consent ID | |
add_filter( 'gdpr_consent_data_id', 'add_gdpr_consent_data_id', 10, 2 ); | |
/** | |
* Function to set new consent ID. | |
* | |
* @param int $return_id consent id | |
* @param string $short_description consent short description value. | |
*/ | |
function add_gdpr_consent_data_id( $return_id, $short_description ){ | |
if( $short_description === wpas_get_option( 'join_mailing_list_short_note', false ) ) { | |
$return_id = 'join_mailinglist'; | |
} | |
return $return_id; | |
} | |
// backend tool bulk option. | |
add_filter( 'gdpr_consent_list_array', 'add_gdpr_consent_list_array_item', 10, 1 ); | |
/** | |
* Function to add new gdpr consent in tool bulk option in Awesome support setting. | |
* | |
* @param array $consent_array consent array. | |
*/ | |
function add_gdpr_consent_list_array_item( $consent_array ){ | |
$consent_array[] = 'join_mailing_list_short_note'; | |
return $consent_array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment