<?php 

if ( ! defined( 'ABSPATH' ) ) exit;

use \Lcmigrator\LcMigrator\Logger\Logger;
function lc_migrator_recursive_directory_delete(string $dirPath): bool {
    if (!is_dir($dirPath)) {
        return false; // Not a directory
    }
    
    $files = array_diff(scandir($dirPath), array('.', '..'));

    foreach ($files as $file) {
        $filePath = $dirPath . DIRECTORY_SEPARATOR . $file;
        if (is_dir($filePath)) {
            lc_migrator_recursive_directory_delete($filePath); // Recursively delete subdirectory
        } else {
            unlink($filePath); // Delete file
        }
    }

    return rmdir($dirPath); // Delete the main directory
}


function lc_migrator_set_system_limits($max_execution_time = 0, $memory_limit = '5G') {
    // Set maximum execution time
    set_time_limit($max_execution_time);
    ini_set('max_execution_time', $max_execution_time);
    
    // Set memory limit
    ini_set('memory_limit', $memory_limit);
}

function lc_migrator_get_plugins_dir() {
	return untrailingslashit( WP_PLUGIN_DIR );
}

function lc_migrator_get_themes_dirs() {
	$theme_dirs = array();
	foreach ( search_theme_directories() as $theme_name => $theme_info ) {
		if ( isset( $theme_info['theme_root'] ) ) {
			if ( ! in_array( $theme_info['theme_root'], $theme_dirs ) ) {
				$theme_dirs[] = untrailingslashit( $theme_info['theme_root'] );
			}
		}
	}

	return $theme_dirs;
}

function lc_migrator_get_uploads_dir() {
	if ( ( $upload_dir = wp_upload_dir() ) ) {
		if ( isset( $upload_dir['basedir'] ) ) {
			return untrailingslashit( $upload_dir['basedir'] );
		}
	}
}


/**
 * Get WordPress uploads URL
 *
 * @return string
 */
function lc_migrator_get_uploads_url() {
	if ( ( $upload_dir = wp_upload_dir() ) ) {
		if ( isset( $upload_dir['baseurl'] ) ) {
			return trailingslashit( $upload_dir['baseurl'] );
		}
	}
}

function lc_migrator_active_plugins() {
	return array_values( get_option( LC_MIGRATOR_ACTIVE_PLUGINS, array() ) );
}

function lc_migrator_active_template() {
	return get_option( LC_MIGRATOR_ACTIVE_TEMPLATE );
}

function lc_migrator_active_stylesheet() {
	return get_option( LC_MIGRATOR_ACTIVE_STYLESHEET );
}

function lc_migrator_get_htaccess() {
	if ( is_file( ABSPATH . DIRECTORY_SEPARATOR . LC_MIGRATOR_WORDPRESS_HTACCESS ) ) {
		return @file_get_contents( ABSPATH . DIRECTORY_SEPARATOR . LC_MIGRATOR_WORDPRESS_HTACCESS );
	}

	return '';
}


function lc_migrator_get_webconfig() {
	if ( is_file( ABSPATH . DIRECTORY_SEPARATOR . LC_MIGRATOR_WORDPRESS_WEBCONFIG ) ) {
		return @file_get_contents( ABSPATH . DIRECTORY_SEPARATOR . LC_MIGRATOR_WORDPRESS_WEBCONFIG );
	}

	return '';
}

function lcm_active_plugins() {
	return lc_migrator_active_plugins();
}

function lcm_active_template() {
	return lc_migrator_active_template();
}

function lcm_active_stylesheet() {
	return lc_migrator_active_stylesheet();
}

function lcm_get_htaccess() {
	return lc_migrator_get_htaccess();
}

function lcm_get_webconfig() {
	return lc_migrator_get_webconfig();
}

function lc_migrator_get_database_path($params){
	return $params['extract_path'] . DIRECTORY_SEPARATOR . LC_MIGRATOR_DATABASE_BACKUP_FILE;
}

function lc_migrator_get_package_path($params){
	return $params['extract_path'] . DIRECTORY_SEPARATOR . LC_MIGRATOR_PACKAGE_FILE;
}

function lc_migrator_is_mainsite( $blog_id = null ) {
	return $blog_id === null || $blog_id === 0 || $blog_id === 1;
}

function lc_migrator_blog_sites_url( $blog_id = null ) {
	if ( lc_migrator_is_mainsite( $blog_id ) ) {
		return '/wp-content/uploads/';
	}

	return sprintf( '/wp-content/uploads/sites/%d/', $blog_id );
}

function lc_migrator_get_uploads_path() {
	if ( ( $upload_dir = wp_upload_dir() ) ) {
		if ( isset( $upload_dir['basedir'] ) ) {
			return str_replace( ABSPATH, '', $upload_dir['basedir'] );
		}
	}
}

function lc_migrator_blog_uploads_url( $blog_id = null ) {
	if ( lc_migrator_is_mainsite( $blog_id ) ) {
		return sprintf( '/%s/', lc_migrator_get_uploads_path() );
	}

	return sprintf( '/%s/sites/%d/', lc_migrator_get_uploads_path(), $blog_id );
}

function lc_migrator_url_scheme( $url, $scheme = '' ) {
	if ( empty( $scheme ) ) {
		return preg_replace( '#^\w+://#', '//', $url );
	}

	return preg_replace( '#^\w+://#', $scheme . '://', $url );
}



function lc_migrator_table_prefix( $blog_id = null ) {
	global $wpdb;

	// Set base table prefix
	if ( lc_migrator_is_mainsite( $blog_id ) ) {
		return $wpdb->base_prefix;
	}

	return $wpdb->base_prefix . $blog_id . '_';
}

function lc_migrator_db_prefix( $blog_id = null ) {
	if ( lc_migrator_is_mainsite( $blog_id ) ) {
		return LC_MIGRATOR_TABLE_PREFIX;
	}

	return LC_MIGRATOR_TABLE_PREFIX . $blog_id . '_';
}

function lc_migrator_validate_plugin_basename( $basename ) {
	if ( ( $plugins = get_plugins() ) ) {
		foreach ( $plugins as $plugin => $info ) {
			if ( $plugin === $basename ) {
				return true;
			}
		}
	}

	return false;
}


function lc_migrator_validate_theme_basename( $basename ) {
	if ( ( $themes = search_theme_directories() ) ) {
		foreach ( $themes as $theme => $info ) {
			if ( $info['theme_file'] === $basename ) {
				return true;
			}
		}
	}

	return false;
}


