| 1 |
<?php |
| 2 |
|
| 3 |
namespace Code_Snippets\Flat_Files; |
| 4 |
|
| 5 |
use Code_Snippets\Flat_Files\Interfaces\Filesystem_Adapter; |
| 6 |
use WP_Filesystem_Base; |
| 7 |
|
| 8 |
/** |
| 9 |
* Adaptor that implements File_System_Interface using the WP_Filesystem API. |
| 10 |
*/ |
| 11 |
class WP_Filesystem_Adapter implements Filesystem_Adapter { |
| 12 |
|
| 13 |
/** |
| 14 |
* Instance of WP_Filesystem |
| 15 |
* |
| 16 |
* @var WP_Filesystem_Base |
| 17 |
*/ |
| 18 |
private $fs; |
| 19 |
|
| 20 |
/** |
| 21 |
* Constructor. |
| 22 |
* |
| 23 |
* Initialises WP_Filesystem and stores the instance in this class. |
| 24 |
*/ |
| 25 |
public function __construct() { |
| 26 |
if ( ! function_exists( 'WP_Filesystem' ) ) { |
| 27 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 28 |
} |
| 29 |
|
| 30 |
WP_Filesystem(); |
| 31 |
global $wp_filesystem; |
| 32 |
$this->fs = $wp_filesystem; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Writes a string to a file. |
| 37 |
* |
| 38 |
* @param string $path Remote path to the file where to write the data. |
| 39 |
* @param string $contents The data to write. |
| 40 |
* @param int|false $chmod Optional. The file permissions as octal number, usually 0644. Default false. |
| 41 |
* |
| 42 |
* @return bool True on success, false on failure. |
| 43 |
*/ |
| 44 |
public function put_contents( string $path, string $contents, $chmod ): bool { |
| 45 |
return $this->fs->put_contents( $path, $contents, $chmod ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Checks if a file or directory exists. |
| 50 |
* |
| 51 |
* @param string $path Path to file or directory. |
| 52 |
* |
| 53 |
* @return bool Whether $path exists or not. |
| 54 |
*/ |
| 55 |
public function exists( string $path ): bool { |
| 56 |
return $this->fs->exists( $path ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Deletes a file or directory. |
| 61 |
* |
| 62 |
* @param string $file Path to the file or directory. |
| 63 |
* @param bool $recursive Optional. If set to true, deletes files and folders recursively. |
| 64 |
* Default false. |
| 65 |
* @param string|false $type Type of resource. 'f' for file, 'd' for directory. |
| 66 |
* Default false. |
| 67 |
* |
| 68 |
* @return bool True on success, false on failure. |
| 69 |
*/ |
| 70 |
public function delete( string $file, bool $recursive = false, $type = false ): bool { |
| 71 |
return $this->fs->delete( $file, $recursive, $type ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Checks if resource is a directory. |
| 76 |
* * |
| 77 |
* |
| 78 |
* @param string $path Directory path. |
| 79 |
* |
| 80 |
* @return bool Whether $path is a directory. |
| 81 |
*/ |
| 82 |
public function is_dir( string $path ): bool { |
| 83 |
return $this->fs->is_dir( $path ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Creates a directory. |
| 88 |
* |
| 89 |
* @param string $path Path for new directory. |
| 90 |
* @param int|false $chmod Optional. The permissions as octal number (or false to skip chmod). |
| 91 |
* Default false. |
| 92 |
* |
| 93 |
* @return bool True on success, false on failure. |
| 94 |
*/ |
| 95 |
public function mkdir( string $path, $chmod ): bool { |
| 96 |
return $this->fs->mkdir( $path, $chmod ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Deletes a directory. |
| 101 |
* |
| 102 |
* @param string $path Path to directory. |
| 103 |
* @param bool $recursive Optional. Whether to recursively remove files/directories. |
| 104 |
* Default false. |
| 105 |
* |
| 106 |
* @return bool True on success, false on failure. |
| 107 |
*/ |
| 108 |
public function rmdir( string $path, bool $recursive = false ): bool { |
| 109 |
return $this->fs->rmdir( $path, $recursive ); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Changes filesystem permissions. |
| 114 |
* |
| 115 |
* @param string $path Path to the file. |
| 116 |
* @param int|false $chmod Optional. The permissions as octal number, usually 0644 for files. |
| 117 |
* |
| 118 |
* @return bool True on success, false on failure. |
| 119 |
*/ |
| 120 |
public function chmod( string $path, $chmod ): bool { |
| 121 |
return $this->fs->chmod( $path, $chmod ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Checks if a file or directory is writable. |
| 126 |
* |
| 127 |
* @param string $path Path to file or directory. |
| 128 |
* |
| 129 |
* @return bool Whether $path is writable. |
| 130 |
*/ |
| 131 |
public function is_writable( string $path ): bool { |
| 132 |
return $this->fs->is_writable( $path ); |
| 133 |
} |
| 134 |
} |
| 135 |
|