AnonymousAssetBuilder
in package
Create `DeliverAnonymousAsset` instances and own the uploads-scoped bucket lifecycle.
Table of Contents
Constants
- BUCKET_INDEX_FILENAME : mixed = '.bucket-index'
- COPY_EXTENSIONS : mixed = ['js', 'css']
Properties
- $contentDir : string|false
- Cache of `Utils::getUploadsRoot()`.
- $ensureFailed : bool
- Request-scoped: uploads/mkdir failed. Later `ready()` calls fail closed without retrying mkdir.
- $folder : mixed
- $legacyCleanupMarkers : array<string|int, string>
- File-body markers for leftover v1 sweep.
- $localizeScriptResources : LocalizeScriptResources|null
- Localize-resource orchestration.
- $namespace : string
- Plugin slug mixed into the bucket hash so plugins do not share folders.
- $pool : array<string|int, DeliverAnonymousAsset>
- The pool of collected built `DeliverAnonymousAsset` instances.
- $sourceMapBaseUrlResolver : callable|null
- Optional sourceMappingURL directory resolver.
Methods
- __construct() : mixed
- C'tor.
- build() : mixed
- Create an anonymous asset. Do not forget to make it `->ready()` after you enqueued it! This must be done in `wp` hook as it is the first available hook.
- ensureAnonymousFolder() : mixed
- Create the anonymous folder under `uploads/<bucket-id>/<dist|dev>/`.
- forceRecreation() : mixed
- Recopy files in the current bucket (e.g. after a plugin update).
- generateFilename() : mixed
- Generate the filename for a given original filename.
- generateFolderSrc() : mixed
- Get the URL to the anonymous folder.
- getContentDir() : mixed
- Uploads base directory, or false when not writable.
- getHash() : mixed
- Active bucket id (10-char hex).
- getLocalizeScriptResources() : LocalizeScriptResources
- Localize-resource orchestration for this builder.
- readFileAndCorrectSourceMap() : mixed
- Rewrite sourceMappingURL in a copied file using the constructor resolver.
- ready() : mixed
- Make a handle ready. Do not forget to `->build()` it previously!
- uninstall() : mixed
- Remove this plugin's buckets under `uploads/`. Use this in `uninstall.php`.
- ensureUploadsRoot() : string|false
- Uploads root must be writable. WordPress already created it.
- purgeExpiredBuckets() : mixed
- Delete this plugin's buckets older than current + previous. Never delete `uploads/` itself.
- listHexBucketDirs() : array<string|int, string>
- Depth-1 `uploads/<10-hex>/` directories only. Does not recurse into year/month media trees.
- ownedBucketIndex() : int|false
- Sidecar index when `$name` is this plugin's bucket, otherwise false.
- rmdir() : mixed
- Recursively remove a directory via WP_Filesystem_Direct.
Constants
BUCKET_INDEX_FILENAME
public
mixed
BUCKET_INDEX_FILENAME
= '.bucket-index'
COPY_EXTENSIONS
public
mixed
COPY_EXTENSIONS
= ['js', 'css']
Properties
$contentDir
Cache of `Utils::getUploadsRoot()`.
private
string|false
$contentDir
false when parent is not writable
$ensureFailed
Request-scoped: uploads/mkdir failed. Later `ready()` calls fail closed without retrying mkdir.
private
bool
$ensureFailed
= false
$folder
private
mixed
$folder
$legacyCleanupMarkers
File-body markers for leftover v1 sweep.
private
array<string|int, string>
$legacyCleanupMarkers
$localizeScriptResources
Localize-resource orchestration.
private
LocalizeScriptResources|null
$localizeScriptResources
$namespace
Plugin slug mixed into the bucket hash so plugins do not share folders.
private
string
$namespace
Not used as a path segment — a slug in the URL is trivial to block.
$pool
The pool of collected built `DeliverAnonymousAsset` instances.
private
array<string|int, DeliverAnonymousAsset>
$pool
= []
$sourceMapBaseUrlResolver
Optional sourceMappingURL directory resolver.
private
callable|null
$sourceMapBaseUrlResolver
Methods
__construct()
C'tor.
public
__construct(string $namespace, string $folder[, callable|null $sourceMapBaseUrlResolver = null ][, array<string|int, string> $legacyCleanupMarkers = [] ]) : mixed
Parameters
- $namespace : string
-
Plugin slug, e.g.
real-cookie-banner - $folder : string
-
The absolute path to your original files
- $sourceMapBaseUrlResolver : callable|null = null
-
function (string $originalFilePath): stringURL prefix for.mapfiles - $legacyCleanupMarkers : array<string|int, string> = []
-
Restrict leftover v1 deletion to files containing one of these substrings
build()
Create an anonymous asset. Do not forget to make it `->ready()` after you enqueued it! This must be done in `wp` hook as it is the first available hook.
public
build(string $handle, string $file[, string $id = null ]) : mixed
Parameters
- $handle : string
- $file : string
- $id : string = null
-
If you pass an ID, the instance will be hold in this class pool and you can use
this::ready()
ensureAnonymousFolder()
Create the anonymous folder under `uploads/<bucket-id>/<dist|dev>/`.
public
ensureAnonymousFolder([bool|null $skipExistenceCheck = null ]) : mixed
Parameters
- $skipExistenceCheck : bool|null = null
forceRecreation()
Recopy files in the current bucket (e.g. after a plugin update).
public
forceRecreation() : mixed
generateFilename()
Generate the filename for a given original filename.
public
static generateFilename(string $hash, string $originalFilenameOrPath) : mixed
Parameters
- $hash : string
-
The hash to use
- $originalFilenameOrPath : string
generateFolderSrc()
Get the URL to the anonymous folder.
public
generateFolderSrc() : mixed
getContentDir()
Uploads base directory, or false when not writable.
public
getContentDir() : mixed
getHash()
Active bucket id (10-char hex).
public
getHash() : mixed
getLocalizeScriptResources()
Localize-resource orchestration for this builder.
public
getLocalizeScriptResources() : LocalizeScriptResources
Return values
LocalizeScriptResourcesreadFileAndCorrectSourceMap()
Rewrite sourceMappingURL in a copied file using the constructor resolver.
public
readFileAndCorrectSourceMap(string $path) : mixed
Parameters
- $path : string
ready()
Make a handle ready. Do not forget to `->build()` it previously!
public
ready(string $id[, bool $condition = true ]) : mixed
Parameters
- $id : string
- $condition : bool = true
uninstall()
Remove this plugin's buckets under `uploads/`. Use this in `uninstall.php`.
public
static uninstall(string $namespace) : mixed
Parameters
- $namespace : string
-
Plugin slug passed to the constructor
ensureUploadsRoot()
Uploads root must be writable. WordPress already created it.
protected
ensureUploadsRoot() : string|false
Return values
string|falsepurgeExpiredBuckets()
Delete this plugin's buckets older than current + previous. Never delete `uploads/` itself.
protected
purgeExpiredBuckets(string $uploadsRoot) : mixed
Parameters
- $uploadsRoot : string
listHexBucketDirs()
Depth-1 `uploads/<10-hex>/` directories only. Does not recurse into year/month media trees.
private
static listHexBucketDirs(string $uploadsRoot) : array<string|int, string>
Parameters
- $uploadsRoot : string
Return values
array<string|int, string>ownedBucketIndex()
Sidecar index when `$name` is this plugin's bucket, otherwise false.
private
static ownedBucketIndex(string $bucketDir, string $name, string $namespace) : int|false
Parameters
- $bucketDir : string
- $name : string
- $namespace : string
Return values
int|falsermdir()
Recursively remove a directory via WP_Filesystem_Direct.
private
static rmdir(string $path) : mixed
Parameters
- $path : string