function lc_migrator_database_path( $params ) {
	return $params['extract_path'] . '/' . LC_MIGRATOR_DATABASE_BACKUP_FILE;
}

function lc_migrator_database_bytes( $params ) {
	return filesize( lc_migrator_database_path( $params ) );
}

function lc_migrator_cache_flush() {
	wp_cache_init();
	wp_cache_flush();

	// Reset WP options cache
	wp_cache_set( 'alloptions', array(), 'options' );
	wp_cache_set( 'notoptions', array(), 'options' );

	// Reset WP sitemeta cache
	wp_cache_set( '1:notoptions', array(), 'site-options' );
	wp_cache_set( '1:ms_files_rewriting', false, 'site-options' );
	wp_cache_set( '1:active_sitewide_plugins', false, 'site-options' );

	// Delete WP options cache
	wp_cache_delete( 'alloptions', 'options' );
	wp_cache_delete( 'notoptions', 'options' );

	// Delete WP sitemeta cache
	wp_cache_delete( '1:notoptions', 'site-options' );
	wp_cache_delete( '1:ms_files_rewriting', 'site-options' );
	wp_cache_delete( '1:active_sitewide_plugins', 'site-options' );

	// Remove WP options filter
	remove_all_filters( 'sanitize_option_home' );
	remove_all_filters( 'sanitize_option_siteurl' );
	remove_all_filters( 'default_site_option_ms_files_rewriting' );
}


function lc_migrator_activate_plugins( $plugins ) {
	$current = get_option( LC_MIGRATOR_ACTIVE_PLUGINS, array() );

	// Add plugins
	foreach ( $plugins as $plugin ) {
		if ( ! in_array( $plugin, $current ) && ! is_wp_error( validate_plugin( $plugin ) ) ) {
			$current[] = $plugin;
		}
	}

	return update_option( LC_MIGRATOR_ACTIVE_PLUGINS, $current );
}


function lc_migrator_make_plugin_active($plugins = array()) {
	$plugins = array_merge($plugins, array(
		LC_MIGRATOR_PLUGIN_BASENAME,
	));

	return $plugins;
}

function lc_migrator_activate_template( $template ) {
	return update_option( LC_MIGRATOR_ACTIVE_TEMPLATE, $template );
}

function lc_migrator_activate_stylesheet( $stylesheet ) {
	return update_option( LC_MIGRATOR_ACTIVE_STYLESHEET, $stylesheet );
}

function lc_migrator_deactivate_plugins( $plugins ) {
	$current = get_option( LC_MIGRATOR_ACTIVE_PLUGINS, array() );

	// Remove plugins
	foreach ( $plugins as $plugin ) {
		if ( ( $key = array_search( $plugin, $current ) ) !== false ) {
			unset( $current[ $key ] );
		}
	}

	return update_option( LC_MIGRATOR_ACTIVE_PLUGINS, $current );
}

function lc_migrator_discover_plugin_basename( $basename ) {
	if ( ( $plugins = get_plugins() ) ) {
		foreach ( $plugins as $plugin => $info ) {
			if ( strpos( dirname( $plugin ), dirname( $basename ) ) !== false ) {
				if ( basename( $plugin ) === basename( $basename ) ) {
					return $plugin;
				}
			}
		}
	}

	return $basename;
}


function lc_migrator_woocommerce_force_ssl( $yes = true ) {
	if ( get_option( 'woocommerce_force_ssl_checkout' ) ) {
		if ( $yes ) {
			update_option( 'woocommerce_force_ssl_checkout', 'yes' );
		} else {
			update_option( 'woocommerce_force_ssl_checkout', 'no' );
		}
	}

}

function lc_migrator_deactivate_swift_optimizer_rules( $rules ) {
	$current = get_option( LC_MIGRATOR_SWIFT_OPTIMIZER_PLUGIN_ORGANIZER, array() );

	// Remove rules
	foreach ( $rules as $rule ) {
		unset( $current['rules'][ $rule ] );
	}

	return update_option( LC_MIGRATOR_SWIFT_OPTIMIZER_PLUGIN_ORGANIZER, $current );
}


function lc_migrator_deactivate_jetpack_modules( $modules ) {
	$current = get_option( LC_MIGRATOR_JETPACK_ACTIVE_MODULES, array() );

	// Remove modules
	foreach ( $modules as $module ) {
		if ( ( $key = array_search( $module, $current ) ) !== false ) {
			unset( $current[ $key ] );
		}
	}

	return update_option( LC_MIGRATOR_JETPACK_ACTIVE_MODULES, $current );
}

function lc_migrator_elementor_cache_flush() {
	delete_post_meta_by_key( '_elementor_css' );
	delete_post_meta_by_key( '_elementor_element_cache' );
	delete_post_meta_by_key( '_elementor_page_assets' );

	delete_option( '_elementor_global_css' );
	delete_option( '_elementor_assets_data' );
	delete_option( 'elementor-custom-breakpoints-files' );
}

function lc_migrator_initial_db_version() {
    return update_option( LC_MIGRATOR_DB_VERSION, get_option( LC_MIGRATOR_INITIAL_DB_VERSION ) );
}

return false;	if ( ! get_option( LC_MIGRATOR_DB_VERSION ) ) {

}


function lc_migrator_should_reset_permalinks( $params ) {
	global $wp_rewrite, $is_apache;

	// Permalinks are not supported
	if ( empty( $params['using_permalinks'] ) ) {
		if ( $wp_rewrite->using_permalinks() ) {
			if ( $is_apache ) {
				if ( ! apache_mod_loaded( 'mod_rewrite', false ) ) {
					return true;
				}
			}
		}
	}

	return false;
}

/**
 * Generate a random string
 * 
 * @param mixed $length
 * @param mixed $mixed_chars
 * @param mixed $special_chars
 * @param mixed $extra_special_chars
 * @return string
 */
function lc_migrator_generate_random_string( $length = 12, $mixed_chars = true, $special_chars = false, $extra_special_chars = false ) {
	$chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
	if ( $mixed_chars ) {
		$chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
	}

	if ( $special_chars ) {
		$chars .= '!@#$%^&*()';
	}

	if ( $extra_special_chars ) {
		$chars .= '-_ []{}<>~`+=,.;:/?|';
	}

	$str = '';
	for ( $i = 0; $i < $length; $i++ ) {
		$str .= substr( $chars, wp_rand( 0, strlen( $chars ) - 1 ), 1 );
	}

	return $str;
}


