Skip to content

Commit

Permalink
Extend File System Class
Browse files Browse the repository at this point in the history
Extend File System Class
  • Loading branch information
namithj committed Nov 29, 2024
1 parent f4326db commit a042efc
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 19 deletions.
29 changes: 10 additions & 19 deletions includes/class-debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,20 @@ private static function get_file_path() {
/**
* Initializes the WordPress Filesystem.
*
* @return WP_Filesystem_Base|false The filesystem object or false on failure.
* @return WP_Filesystem_Direct|false The filesystem object or false on failure.
*/
private static function init_filesystem() {
global $wp_filesystem;

if ( ! $wp_filesystem ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
WP_Filesystem();
}

return $wp_filesystem;
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php';
WP_Filesystem();
return new Filesystem_Direct( false );
}

/**
* Checks the filesystem status and logs error to debug log.
*
* @param WP_Filesystem_Base $wp_filesystem The filesystem object.
* @param WP_Filesystem_Direct $wp_filesystem The filesystem object.
*
* @return boolean true on success and false on failure.
*/
Expand Down Expand Up @@ -144,17 +141,11 @@ public static function log( $message, $type = 'string' ) {
) . PHP_EOL;

$file_path = self::get_file_path();

$content = '';
if ( $wp_filesystem->exists( $file_path ) ) {
if ( $wp_filesystem->is_readable( $file_path ) ) {
$content = $wp_filesystem->get_contents( $file_path );
}
}
$wp_filesystem->put_contents(
$file_path,
$formatted_message . $content,
FS_CHMOD_FILE
$formatted_message,
FS_CHMOD_FILE,
'a'
);
}
}
Expand Down
57 changes: 57 additions & 0 deletions includes/class-filesystem-direct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* The Class for WordPress Direct Filesystem with optimized read and write routines.
*
* @package aspire-update
*/

namespace AspireUpdate;

/**
* The Class for WordPress Direct Filesystem with optimized read and write routines.
*/
class Filesystem_Direct extends \WP_Filesystem_Direct {
/**
* Write contents to a file with additional modes.
*
* @param string $file The path to the file.
* @param string $contents The content to write.
* @param int|false $mode Optional. The file permissions as octal number, usually 0644.
* Default false.
* @param string $write_mode The write mode:
* 'w' - Overwrite the file (default).
* 'a' - Append to the file.
* 'x' - Create a new file and write, fail if the file exists.
* 'c' - Open the file for writing, but do not truncate.
* @return bool True on success, false on failure.
*/
public function put_contents( $file, $contents, $mode = false, $write_mode = 'w' ) {
$valid_write_modes = [ 'w', 'a', 'x', 'c' ];
if ( ! in_array( $write_mode, $valid_write_modes, true ) ) {
return false;
}
$fp = @fopen( $file, $write_mode );

if ( ! $fp ) {
return false;
}

mbstring_binary_safe_encoding();

$data_length = strlen( $contents );

$bytes_written = fwrite( $fp, $contents );

reset_mbstring_encoding();

fclose( $fp );

if ( $data_length !== $bytes_written ) {
return false;
}

$this->chmod( $file, $mode );

return true;
}
}

0 comments on commit a042efc

Please sign in to comment.