function ot_validate_setting($input, $type, $field_id, $wmpl_id = '')
 {
     /* exit early if missing data */
     if (!$input || !$type || !$field_id) {
         return $input;
     }
     $input = apply_filters('ot_validate_setting', $input, $type, $field_id);
     /* WPML Register and Unregister strings */
     if (!empty($wmpl_id)) {
         /* Allow filtering on the WPML option types */
         $single_string_types = apply_filters('ot_wpml_option_types', array('text', 'textarea', 'textarea-simple'));
         if (in_array($type, $single_string_types)) {
             if (!empty($input)) {
                 ot_wpml_register_string($wmpl_id, $input);
             } else {
                 ot_wpml_unregister_string($wmpl_id);
             }
         }
     }
     if ('background' == $type) {
         $input['background-color'] = ot_validate_setting($input['background-color'], 'colorpicker', $field_id);
         $input['background-image'] = ot_validate_setting($input['background-image'], 'upload', $field_id);
         // Loop over array and check for values
         foreach ((array) $input as $key => $value) {
             if (!empty($value)) {
                 $has_value = true;
             }
         }
         // No value; set to empty
         if (!isset($has_value)) {
             $input = '';
         }
     } else {
         if ('colorpicker' == $type) {
             /* return empty & set error */
             if (0 === preg_match('/^#([a-f0-9]{6}|[a-f0-9]{3})$/i', $input)) {
                 $input = '';
                 add_settings_error('option-tree', 'invalid_hex', __('The Colorpicker only allows valid hexadecimal values.', 'option-tree'), 'error');
             }
         } else {
             if (in_array($type, array('css', 'text', 'textarea', 'textarea-simple'))) {
                 if (!current_user_can('unfiltered_html') && OT_ALLOW_UNFILTERED_HTML == false) {
                     $input = wp_kses_post($input);
                 }
             } else {
                 if ('measurement' == $type) {
                     $input[0] = sanitize_text_field($input[0]);
                     // No value; set to empty
                     if (empty($input[0]) && empty($input[1])) {
                         $input = '';
                     }
                 } else {
                     if ('typography' == $type && isset($input['font-color'])) {
                         $input['font-color'] = ot_validate_setting($input['font-color'], 'colorpicker', $field_id);
                         // Loop over array and check for values
                         foreach ($input as $key => $value) {
                             if (!empty($value)) {
                                 $has_value = true;
                             }
                         }
                         // No value; set to empty
                         if (!isset($has_value)) {
                             $input = '';
                         }
                     } else {
                         if ('upload' == $type) {
                             $input = sanitize_text_field($input);
                         } else {
                             if ('gallery' == $type) {
                                 $input = trim($input);
                             } else {
                                 if ('social-links' == $type) {
                                     // Loop over array and check for values, plus sanitize the text field
                                     foreach ((array) $input as $key => $value) {
                                         if (!empty($value)) {
                                             $has_value = true;
                                             $input[$key] = sanitize_text_field($value);
                                         }
                                     }
                                     // No value; set to empty
                                     if (!isset($has_value)) {
                                         $input = '';
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     $input = apply_filters('ot_after_validate_setting', $input, $type, $field_id);
     return $input;
 }
 function ot_validate_setting($input, $type, $field_id, $wmpl_id = '')
 {
     /* exit early if missing data */
     if (!$input || !$type || !$field_id) {
         return $input;
     }
     $input = apply_filters('ot_validate_setting', $input, $type, $field_id);
     /* WPML Register and Unregister strings */
     if (!empty($wmpl_id)) {
         /* Allow filtering on the WPML option types */
         $single_string_types = apply_filters('ot_wpml_option_types', array('text', 'textarea', 'textarea-simple'));
         if (in_array($type, $single_string_types)) {
             if (!empty($input)) {
                 ot_wpml_register_string($wmpl_id, $input);
             } else {
                 ot_wpml_unregister_string($wmpl_id);
             }
         }
     }
     if ('background' == $type) {
         $input['background-color'] = ot_validate_setting($input['background-color'], 'colorpicker', $field_id);
         $input['background-image'] = ot_validate_setting($input['background-image'], 'upload', $field_id);
         // Loop over array and check for values
         foreach ((array) $input as $key => $value) {
             if (!empty($value)) {
                 $has_value = true;
             }
         }
         // No value; set to empty
         if (!isset($has_value)) {
             $input = '';
         }
     } else {
         if ('border' == $type) {
             // Loop over array and set errors or unset key from array.
             foreach ($input as $key => $value) {
                 // Validate width
                 if ($key == 'width' && !empty($value) && !is_numeric($value)) {
                     $input[$key] = '0';
                     add_settings_error('option-tree', 'invalid_border_width', sprintf(__('The %s input field for %s only allows numeric values.', 'option-tree'), '<code>width</code>', '<code>' . $field_id . '</code>'), 'error');
                 }
                 // Validate color
                 if ($key == 'color' && !empty($value) && 0 === preg_match('/^#([a-f0-9]{6}|[a-f0-9]{3})$/i', $value)) {
                     $input[$key] = '';
                     $value = '';
                     add_settings_error('option-tree', 'invalid_hex', __('The Colorpicker only allows valid hexadecimal values.', 'option-tree'), 'error');
                 }
                 // Unset keys with empty values.
                 if (empty($value)) {
                     unset($input[$key]);
                 }
             }
             if (empty($input)) {
                 $input = '';
             }
         } else {
             if ('box-shadow' == $type) {
                 // Validate inset
                 $input['inset'] = isset($input['inset']) ? 'inset' : '';
                 // Validate offset-x
                 $input['offset-x'] = ot_validate_setting($input['offset-x'], 'text', $field_id);
                 // Validate offset-y
                 $input['offset-y'] = ot_validate_setting($input['offset-y'], 'text', $field_id);
                 // Validate blur-radius
                 $input['blur-radius'] = ot_validate_setting($input['blur-radius'], 'text', $field_id);
                 // Validate spread-radius
                 $input['spread-radius'] = ot_validate_setting($input['spread-radius'], 'text', $field_id);
                 // Validate color
                 $input['color'] = ot_validate_setting($input['color'], 'colorpicker', $field_id);
                 // Unset keys with empty values.
                 foreach ($input as $key => $value) {
                     if (empty($value)) {
                         unset($input[$key]);
                     }
                 }
                 // Set empty array to empty string.
                 if (empty($input)) {
                     $input = '';
                 }
             } else {
                 if ('colorpicker' == $type) {
                     /* return empty & set error */
                     if (0 === preg_match('/^#([a-f0-9]{6}|[a-f0-9]{3})$/i', $input)) {
                         $input = '';
                         add_settings_error('option-tree', 'invalid_hex', sprintf(__('The %s Colorpicker only allows valid hexadecimal values.', 'option-tree'), '<code>' . $field_id . '</code>'), 'error');
                     }
                 } else {
                     if ('colorpicker-opacity' == $type) {
                         // Validate color
                         $input['color'] = ot_validate_setting($input['color'], 'colorpicker', $field_id);
                         // Unset keys with empty values.
                         foreach ($input as $key => $value) {
                             if (empty($value)) {
                                 unset($input[$key]);
                             }
                         }
                         // Set empty array to empty string.
                         if (empty($input)) {
                             $input = '';
                         }
                     } else {
                         if (in_array($type, array('css', 'javascript', 'text', 'textarea', 'textarea-simple'))) {
                             if (!current_user_can('unfiltered_html') && OT_ALLOW_UNFILTERED_HTML == false) {
                                 $input = wp_kses_post($input);
                             }
                         } else {
                             if ('dimension' == $type) {
                                 // Loop over array and set error keys or unset key from array.
                                 foreach ($input as $key => $value) {
                                     if (!empty($value) && !is_numeric($value) && $key !== 'unit') {
                                         $errors[] = $key;
                                     }
                                     if (empty($value)) {
                                         unset($input[$key]);
                                     }
                                 }
                                 /* return 0 & set error */
                                 if (isset($errors)) {
                                     foreach ($errors as $error) {
                                         $input[$error] = '0';
                                         add_settings_error('option-tree', 'invalid_dimension_' . $error, sprintf(__('The %s input field for %s only allows numeric values.', 'option-tree'), '<code>' . $error . '</code>', '<code>' . $field_id . '</code>'), 'error');
                                     }
                                 }
                                 if (empty($input)) {
                                     $input = '';
                                 }
                             } else {
                                 if ('google-fonts' == $type) {
                                     unset($input['%key%']);
                                     // Loop over array and check for values
                                     if (is_array($input) && !empty($input)) {
                                         $input = array_values($input);
                                     }
                                     // No value; set to empty
                                     if (empty($input)) {
                                         $input = '';
                                     }
                                 } else {
                                     if ('link-color' == $type) {
                                         // Loop over array and check for values
                                         if (is_array($input) && !empty($input)) {
                                             foreach ($input as $key => $value) {
                                                 if (!empty($value)) {
                                                     $input[$key] = ot_validate_setting($input[$key], 'colorpicker', $field_id . '-' . $key);
                                                     $has_value = true;
                                                 }
                                             }
                                         }
                                         // No value; set to empty
                                         if (!isset($has_value)) {
                                             $input = '';
                                         }
                                     } else {
                                         if ('measurement' == $type) {
                                             $input[0] = sanitize_text_field($input[0]);
                                             // No value; set to empty
                                             if (empty($input[0]) && empty($input[1])) {
                                                 $input = '';
                                             }
                                         } else {
                                             if ('spacing' == $type) {
                                                 // Loop over array and set error keys or unset key from array.
                                                 foreach ($input as $key => $value) {
                                                     if (!empty($value) && !is_numeric($value) && $key !== 'unit') {
                                                         $errors[] = $key;
                                                     }
                                                     if (empty($value)) {
                                                         unset($input[$key]);
                                                     }
                                                 }
                                                 /* return 0 & set error */
                                                 if (isset($errors)) {
                                                     foreach ($errors as $error) {
                                                         $input[$error] = '0';
                                                         add_settings_error('option-tree', 'invalid_spacing_' . $error, sprintf(__('The %s input field for %s only allows numeric values.', 'option-tree'), '<code>' . $error . '</code>', '<code>' . $field_id . '</code>'), 'error');
                                                     }
                                                 }
                                                 if (empty($input)) {
                                                     $input = '';
                                                 }
                                             } else {
                                                 if ('typography' == $type && isset($input['font-color'])) {
                                                     $input['font-color'] = ot_validate_setting($input['font-color'], 'colorpicker', $field_id);
                                                     // Loop over array and check for values
                                                     foreach ($input as $key => $value) {
                                                         if (!empty($value)) {
                                                             $has_value = true;
                                                         }
                                                     }
                                                     // No value; set to empty
                                                     if (!isset($has_value)) {
                                                         $input = '';
                                                     }
                                                 } else {
                                                     if ('upload' == $type) {
                                                         $input = esc_url_raw($input);
                                                     } else {
                                                         if ('gallery' == $type) {
                                                             $input = trim($input);
                                                         } else {
                                                             if ('social-links' == $type) {
                                                                 // Loop over array and check for values, plus sanitize the text field
                                                                 foreach ((array) $input as $key => $value) {
                                                                     if (!empty($value) && is_array($value)) {
                                                                         foreach ((array) $value as $item_key => $item_value) {
                                                                             if (!empty($item_value)) {
                                                                                 $has_value = true;
                                                                                 $input[$key][$item_key] = sanitize_text_field($item_value);
                                                                             }
                                                                         }
                                                                     }
                                                                 }
                                                                 // No value; set to empty
                                                                 if (!isset($has_value)) {
                                                                     $input = '';
                                                                 }
                                                             }
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     $input = apply_filters('ot_after_validate_setting', $input, $type, $field_id);
     return $input;
 }