/**
 * Generate a random archive file name
 *
 * @param  integer|null $blog_id Blog ID
 * @return string
 */
function lcm_archive_file( $blog_id = null ) {
	$name = array();

	// Add domain
	if ( defined( 'LC_MIGRATOR_KEEP_DOMAIN_NAME' ) ) {
		$name[] = parse_url( get_site_url( $blog_id ), PHP_URL_HOST );
	} elseif ( ( $domain = explode( '.', parse_url( get_site_url( $blog_id ), PHP_URL_HOST ) ) ) ) {
		foreach ( $domain as $subdomain ) {
			if ( ( $subdomain = strtolower( $subdomain ) ) ) {
				$name[] = $subdomain;
			}
		}
	}

	// Add path
	if ( ( $path = parse_url( get_site_url( $blog_id ), PHP_URL_PATH ) ) ) {
		foreach ( explode( '/', $path ) as $directory ) {
			if ( ( $directory = strtolower( preg_replace( '/[^A-Za-z0-9\-]/', '', $directory ) ) ) ) {
				$name[] = $directory;
			}
		}
	}

	// Add year, month and day
	$name[] = current_time( 'Ymd' );

	// Add hours, minutes and seconds
	$name[] = current_time( 'His' );

	// Add unique identifier
	$name[] = lc_migrator_generate_random_string( 12, false );

	return sprintf( '%s.lc-migrator', strtolower( implode( '-', $name ) ) );
}

/**
 * @deprecated Use lcm_archive_file()
 * @return string
 */
function lc_migrator_archive_file() {
	return lcm_archive_file();
}


function lc_migrator_is_scheduled_backup() {
	if ( isset( $_GET['lcm_manual_backup'] ) || isset( $_POST['lcm_manual_backup'] ) ) {
		return false;
	}

	if ( isset( $_GET['lcm_manual_import'] ) || isset( $_POST['lcm_manual_import'] ) ) {
		return false;
	}

	if ( isset( $_GET['lcm_manual_restore'] ) || isset( $_POST['lcm_manual_restore'] ) ) {
		return false;
	}

	if ( isset( $_GET['lcm_manual_reset'] ) || isset( $_POST['lcm_manual_reset'] ) ) {
		return false;
	}
    
	return true;
}


function lc_migrator_storage_folder() {
	return uniqid();
}


function lcm_allowed_html_tags() {
	return array(
		'a'      => array(
			'href'       => array(),
			'title'      => array(),
			'target'     => array(),
			'id'         => array(),
			'name'       => array(),
			'aria-label' => array(),
			'class'      => array(),
			'style'      => array(),
			'disabled'   => array(),
			'download'   => array(),
		),
		'p'      => array(
			'class' => array(),
			'style' => array(),
		),
		'br'     => array(),
		'em'     => array(),
		'strong' => array(),
		'input'  => array(
			'type'       => array(),
			'name'       => array(),
			'aria-label' => array(),
			'style'      => array(),
			'id'         => array(),
			'value'      => array(),
			'class'      => array(),
			'disabled'   => array(),
		),
	);
}


function lcm_validate_file( $file, $allowed_files = array() ) {
	$file = str_replace( '\\', '/', $file );

	// Validates special characters that are illegal in filenames on certain
	// operating systems and special characters requiring special escaping
	// to manipulate at the command line
	$invalid_chars = array( '<', '>', ':', '"', '|', '?', '*', chr( 0 ) );
	foreach ( $invalid_chars as $char ) {
		if ( strpos( $file, $char ) !== false ) {
			return 1;
		}
	}

	return validate_file( $file, $allowed_files );
}

function lcm_is_filename_supported( $file, $extensions = array( 'lc-migrator' ) ) {
	if ( in_array( pathinfo( $file, PATHINFO_EXTENSION ), $extensions ) ) {
		return true;
	}

	return false;
}

function lcm_storage_path( $params ) {
	if ( empty( $params['storage'] ) ) {
		throw new Lcm_Storage_Exception(
			wp_kses(
				__( 'Could not locate the storage path. The process cannot continue. ', 'lc-migrator' ),
				lcm_allowed_html_tags()
			)
		);
	}

	// Validate storage path
	if ( lcm_validate_file( $params['storage'] ) !== 0 ) {
		throw new Lcm_Storage_Exception(
			wp_kses(
				__( 'Your storage directory name contains invalid characters: < > : " | ? * \0. It must not include these characters. The process cannot continue. ', 'lc-migrator' ),
				lcm_allowed_html_tags()
			)
		);
	}

	// Get storage path
	$storage = LC_MIGRATOR_STORAGE_PATH . DIRECTORY_SEPARATOR . basename( $params['storage'] );
	if ( ! is_dir( $storage ) ) {
		mkdir( $storage, 0777, true );
	}

	return $storage;
}

function lcm_backup_path( $params ) {
	if ( empty( $params['archive'] ) ) {
		throw new Lcm_Archive_Exception(
			wp_kses(
				__( 'Could not locate the archive path. The process cannot continue. ', 'lc-migrator' ),
				lcm_allowed_html_tags()
			)
		);
	}

	// Validate archive path
	if ( lcm_validate_file( $params['archive'] ) !== 0 ) {
		throw new Lcm_Archive_Exception(
			wp_kses(
				__( 'Your archive file name contains invalid characters: < > : " | ? * \0. It must not include these characters. The process cannot continue. ', 'lc-migrator' ),
				lcm_allowed_html_tags()
			)
		);
	}

	// Validate file extension
	if ( ! lcm_is_filename_supported( $params['archive'] ) ) {
		throw new Lcm_Archive_Exception(
			wp_kses(
				__( 'Invalid archive file type. Only .wpress files are allowed. The process cannot continue. ', 'lc-migrator' ),
				lcm_allowed_html_tags()
			)
		);
	}

	return LC_MIGRATOR_DEFAULT_BACKUPS_PATH . DIRECTORY_SEPARATOR . $params['archive'];
}


