<?php
/**
 * Simple Giveaways Helpers class
 */
namespace Simple_Giveaways;

/**
 * Class Helpers
 *
 * @package Simple_Giveaways
 */
class Helpers {

	/**
	 * Clean variables using sanitize_text_field. Arrays are cleaned recursively.
	 * Non-scalar values are ignored.
	 *
	 * Copied from WooCommerce.
	 *
	 * @param string|array $var Data to sanitize.
	 * @return string|array
	 */
	public static function clean( $var ) {
		if ( is_array( $var ) ) {
			return array_map( '\Simple_Giveaways\Helpers::clean', $var );
		} else {
			return is_scalar( $var ) ? sanitize_text_field( $var ) : $var;
		}
	}

	/**
	 * Clean variables using sanitize_text_field. Arrays are cleaned recursively.
	 * Non-scalar values are ignored.
	 *
	 * Copied from WooCommerce.
	 *
	 * @param string|array $var Data to sanitize.
	 * @return string|array
	 */
	public static function unslash_and_clean( $var ) {
		$var = wp_unslash( $var );
		return self::clean( $var );
	}


    /**
     * Check if the uploaded file has an error.
     *
     * @param $uploaded_file
     * @return false|\WP_Error
     */
    public static function check_uploaded_file_error( $uploaded_file ) {
        if ( $uploaded_file['error'] !== 0 ) {
            switch ( $uploaded_file['error'] ) {
                case 1:
                case 2:
                    $message = __( 'The uploaded file is too big.', 'giveasap' );
                    break;
                case 3:
                    $message = __( 'The file was only partially uploaded.', 'giveasap' );
                    break;
                case 4:
                    $message = __( 'No file was uploaded.', 'giveasap' );
                    break;
                case 6:
                    $message = __( 'File could not be uploaded. No Folder.', 'giveasap' );
                    break;
                case 7:
                    $message = __( 'File could not be uploaded. No Permissions.', 'giveasap' );
                    break;
                default:
                    $message = __( 'File could not be uploaded.', 'giveasap' );
                    break;

            }
            return new \WP_Error( 'file-error', $message );
        }

        return false;
    }

    /**
     * @param $uploaded_file
     * @param $supported_ext
     * @return bool
     */
    public static function check_uploaded_file_extension( $uploaded_file, $supported_ext = array( 'gif', 'jpg', 'jpeg', 'png' )) {
        $ext = strtolower( pathinfo( $uploaded_file['name'], PATHINFO_EXTENSION ) );

        if ( ! in_array( $ext, $supported_ext, true ) ) {
            return false;
        }

        return true;
    }

    /**
     * @param $uploaded_file
     * @param $extensions
     * @param $extension_error_message
     * @return bool|\WP_Error
     */
    public static function check_uploaded_file( $uploaded_file, $extensions, $extension_error_message = null ) {
        $upload_check = Helpers::check_uploaded_file_error( $uploaded_file );

        if ( is_wp_error( $upload_check ) ) {
            return $upload_check;
        }

        if ( ! Helpers::check_uploaded_file_extension( $uploaded_file, $extensions ) ) {
            if ( ! $extension_error_message ) {
                $extension_error_message = __( 'The file uploaded is not the right type.', 'giveasap' );
            }
            return new \WP_Error( 'not-file-type', $extension_error_message );
        }

        return true;
    }

    /**
     * @param $input_name
     * @return false|int|\WP_Error
     */
    public static function upload_image( $input_name = 'image', $extensions = array( 'gif', 'jpg', 'jpeg', 'png' ) ) {
        $uploaded_file = wp_unslash( $_FILES[ $input_name ] );
        $upload_check  = Helpers::check_uploaded_file( $uploaded_file, $extensions, __( 'The file uploaded is not an image', 'giveasap' ) );

        if ( is_wp_error( $upload_check ) ) {
            return $upload_check;
        }

        if ( ! function_exists( 'media_handle_upload' ) ) {
            require_once ABSPATH . 'wp-admin/includes/image.php';
            require_once ABSPATH . 'wp-admin/includes/file.php';
            require_once ABSPATH . 'wp-admin/includes/media.php';
        }

        $attachment_id = media_handle_upload( $input_name, 0 );

        return $attachment_id;
    }
}