| 1 |
<?php |
| 2 |
namespace HelloPlus\Modules\Forms\Classes; |
| 3 |
|
| 4 |
use HelloPlus\Includes\Utils; |
| 5 |
use HelloPlus\Modules\Forms\Components\Ajax_Handler; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; // Exit if accessed directly |
| 9 |
} |
| 10 |
|
| 11 |
class Form_Record { |
| 12 |
protected $sent_data; |
| 13 |
protected $fields; |
| 14 |
protected $form_type; |
| 15 |
protected $form_settings; |
| 16 |
protected $files = []; |
| 17 |
protected $meta = []; |
| 18 |
|
| 19 |
public function get_formatted_data( $with_meta = false ): array { |
| 20 |
$formatted = []; |
| 21 |
$no_label = esc_html__( 'No Label', 'hello-plus' ); |
| 22 |
$fields = $this->fields; |
| 23 |
|
| 24 |
if ( $with_meta ) { |
| 25 |
$fields = array_merge( $fields, $this->meta ); |
| 26 |
} |
| 27 |
|
| 28 |
foreach ( $fields as $key => $field ) { |
| 29 |
if ( empty( $field['title'] ) ) { |
| 30 |
$formatted[ $no_label . ' ' . $key ] = $field['value']; |
| 31 |
} else { |
| 32 |
$formatted[ $field['title'] ] = $field['value']; |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
return $formatted; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* @param Ajax_Handler $ajax_handler An instance of the ajax handler. |
| 41 |
* |
| 42 |
* @return bool |
| 43 |
*/ |
| 44 |
public function validate( Ajax_Handler $ajax_handler ): bool { |
| 45 |
foreach ( $this->fields as $id => $field ) { |
| 46 |
$field_type = $field['type']; |
| 47 |
if ( ! empty( $field['required'] ) && '' === $field['value'] && 'upload' !== $field_type ) { |
| 48 |
$ajax_handler->add_error( $id, Ajax_Handler::get_default_message( Ajax_Handler::FIELD_REQUIRED, $this->form_settings ) ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Hello+ form field validation. |
| 53 |
* |
| 54 |
* Fires when a single form field is being validated. This hook allows developers |
| 55 |
* to validate individual field types. |
| 56 |
* |
| 57 |
* The dynamic portion of the hook name, `$field_type`, refers to the field type. |
| 58 |
* |
| 59 |
* @since 1.0.0 |
| 60 |
* |
| 61 |
* @param array $field Form field. |
| 62 |
* @param Form_Record $this An instance of the form record. |
| 63 |
* @param Ajax_Handler $ajax_handler An instance of the ajax handler. |
| 64 |
*/ |
| 65 |
do_action( "hello_plus/forms/validation/{$field_type}", $field, $this, $ajax_handler ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Hello+ form validation. |
| 70 |
* |
| 71 |
* Fires when form fields are being validated. This hook allows developers |
| 72 |
* to validate all form fields. |
| 73 |
* |
| 74 |
* @since 1.0.0 |
| 75 |
* |
| 76 |
* @param Form_Record $this An instance of the form record. |
| 77 |
* @param Ajax_Handler $ajax_handler An instance of the ajax handler. |
| 78 |
*/ |
| 79 |
do_action( 'hello_plus/forms/validation', $this, $ajax_handler ); |
| 80 |
|
| 81 |
return empty( $ajax_handler->errors ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* @param Ajax_Handler $ajax_handler An instance of the ajax handler. |
| 86 |
* |
| 87 |
*/ |
| 88 |
public function process_fields( Ajax_Handler $ajax_handler ) { |
| 89 |
foreach ( $this->fields as $id => $field ) { |
| 90 |
$field_type = $field['type']; |
| 91 |
|
| 92 |
/** |
| 93 |
* Hello+ form field process. |
| 94 |
* |
| 95 |
* Fires when a single form field is being processed. This hook allows developers |
| 96 |
* to process individual field types. |
| 97 |
* |
| 98 |
* The dynamic portion of the hook name, `$field_type`, refers to the field type. |
| 99 |
* |
| 100 |
* @since 1.0.0 |
| 101 |
* |
| 102 |
* @param array $field Form field. |
| 103 |
* @param Form_Record $this An instance of the form record. |
| 104 |
* @param Ajax_Handler $ajax_handler An instance of the ajax handler. |
| 105 |
*/ |
| 106 |
do_action( "hello_plus/forms/process/{$field_type}", $field, $this, $ajax_handler ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Hello+ form process. |
| 111 |
* |
| 112 |
* Fires when form fields are being processed. This hook allows developers |
| 113 |
* to process all form fields. |
| 114 |
* |
| 115 |
* @since 1.0.0 |
| 116 |
* |
| 117 |
* @param Form_Record $this An instance of the form record. |
| 118 |
* @param Ajax_Handler $ajax_handler An instance of the ajax handler. |
| 119 |
*/ |
| 120 |
do_action( 'hello_plus/forms/process', $this, $ajax_handler ); |
| 121 |
} |
| 122 |
|
| 123 |
public function get( $property ) { |
| 124 |
if ( isset( $this->{$property} ) ) { |
| 125 |
return $this->{$property}; |
| 126 |
} |
| 127 |
|
| 128 |
return null; |
| 129 |
} |
| 130 |
|
| 131 |
public function set( $property, $value ) { |
| 132 |
$this->{$property} = $value; |
| 133 |
} |
| 134 |
|
| 135 |
public function get_form_settings( $setting ) { |
| 136 |
if ( isset( $this->form_settings[ $setting ] ) ) { |
| 137 |
$setting_value = $this->form_settings[ $setting ]; |
| 138 |
if ( 'submit_actions' === $setting ) { |
| 139 |
$setting_value = [ $setting_value ]; |
| 140 |
} |
| 141 |
|
| 142 |
return $setting_value; |
| 143 |
} |
| 144 |
|
| 145 |
return []; |
| 146 |
} |
| 147 |
|
| 148 |
public function get_field( $args ) { |
| 149 |
return wp_list_filter( $this->fields, $args ); |
| 150 |
} |
| 151 |
|
| 152 |
public function remove_field( $id ) { |
| 153 |
unset( $this->fields[ $id ] ); |
| 154 |
} |
| 155 |
|
| 156 |
public function update_field( $field_id, $property, $value ) { |
| 157 |
if ( ! isset( $this->fields[ $field_id ] ) || ! isset( $this->fields[ $field_id ][ $property ] ) ) { |
| 158 |
return; |
| 159 |
} |
| 160 |
$this->fields[ $field_id ][ $property ] = $value; |
| 161 |
} |
| 162 |
|
| 163 |
public function get_form_meta( $meta_keys = [] ) { |
| 164 |
|
| 165 |
$result = []; |
| 166 |
|
| 167 |
foreach ( $meta_keys as $metadata_type ) { |
| 168 |
switch ( $metadata_type ) { |
| 169 |
case 'date': |
| 170 |
$result['date'] = [ |
| 171 |
'title' => esc_html__( 'Date', 'hello-plus' ), |
| 172 |
'value' => date_i18n( get_option( 'date_format' ) ), |
| 173 |
]; |
| 174 |
break; |
| 175 |
|
| 176 |
case 'time': |
| 177 |
$result['time'] = [ |
| 178 |
'title' => esc_html__( 'Time', 'hello-plus' ), |
| 179 |
'value' => date_i18n( get_option( 'time_format' ) ), |
| 180 |
]; |
| 181 |
break; |
| 182 |
|
| 183 |
case 'page_url': |
| 184 |
$referrer = filter_input( INPUT_POST, 'referrer', FILTER_SANITIZE_URL ); |
| 185 |
$result['page_url'] = [ |
| 186 |
'title' => esc_html__( 'Page URL', 'hello-plus' ), |
| 187 |
'value' => $referrer ? esc_url_raw( wp_unslash( $referrer ) ) : '', |
| 188 |
]; |
| 189 |
break; |
| 190 |
|
| 191 |
case 'page_title': |
| 192 |
$referrer_title = filter_input( INPUT_POST, 'referer_title', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); |
| 193 |
$result['page_title'] = [ |
| 194 |
'title' => esc_html__( 'Page Title', 'hello-plus' ), |
| 195 |
'value' => $referrer_title ? sanitize_text_field( wp_unslash( $referrer_title ) ) : '', |
| 196 |
]; |
| 197 |
break; |
| 198 |
|
| 199 |
case 'user_agent': |
| 200 |
$user_agent = filter_input( INPUT_SERVER, 'HTTP_USER_AGENT', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); |
| 201 |
$result['user_agent'] = [ |
| 202 |
'title' => esc_html__( 'User Agent', 'hello-plus' ), |
| 203 |
'value' => $user_agent ? sanitize_textarea_field( wp_unslash( $user_agent ) ) : '', |
| 204 |
]; |
| 205 |
break; |
| 206 |
|
| 207 |
case 'remote_ip': |
| 208 |
$result['remote_ip'] = [ |
| 209 |
'title' => esc_html__( 'Remote IP', 'hello-plus' ), |
| 210 |
'value' => Utils::get_client_ip(), |
| 211 |
]; |
| 212 |
break; |
| 213 |
case 'credit': |
| 214 |
$result['credit'] = [ |
| 215 |
'title' => esc_html__( 'Powered by', 'hello-plus' ), |
| 216 |
'value' => esc_html__( 'Elementor', 'hello-plus' ), |
| 217 |
]; |
| 218 |
break; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
return $result; |
| 223 |
} |
| 224 |
|
| 225 |
private function set_meta() { |
| 226 |
$form_metadata = $this->form_settings['form_metadata']; |
| 227 |
|
| 228 |
if ( empty( $form_metadata ) ) { |
| 229 |
return; |
| 230 |
} |
| 231 |
|
| 232 |
$this->meta = $this->get_form_meta( $form_metadata ); |
| 233 |
} |
| 234 |
|
| 235 |
private function set_fields() { |
| 236 |
foreach ( $this->form_settings['form_fields'] as $form_field ) { |
| 237 |
$field = [ |
| 238 |
'id' => $form_field['custom_id'], |
| 239 |
'type' => $form_field['field_type'], |
| 240 |
'title' => $form_field['field_label'], |
| 241 |
'value' => '', |
| 242 |
'raw_value' => '', |
| 243 |
'required' => ! empty( $form_field['required'] ), |
| 244 |
]; |
| 245 |
|
| 246 |
if ( 'upload' === $field['type'] ) { |
| 247 |
$field['file_sizes'] = $form_field['file_sizes'] ?? ''; |
| 248 |
$field['file_types'] = $form_field['file_types'] ?? ''; |
| 249 |
$field['max_files'] = $form_field['max_files'] ?? ''; |
| 250 |
$field['attachment_type'] = $form_field['attachment_type'] ?? ''; |
| 251 |
} |
| 252 |
|
| 253 |
if ( isset( $this->sent_data[ $form_field['custom_id'] ] ) ) { |
| 254 |
$field['raw_value'] = $this->sent_data[ $form_field['custom_id'] ]; |
| 255 |
|
| 256 |
$value = $field['raw_value']; |
| 257 |
|
| 258 |
if ( is_array( $value ) ) { |
| 259 |
$value = implode( ', ', $value ); |
| 260 |
} |
| 261 |
|
| 262 |
$field['value'] = $this->sanitize_field( $field, $value ); |
| 263 |
} |
| 264 |
$this->fields[ $form_field['custom_id'] ] = $field; |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
private function sanitize_field( $field, $value ) { |
| 269 |
$field_type = $field['type']; |
| 270 |
switch ( $field_type ) { |
| 271 |
case 'text': |
| 272 |
case 'password': |
| 273 |
case 'hidden': |
| 274 |
case 'search': |
| 275 |
case 'checkbox': |
| 276 |
case 'radio': |
| 277 |
case 'select': |
| 278 |
$value = sanitize_text_field( $value ); |
| 279 |
break; |
| 280 |
case 'url': |
| 281 |
$value = esc_url_raw( $value ); |
| 282 |
break; |
| 283 |
case 'textarea': |
| 284 |
$value = sanitize_textarea_field( $value ); |
| 285 |
break; |
| 286 |
case 'email': |
| 287 |
$value = sanitize_email( $value ); |
| 288 |
break; |
| 289 |
default: |
| 290 |
/** |
| 291 |
* Sanitize field value. |
| 292 |
* |
| 293 |
* Filters the value of the form field for sanitization purpose. This hook allows |
| 294 |
* developers to add custom sanitization for field values. |
| 295 |
* |
| 296 |
* The dynamic portion of the hook name, `$field_type`, refers to the field type. |
| 297 |
* |
| 298 |
* @since 1.0.0 |
| 299 |
* |
| 300 |
* @param string $value The field value. |
| 301 |
* @param array $field The field array. |
| 302 |
*/ |
| 303 |
$value = apply_filters( "hello_plus/forms/sanitize/{$field_type}", $value, $field ); |
| 304 |
} |
| 305 |
|
| 306 |
return $value; |
| 307 |
} |
| 308 |
|
| 309 |
public function replace_setting_shortcodes( $setting, $urlencode = false ) { |
| 310 |
// Shortcode can be `[field id="fds21fd"]` or `[field title="Email" id="fds21fd"]`, multiple shortcodes are allowed |
| 311 |
return preg_replace_callback( '/(\[field[^]]*id="(\w+)"[^]]*\])/', function ( $matches ) use ( $urlencode ) { |
| 312 |
$value = ''; |
| 313 |
|
| 314 |
if ( isset( $this->fields[ $matches[2] ] ) ) { |
| 315 |
$value = $this->fields[ $matches[2] ]['value']; |
| 316 |
} |
| 317 |
|
| 318 |
if ( $urlencode ) { |
| 319 |
$value = rawurlencode( $value ); |
| 320 |
} |
| 321 |
return $value; |
| 322 |
}, $setting ); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
public function has_field_type( $type ) { |
| 327 |
foreach ( $this->fields as $id => $field ) { |
| 328 |
if ( $type === $field['field_type'] ) { |
| 329 |
return true; |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
return false; |
| 334 |
} |
| 335 |
|
| 336 |
public function __construct( $sent_data, $form ) { |
| 337 |
$this->form_type = $form['widgetType']; |
| 338 |
$this->form_settings = $form['settings']; |
| 339 |
$this->sent_data = stripslashes_deep( $sent_data ); |
| 340 |
|
| 341 |
$this->set_fields(); |
| 342 |
$this->set_meta(); |
| 343 |
} |
| 344 |
} |
| 345 |
|