| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; // Exit if accessed directly |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Handle frontend form submissions |
| 9 |
* |
| 10 |
* @class PH_Form_Handler |
| 11 |
* @version 1.0.0 |
| 12 |
* @package PropertyHive/Classes/ |
| 13 |
* @category Class |
| 14 |
* @author PropertyHive |
| 15 |
*/ |
| 16 |
class PH_Form_Handler { |
| 17 |
|
| 18 |
/** Normalize only database-backed form markup, before trusted PHP filters. */ |
| 19 |
public static function sanitize_saved_search_form_fields( $fields ) { |
| 20 |
foreach ( $fields as $field_id => $field ) { |
| 21 |
if ( ! is_array( $field ) ) { |
| 22 |
unset( $fields[$field_id] ); |
| 23 |
continue; |
| 24 |
} |
| 25 |
foreach ( array( 'label', 'before', 'after' ) as $key ) { |
| 26 |
if ( isset( $field[$key] ) ) { |
| 27 |
$fields[$field_id][$key] = is_string( $field[$key] ) ? wp_kses_post( $field[$key] ) : ''; |
| 28 |
} |
| 29 |
} |
| 30 |
} |
| 31 |
return $fields; |
| 32 |
} |
| 33 |
|
| 34 |
public function __construct() { |
| 35 |
|
| 36 |
add_action( 'init', array( $this, 'add_captcha_to_forms' ) ); |
| 37 |
|
| 38 |
$current_settings = get_option( 'propertyhive_template_assistant', array() ); |
| 39 |
|
| 40 |
if ( isset($current_settings['search_forms']) && !empty($current_settings['search_forms']) ) |
| 41 |
{ |
| 42 |
foreach ( $current_settings['search_forms'] as $id => $form ) |
| 43 |
{ |
| 44 |
add_filter( 'propertyhive_search_form_fields_' . $id, function($fields) |
| 45 |
{ |
| 46 |
$form_id = str_replace( "propertyhive_search_form_fields_", "", current_filter() ); |
| 47 |
|
| 48 |
$current_settings = get_option( 'propertyhive_template_assistant', array() ); |
| 49 |
|
| 50 |
$new_fields = ( |
| 51 |
( |
| 52 |
isset($current_settings['search_forms'][$form_id]['active_fields']) |
| 53 |
&& |
| 54 |
is_array($current_settings['search_forms'][$form_id]['active_fields']) && !empty($current_settings['search_forms'][$form_id]['active_fields']) |
| 55 |
) ? |
| 56 |
self::sanitize_saved_search_form_fields( $current_settings['search_forms'][$form_id]['active_fields'] ) : |
| 57 |
$fields |
| 58 |
); |
| 59 |
|
| 60 |
// Remove any fields that are in the $fields array but not active in active_fields, excluding hidden fields |
| 61 |
$hidden_fields = array(); |
| 62 |
foreach ( $fields as $field_id => $field ) |
| 63 |
{ |
| 64 |
if ( !isset($new_fields[$field_id]) && $field['type'] != 'hidden' ) |
| 65 |
{ |
| 66 |
unset($fields[$field_id]); |
| 67 |
} |
| 68 |
|
| 69 |
if ( isset($field['type']) && $field['type'] == 'hidden' && !isset($new_fields[$field_id]) ) |
| 70 |
{ |
| 71 |
$new_fields[$field_id] = $field; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
// Merge the new with existing (if existing exists) |
| 76 |
foreach ( $new_fields as $field_id => $new_field ) |
| 77 |
{ |
| 78 |
$fields[$field_id] = array_merge( ( isset($fields[$field_id]) ? $fields[$field_id] : array() ), $new_field ); |
| 79 |
} |
| 80 |
|
| 81 |
// Set order |
| 82 |
$new_ordered_fields = array(); |
| 83 |
foreach ( $new_fields as $field_id => $new_field ) |
| 84 |
{ |
| 85 |
$new_ordered_fields[$field_id] = $fields[$field_id]; |
| 86 |
} |
| 87 |
$fields = $new_ordered_fields; |
| 88 |
|
| 89 |
// Check if any of the fields at this point are setup as additional fields |
| 90 |
$custom_fields = ( ( isset($current_settings['custom_fields']) ) ? $current_settings['custom_fields'] : array() ); |
| 91 |
|
| 92 |
foreach ( $fields as $field_id => $field ) |
| 93 |
{ |
| 94 |
foreach ( $custom_fields as $custom_field ) |
| 95 |
{ |
| 96 |
if ( $custom_field['field_name'] == $field_id && ( $custom_field['field_type'] == 'select' || $custom_field['field_type'] == 'multiselect' ) && isset($custom_field['dropdown_options']) && is_array($custom_field['dropdown_options']) ) |
| 97 |
{ |
| 98 |
$options = array('' => ( (isset($field['blank_option'])) ? $field['blank_option'] : '' ) ); |
| 99 |
|
| 100 |
foreach ( $custom_field['dropdown_options'] as $dropdown_option ) |
| 101 |
{ |
| 102 |
$options[$dropdown_option] = $dropdown_option; |
| 103 |
} |
| 104 |
|
| 105 |
$fields[$field_id]['options'] = $options; |
| 106 |
|
| 107 |
if ( $custom_field['field_type'] == 'multiselect' ) { $fields[$field_id]['type'] = 'select'; } |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
return $fields; |
| 113 |
} , 99, 1 ); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
public function add_captcha_to_forms() |
| 120 |
{ |
| 121 |
if ( in_array(get_option( 'propertyhive_captcha_service', '' ), array('recaptcha', 'recaptcha-v3', 'hCaptcha', 'turnstile')) ) |
| 122 |
{ |
| 123 |
add_filter( 'propertyhive_property_enquiry_form_fields', array( $this, 'add_captcha_to_form' ) ); |
| 124 |
add_filter( 'propertyhive_applicant_registration_form_fields', array( $this, 'add_captcha_to_form' ) ); |
| 125 |
add_filter( 'propertyhive_send_to_friend_form_fields', array( $this, 'add_captcha_to_form' ) ); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
public function add_captcha_to_form($fields) |
| 130 |
{ |
| 131 |
$captcha_service = get_option( 'propertyhive_captcha_service', '' ); |
| 132 |
|
| 133 |
switch ( $captcha_service ) |
| 134 |
{ |
| 135 |
case "recaptcha": |
| 136 |
{ |
| 137 |
$fields['recaptcha'] = array( |
| 138 |
'type' => $captcha_service, |
| 139 |
'site_key' => get_option( 'propertyhive_captcha_site_key', '' ), |
| 140 |
'secret' => get_option( 'propertyhive_captcha_secret', '' ), |
| 141 |
); |
| 142 |
break; |
| 143 |
} |
| 144 |
case "recaptcha-v3": |
| 145 |
{ |
| 146 |
$fields['recaptcha-v3'] = array( |
| 147 |
'type' => $captcha_service, |
| 148 |
'site_key' => get_option( 'propertyhive_captcha_site_key', '' ), |
| 149 |
'secret' => get_option( 'propertyhive_captcha_secret', '' ), |
| 150 |
); |
| 151 |
break; |
| 152 |
} |
| 153 |
case "hCaptcha": |
| 154 |
{ |
| 155 |
$fields['hCaptcha'] = array( |
| 156 |
'type' => $captcha_service, |
| 157 |
'site_key' => get_option( 'propertyhive_captcha_site_key', '' ), |
| 158 |
'secret' => get_option( 'propertyhive_captcha_secret', '' ), |
| 159 |
); |
| 160 |
break; |
| 161 |
} |
| 162 |
case "turnstile": |
| 163 |
{ |
| 164 |
$fields['turnstile'] = array( |
| 165 |
'type' => $captcha_service, |
| 166 |
'site_key' => get_option( 'propertyhive_captcha_site_key', '' ), |
| 167 |
'secret' => get_option( 'propertyhive_captcha_secret', '' ), |
| 168 |
); |
| 169 |
break; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
return $fields; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
new PH_Form_Handler(); |
| 178 |
|