/** * Given a "local" filepath (what you probably thought was the only filepath), * converts it into a "remote" filepath (the filepath the currently-in-use * $wp_filesystem needs to use access the folder or file). * See http://wordpress.stackexchange.com/questions/124900/using-wp-filesystem-in-plugins * @param WP_Filesystem_Base $wp_filesystem we aren't initially sure which one * is in use, so you need to provide it * @param string $local_filepath the filepath to the folder/file locally * @throws EE_Error if filesystem credentials are required * @return string the remote filepath (eg the filepath the filesystem method, eg * ftp or ssh, will use to access the folder */ public static function convert_local_filepath_to_remote_filepath($local_filepath) { $wp_filesystem = EEH_File::_get_wp_filesystem($local_filepath); return str_replace(WP_CONTENT_DIR . DS, $wp_filesystem->wp_content_dir(), $local_filepath); }
/** * is_readable * checks if a file is_readable using the WP filesystem * * @param string $full_file_path * @return bool */ public static function is_readable($full_file_path = '') { $wp_filesystem = EEH_File::_get_wp_filesystem(); return $wp_filesystem->is_readable($full_file_path) ? TRUE : FALSE; }
/** * Copies a file. Mostly a wrapper of WP_Filesystem::copy * @param string $source_file * @param string $destination_file * @param boolean $overwrite * @return boolean success * @throws EE_Error */ public static function copy($source_file, $destination_file, $overwrite = FALSE) { $full_source_path = EEH_File::standardise_directory_separators($source_file); if (!EEH_File::exists($full_source_path)) { if (defined('WP_DEBUG') && WP_DEBUG) { $msg = sprintf(__('The file located at "%2$s" is not readable or doesn\'t exist.', 'event_espresso'), $full_source_path); $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_source_path); throw new EE_Error($msg); } return FALSE; } $full_dest_path = EEH_File::standardise_directory_separators($destination_file); $folder = EEH_File::remove_filename_from_filepath($full_dest_path); if (!EEH_File::verify_is_writable($folder, 'folder')) { if (defined('WP_DEBUG') && WP_DEBUG) { $msg = sprintf(__('The file located at "%2$s" is not writable.', 'event_espresso'), $full_dest_path); $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_dest_path); throw new EE_Error($msg); } return FALSE; } // load WP_Filesystem and set file permissions $wp_filesystem = EEH_File::_get_wp_filesystem(); // write the file if (!$wp_filesystem->copy($full_source_path, $full_dest_path, $overwrite)) { if (defined('WP_DEBUG') && WP_DEBUG) { $msg = sprintf(__('Attempted writing to file %1$s, but could not, probably because of permissions issues', 'event_espresso'), $full_source_path); $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_source_path, 'f'); throw new EE_Error($msg); } return FALSE; } return TRUE; }