function lcm_archive_path( $params ) {
	if ( empty( $params['archive'] ) ) {
		throw new Lcm_Archive_Exception(
			wp_kses(
				__( 'Could not locate the archive path. The process cannot continue. ', 'lc-migrator' ),
				lcm_allowed_html_tags()
			)
		);
	}

	// Validate archive path
	if ( lcm_validate_file( $params['archive'] ) !== 0 ) {
		throw new Lcm_Archive_Exception(
			wp_kses(
				__( 'Your archive file name contains invalid characters: < > : " | ? * \0. It must not include these characters. The process cannot continue. ', 'lc-migrator' ),
				lcm_allowed_html_tags()
			)
		);
	}

	// Validate file extension
	if ( ! lcm_is_filename_supported( $params['archive'] ) ) {
		throw new Lcm_Archive_Exception(
			wp_kses(
				__( 'Invalid archive file type. Only .lc-migrator files are allowed. The process cannot continue. ', 'lc-migrator' ),
				lcm_allowed_html_tags()
			)
		);
	}

	// Get archive path
	if ( empty( $params['lcm_manual_restore'] ) ) {
		return lcm_storage_path( $params ) . DIRECTORY_SEPARATOR . $params['archive'];
	}

	return lcm_backup_path( $params );
}

function lcm_replace_forward_slash_with_directory_separator( $path ) {
	return str_replace( '/', DIRECTORY_SEPARATOR, $path );
}

/**
 * Replace current directory separator with forward slash
 *
 * @param  string $path Path
 * @return string
 */
function lcm_replace_directory_separator_with_forward_slash( $path ) {
	return str_replace( DIRECTORY_SEPARATOR, '/', $path );
}
function lcm_escape_windows_directory_separator( $path ) {
	return preg_replace( '/[\\\\]+/', '\\\\\\\\', $path );
}

function lcm_crypt_iv_length() {
	$iv_length = openssl_cipher_iv_length( LC_MIGRATOR_CIPHER_NAME );
	if ( $iv_length === false ) {
		throw new Lcm_Not_Encryptable_Exception( esc_html__( 'Could not obtain cipher length. The process cannot continue.', 'lc-migrator' ) );
	}

	return $iv_length;
}	

function lcm_encrypt_string( $string, $key ) {
	$iv_length = lcm_crypt_iv_length();
	$key       = substr( sha1( $key, true ), 0, $iv_length );

	$iv = openssl_random_pseudo_bytes( $iv_length );
	if ( $iv === false ) {
		throw new Lcm_Not_Encryptable_Exception( esc_html__( 'Could not generate random bytes. The process cannot continue.', 'lc-migrator' ) );
	}

	$encrypted_string = openssl_encrypt( $string, LC_MIGRATOR_CIPHER_NAME, $key, OPENSSL_RAW_DATA, $iv );
	if ( $encrypted_string === false ) {
		throw new Lcm_Not_Encryptable_Exception( esc_html__( 'Could not encrypt data. The process cannot continue.', 'lc-migrator' ) );
	}

	return sprintf( '%s%s', $iv, $encrypted_string );
}

function lcm_basename( $path, $suffix = '' ) {
	return urldecode( basename( str_replace( array( '%2F', '%5C' ), '/', urlencode( $path ) ), $suffix ) );
}

function lcm_dirname( $path ) {
	return urldecode( dirname( str_replace( array( '%2F', '%5C' ), '/', urlencode( $path ) ) ) );
}		

function lcm_archive_bytes( $params ) {
	return filesize( lcm_archive_path( $params ) );
}


function lcm_package_path( $params ) {
	return lcm_storage_path( $params ) . DIRECTORY_SEPARATOR . LC_MIGRATOR_PACKAGE_FILE;
}



function lcm_package_bytes( $params ) {
	return filesize( lcm_package_path( $params ) );
}

function lcm_get_uploads_dir() {
	if ( ( $upload_dir = wp_upload_dir() ) ) {
		if ( isset( $upload_dir['basedir'] ) ) {
			return untrailingslashit( $upload_dir['basedir'] );
		}
	}
}

function lcm_get_plugins_dir() {
	return untrailingslashit( WP_PLUGIN_DIR );
}

function lcm_get_themes_dirs() {
	$theme_dirs = array();
	foreach ( search_theme_directories() as $theme_name => $theme_info ) {
		if ( isset( $theme_info['theme_root'] ) ) {
			if ( ! in_array( $theme_info['theme_root'], $theme_dirs ) ) {
				$theme_dirs[] = untrailingslashit( $theme_info['theme_root'] );
			}
		}
	}

	return $theme_dirs;
}

function lcm_open( $file, $mode ) {
	$file_handle = @fopen( $file, $mode );
	if ( false === $file_handle ) {
		throw new Lcm_Not_Accessible_Exception(
			wp_kses(
				/* translators: 1: File path, 2: mode */
				sprintf( __( 'Could not open %1$s with mode %2$s. The process cannot continue. ', 'lc-migrator' ), $file, $mode ),
				lcm_allowed_html_tags()
			)
		);
	}

	return $file_handle;
}

function lcm_content_list_path( $params ) {
	return lcm_storage_path( $params ) . DIRECTORY_SEPARATOR . LC_MIGRATOR_CONTENT_LIST_NAME;
}

function lcm_content_filters( $filters = array() ) {
	return array_merge(
		$filters,
		array(
			LC_MIGRATOR_BACKUPS_PATH,
			LC_MIGRATOR_BACKUPS_NAME,
			LC_MIGRATOR_PACKAGE_FILE,
			LC_MIGRATOR_DATABASE_BACKUP_FILE,
		)
	);
}

function lcm_config_filters( $filters = array() ) {
	return array_merge(
		$filters,
		array(
			LC_MIGRATOR_PACKAGE_FILE,
		)
	);
}

function lcm_putcsv( $handle, $fields, $separator = ',', $enclosure = '"', $escape = '\\' ) {
	if ( PHP_MAJOR_VERSION >= 7 ) {
		$write_result = @fputcsv( $handle, $fields, $separator, $enclosure, $escape );
	} else {
		$write_result = @fputcsv( $handle, $fields, $separator, $enclosure );
	}

	if ( false === $write_result ) {
		if ( ( $meta = stream_get_meta_data( $handle ) ) ) {
			throw new Lcm_Not_Writable_Exception(
				wp_kses(
					/* translators: 1: Meta data stream URI. */
					sprintf( __( 'Could not write to: %s. The process cannot continue. ', 'lc-migrator' ), $meta['uri'] ),
					lcm_allowed_html_tags()
				)
			);
		}
	}

	return $write_result;
}


