| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* @since 6.19 |
| 8 |
*/ |
| 9 |
class FrmFieldGdpr extends FrmFieldType { |
| 10 |
|
| 11 |
/** |
| 12 |
* @since 6.19 |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
protected $type = 'gdpr'; |
| 16 |
|
| 17 |
/** |
| 18 |
* @since 6.19 |
| 19 |
* @var bool |
| 20 |
*/ |
| 21 |
protected $has_for_label = false; |
| 22 |
|
| 23 |
/** |
| 24 |
* @since 6.19 |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
const VIEW_PATH = '/classes/views/frm-fields/front-end/gdpr/gdpr-field.php'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Get the new field defaults. |
| 31 |
* |
| 32 |
* @since 6.19 |
| 33 |
* @return array |
| 34 |
*/ |
| 35 |
public function get_new_field_defaults() { |
| 36 |
if ( FrmFieldGdprHelper::hide_gdpr_field() ) { |
| 37 |
return array( |
| 38 |
'name' => false, |
| 39 |
'description' => false, |
| 40 |
'type' => $this->type, |
| 41 |
'options' => false, |
| 42 |
'required' => false, |
| 43 |
'field_options' => false, |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
return array( |
| 48 |
'name' => $this->get_new_field_name() . __( ' Consent', 'formidable' ), |
| 49 |
'description' => '', |
| 50 |
'type' => $this->type, |
| 51 |
'options' => '', |
| 52 |
'required' => true, |
| 53 |
'field_options' => $this->get_default_field_options(), |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Get the field settings for the field type. |
| 59 |
* |
| 60 |
* @since 6.19 |
| 61 |
* @return bool[] |
| 62 |
*/ |
| 63 |
protected function field_settings_for_type() { |
| 64 |
if ( FrmFieldGdprHelper::hide_gdpr_field() ) { |
| 65 |
return array( |
| 66 |
'size' => false, |
| 67 |
'clear_on_focus' => false, |
| 68 |
'default' => false, |
| 69 |
'invalid' => false, |
| 70 |
'max' => false, |
| 71 |
'required' => false, |
| 72 |
'label' => false, |
| 73 |
'css' => false, |
| 74 |
'label_position' => false, |
| 75 |
'description' => false, |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
return array( |
| 80 |
'size' => true, |
| 81 |
'clear_on_focus' => false, |
| 82 |
'default' => false, |
| 83 |
'invalid' => false, |
| 84 |
'max' => false, |
| 85 |
'readonly_required' => true, |
| 86 |
'required' => true, |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Show the primary options for the field. |
| 92 |
* |
| 93 |
* @since 6.19 |
| 94 |
* @param array $args The arguments. |
| 95 |
*/ |
| 96 |
public function show_primary_options( $args ) { |
| 97 |
if ( FrmFieldGdprHelper::hide_gdpr_field() ) { |
| 98 |
return; |
| 99 |
} |
| 100 |
$field = $args['field']; |
| 101 |
include FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/gdpr/primary-options.php'; |
| 102 |
} |
| 103 |
|
| 104 |
public function show_label_on_form_builder() { |
| 105 |
if ( FrmFieldGdprHelper::hide_gdpr_field() ) { |
| 106 |
return; |
| 107 |
} |
| 108 |
parent::show_label_on_form_builder(); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Gets extra field options. |
| 113 |
* |
| 114 |
* @since 6.19 |
| 115 |
* @return string[] |
| 116 |
*/ |
| 117 |
protected function extra_field_opts() { |
| 118 |
return array( |
| 119 |
'gdpr_agreement_text' => __( 'I consent to having this website store my submitted information so they can respond to my inquiry.', 'formidable' ), |
| 120 |
); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Include the form builder file. |
| 125 |
* |
| 126 |
* @since 6.19 |
| 127 |
* @return string |
| 128 |
*/ |
| 129 |
protected function include_form_builder_file() { |
| 130 |
return FrmAppHelper::plugin_path() . self::VIEW_PATH; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Include the front form file. |
| 135 |
* |
| 136 |
* @since 6.19 |
| 137 |
* @return string |
| 138 |
*/ |
| 139 |
protected function include_front_form_file() { |
| 140 |
return FrmAppHelper::plugin_path() . self::VIEW_PATH; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Hide the field name/label if the GDPR field is disabled. |
| 145 |
* |
| 146 |
* @since 6.19 |
| 147 |
* |
| 148 |
* @param array $args The arguments. |
| 149 |
* @param string $html The HTML. |
| 150 |
* @return string |
| 151 |
*/ |
| 152 |
protected function before_replace_html_shortcodes( $args, $html ) { |
| 153 |
if ( FrmFieldGdprHelper::hide_gdpr_field() && ! current_user_can( 'frm_edit_forms' ) ) { |
| 154 |
return ''; |
| 155 |
} |
| 156 |
return $html; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Make sure the GDPR field is required even if the required setting is disabled. |
| 161 |
* |
| 162 |
* @since 6.20 |
| 163 |
* |
| 164 |
* @param array $args |
| 165 |
* @return array |
| 166 |
*/ |
| 167 |
public function validate( $args ) { |
| 168 |
$errors = parent::validate( $args ); |
| 169 |
|
| 170 |
if ( ! $errors && ! FrmFieldGdprHelper::hide_gdpr_field() ) { |
| 171 |
$required = FrmField::get_option( $this->field, 'required' ); |
| 172 |
|
| 173 |
if ( ! $required && empty( $args['value'] ) ) { |
| 174 |
$frm_settings = FrmAppHelper::get_settings(); |
| 175 |
$errors[ 'field' . $args['id'] ] = str_replace( '[field_name]', is_object( $this->field ) ? $this->field->name : $this->field['name'], $frm_settings->blank_msg ); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
return $errors; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Make sure the GDPR field is required even if the required setting is disabled. |
| 184 |
* |
| 185 |
* @since 6.20 |
| 186 |
* |
| 187 |
* @param bool $required |
| 188 |
* @param array $field |
| 189 |
* @return bool |
| 190 |
*/ |
| 191 |
public static function force_required_field( $required, $field ) { |
| 192 |
if ( ! $required && 'gdpr' === $field['type'] && ! FrmFieldGdprHelper::hide_gdpr_field() ) { |
| 193 |
$required = true; |
| 194 |
} |
| 195 |
|
| 196 |
return $required; |
| 197 |
} |
| 198 |
} |
| 199 |
|