| 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 |
public function __construct() { |
| 19 |
|
| 20 |
add_action( 'init', array( $this, 'add_captcha_to_forms' ) ); |
| 21 |
|
| 22 |
$current_settings = get_option( 'propertyhive_template_assistant', array() ); |
| 23 |
|
| 24 |
if ( isset($current_settings['search_forms']) && !empty($current_settings['search_forms']) ) |
| 25 |
{ |
| 26 |
foreach ( $current_settings['search_forms'] as $id => $form ) |
| 27 |
{ |
| 28 |
add_filter( 'propertyhive_search_form_fields_' . $id, function($fields) |
| 29 |
{ |
| 30 |
$form_id = str_replace( "propertyhive_search_form_fields_", "", current_filter() ); |
| 31 |
|
| 32 |
$current_settings = get_option( 'propertyhive_template_assistant', array() ); |
| 33 |
|
| 34 |
$new_fields = ( |
| 35 |
( |
| 36 |
isset($current_settings['search_forms'][$form_id]['active_fields']) |
| 37 |
&& |
| 38 |
!empty($current_settings['search_forms'][$form_id]['active_fields']) |
| 39 |
) ? |
| 40 |
$current_settings['search_forms'][$form_id]['active_fields'] : |
| 41 |
$fields |
| 42 |
); |
| 43 |
|
| 44 |
// Remove any fields that are in the $fields array but not active in active_fields, excluding hidden fields |
| 45 |
$hidden_fields = array(); |
| 46 |
foreach ( $fields as $field_id => $field ) |
| 47 |
{ |
| 48 |
if ( !isset($new_fields[$field_id]) && $field['type'] != 'hidden' ) |
| 49 |
{ |
| 50 |
unset($fields[$field_id]); |
| 51 |
} |
| 52 |
|
| 53 |
if ( isset($field['type']) && $field['type'] == 'hidden' && !isset($new_fields[$field_id]) ) |
| 54 |
{ |
| 55 |
$new_fields[$field_id] = $field; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
// Merge the new with existing (if existing exists) |
| 60 |
foreach ( $new_fields as $field_id => $new_field ) |
| 61 |
{ |
| 62 |
$fields[$field_id] = array_merge( ( isset($fields[$field_id]) ? $fields[$field_id] : array() ), $new_field ); |
| 63 |
} |
| 64 |
|
| 65 |
// Set order |
| 66 |
$new_ordered_fields = array(); |
| 67 |
foreach ( $new_fields as $field_id => $new_field ) |
| 68 |
{ |
| 69 |
$new_ordered_fields[$field_id] = $fields[$field_id]; |
| 70 |
} |
| 71 |
$fields = $new_ordered_fields; |
| 72 |
|
| 73 |
// Check if any of the fields at this point are setup as additional fields |
| 74 |
$custom_fields = ( ( isset($current_settings['custom_fields']) ) ? $current_settings['custom_fields'] : array() ); |
| 75 |
|
| 76 |
foreach ( $fields as $field_id => $field ) |
| 77 |
{ |
| 78 |
foreach ( $custom_fields as $custom_field ) |
| 79 |
{ |
| 80 |
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']) ) |
| 81 |
{ |
| 82 |
$options = array('' => ( (isset($field['blank_option'])) ? $field['blank_option'] : '' ) ); |
| 83 |
|
| 84 |
foreach ( $custom_field['dropdown_options'] as $dropdown_option ) |
| 85 |
{ |
| 86 |
$options[$dropdown_option] = $dropdown_option; |
| 87 |
} |
| 88 |
|
| 89 |
$fields[$field_id]['options'] = $options; |
| 90 |
|
| 91 |
if ( $custom_field['field_type'] == 'multiselect' ) { $fields[$field_id]['type'] = 'select'; } |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
return $fields; |
| 97 |
} , 99, 1 ); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
public function add_captcha_to_forms() |
| 104 |
{ |
| 105 |
if ( in_array(get_option( 'propertyhive_captcha_service', '' ), array('recaptcha', 'recaptcha-v3', 'hCaptcha', 'turnstile')) ) |
| 106 |
{ |
| 107 |
add_filter( 'propertyhive_property_enquiry_form_fields', array( $this, 'add_captcha_to_form' ) ); |
| 108 |
add_filter( 'propertyhive_applicant_registration_form_fields', array( $this, 'add_captcha_to_form' ) ); |
| 109 |
add_filter( 'propertyhive_send_to_friend_form_fields', array( $this, 'add_captcha_to_form' ) ); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
public function add_captcha_to_form($fields) |
| 114 |
{ |
| 115 |
$captcha_service = get_option( 'propertyhive_captcha_service', '' ); |
| 116 |
|
| 117 |
switch ( $captcha_service ) |
| 118 |
{ |
| 119 |
case "recaptcha": |
| 120 |
{ |
| 121 |
$fields['recaptcha'] = array( |
| 122 |
'type' => $captcha_service, |
| 123 |
'site_key' => get_option( 'propertyhive_captcha_site_key', '' ), |
| 124 |
'secret' => get_option( 'propertyhive_captcha_secret', '' ), |
| 125 |
); |
| 126 |
break; |
| 127 |
} |
| 128 |
case "recaptcha-v3": |
| 129 |
{ |
| 130 |
$fields['recaptcha-v3'] = array( |
| 131 |
'type' => $captcha_service, |
| 132 |
'site_key' => get_option( 'propertyhive_captcha_site_key', '' ), |
| 133 |
'secret' => get_option( 'propertyhive_captcha_secret', '' ), |
| 134 |
); |
| 135 |
break; |
| 136 |
} |
| 137 |
case "hCaptcha": |
| 138 |
{ |
| 139 |
$fields['hCaptcha'] = array( |
| 140 |
'type' => $captcha_service, |
| 141 |
'site_key' => get_option( 'propertyhive_captcha_site_key', '' ), |
| 142 |
'secret' => get_option( 'propertyhive_captcha_secret', '' ), |
| 143 |
); |
| 144 |
break; |
| 145 |
} |
| 146 |
case "turnstile": |
| 147 |
{ |
| 148 |
$fields['turnstile'] = array( |
| 149 |
'type' => $captcha_service, |
| 150 |
'site_key' => get_option( 'propertyhive_captcha_site_key', '' ), |
| 151 |
'secret' => get_option( 'propertyhive_captcha_secret', '' ), |
| 152 |
); |
| 153 |
break; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
return $fields; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
new PH_Form_Handler(); |
| 162 |
|