<?php
class WPEFB_Sanitizer {

	public function get_sanitized_fields( $identifier, $field, $value ) {
         $sanitize_method = 'get_sanitized_' . strtolower( $identifier ) . '_field';

        if ( method_exists( $this, $sanitize_method ) ) {
            $sanitized_value = $this->$sanitize_method( $field, $value );
        } else {
            $classname = "EFB_Helper_$identifier";
        }

		if ( isset( $classname ) && class_exists( $classname ) ) {
			$externalclass   = new $classname();
			$sanitized_value = $externalclass->get_sanitized_fields( $identifier, $field, $value );
		}

		return $sanitized_value;
	}


	public function get_sanitized_email_tmpl_field( $field, $value ) {
		switch ( $field ) {
			case 'id':
				$value = sanitize_text_field( $value );
				break;
			case 'tmpl_name':
				$value = sanitize_text_field( $value );
				break;
			case 'email_subject':
				$value = sanitize_text_field( $value );
				break;
			case 'email_body':
				$value = wp_kses_post( $value );
				break;
			default:
				$value = sanitize_text_field( $value );

		}
		return $value;
	}


	public function get_sanitized_forms_field( $field, $value ) {
		switch ( $field ) {
			case 'id':
				$value = sanitize_text_field( $value );
                break;
			case 'name':
                    $value = sanitize_text_field( $value );
                break;
			case 'desc':
                $value = wp_kses_post( $value );
                break;
			case 'header_text':
			case 'submission_limit_message':
			case 'wpefb_success_msg_content':
			case 'wpefb_email_content':
			case 'new_user_email_content':
			case 'user_email_activation_content':
			case 'new_submission_email_content':
                $value = wp_kses_post( $value );
                break;
			case 'type':
                $value = sanitize_file_name( $value );
                break;
			case 'user_role':
				$value = sanitize_text_field( $value );
                break;
			case 'redirect':
				$value = sanitize_text_field( $value );
                break;
			case 'redirect_to':
				$value = wp_kses_post( $value );
                break;
			case 'meta':
				$value = sanitize_text_field( $value );
                break;
			default:
				$value = sanitize_text_field( $value );
                break;
		}
            return $value;
	}

	public function get_sanitized_fields_field( $field, $value ) {
		switch ( $field ) {
			case 'field_id':
				$value = sanitize_text_field( $value );
                break;
			case 'field_name':
				$value = sanitize_text_field( $value );
                break;
			case 'field_desc':
				$value = sanitize_text_field( $value );
                break;
			case 'field_type':
				$value = sanitize_text_field( $value );
                break;
			case 'field_options':
				$value = $value;
                break;
			case 'field_icon':
				$value = sanitize_text_field( $value );
                break;
			case 'associate_group':
				$value = sanitize_text_field( $value );
                break;
			case 'show_in_signup_form':
				$value = sanitize_text_field( $value );
                break;
			case 'is_required':
				$value = sanitize_text_field( $value );
                break;
			case 'is_editable':
				$value = sanitize_text_field( $value );
                break;
			case 'display_on_form':
				$value = sanitize_text_field( $value );
                break;
			case 'display_on_group':
				$value = sanitize_text_field( $value );
                break;
			case 'visibility':
				$value = sanitize_text_field( $value );
                break;
			case 'ordering':
				$value = sanitize_text_field( $value );
                break;
			default:
				$value = sanitize_text_field( $value );

		}
            return $value;
	}

	public function get_sanitized_frontend_field( $type, $value ) {
		switch ( $type ) {
            case 'user_name':
                $value = sanitize_user( $value, true );
                break;
            case 'user_login':
                $value = sanitize_user( $value, true );
                break;
            case 'user_email':
                $value = sanitize_email( $value );
                break;
            case 'email':
                $value = sanitize_email( $value );
                break;
            case 'first_name':
                $value = sanitize_text_field( $value );
                break;
            case 'last_name':
                $value = sanitize_text_field( $value );
                break;
            case 'radio':
                $value = sanitize_text_field( $value );
                break;
            case 'description':
                $value = wp_kses_post( wp_rel_nofollow( $value ) );
                break;
            case 'text':
                $value = sanitize_text_field( $value );
                break;
            case 'select':
                $value = sanitize_text_field( $value );
                break;
            case 'heading':
                $value = sanitize_text_field( $value );
                break;
            case 'paragraph':
                $value = sanitize_text_field( $value );
                break;
            case 'number':
                $value = filter_var( $value, FILTER_SANITIZE_NUMBER_INT );
                break;
            case 'country':
                $value = sanitize_text_field( $value );
                break;
            case 'timezone':
                $value = sanitize_text_field( $value );
                break;
            case 'term_checkbox':
                $value = sanitize_text_field( $value );
                break;
            case 'user_url':
                $value = esc_url( $value );
                break;
            case 'textarea':
                $value = sanitize_text_field( $value );
                break;
            case 'DatePicker':
                $value = filter_var( $value, FILTER_SANITIZE_NUMBER_INT );
                break;
            case 'mobile_number':
                $value = filter_var( $value, FILTER_SANITIZE_NUMBER_INT );
                break;
            case 'phone_number':
                $value = filter_var( $value, FILTER_SANITIZE_NUMBER_INT );
                break;
            case 'gender':
                $value = sanitize_text_field( $value );
                break;
            case 'language':
                $value = sanitize_text_field( $value );
                break;
            case 'birth_date':
                $value = filter_var( $value, FILTER_SANITIZE_NUMBER_INT );
                break;
            case 'multi_dropdown':
                $value = sanitize_text_field( $value );
                break;
            case 'facebook':
                $value = esc_url( $value );
                break;
            case 'twitter':
                $value = esc_url( $value );
                break;
            case 'google':
                $value = esc_url( $value );
                break;
            case 'linked_in':
                $value = esc_url( $value );
                break;
            case 'youtube':
                $value = esc_url( $value );
                break;
            case 'instagram':
                $value = esc_url( $value );
                break;
            case 'address':
                $value = esc_url( $value );
                break;

            default:
                $value = $value;
		}
        return $value;
    }

