From 352c490292f6b54eff97ae852749fccee6fcf53c Mon Sep 17 00:00:00 2001 From: marcusquinn <6428977+marcusquinn@users.noreply.github.com> Date: Wed, 16 Apr 2025 00:24:38 +0100 Subject: [PATCH] Performance Optimization: Improve filesystem operations --- inc/functions/fs.php | 94 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 79 insertions(+), 15 deletions(-) diff --git a/inc/functions/fs.php b/inc/functions/fs.php index ce45a4a5..df90c1c3 100644 --- a/inc/functions/fs.php +++ b/inc/functions/fs.php @@ -17,6 +17,13 @@ */ function wu_get_main_site_upload_dir() { + // Check for cached value + $cached_uploads = wp_cache_get('wu_main_site_upload_dir', 'wu_filesystem'); + + if (false !== $cached_uploads) { + return $cached_uploads; + } + global $current_site; is_multisite() && switch_to_blog($current_site->blog_id); @@ -29,6 +36,9 @@ function wu_get_main_site_upload_dir() { is_multisite() && restore_current_blog(); + // Cache the result for 1 hour - this rarely changes + wp_cache_set('wu_main_site_upload_dir', $uploads, 'wu_filesystem', HOUR_IN_SECONDS); + return $uploads; } @@ -43,14 +53,33 @@ function wu_get_main_site_upload_dir() { */ function wu_maybe_create_folder($folder, ...$path) { + // Generate a cache key for this folder path + $cache_key = 'wu_folder_path_' . md5($folder . implode('/', $path)); + + // Check for cached path + $cached_path = wp_cache_get($cache_key, 'wu_filesystem'); + + if (false !== $cached_path) { + return $cached_path; + } + $uploads = wu_get_main_site_upload_dir(); $folder_path = trailingslashit($uploads['basedir'] . '/' . $folder); + // Use WordPress Filesystem API for better compatibility + global $wp_filesystem; + + // Initialize the WP filesystem if needed + if (empty($wp_filesystem)) { + require_once ABSPATH . '/wp-admin/includes/file.php'; + WP_Filesystem(); + } + /* * Checks if the folder exists. */ - if ( ! file_exists($folder_path)) { + if (!$wp_filesystem->exists($folder_path)) { // Creates the Folder wp_mkdir_p($folder_path); @@ -58,27 +87,24 @@ function wu_maybe_create_folder($folder, ...$path) { // Creates htaccess $htaccess = $folder_path . '.htaccess'; - if ( ! file_exists($htaccess)) { - $fp = @fopen($htaccess, 'w'); - - @fwrite($fp, 'deny from all'); // phpcs:ignore - - @fclose($fp); // phpcs:ignore + if (!$wp_filesystem->exists($htaccess)) { + $wp_filesystem->put_contents($htaccess, 'deny from all', FS_CHMOD_FILE); } // Creates index $index = $folder_path . 'index.html'; - if ( ! file_exists($index)) { - $fp = @fopen($index, 'w'); - - @fwrite($fp, ''); // phpcs:ignore - - @fclose($fp); // phpcs:ignore + if (!$wp_filesystem->exists($index)) { + $wp_filesystem->put_contents($index, '', FS_CHMOD_FILE); } } - return $folder_path . implode('/', $path); + $full_path = $folder_path . implode('/', $path); + + // Cache the result for 1 hour + wp_cache_set($cache_key, $full_path, 'wu_filesystem', HOUR_IN_SECONDS); + + return $full_path; } /** @@ -92,9 +118,47 @@ function wu_maybe_create_folder($folder, ...$path) { */ function wu_get_folder_url($folder) { + // Generate a cache key for this folder URL + $cache_key = 'wu_folder_url_' . md5($folder); + + // Check for cached URL + $cached_url = wp_cache_get($cache_key, 'wu_filesystem'); + + if (false !== $cached_url) { + return $cached_url; + } + $uploads = wu_get_main_site_upload_dir(); $folder_url = trailingslashit($uploads['baseurl'] . '/' . $folder); - return set_url_scheme($folder_url); + $url = set_url_scheme($folder_url); + + // Cache the result for 1 hour + wp_cache_set($cache_key, $url, 'wu_filesystem', HOUR_IN_SECONDS); + + return $url; +} + +/** + * Clears the filesystem cache. + * + * @since 2.0.0 + * + * @param string $folder Optional. If provided, only clears cache for this folder. + * @return void + */ +function wu_clear_filesystem_cache($folder = '') { + + // Clear main site upload dir cache + wp_cache_delete('wu_main_site_upload_dir', 'wu_filesystem'); + + // If a specific folder is provided, only clear that folder's cache + if (!empty($folder)) { + $folder_path_key = 'wu_folder_path_' . md5($folder); + $folder_url_key = 'wu_folder_url_' . md5($folder); + + wp_cache_delete($folder_path_key, 'wu_filesystem'); + wp_cache_delete($folder_url_key, 'wu_filesystem'); + } }