| 1 |
<?php |
| 2 |
|
| 3 |
namespace Averta\WordPress\File; |
| 4 |
|
| 5 |
|
| 6 |
class FileSystem |
| 7 |
{ |
| 8 |
|
| 9 |
protected $wpFilesystem; |
| 10 |
|
| 11 |
|
| 12 |
public function __construct() |
| 13 |
{ |
| 14 |
global $wp_filesystem; |
| 15 |
|
| 16 |
if ( ! function_exists( 'get_filesystem_method' ) || empty( $wp_filesystem ) ) { |
| 17 |
require_once ( ABSPATH.'/wp-admin/includes/file.php' ); |
| 18 |
WP_Filesystem(); |
| 19 |
} |
| 20 |
|
| 21 |
$this->wpFilesystem = $wp_filesystem; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Validates filesystem credentials. |
| 26 |
*/ |
| 27 |
public function validate( $url = null ) |
| 28 |
{ |
| 29 |
if ( get_filesystem_method() === 'direct' ) {} |
| 30 |
return true; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Reads a file if exists. |
| 35 |
* |
| 36 |
* @param string $filename File name. |
| 37 |
* |
| 38 |
* @return string |
| 39 |
*/ |
| 40 |
public function read( $filename ) |
| 41 |
{ |
| 42 |
if ( ! $this->validate() ){ |
| 43 |
return false; |
| 44 |
} |
| 45 |
return $this->wpFilesystem->get_contents( $filename ); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
/** |
| 50 |
* Creates and stores content in a file |
| 51 |
* |
| 52 |
* @param string $file_location The address that we plan to create the file in. |
| 53 |
* @param string $content The content for writing in the file |
| 54 |
* @param int $chmod |
| 55 |
* |
| 56 |
* @return boolean Returns true if the file is created and updated successfully, false on failure |
| 57 |
*/ |
| 58 |
public function write( $file_location = '', $content = '', $chmod = 0644 ) |
| 59 |
{ |
| 60 |
if ( ! $this->validate() ){ |
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
$_chmod = defined( 'FS_CHMOD_FILE' ) ? FS_CHMOD_FILE : $chmod; |
| 65 |
// Write the content, if possible |
| 66 |
if ( wp_mkdir_p( dirname( $file_location ) ) && ! $this->wpFilesystem->put_contents( $file_location, $content, $_chmod ) ) { |
| 67 |
// If writing the content in the file was not successful |
| 68 |
return false; |
| 69 |
} else { |
| 70 |
return true; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Whether the file/path exists or not. |
| 76 |
* |
| 77 |
* @param string $file File name or file path. |
| 78 |
* |
| 79 |
* @return bool |
| 80 |
*/ |
| 81 |
public function exists( $file ) |
| 82 |
{ |
| 83 |
if ( ! $this->validate() ){ |
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
return $this->wpFilesystem->exists( $file ); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Whether the path is a file or not. |
| 92 |
* |
| 93 |
* @return bool |
| 94 |
*/ |
| 95 |
public function isFile( $file ) |
| 96 |
{ |
| 97 |
if ( ! $this->validate() ){ |
| 98 |
return false; |
| 99 |
} |
| 100 |
return $this->wpFilesystem->is_file( $file ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Whether the path is a directory or not. |
| 105 |
* |
| 106 |
* @return bool |
| 107 |
*/ |
| 108 |
public function isDir( $path ) |
| 109 |
{ |
| 110 |
if ( ! $this->validate() ){ |
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
return $this->wpFilesystem->is_dir( $path ); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Creates a folder path recursively. |
| 119 |
* |
| 120 |
* @param string $path Path Full path to directory to create |
| 121 |
* @param bool $recursive Whether to create directory recursively or not |
| 122 |
* |
| 123 |
* @return bool |
| 124 |
*/ |
| 125 |
public function mkdir( $path, $recursive = false ) |
| 126 |
{ |
| 127 |
if ( ! $this->validate() ){ |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
if( $recursive ){ |
| 132 |
return wp_mkdir_p( $path ); |
| 133 |
} |
| 134 |
|
| 135 |
return $this->wpFilesystem->mkdir( $path ); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Removes folder path and contents. |
| 140 |
* |
| 141 |
* @return bool |
| 142 |
* @global $wp_filesytem |
| 143 |
* @since 0.9.0 |
| 144 |
* |
| 145 |
*/ |
| 146 |
public function rmdir( $path, $recursive = false ) |
| 147 |
{ |
| 148 |
if ( ! $this->validate() ){ |
| 149 |
return false; |
| 150 |
} |
| 151 |
|
| 152 |
return $this->wpFilesystem->rmdir( $path, $recursive ); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Gets details for files in a directory or a specific file. |
| 157 |
* |
| 158 |
* |
| 159 |
* @param string $path Path to directory or file. |
| 160 |
* @param bool $include_hidden Optional. Whether to include details of hidden ("." prefixed) files. |
| 161 |
* Default true. |
| 162 |
* @param bool $recursive Optional. Whether to recursively include file details in nested directories. |
| 163 |
* Default false. |
| 164 |
* @return array|false { |
| 165 |
* Array of files. False if unable to list directory contents. |
| 166 |
* |
| 167 |
* @type string $name Name of the file or directory. |
| 168 |
* @type string $perms *nix representation of permissions. |
| 169 |
* @type string $permsn Octal representation of permissions. |
| 170 |
* @type string $owner Owner name or ID. |
| 171 |
* @type int $size Size of file in bytes. |
| 172 |
* @type int $lastmodunix Last modified unix timestamp. |
| 173 |
* @type mixed $lastmod Last modified month (3 letter) and day (without leading 0). |
| 174 |
* @type int $time Last modified time. |
| 175 |
* @type string $type Type of resource. 'f' for file, 'd' for directory. |
| 176 |
* @type mixed $files If a directory and `$recursive` is true, contains another array of files. |
| 177 |
* } |
| 178 |
*/ |
| 179 |
public function scan( $path, $include_hidden = true, $recursive = false ) { |
| 180 |
if ( ! $this->validate() ){ |
| 181 |
return false; |
| 182 |
} |
| 183 |
|
| 184 |
return $this->wpFilesystem->dirlist( $path, $include_hidden, $recursive ); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Copy files from source to destination |
| 189 |
* |
| 190 |
* @param string $source |
| 191 |
* @param string $destination |
| 192 |
* @param false $overwrite |
| 193 |
* @param false $mode |
| 194 |
* |
| 195 |
* @return bool True on success, false on failure. |
| 196 |
*/ |
| 197 |
public function copy( $source, $destination, $overwrite = false, $mode = false ) { |
| 198 |
if ( ! $this->validate() ){ |
| 199 |
return false; |
| 200 |
} |
| 201 |
|
| 202 |
return $this->wpFilesystem->copy( $source, $destination, $overwrite, $mode ); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Move files from source to destination |
| 207 |
* |
| 208 |
* @param string $source |
| 209 |
* @param string $destination |
| 210 |
* @param false $overwrite |
| 211 |
* |
| 212 |
* @return bool True on success, false on failure. |
| 213 |
*/ |
| 214 |
public function move( $source, $destination, $overwrite = false ) { |
| 215 |
if ( ! $this->validate() ){ |
| 216 |
return false; |
| 217 |
} |
| 218 |
|
| 219 |
return $this->wpFilesystem->move( $source, $destination, $overwrite ); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Gets the file modification time. |
| 224 |
* |
| 225 |
* @param string $file Path to file. |
| 226 |
* |
| 227 |
* @return mixed |
| 228 |
*/ |
| 229 |
public function mtime( $file ) { |
| 230 |
return $this->wpFilesystem->mtime( $file ); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Retrieves FileSystem instance. |
| 235 |
* |
| 236 |
* @return mixed |
| 237 |
*/ |
| 238 |
public function proxy(){ |
| 239 |
return $this->wpFilesystem; |
| 240 |
} |
| 241 |
} |
| 242 |
|