From d4087f5299525eb5f35f2034f0a5088c197c0cb0 Mon Sep 17 00:00:00 2001 From: Brasdrive Date: Thu, 6 Nov 2025 22:47:44 -0400 Subject: [PATCH] 2.0.2 --- src/Admin/HealthCheck.php | 3 +- src/Media/MediaHooks.php | 84 ++++++++++++++++++-- src/Plugin.php | 4 +- src/StreamWrapper/FlysystemStreamWrapper.php | 4 +- 4 files changed, 83 insertions(+), 12 deletions(-) diff --git a/src/Admin/HealthCheck.php b/src/Admin/HealthCheck.php index 91d7052..a8eeb8f 100644 --- a/src/Admin/HealthCheck.php +++ b/src/Admin/HealthCheck.php @@ -1,10 +1,11 @@ */ + 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) { diff --git a/src/Plugin.php b/src/Plugin.php index 3f7c744..644c26d 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -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']); } } diff --git a/src/StreamWrapper/FlysystemStreamWrapper.php b/src/StreamWrapper/FlysystemStreamWrapper.php index 69e47af..6f514b1 100644 --- a/src/StreamWrapper/FlysystemStreamWrapper.php +++ b/src/StreamWrapper/FlysystemStreamWrapper.php @@ -1,4 +1,5 @@ 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);