| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
|
| 4 |
/* handle field output */ |
| 5 |
function wppb_gdpr_handler( $output, $form_location, $field, $user_id, $field_check_errors, $request_data ){ |
| 6 |
if ( $field['field'] == 'GDPR Checkbox' ){ |
| 7 |
|
| 8 |
$item_title = apply_filters( 'wppb_'.$form_location.'_gdpr_custom_field_'.$field['id'].'_item_title', wppb_icl_t( 'plugin profile-builder-pro', 'custom_field_'.$field['id'].'_title_translation', $field['field-title'], true ) ); |
| 9 |
$item_description = wppb_icl_t( 'plugin profile-builder-pro', 'custom_field_'.$field['id'].'_description_translation', $field['description'], true ); |
| 10 |
|
| 11 |
if( $form_location != 'register' ) |
| 12 |
$input_value = ((wppb_user_meta_exists($user_id, $field['meta-name']) != null) ? get_user_meta($user_id, $field['meta-name'], true) : ''); |
| 13 |
else |
| 14 |
$input_value = ( isset( $request_data[wppb_handle_meta_name( $field['meta-name'] )] ) ? trim( $request_data[wppb_handle_meta_name( $field['meta-name'] )] ) : '' ); |
| 15 |
|
| 16 |
$error_mark = ( ( $field['required'] == 'Yes' ) ? '<span class="wppb-required" title="'.wppb_required_field_error($field["field-title"]).'">*</span>' : '' ); |
| 17 |
if ( array_key_exists( $field['id'], $field_check_errors ) ) |
| 18 |
$error_mark = '<img src="'.WPPB_PLUGIN_URL.'assets/images/pencil_delete.png" title="'.wppb_required_field_error($field["field-title"]).'"/>'; |
| 19 |
|
| 20 |
$extra_attr = apply_filters( 'wppb_extra_attribute', '', $field, $form_location ); |
| 21 |
|
| 22 |
if ( $form_location != 'back_end' ){ |
| 23 |
|
| 24 |
$output = ' |
| 25 |
<label for="'.$field['meta-name'].'"> |
| 26 |
<input value="agree" name="'.$field['meta-name'].'" id="'.$field['meta-name'].'" type="checkbox" class="custom_field_gdpr" '. $extra_attr .' '; |
| 27 |
|
| 28 |
if ( isset( $input_value ) && ( $input_value == 'agree' ) ) |
| 29 |
$output .= ' checked="yes"'; |
| 30 |
|
| 31 |
$output .= ' /><span>'.trim( html_entity_decode ( $item_description ) ).$error_mark.'</span></label>'; |
| 32 |
|
| 33 |
} |
| 34 |
else |
| 35 |
if($form_location == 'back_end') |
| 36 |
{ |
| 37 |
$gdpr_agreement_time=get_user_meta($user_id, 'gdpr_agreement_time',true); |
| 38 |
|
| 39 |
if($gdpr_agreement_time) |
| 40 |
{ |
| 41 |
//these works as well, they are just an alternative solution |
| 42 |
//$gdpr_formated_time= date('d.m.Y H:i',$gdpr_agreement_time); |
| 43 |
//$gdpr_formated_time=date_i18n( 'd.m.Y H:i', $gdpr_agreement_time + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ); |
| 44 |
|
| 45 |
$gdpr_format=date('Y-m-d H:i:s',$gdpr_agreement_time); |
| 46 |
$gdpr_formated_time=get_date_from_gmt($gdpr_format, $format='d.m.Y H:i'); |
| 47 |
|
| 48 |
$output='<table class="form-table"> |
| 49 |
<tr> |
| 50 |
<th>GDPR</th> |
| 51 |
<td>Agreed on '.$gdpr_formated_time.'</td> |
| 52 |
</tr> |
| 53 |
</table>'; |
| 54 |
} |
| 55 |
else{ |
| 56 |
$output='<table class="form-table"> |
| 57 |
<tr> |
| 58 |
<th>GDPR</th> |
| 59 |
<td>Not Agreed</td> |
| 60 |
</tr> |
| 61 |
</table>'; |
| 62 |
} |
| 63 |
|
| 64 |
} |
| 65 |
return apply_filters( 'wppb_'.$form_location.'_gdpr_custom_field_'.$field['id'], $output, $form_location, $field, $user_id, $field_check_errors, $request_data, $input_value ); |
| 66 |
} |
| 67 |
} |
| 68 |
add_filter( 'wppb_output_form_field_gdpr-checkbox', 'wppb_gdpr_handler', 10, 6 ); |
| 69 |
add_filter( 'wppb_admin_output_form_field_gdpr-checkbox', 'wppb_gdpr_handler', 10, 6 ); |
| 70 |
|
| 71 |
|
| 72 |
/* handle field save */ |
| 73 |
function wppb_save_gdpr_value( $field, $user_id, $request_data, $form_location ){ |
| 74 |
if( $field['field'] == 'GDPR Checkbox' ){ |
| 75 |
if ( $form_location == 'register' || $form_location == 'edit_profile' ){ |
| 76 |
if ( isset( $request_data[wppb_handle_meta_name( $field['meta-name'] )] ) ) |
| 77 |
update_user_meta( $user_id, $field['meta-name'], sanitize_text_field( $request_data[wppb_handle_meta_name( $field['meta-name'] )] ) ); |
| 78 |
//save the time when the user agreed |
| 79 |
update_user_meta( $user_id, 'gdpr_agreement_time', time() ); |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
add_action( 'wppb_save_form_field', 'wppb_save_gdpr_value', 10, 4 ); |
| 84 |
|
| 85 |
/* handle field validation */ |
| 86 |
function wppb_check_gdpr_value( $message, $field, $request_data, $form_location ){ |
| 87 |
if( $field['field'] == 'GDPR Checkbox' ){ |
| 88 |
if ( $form_location != 'back_end' ){ |
| 89 |
$checked_values = ''; |
| 90 |
|
| 91 |
if( isset( $request_data[ wppb_handle_meta_name( $field['meta-name'] ) ] ) ) { |
| 92 |
|
| 93 |
if( is_array( $request_data[ wppb_handle_meta_name( $field['meta-name'] ) ] ) ) |
| 94 |
$checked_values = implode( ',', $request_data[ wppb_handle_meta_name( $field['meta-name'] ) ] ); |
| 95 |
else |
| 96 |
$checked_values = $request_data[ wppb_handle_meta_name( $field['meta-name'] ) ]; |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
if ( ( $field['required'] == 'Yes' ) && empty( $checked_values ) ) |
| 101 |
return wppb_required_field_error($field['field-title']); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
return $message; |
| 106 |
} |
| 107 |
add_filter( 'wppb_check_form_field_gdpr-checkbox', 'wppb_check_gdpr_value', 10, 4 ); |
| 108 |
|
| 109 |
add_filter( 'wppb_field_css_class', 'wppb_gdpr_add_checkbox_class', 20, 2); |
| 110 |
function wppb_gdpr_add_checkbox_class( $classes, $field ){ |
| 111 |
if( $field['field'] == 'GDPR Checkbox' ) |
| 112 |
$classes .= ' wppb-checkbox'; |
| 113 |
|
| 114 |
return $classes; |
| 115 |
} |
| 116 |
|