This commit is contained in:
Brasdrive 2025-11-06 22:47:44 -04:00
parent f0f1ee8c00
commit d4087f5299
4 changed files with 83 additions and 12 deletions

View File

@ -1,10 +1,11 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace FlysystemOffload\Admin; namespace FlysystemOffload\Admin;
use FlysystemOffload\Plugin;
use FlysystemOffload\Helpers\PathHelper; use FlysystemOffload\Helpers\PathHelper;
use FlysystemOffload\Plugin;
use League\Flysystem\PortableVisibility; use League\Flysystem\PortableVisibility;
use Throwable; use Throwable;

View File

@ -12,6 +12,10 @@ class MediaHooks
private ?FilesystemOperator $filesystem = null; private ?FilesystemOperator $filesystem = null;
private string $basePrefix; 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_IMAGICK = 'FlysystemOffload\\Media\\ImageEditorImagick';
private const IMAGE_EDITOR_GD = 'FlysystemOffload\\Media\\ImageEditorGD'; private const IMAGE_EDITOR_GD = 'FlysystemOffload\\Media\\ImageEditorGD';
@ -22,13 +26,45 @@ class MediaHooks
public function register(): void public function register(): void
{ {
add_filter('upload_dir', [$this, 'filterUploadDir'], 20); if ($this->registered) {
add_filter('wp_get_attachment_url', [$this, 'filterAttachmentUrl'], 20, 2); return;
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); $this->attachFilter('upload_dir', [$this, 'filterUploadDir'], 20, 1);
add_filter('image_editors', [$this, 'filterImageEditors'], 5); $this->attachFilter('wp_get_attachment_url', [$this, 'filterAttachmentUrl'], 20, 2);
add_action('delete_attachment', [$this, 'handleDeleteAttachment'], 20); $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 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) private function downloadToTemp(string $remotePath)
{ {
if (! $this->filesystem) { if (! $this->filesystem) {

View File

@ -84,8 +84,8 @@ final class Plugin
add_action('switch_blog', [$this, 'handleSwitchBlog']); add_action('switch_blog', [$this, 'handleSwitchBlog']);
add_action('flysystem_offload_reload_config', [$this, 'reloadConfig']); add_action('flysystem_offload_reload_config', [$this, 'reloadConfig']);
if (defined('WP_CLI') && WP_CLI && class_exists(HealthCheck::class)) { if (defined('WP_CLI') && WP_CLI && class_exists(\FlysystemOffload\Admin\HealthCheck::class)) {
\WP_CLI::add_command('flysystem-offload health-check', [HealthCheck::class, 'run']); \WP_CLI::add_command('flysystem-offload health-check', [\FlysystemOffload\Admin\HealthCheck::class, 'run']);
} }
} }

View File

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace FlysystemOffload\StreamWrapper; namespace FlysystemOffload\StreamWrapper;
@ -287,7 +288,6 @@ final class FlysystemStreamWrapper
$size = $isDirectory ? 0 : $filesystem->fileSize($flyPath); $size = $isDirectory ? 0 : $filesystem->fileSize($flyPath);
$mtime = $filesystem->lastModified($flyPath); $mtime = $filesystem->lastModified($flyPath);
$mode = $isDirectory ? 0040777 : 0100777; $mode = $isDirectory ? 0040777 : 0100777;
return [ return [
@ -489,7 +489,7 @@ final class FlysystemStreamWrapper
$pos = strpos($path, '://'); $pos = strpos($path, '://');
if ($pos === false) { if ($pos === false) {
return $this->protocol ?: 'fly'; return $this->protocol !== '' ? $this->protocol : 'fly';
} }
return substr($path, 0, $pos); return substr($path, 0, $pos);