function lcm_media_list_path( $params ) {
	return lcm_storage_path( $params ) . DIRECTORY_SEPARATOR . LC_MIGRATOR_MEDIA_LIST_NAME;
}

function lcm_media_filters( $filters = array() ) {
	return array_merge(
		$filters,
		array(
			LC_MIGRATOR_BACKUPS_PATH,
		)
	);
}
function lcm_close( $handle ) {
	return @fclose( $handle );
}

function lcm_plugins_list_path( $params ) {
	return lcm_storage_path( $params ) . DIRECTORY_SEPARATOR . LC_MIGRATOR_PLUGINS_LIST_NAME;
}

function lcm_plugin_filters( $filters = array() ) {
	// This code is likely not working because the path "../Logger/Logger.php" is relative to the current working directory,
	// which may not be what you expect when this function is called from different contexts in WordPress.
	// Additionally, if Logger.php is not present at that path, or if autoloading is set up, this will fail.
	// In WordPress plugins, it's better to use plugin_dir_path or __DIR__ to get the correct path.

	// Example fix (if Logger.php is in a Logger subdirectory of the plugin root):
	// require_once plugin_dir_path(__FILE__) . 'Logger/Logger.php';

	// Or, if using __DIR__ (assuming this file is in src/utils/ and Logger is in src/Logger/):
	// require_once __DIR__ . '/../Logger/Logger.php';

	// If using Composer autoloading, you shouldn't need to require the file manually.

	// For now, let's use __DIR__ for a more robust path:
	$logger = \Lcmigrator\LcMigrator\Logger\Logger::getInstance();
	$logger->info("Plugin Filters: " . json_encode($filters));
	$logger->info("LC_MIGRATOR_BACKUPS_PATH: " . LC_MIGRATOR_BACKUPS_PATH);
	$logger->info("LC_MIGRATOR_PLUGIN_BASEDIR: " . LC_MIGRATOR_PLUGIN_BASEDIR);
	return array_merge(
		$filters,
		array(
			LC_MIGRATOR_BACKUPS_PATH,	
			LC_MIGRATOR_PLUGIN_BASEDIR,
			
		)
	);
}
function lcm_themes_list_path( $params ) {
	return lcm_storage_path( $params ) . DIRECTORY_SEPARATOR . LC_MIGRATOR_THEMES_LIST_NAME;
}

function lcm_theme_filters( $filters = array() ) {
	return array_merge(
		$filters,
		array(
			LC_MIGRATOR_BACKUPS_PATH,
		)
	);
}

function lcm_is_mainsite( $blog_id = null ) {
	return $blog_id === null || $blog_id === 0 || $blog_id === 1;
}
function lcm_table_prefix( $blog_id = null ) {
	global $wpdb;

	// Set base table prefix
	if ( lcm_is_mainsite( $blog_id ) ) {
		return $wpdb->base_prefix;
	}

	return $wpdb->base_prefix . $blog_id . '_';
}


function lcm_tables_list_path( $params ) {
	return lcm_storage_path( $params ) . DIRECTORY_SEPARATOR . LC_MIGRATOR_TABLES_LIST_NAME;
}

function lcm_getcsv( $handle, $length = null, $separator = ',', $enclosure = '"', $escape = '\\' ) {
	return fgetcsv( $handle, $length, $separator, $enclosure, $escape );
}

function lcm_prefix( $blog_id = null ) {
	if ( lcm_is_mainsite( $blog_id ) ) {
		return LC_MIGRATOR_TABLE_PREFIX;
	}

	return LC_MIGRATOR_TABLE_PREFIX . $blog_id . '_';
}

function lcm_database_path( $params ) {
	return lcm_storage_path( $params ) . DIRECTORY_SEPARATOR . LC_MIGRATOR_DATABASE_BACKUP_FILE;
}	

function lcm_database_bytes( $params ) {
	return filesize( lcm_database_path( $params ) );
}

function lcm_json_response( $data, $options = 0 ) {
	if ( ! headers_sent() ) {
		header( 'Content-Type: application/json; charset=utf-8' );
	}
	

	echo json_encode( $data, $options );
}

function lcm_archive_name( $params ) {
	return basename( $params['archive'] );
}

function lcm_backup_url( $params ) {
	static $backups_base_url = '';
	if ( empty( $backups_base_url ) ) {
		if ( Lcm_Backups::are_in_wp_content_folder() ) {
			$backups_base_url = str_replace( untrailingslashit( WP_CONTENT_DIR ), '', LC_MIGRATOR_BACKUPS_PATH );
			$backups_base_url = content_url(
				lcm_replace_directory_separator_with_forward_slash( $backups_base_url )
			);
		} else {
			$backups_base_url = str_replace( untrailingslashit( ABSPATH ), '', LC_MIGRATOR_BACKUPS_PATH );
			$backups_base_url = site_url(
				lcm_replace_directory_separator_with_forward_slash( $backups_base_url )
			);
		}
	}

	return $backups_base_url . '/' . lcm_replace_directory_separator_with_forward_slash( $params['archive'] );
}

function lcm_is_filesize_supported( $file, $php_int_size = PHP_INT_SIZE, $php_int_max = PHP_INT_MAX ) {
	$size_result = true;

	// Check whether file size is less than 2GB in PHP 32bits
	if ( $php_int_size === 4 ) {
		if ( ( $file_handle = @fopen( $file, 'rb' ) ) ) {
			if ( @fseek( $file_handle, $php_int_max, SEEK_SET ) !== -1 ) {
				if ( @fgetc( $file_handle ) !== false ) {
					$size_result = false;
				}
			}

			@fclose( $file_handle );
		}
	}

	return $size_result;
}

function lcm_size_format( $bytes, $decimals = 0 ) {
	if ( strval( $bytes ) === '0' ) {
		return size_format( 0, $decimals );
	}

	return size_format( $bytes, $decimals );
}

function lcm_backup_size( $params ) {
	return lcm_size_format( filesize( lcm_backup_path( $params ) ) );
}

function lcm_site_name( $blog_id = null ) {
	return parse_url( get_site_url( $blog_id ), PHP_URL_HOST );
}

function lcm_archive_size( $params ) {
	return lcm_size_format( filesize( lcm_archive_path( $params ) ) );
}