    public function get_sanitized_settings_field( $field, $value ) {
        switch ( $field ) {
			case 'pm_logout_prompt_text':
				$value = sanitize_textarea_field( $value );
                break;
			case 'pm_wpadmin_allow_ips':
				$value = sanitize_textarea_field( $value );
                break;
			case 'pm_blocked_ips':
				$value = sanitize_textarea_field( $value );
                break;
			case 'pm_blocked_emails':
				$value = sanitize_textarea_field( $value );
                break;
			case 'pm_blacklist_word':
				$value = sanitize_textarea_field( $value );
                break;
			case 'pm_account_deletion_alert_text':
				$value = sanitize_textarea_field( $value );
                break;
			case 'pm_recaptcha_site_key':
				$value = sanitize_text_field( $value );
                break;
			case 'pm_recaptcha_secret_key':
				$value = sanitize_text_field( $value );
                break;
			case 'pm_account_review_email_subject':
				$value = sanitize_text_field( $value );
                break;
			case 'pm_account_delete_email_subject':
				$value = sanitize_text_field( $value );
                break;
			case 'pm_from_email_name':
				$value = sanitize_text_field( $value );
                break;
			case 'pm_admin_email':
				$value = sanitize_email( $value );
                break;
			case 'pm_from_email_address':
				$value = sanitize_email( $value );
                break;
			case 'pm_paypal_email':
				$value = sanitize_email( $value );
                break;
			case 'pm_account_review_email_body':
				$value = wp_kses_post( $value );
                break;
			case 'pm_account_delete_email_body':
				$value = wp_kses_post( $value );
                break;
			case 'pm_unread_message_email_body':
				$value = wp_kses_post( $value );
                break;
			case 'pm_activation_email_body':
				$value = wp_kses_post( $value );
                break;
			case 'pg_user_form_seo_title':
				$value = wp_kses_post( $value );
                break;
			case 'pg_user_form_seo_desc':
				$value = wp_kses_post( $value );
                break;
			case 'pm_blog_notification_email_body':
				$value = wp_kses_post( $value );
                break;
			case 'pm_new_user_create_admin_email_body':
				$value = wp_kses_post( $value );
                break;
			default:
				$value = sanitize_text_field( $value );
		}
            return $value;
	}






    public function remove_magic_quotes( $input ) {
		foreach ( $input as $key => $value ) {
            if ( is_array( $value ) ) {
                $input[ $key ] = $this->remove_magic_quotes( $value );
            } elseif ( is_string( $value ) ) {
                $input[ $key ] = stripslashes( $value );
            }
		}
        return $input;
    }

    public function sanitize( $input ) {
            // Initialize the new array or object that will hold the sanitized values
		if ( is_array( $input ) ) {
			$new_input = array();
		} elseif ( is_object( $input ) ) {
			$new_input = new stdClass();
		} else {
			return $input; // If it's not an array or object, return the input as is
		}
            // Loop through the input and sanitize each of the values
		foreach ( $input as $key => $val ) {
			if ( empty( $val ) ) {
				if ( is_array( $new_input ) ) {
					$new_input[ $key ] = $val;
				} else {
					$new_input->{$key} = $val;
				}
				continue;
			}

			if ( is_array( $val ) || is_object( $val ) ) {
				// Recursively sanitize arrays or objects
				if ( is_array( $new_input ) ) {
					$new_input[ $key ] = $this->sanitize( $val );
				} else {
					$new_input->{$key} = $this->sanitize( $val );
				}
			} else {
				// Sanitize scalar values based on the key

				switch ( $key ) {
					case 'login':
					case 'uname':
						$sanitized_value = sanitize_user( $val );
						break;
					case 'user_email':
						$sanitized_value = sanitize_email( $val );
						break;
					case 'key':
						$sanitized_value = sanitize_text_field( wp_strip_all_tags( $val ) );
						break;
					case 'nonce':
					case '_wpnonce':
					case 'security':
						$sanitized_value = sanitize_key( $val );
						break;
					case 'user_login':
					case 'userdata':
						if ( is_email( $val ) ) {
							$sanitized_value = sanitize_email( $val );
						} else {
							$sanitized_value = sanitize_user( $val );
						}
						break;
					default:
						if ( is_email( $val ) ) {
							$sanitized_value = sanitize_email( $val );
						} else {
							$val             = html_entity_decode( $val, ENT_QUOTES, 'UTF-8' );
							$sanitized_value = sanitize_text_field( wp_strip_all_tags( $val ) );
						}
						break;
				}

				// Assign sanitized value back to the new input array or object
				if ( is_array( $new_input ) ) {
					$new_input[ $key ] = $sanitized_value;
				} else {
					$new_input->{$key} = $sanitized_value;
				}
			}
		}

            return $new_input;
	}


}

if ( ! class_exists( 'WPEFB_sanitizer' ) ) {
	class_alias( 'WPEFB_Sanitizer', 'WPEFB_sanitizer' );
}
