| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly |
| 4 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 5 |
|
| 6 |
$redirect_url = wppb_toolbox_get_settings( 'fields', 'redirect-if-empty-required-url' ); |
| 7 |
|
| 8 |
if ( !empty( $redirect_url ) ) |
| 9 |
add_action( 'template_redirect', 'wppb_toolbox_redirect_if_empty_required' ); |
| 10 |
|
| 11 |
function wppb_toolbox_redirect_if_empty_required() { |
| 12 |
if ( current_user_can( 'manage_options' ) ) |
| 13 |
return; |
| 14 |
|
| 15 |
$user_id = get_current_user_id(); |
| 16 |
$current_url = wppb_curpageurl(); |
| 17 |
$redirect_url = get_permalink( wppb_toolbox_get_settings( 'fields', 'redirect-if-empty-required-url' ) ); |
| 18 |
|
| 19 |
if ( !empty( $user_id ) && ( $current_url != $redirect_url ) && apply_filters( 'wppb_toolbox_redirect_if_empty_required', true, $user_id, $current_url, $redirect_url ) ) { |
| 20 |
|
| 21 |
$fields = get_option( 'wppb_manage_fields', array() ); |
| 22 |
$without_meta_name = array( 'user_url' => 'Default - Website', 'display_name' => 'Default - Display name publicly as' ); |
| 23 |
|
| 24 |
foreach ( $fields as $field ){ |
| 25 |
// Skip fields that are hidden by conditional logic |
| 26 |
if ( !empty( $field["conditional-logic-enabled"] ) && !empty( $field["conditional-logic"] ) && $field["conditional-logic-enabled"] === 'yes' ) { |
| 27 |
$field_conditional_logic = json_decode($field["conditional-logic"], true); |
| 28 |
|
| 29 |
// Set initial conditions based on action_type. If action type is show then it means that at the beginning it is hidden |
| 30 |
if( $field_conditional_logic['action_type'] == 'show' ) |
| 31 |
$hide_field = true; |
| 32 |
else if( $field_conditional_logic['action_type'] == 'hide' ) |
| 33 |
$hide_field = false; |
| 34 |
|
| 35 |
// If all the rules must be met then we start with a true value and pass through all of them and if one is not true then we invalidate them |
| 36 |
if ($field_conditional_logic['logic_type'] == 'all') { |
| 37 |
$all_conditions = true; |
| 38 |
} |
| 39 |
|
| 40 |
if (!empty($field_conditional_logic['rules'])) { |
| 41 |
foreach ($field_conditional_logic['rules'] as $rule) { |
| 42 |
// Get the value of the field that this condition depends on |
| 43 |
$dependency_field_meta_name = ''; |
| 44 |
foreach ($fields as $dependency_field) { |
| 45 |
if ($dependency_field['id'] == $rule['field']) { |
| 46 |
if (!empty($dependency_field['meta-name'])) { |
| 47 |
$dependency_field_meta_name = $dependency_field['meta-name']; |
| 48 |
} else { |
| 49 |
// Handle default fields |
| 50 |
if (class_exists('Wordpress_Creation_Kit_PB')) { |
| 51 |
switch (Wordpress_Creation_Kit_PB::wck_generate_slug($dependency_field['field'])) { |
| 52 |
case 'default-username': |
| 53 |
$dependency_field_meta_name = 'username'; |
| 54 |
break; |
| 55 |
case 'default-display-name-publicly-as': |
| 56 |
$dependency_field_meta_name = "display_name"; |
| 57 |
break; |
| 58 |
case 'default-e-mail': |
| 59 |
$dependency_field_meta_name = 'email'; |
| 60 |
break; |
| 61 |
case 'default-website': |
| 62 |
$dependency_field_meta_name = 'website'; |
| 63 |
break; |
| 64 |
case 'select-user-role': |
| 65 |
$dependency_field_meta_name = 'custom_field_user_role'; |
| 66 |
break; |
| 67 |
case 'subscription-plans': |
| 68 |
$dependency_field_meta_name = 'subscription_plans'; |
| 69 |
break; |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
break; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
if (!empty($dependency_field_meta_name)) { |
| 78 |
// Get the value of the dependency field |
| 79 |
$dependency_value = get_user_meta($user_id, $dependency_field_meta_name, true); |
| 80 |
|
| 81 |
if ($dependency_value !== false) { |
| 82 |
if (!is_array($dependency_value)) { |
| 83 |
// Check if the value is a comma-separated string |
| 84 |
if (is_string($dependency_value) && strpos($dependency_value, ',') !== false) { |
| 85 |
$dependency_value = array_map('trim', explode(',', $dependency_value)); |
| 86 |
} else { |
| 87 |
$dependency_value = array($dependency_value); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
if ($rule['operator'] == 'is' && in_array($rule['value'], $dependency_value)) { |
| 92 |
if ($field_conditional_logic['logic_type'] == 'any') { |
| 93 |
$hide_field = !$hide_field; |
| 94 |
break; |
| 95 |
} |
| 96 |
} else if ($rule['operator'] == 'is' && !in_array($rule['value'], $dependency_value)) { |
| 97 |
if ($field_conditional_logic['logic_type'] == 'all') { |
| 98 |
$all_conditions = false; |
| 99 |
break; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
if ($rule['operator'] == 'is not' && !in_array($rule['value'], $dependency_value)) { |
| 104 |
if ($field_conditional_logic['logic_type'] == 'any') { |
| 105 |
$hide_field = !$hide_field; |
| 106 |
break; |
| 107 |
} |
| 108 |
} else if ($rule['operator'] == 'is not' && in_array($rule['value'], $dependency_value)) { |
| 109 |
if ($field_conditional_logic['logic_type'] == 'all') { |
| 110 |
$all_conditions = false; |
| 111 |
break; |
| 112 |
} |
| 113 |
} |
| 114 |
} else { |
| 115 |
if ($field_conditional_logic['logic_type'] == 'all') { |
| 116 |
$all_conditions = false; |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
// If all the rules must be valid and the boolean is still true then the initial state of the field changed |
| 124 |
if ($field_conditional_logic['logic_type'] == 'all' && $all_conditions) { |
| 125 |
$hide_field = !$hide_field; |
| 126 |
} |
| 127 |
|
| 128 |
// If the field is hidden by conditional logic, skip it |
| 129 |
if ($hide_field) { |
| 130 |
continue; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
if ( $field['required'] == 'Yes' && !empty( $field['meta-name'] ) && $field['field'] != 'Checkbox (Terms and Conditions)' ){ |
| 135 |
|
| 136 |
if( $field['meta-name'] == 'map' && function_exists( 'wppb_get_user_map_markers' ) ) |
| 137 |
$value = wppb_get_user_map_markers( $user_id, $field['meta-name'] ); |
| 138 |
else |
| 139 |
$value = get_user_meta( $user_id, $field['meta-name'], true ); |
| 140 |
|
| 141 |
if ( empty( $value ) ){ |
| 142 |
wp_redirect( $redirect_url ); |
| 143 |
exit(); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
if( $field['required'] == 'Yes' && ( $field['field'] == 'Default - Website' || $field['field'] == 'Default - Display name publicly as' ) ){ |
| 148 |
$user = get_userdata( $user_id ); |
| 149 |
|
| 150 |
$key = array_search( $field['field'], $without_meta_name ); |
| 151 |
$value = $user->$key; |
| 152 |
|
| 153 |
if ( empty( $value ) ){ |
| 154 |
wp_redirect( $redirect_url ); |
| 155 |
exit(); |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|