2.0.2
This commit is contained in:
parent
f0f1ee8c00
commit
d4087f5299
|
|
@ -1,10 +1,11 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FlysystemOffload\Admin;
|
||||
|
||||
use FlysystemOffload\Plugin;
|
||||
use FlysystemOffload\Helpers\PathHelper;
|
||||
use FlysystemOffload\Plugin;
|
||||
use League\Flysystem\PortableVisibility;
|
||||
use Throwable;
|
||||
|
||||
|
|
|
|||
|
|
@ -12,6 +12,10 @@ class MediaHooks
|
|||
private ?FilesystemOperator $filesystem = null;
|
||||
private string $basePrefix;
|
||||
|
||||
/** @var array<int, array{type:'filter'|'action', hook:string, callback:callable, priority:int, accepted_args:int}> */
|
||||
private array $attachedCallbacks = [];
|
||||
private bool $registered = false;
|
||||
|
||||
private const IMAGE_EDITOR_IMAGICK = 'FlysystemOffload\\Media\\ImageEditorImagick';
|
||||
private const IMAGE_EDITOR_GD = 'FlysystemOffload\\Media\\ImageEditorGD';
|
||||
|
||||
|
|
@ -22,13 +26,45 @@ class MediaHooks
|
|||
|
||||
public function register(): void
|
||||
{
|
||||
add_filter('upload_dir', [$this, 'filterUploadDir'], 20);
|
||||
add_filter('wp_get_attachment_url', [$this, 'filterAttachmentUrl'], 20, 2);
|
||||
add_filter('get_attached_file', [$this, 'filterGetAttachedFile'], 20, 2);
|
||||
add_filter('update_attached_file', [$this, 'filterUpdateAttachedFile'], 20, 2);
|
||||
add_filter('wp_read_image_metadata', [$this, 'ensureLocalPathForMetadata'], 5, 2);
|
||||
add_filter('image_editors', [$this, 'filterImageEditors'], 5);
|
||||
add_action('delete_attachment', [$this, 'handleDeleteAttachment'], 20);
|
||||
if ($this->registered) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->attachFilter('upload_dir', [$this, 'filterUploadDir'], 20, 1);
|
||||
$this->attachFilter('wp_get_attachment_url', [$this, 'filterAttachmentUrl'], 20, 2);
|
||||
$this->attachFilter('get_attached_file', [$this, 'filterGetAttachedFile'], 20, 2);
|
||||
$this->attachFilter('update_attached_file', [$this, 'filterUpdateAttachedFile'], 20, 2);
|
||||
$this->attachFilter('wp_read_image_metadata', [$this, 'ensureLocalPathForMetadata'], 5, 2);
|
||||
$this->attachFilter('image_editors', [$this, 'filterImageEditors'], 5, 1);
|
||||
$this->attachAction('delete_attachment', [$this, 'handleDeleteAttachment'], 20, 1);
|
||||
|
||||
$this->registered = true;
|
||||
}
|
||||
|
||||
public function unregister(): void
|
||||
{
|
||||
if (! $this->registered) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($this->attachedCallbacks as $hookData) {
|
||||
if ($hookData['type'] === 'filter') {
|
||||
remove_filter(
|
||||
$hookData['hook'],
|
||||
$hookData['callback'],
|
||||
$hookData['priority']
|
||||
);
|
||||
} else {
|
||||
remove_action(
|
||||
$hookData['hook'],
|
||||
$hookData['callback'],
|
||||
$hookData['priority']
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$this->attachedCallbacks = [];
|
||||
$this->registered = false;
|
||||
}
|
||||
|
||||
public function setFilesystem(?FilesystemOperator $filesystem): void
|
||||
|
|
@ -172,6 +208,40 @@ class MediaHooks
|
|||
}
|
||||
}
|
||||
|
||||
private function attachFilter(
|
||||
string $hook,
|
||||
callable $callback,
|
||||
int $priority = 10,
|
||||
int $acceptedArgs = 1
|
||||
): void {
|
||||
add_filter($hook, $callback, $priority, $acceptedArgs);
|
||||
|
||||
$this->attachedCallbacks[] = [
|
||||
'type' => 'filter',
|
||||
'hook' => $hook,
|
||||
'callback' => $callback,
|
||||
'priority' => $priority,
|
||||
'accepted_args' => $acceptedArgs,
|
||||
];
|
||||
}
|
||||
|
||||
private function attachAction(
|
||||
string $hook,
|
||||
callable $callback,
|
||||
int $priority = 10,
|
||||
int $acceptedArgs = 1
|
||||
): void {
|
||||
add_action($hook, $callback, $priority, $acceptedArgs);
|
||||
|
||||
$this->attachedCallbacks[] = [
|
||||
'type' => 'action',
|
||||
'hook' => $hook,
|
||||
'callback' => $callback,
|
||||
'priority' => $priority,
|
||||
'accepted_args' => $acceptedArgs,
|
||||
];
|
||||
}
|
||||
|
||||
private function downloadToTemp(string $remotePath)
|
||||
{
|
||||
if (! $this->filesystem) {
|
||||
|
|
|
|||
|
|
@ -84,8 +84,8 @@ final class Plugin
|
|||
add_action('switch_blog', [$this, 'handleSwitchBlog']);
|
||||
add_action('flysystem_offload_reload_config', [$this, 'reloadConfig']);
|
||||
|
||||
if (defined('WP_CLI') && WP_CLI && class_exists(HealthCheck::class)) {
|
||||
\WP_CLI::add_command('flysystem-offload health-check', [HealthCheck::class, 'run']);
|
||||
if (defined('WP_CLI') && WP_CLI && class_exists(\FlysystemOffload\Admin\HealthCheck::class)) {
|
||||
\WP_CLI::add_command('flysystem-offload health-check', [\FlysystemOffload\Admin\HealthCheck::class, 'run']);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FlysystemOffload\StreamWrapper;
|
||||
|
|
@ -287,7 +288,6 @@ final class FlysystemStreamWrapper
|
|||
|
||||
$size = $isDirectory ? 0 : $filesystem->fileSize($flyPath);
|
||||
$mtime = $filesystem->lastModified($flyPath);
|
||||
|
||||
$mode = $isDirectory ? 0040777 : 0100777;
|
||||
|
||||
return [
|
||||
|
|
@ -489,7 +489,7 @@ final class FlysystemStreamWrapper
|
|||
$pos = strpos($path, '://');
|
||||
|
||||
if ($pos === false) {
|
||||
return $this->protocol ?: 'fly';
|
||||
return $this->protocol !== '' ? $this->protocol : 'fly';
|
||||
}
|
||||
|
||||
return substr($path, 0, $pos);
|
||||
|
|
|
|||
Loading…
Reference in New Issue