| 1 |
<?php |
| 2 |
/** |
| 3 |
* Our class responsible for reading and writing the static version file. |
| 4 |
* |
| 5 |
* @package ForceRefresh |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace JordanLeven\Plugins\ForceRefresh\Services; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class for static version file I/O. |
| 12 |
*/ |
| 13 |
class Version_File_Service { |
| 14 |
|
| 15 |
const FILE_NAME = 'version.json'; |
| 16 |
const DIR_NAME = 'force-refresh'; |
| 17 |
|
| 18 |
/** |
| 19 |
* Read the current version data from disk. |
| 20 |
* |
| 21 |
* Returns an empty array when the file does not exist, so callers |
| 22 |
* never need to null-check the result. |
| 23 |
* |
| 24 |
* @return array The decoded version data, or [] when the file is absent. |
| 25 |
*/ |
| 26 |
public static function read(): array { |
| 27 |
$filesystem = self::get_filesystem(); |
| 28 |
|
| 29 |
if ( null === $filesystem || ! $filesystem->exists( self::get_file_path() ) ) { |
| 30 |
return array(); |
| 31 |
} |
| 32 |
|
| 33 |
return self::decode_file( self::get_file_path() ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Write version data to disk, creating the directory if needed. |
| 38 |
* |
| 39 |
* Uses LOCK_EX to serialize concurrent writes. |
| 40 |
* |
| 41 |
* @param array $data The version data to persist. |
| 42 |
* |
| 43 |
* @return void |
| 44 |
*/ |
| 45 |
public static function write( array $data ): void { |
| 46 |
$filesystem = self::get_filesystem(); |
| 47 |
|
| 48 |
if ( null === $filesystem ) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
$dir = self::get_upload_dir(); |
| 53 |
self::ensure_directory_exists( $dir ); |
| 54 |
$filesystem->put_contents( self::get_file_path(), wp_json_encode( $data ), FS_CHMOD_FILE ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Delete the version file from disk. |
| 59 |
* |
| 60 |
* Safe to call when the file does not exist — no error is raised. |
| 61 |
* |
| 62 |
* @return void |
| 63 |
*/ |
| 64 |
public static function delete(): void { |
| 65 |
$filesystem = self::get_filesystem(); |
| 66 |
$path = self::get_file_path(); |
| 67 |
|
| 68 |
if ( null === $filesystem || ! $filesystem->exists( $path ) ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
$filesystem->delete( $path ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Return the public URL clients use to fetch the version file. |
| 77 |
* |
| 78 |
* @return string The full public URL. |
| 79 |
*/ |
| 80 |
public static function get_public_url(): string { |
| 81 |
return wp_upload_dir()['baseurl'] . '/' . self::DIR_NAME . '/' . self::FILE_NAME; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Return the absolute filesystem path to the version file. |
| 86 |
* |
| 87 |
* @return string The absolute path. |
| 88 |
*/ |
| 89 |
private static function get_file_path(): string { |
| 90 |
return self::get_upload_dir() . '/' . self::FILE_NAME; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Return the absolute filesystem path to the plugin uploads directory. |
| 95 |
* |
| 96 |
* @return string The absolute path. |
| 97 |
*/ |
| 98 |
private static function get_upload_dir(): string { |
| 99 |
return wp_upload_dir()['basedir'] . '/' . self::DIR_NAME; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Create the given directory if it does not already exist. |
| 104 |
* |
| 105 |
* @param string $path The directory path to create. |
| 106 |
* |
| 107 |
* @return void |
| 108 |
*/ |
| 109 |
private static function ensure_directory_exists( string $path ): void { |
| 110 |
$filesystem = self::get_filesystem(); |
| 111 |
|
| 112 |
if ( null === $filesystem || $filesystem->is_dir( $path ) ) { |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
wp_mkdir_p( $path ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Read and JSON-decode a file at the given path. |
| 121 |
* |
| 122 |
* Returns an empty array when the file content is not valid JSON. |
| 123 |
* |
| 124 |
* @param string $path The absolute file path. |
| 125 |
* |
| 126 |
* @return array The decoded content, or [] on parse failure. |
| 127 |
*/ |
| 128 |
private static function decode_file( string $path ): array { |
| 129 |
$filesystem = self::get_filesystem(); |
| 130 |
|
| 131 |
if ( null === $filesystem ) { |
| 132 |
return array(); |
| 133 |
} |
| 134 |
|
| 135 |
$contents = $filesystem->get_contents( $path ); |
| 136 |
$decoded = json_decode( $contents, true ); |
| 137 |
|
| 138 |
return is_array( $decoded ) ? $decoded : array(); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Return an initialized WP_Filesystem instance. |
| 143 |
* |
| 144 |
* @return \WP_Filesystem_Base |
| 145 |
*/ |
| 146 |
private static function get_filesystem(): ?\WP_Filesystem_Base { |
| 147 |
global $wp_filesystem; |
| 148 |
|
| 149 |
if ( empty( $wp_filesystem ) ) { |
| 150 |
require_once ABSPATH . '/wp-admin/includes/file.php'; |
| 151 |
WP_Filesystem(); |
| 152 |
} |
| 153 |
|
| 154 |
return $wp_filesystem ?? null; |
| 155 |
} |
| 156 |
} |
| 157 |
|