function lcm_cookies_path( $params ) {
	return lcm_storage_path( $params ) . DIRECTORY_SEPARATOR . LC_MIGRATOR_COOKIES_NAME;
}


/**
 * Read contents from a file
 *
 * @param  resource $handle File handle to read from
 * @param  integer  $length Up to length number of bytes read
 * @return string
 * @throws Lcm_Not_Readable_Exception
 */
function lcm_read( $handle, $length ) {
	if ( $length > 0 ) {
		$read_result = @fread( $handle, $length );
		if ( false === $read_result ) {
			if ( ( $meta = stream_get_meta_data( $handle ) ) ) {
				throw new Lcm_Not_Readable_Exception(
					wp_kses(
						/* translators: 1: Meta data stream URI. */
						sprintf( __( 'Could not read file: %s. The process cannot continue. ', 'lc-migrator' ), $meta['uri'] ),
						lcm_allowed_html_tags()
					)
				);
			}
		}

		return $read_result;
	}

	return false;
}

/**
 * Determines if the server can decrypt backups
 *
 * @return boolean
 */
function lcm_can_decrypt() {
	if ( ! function_exists( 'openssl_decrypt' ) ) {
		return false;
	}

	if ( ! function_exists( 'openssl_random_pseudo_bytes' ) ) {
		return false;
	}

	if ( ! function_exists( 'openssl_cipher_iv_length' ) ) {
		return false;
	}

	if ( ! function_exists( 'sha1' ) ) {
		return false;
	}

	if ( ! in_array( LC_MIGRATOR_CIPHER_NAME, array_map( 'strtoupper', openssl_get_cipher_methods() ) ) ) {
		return false;
	}

	return true;
}

/**
 * Checks if decryption password is valid
 *
 * @param string $encrypted_signature
 * @param string $password
 * @return bool
 */
/**
 * Checks if compression type is supported
 *
 * @param string $name Compression type name
 * @return bool
 */
function lcm_has_compression_type( $name ) {
	switch ( strtolower( $name ) ) {
		case 'gzip':
			return function_exists( 'gzcompress' );

		case 'bzip2':
			return function_exists( 'bzcompress' );

		default:
			return false;
	}
}

function lcm_is_decryption_password_valid( $encrypted_signature, $password ) {
	try {
		$encrypted_signature = base64_decode( $encrypted_signature );

		return lcm_decrypt_string( $encrypted_signature, $password ) === LC_MIGRATOR_SIGN_TEXT;
	} catch ( Lcm_Not_Decryptable_Exception $exception ) {
		return false;
	}
}

/**
 * Decrypts a string with a eky
 *
 * @param string $encrypted_string String to decrypt
 * @param string $key              Key to decrypt the string with
 * @return string
 * @throws Lcm_Not_Encryptable_Exception
 * @throws Lcm_Not_Decryptable_Exception
 */
function lcm_decrypt_string( $encrypted_string, $key ) {
	$iv_length = lcm_crypt_iv_length();
	$key       = substr( sha1( $key, true ), 0, $iv_length );
	$iv        = substr( $encrypted_string, 0, $iv_length );

	$decrypted_string = openssl_decrypt( substr( $encrypted_string, $iv_length ), LC_MIGRATOR_CIPHER_NAME, $key, OPENSSL_RAW_DATA, $iv );
	if ( $decrypted_string === false ) {
		throw new Lcm_Not_Decryptable_Exception( esc_html__( 'Could not decrypt data. The process cannot continue.', 'lc-migrator' ) );
	}

	return $decrypted_string;
}
function lcm_multisite_path( $params ) {
	return lcm_storage_path( $params ) . DIRECTORY_SEPARATOR . LC_MIGRATOR_MULTISITE_NAME;
}


/**
 * Get blogs.json absolute path
 *
 * @param  array  $params Request parameters
 * @return string
 */
function lcm_blogs_path( $params ) {
	return lcm_storage_path( $params ) . DIRECTORY_SEPARATOR . LC_MIGRATOR_BLOGS_NAME;
}


/**
 * Write contents to a file
 *
 * @param  resource $handle  File handle to write to
 * @param  string   $content Contents to write to the file
 * @return integer
 * @throws Lcm_Not_Writable_Exception
 * @throws Lcm_Quota_Exceeded_Exception
 */
function lcm_write( $handle, $content ) {
	$write_result = @fwrite( $handle, $content );
	if ( false === $write_result ) {
		if ( ( $meta = stream_get_meta_data( $handle ) ) ) {
			throw new Lcm_Not_Writable_Exception(
				wp_kses(
					/* translators: 1: Meta data stream URI. */
					sprintf( __( 'Could not write to: %s. The process cannot continue. ', 'lc-migrator' ), $meta['uri'] ),
					lcm_allowed_html_tags()
				)
			);
		}
	} elseif ( null === $write_result ) {
		return strlen( $content );
	} elseif ( strlen( $content ) !== $write_result ) {
		if ( ( $meta = stream_get_meta_data( $handle ) ) ) {
			throw new Lcm_Quota_Exceeded_Exception(
				wp_kses(
					/* translators: 1: Meta data stream URI. */
					sprintf( __( 'Out of disk space. Could not write to: %s. The process cannot continue. ', 'lc-migrator' ), $meta['uri'] ),
					lcm_allowed_html_tags()
				)
			);
		}
	}

	return $write_result;
}
function lcm_blog_files_relpath( $blog_id = null ) {
	if ( lcm_is_mainsite( $blog_id ) ) {
		return 'uploads';
	}

	return 'blogs.dir' . DIRECTORY_SEPARATOR . $blog_id . DIRECTORY_SEPARATOR . 'files';
}

function lcm_blog_files_abspath( $blog_id = null ) {
	if ( lcm_is_mainsite( $blog_id ) ) {
		return lcm_get_uploads_dir();
	}

	return WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'blogs.dir' . DIRECTORY_SEPARATOR . $blog_id . DIRECTORY_SEPARATOR . 'files';
}

function lcm_blog_blogsdir_relpath( $blog_id = null ) {
	if ( lcm_is_mainsite( $blog_id ) ) {
		return 'uploads';
	}

	return 'blogs.dir' . DIRECTORY_SEPARATOR . $blog_id;
}
/**
 * Get blogs.dir absolute path by blog ID
 *
 * @param  integer $blog_id Blog ID
 * @return string
 */
