<?php

/**
 * Restricts individual file uploads filesize
 *
 * This filter restricts the maximum filesize
 * of an individual upload. It is possible to
 * upload multiple files as long as they are
 * each under this limit.
 *
 * @function restrict_media_upload_limit()
 *
 * @param N/A
 *
 * @return (int) a byte number
 *
 * @since 1.0.0 
 *
 */
function restrict_media_upload_limit() {
	
    return 1024 * 1024 * 5; // 5 MB limit
}
add_filter( 'upload_size_limit', 'restrict_media_upload_limit' ); 



/**
 * Defines the total upload quota
 *
 * This function defines the total upload
 * quota for media files. This differs
 * between packages.
 *
 * @function media_upload_quota()
 *
 * @param N/A
 *
 * @return N/A
 *
 * @since 1.0.0 
 *
 */
function media_upload_quota() {
	
	if ( is_package_4() ) {
		
		$quota = byte_size( 5 );
	} else if ( is_package_3() ) {
		
		$quota = byte_size( 2 );
	} else if ( is_package_2() ) {
		
		$quota = byte_size( 0.2 );
	} else {
		
		$quota = byte_size( 0.1 );
	}
	define( 'OW_DISK_SPACE_QUOTA', $quota );
}
add_action( 'init' , 'media_upload_quota' );



/**
 * Returns the uploads directory size
 *
 * This function returns the uploads
 * directory size.
 *
 * @function uploads_folder_size()
 *
 * @param N/A
 *
 * @return (int) the filesize of the uploads folder in bytes
 *
 * @since 1.0.0 
 *
 */
function uploads_folder_size() {
	
    $bytestotal = 0;
	$uploads = wp_upload_dir();
	$path = $uploads['basedir'];

    if ( $path !== false ){
		
        foreach ( new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $path, FilesystemIterator::SKIP_DOTS ) ) as $object ) {
            $bytestotal += $object->getSize();
        }
    }
	return $bytestotal;
}



/**
 * Convert GigaBytes to Bytes
 *
 * This function converts gigabytes
 * into bytes to allow for
 * comparison of two byte numbers.
 *
 * @function byte_size()
 *
 * @param (int) number in gigabytes
 *
 * @return (int) number in bytes
 *
 * @since 1.0.0 
 *
 */

function byte_size( $size ) {
	
	return $size * pow( 1000, 3 );
}



/**
 * Convert Bytes into MegaBytes or GigaBytes
 *
 * This function converts byte
 * numbers into B, KB, MB, GB,
 * TB, PB filesize types.
 *
 * @function format_size()
 *
 * @param (int) bytes number
 *
 * @return (string) filesize and type
 *
 * @since 1.0.0 
 *
 */
function format_size( $bytes ) {
	
	$types = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB' );
	for( $i = 0; $bytes >= 1000 && $i < ( count( $types ) -1 ); $bytes /= 1000, $i++ );
	return( round( $bytes, 2 ) . ' ' . $types[$i] );
}



/**
 * Generates error message if limit exceeded
 *
 * This function checks the filesize
 * of the upload, and the filesize
 * of the uploads directory. If the
 * filesize exceeds the pre-defined
 * limit set by each package, then
 * an error message is generated
 * and the upload fails.
 *
 * @function check_if_exceeding_upload_limit()
 *
 * @param N/A
 *
 * @return N/A
 *
 * @since 1.0.0 
 *
 */

function check_if_exceeding_upload_limit( $file ){
	
	$size = filesize( $file['tmp_name'] );
	
	if ( uploads_folder_size() >= ( OW_DISK_SPACE_QUOTA - $size ) ) {
		
    	$file['error'] = 'Upload Failed: You have exceeded your disk space quota. Try deleting some files to create some space.';
	}
    return $file;
}
add_filter( 'wp_handle_upload_prefilter', 'check_if_exceeding_upload_limit' );