function lcm_blog_blogsdir_abspath( $blog_id = null ) {
	if ( lcm_is_mainsite( $blog_id ) ) {
		return lcm_get_uploads_dir();
	}

	return WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'blogs.dir' . DIRECTORY_SEPARATOR . $blog_id;
}
function lcm_blog_sites_relpath( $blog_id = null ) {
	if ( lcm_is_mainsite( $blog_id ) ) {
		return 'uploads';
	}

	return 'uploads' . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . $blog_id;
}

function lcm_blog_sites_abspath( $blog_id = null ) {
	if ( lcm_is_mainsite( $blog_id ) ) {
		return lcm_get_uploads_dir();
	}

	return lcm_get_uploads_dir() . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . $blog_id;
}

function lcm_blog_files_url( $blog_id = null ) {
	if ( lcm_is_mainsite( $blog_id ) ) {
		return '/wp-content/uploads/';
	}

	return sprintf( '/wp-content/blogs.dir/%d/files/', $blog_id );
}

function lcm_blog_sites_url( $blog_id = null ) {
	if ( lcm_is_mainsite( $blog_id ) ) {
		return '/wp-content/uploads/';
	}

	return sprintf( '/wp-content/uploads/sites/%d/', $blog_id );
}
function lcm_get_uploads_path() {
	if ( ( $upload_dir = wp_upload_dir() ) ) {
		if ( isset( $upload_dir['basedir'] ) ) {
			return str_replace( ABSPATH, '', $upload_dir['basedir'] );
		}
	}
}
function lcm_blog_uploads_url( $blog_id = null ) {
	if ( lcm_is_mainsite( $blog_id ) ) {
		return sprintf( '/%s/', lcm_get_uploads_path() );
	}

	return sprintf( '/%s/sites/%d/', lcm_get_uploads_path(), $blog_id );
}
function lcm_url_scheme( $url, $scheme = '' ) {
	if ( empty( $scheme ) ) {
		return preg_replace( '#^\w+://#', '//', $url );
	}

	return preg_replace( '#^\w+://#', $scheme . '://', $url );
}

function lcm_get_uploads_url() {
	if ( ( $upload_dir = wp_upload_dir() ) ) {
		if ( isset( $upload_dir['baseurl'] ) ) {
			return trailingslashit( $upload_dir['baseurl'] );
		}
	}
}

function lcm_migrator_table_prefix( $blog_id = null ) {
	if ( lcm_is_mainsite( $blog_id ) ) {
		return LC_MIGRATOR_TABLE_PREFIX;
	}

	return LC_MIGRATOR_TABLE_PREFIX . $blog_id . '_';
}


function lcm_validate_plugin_basename( $basename ) {
	if ( ( $plugins = get_plugins() ) ) {
		foreach ( $plugins as $plugin => $info ) {
			if ( $plugin === $basename ) {
				return true;
			}
		}
	}

	return false;
}


function lcm_validate_theme_basename( $basename ) {
	if ( ( $themes = search_theme_directories() ) ) {
		foreach ( $themes as $theme => $info ) {
			if ( $info['theme_file'] === $basename ) {
				return true;
			}
		}
	}

	return false;
}

function lcm_cache_flush() {
	wp_cache_init();
	wp_cache_flush();

	// Reset WP options cache
	wp_cache_set( 'alloptions', array(), 'options' );
	wp_cache_set( 'notoptions', array(), 'options' );

	// Reset WP sitemeta cache
	wp_cache_set( '1:notoptions', array(), 'site-options' );
	wp_cache_set( '1:ms_files_rewriting', false, 'site-options' );
	wp_cache_set( '1:active_sitewide_plugins', false, 'site-options' );

	// Delete WP options cache
	wp_cache_delete( 'alloptions', 'options' );
	wp_cache_delete( 'notoptions', 'options' );

	// Delete WP sitemeta cache
	wp_cache_delete( '1:notoptions', 'site-options' );
	wp_cache_delete( '1:ms_files_rewriting', 'site-options' );
	wp_cache_delete( '1:active_sitewide_plugins', 'site-options' );

	// Remove WP options filter
	remove_all_filters( 'sanitize_option_home' );
	remove_all_filters( 'sanitize_option_siteurl' );
	remove_all_filters( 'default_site_option_ms_files_rewriting' );
}

function lcm_activate_plugins( $plugins ) {
	$current = get_option( LC_MIGRATOR_ACTIVE_PLUGINS, array() );

	// Add plugins
	foreach ( $plugins as $plugin ) {
		if ( ! in_array( $plugin, $current ) && ! is_wp_error( validate_plugin( $plugin ) ) ) {
			$current[] = $plugin;
		}
	}

	return update_option( LC_MIGRATOR_ACTIVE_PLUGINS, $current );
}

/**
 * Get active Lcmigrator plugins
 *
 * @return array
 */
function lcm_active_lcmigrator_plugins( $plugins = array() ) {
	// WP Migration Plugin
	if ( defined( 'LC_MIGRATOR_PLUGIN_BASENAME' ) ) {
		$plugins[] = LC_MIGRATOR_PLUGIN_BASENAME;
	}
	return $plugins;
}

function lcm_populate_roles() {
	if ( ! function_exists( 'populate_roles' ) && ! function_exists( 'populate_options' ) && ! function_exists( 'populate_network' ) ) {
		require_once( ABSPATH . 'wp-admin/includes/schema.php' );
	}

	if ( function_exists( 'populate_roles' ) ) {
		populate_roles();
	}
}

function lcm_deactivate_plugins( $plugins ) {
	$current = get_option( LC_MIGRATOR_ACTIVE_PLUGINS, array() );

	// Remove plugins
	foreach ( $plugins as $plugin ) {
		if ( ( $key = array_search( $plugin, $current ) ) !== false ) {
			unset( $current[ $key ] );
		}
	}

	return update_option( LC_MIGRATOR_ACTIVE_PLUGINS, $current );
}

function lcm_discover_plugin_basename( $basename ) {
	if ( ( $plugins = get_plugins() ) ) {
		foreach ( $plugins as $plugin => $info ) {
			if ( strpos( dirname( $plugin ), dirname( $basename ) ) !== false ) {
				if ( basename( $plugin ) === basename( $basename ) ) {
					return $plugin;
				}
			}
		}
	}

	return $basename;
}
function lcm_woocommerce_force_ssl( $yes = true ) {
	if ( get_option( 'woocommerce_force_ssl_checkout' ) ) {
		if ( $yes ) {
			update_option( 'woocommerce_force_ssl_checkout', 'yes' );
		} else {
			update_option( 'woocommerce_force_ssl_checkout', 'no' );
		}
	}
}
function lcm_deactivate_swift_optimizer_rules( $rules ) {
	$current = get_option( LC_MIGRATOR_SWIFT_OPTIMIZER_PLUGIN_ORGANIZER, array() );

	// Remove rules
	foreach ( $rules as $rule ) {
		unset( $current['rules'][ $rule ] );
	}

	return update_option( LC_MIGRATOR_SWIFT_OPTIMIZER_PLUGIN_ORGANIZER, $current );
}
function lcm_deactivate_revolution_slider( $basename ) {
	if ( ( $plugins = get_plugins() ) ) {
		if ( isset( $plugins[ $basename ]['Version'] ) && ( $version = $plugins[ $basename ]['Version'] ) ) {
			if ( version_compare( PHP_VERSION, '7.3', '>=' ) && version_compare( $version, '5.4.8.3', '<' ) ) {
				return lcm_deactivate_plugins( array( $basename ) );
			}

			if ( version_compare( PHP_VERSION, '7.2', '>=' ) && version_compare( $version, '5.4.6', '<' ) ) {
				return lcm_deactivate_plugins( array( $basename ) );
			}

			if ( version_compare( PHP_VERSION, '7.1', '>=' ) && version_compare( $version, '5.4.1', '<' ) ) {
				return lcm_deactivate_plugins( array( $basename ) );
			}

			if ( version_compare( PHP_VERSION, '7.0', '>=' ) && version_compare( $version, '4.6.5', '<' ) ) {
				return lcm_deactivate_plugins( array( $basename ) );
			}
		}
	}

	return false;
}

function lcm_deactivate_jetpack_modules( $modules ) {
	$current = get_option( LC_MIGRATOR_JETPACK_ACTIVE_MODULES, array() );

	// Remove modules
	foreach ( $modules as $module ) {
		if ( ( $key = array_search( $module, $current ) ) !== false ) {
			unset( $current[ $key ] );
		}
	}

	return update_option( LC_MIGRATOR_JETPACK_ACTIVE_MODULES, $current );
}


function lcm_elementor_cache_flush() {
	delete_post_meta_by_key( '_elementor_css' );
	delete_post_meta_by_key( '_elementor_element_cache' );
	delete_post_meta_by_key( '_elementor_page_assets' );

	delete_option( '_elementor_global_css' );
	delete_option( '_elementor_assets_data' );
	delete_option( 'elementor-custom-breakpoints-files' );
}

function lcm_initial_db_version() {
	if ( ! get_option( LC_MIGRATOR_DB_VERSION ) ) {
		return update_option( LC_MIGRATOR_DB_VERSION, get_option( LC_MIGRATOR_INITIAL_DB_VERSION ) );
	}

	return false;
}

function lcm_activate_template( $template ) {
	return update_option( LC_MIGRATOR_ACTIVE_TEMPLATE, $template );
}

function lcm_activate_stylesheet( $stylesheet ) {
	return update_option( LC_MIGRATOR_ACTIVE_STYLESHEET, $stylesheet );
}

function lcm_should_reset_permalinks( $params ) {
	global $wp_rewrite, $is_apache;

	// Permalinks are not supported
	if ( empty( $params['using_permalinks'] ) ) {
		if ( $wp_rewrite->using_permalinks() ) {
			if ( $is_apache ) {
				if ( ! apache_mod_loaded( 'mod_rewrite', false ) ) {
					return true;
				}
			}
		}
	}

	return false;
}

function lcm_storage_folder() {
	return uniqid();
}

function lcm_setup_environment() {
	// Set whether a client disconnect should abort script execution
	@ignore_user_abort( true );

	// Set maximum execution time
	@set_time_limit( 0 );

	// Set maximum time in seconds a script is allowed to parse input data
	@ini_set( 'max_input_time', '-1' );

	// Set maximum backtracking steps
	@ini_set( 'pcre.backtrack_limit', PHP_INT_MAX );

	// Set binary safe encoding
	if ( @function_exists( 'mb_internal_encoding' ) && ( @ini_get( 'mbstring.func_overload' ) & 2 ) ) {
		@mb_internal_encoding( 'ISO-8859-1' );
	}

	// Clean (erase) the output buffer and turn off output buffering
	if ( @ob_get_length() ) {
		@ob_end_clean();
	}
}

function lcm_setup_errors() {
	@set_error_handler( 'Lcm_Handler::error' );
	@register_shutdown_function( 'Lcm_Handler::shutdown' );
}

function lcm_error_path( $nonce ) {
	return LC_MIGRATOR_STORAGE_PATH . DIRECTORY_SEPARATOR . sprintf( LC_MIGRATOR_ERROR_NAME, $nonce );
}

function lcm_verify_secret_key( $secret_key ) {
	if ( $secret_key !== get_option( LC_MIGRATOR_SECRET_KEY_NAME ) ) {
		throw new Lcm_Not_Valid_Secret_Key_Exception(
			wp_kses(
				__( 'Could not authenticate the secret key. The process cannot continue. ', 'lc-migrator' ),
				lcm_allowed_html_tags()
			)
		);
	}

	return true;
}

function lcm_get_filters( $tag ) {
	global $wp_filter;

	// Get WordPress filter hooks
	$filters = array();
	if ( isset( $wp_filter[ $tag ] ) ) {
		if ( ( $filters = $wp_filter[ $tag ] ) ) {
			// WordPress 4.7 introduces new class for working with filters/actions called WP_Hook
			// which adds another level of abstraction and we need to address it.
			if ( isset( $filters->callbacks ) ) {
				$filters = $filters->callbacks;
			}
		}

		ksort( $filters );
	}

	return $filters;
}

function lcm_parse_size( $size, $default = null ) {
	$suffixes = array(
		''  => 1,
		'k' => 1000,
		'm' => 1000000,
		'g' => 1000000000,
	);

	// Parse size format
	if ( preg_match( '/([0-9]+)\s*(k|m|g)?(b?(ytes?)?)/i', $size, $matches ) ) {
		return $matches[1] * $suffixes[ strtolower( $matches[2] ) ];
	}

	return $default;
}