| @@ -79,10 +79,10 @@ | ||
| 79 | 79 | |
| 80 | 80 | // Hook into the universal remote media URL filter for lazy caching. |
| 81 | 81 | \add_filter( 'activitypub_remote_media_url', array( self::class, 'maybe_cache' ), 10, 4 ); |
| 82 | 82 | |
| 83 | - // Invalidate cached avatar when actor is updated so it re-downloads on next access. | |
| 84 | - \add_action( 'save_post_' . Remote_Actors::POST_TYPE, array( self::class, 'clear_cached_avatar' ) ); | |
| 83 | + // Clear cached avatar URL when actor is updated (allows lazy re-caching). | |
| 84 | + \add_action( 'save_post_' . Remote_Actors::POST_TYPE, array( self::class, 'clear_avatar_meta' ) ); | |
| 85 | 85 | |
| 86 | 86 | // Clean up files when actor is deleted. |
| 87 | 87 | \add_action( 'before_delete_post', array( self::class, 'maybe_cleanup' ) ); |
| 88 | 88 | } |
| @@ -87,19 +87,20 @@ | ||
| 87 | 87 | \add_action( 'before_delete_post', array( self::class, 'maybe_cleanup' ) ); |
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | /** |
| 91 | - * Clear the cached avatar when an actor is updated. | |
| 91 | + * Clear the cached avatar URL meta when an actor is updated. | |
| 92 | 92 | * |
| 93 | - * Invalidates cached files so the avatar is re-downloaded on next access. | |
| 93 | + * This allows lazy re-caching of the avatar on next access, | |
| 94 | + * ensuring updated avatars are fetched. | |
| 94 | 95 | * |
| 95 | 96 | * @param int $post_id The actor post ID. |
| 96 | 97 | */ |
| 97 | - public static function clear_cached_avatar( $post_id ) { | |
| 98 | - // Invalidate cached files so next access re-downloads. | |
| 98 | + public static function clear_avatar_meta( $post_id ) { | |
| 99 | + // Invalidate cached files. | |
| 99 | 100 | self::invalidate_entity( $post_id ); |
| 100 | 101 | |
| 101 | - // Clean up legacy meta from previous versions. | |
| 102 | + // Clear the meta so get_avatar_url() will re-cache on next access. | |
| 102 | 103 | \delete_post_meta( $post_id, '_activitypub_avatar_url' ); |
| 103 | 104 | } |
| 104 | 105 | |
| 105 | 106 | /** |
| @@ -105,9 +106,9 @@ | ||
| 105 | 106 | /** |
| 106 | 107 | * Maybe cache an avatar URL. |
| 107 | 108 | * |
| 108 | 109 | * Hooked to the activitypub_remote_media_url filter. |
| 109 | - * Uses filesystem-based caching via get_or_cache() — no persistent meta storage. | |
| 110 | + * Returns cached URL from meta if available, otherwise downloads and caches. | |
| 110 | 111 | * |
| 111 | 112 | * @param string $url The remote URL. |
| 112 | 113 | * @param string $context The context ('avatar', 'media', 'emoji', etc.). |
| 113 | 114 | * @param string|int $entity_id The entity identifier (actor post ID). |
| @@ -119,11 +120,26 @@ | ||
| 119 | 120 | if ( self::CONTEXT !== $context || empty( $url ) || empty( $entity_id ) ) { |
| 120 | 121 | return $url; |
| 121 | 122 | } |
| 122 | 123 | |
| 123 | - $cached_url = self::get_or_cache( $url, $entity_id, array( 'max_dimension' => self::MAX_DIMENSION ) ); | |
| 124 | + // Check if we have a cached avatar URL in meta. | |
| 125 | + $cached_url = \get_post_meta( $entity_id, '_activitypub_avatar_url', true ); | |
| 126 | + if ( $cached_url ) { | |
| 127 | + return $cached_url; | |
| 128 | + } | |
| 124 | 129 | |
| 125 | - return $cached_url ?: $url; | |
| 130 | + // Download and cache the avatar. | |
| 131 | + $local_url = self::cache( | |
| 132 | + $url, | |
| 133 | + $entity_id, | |
| 134 | + array( 'max_dimension' => self::MAX_DIMENSION ) | |
| 135 | + ); | |
| 136 | + | |
| 137 | + // Store the result in meta (local URL if cached, remote URL if not). | |
| 138 | + $avatar_url = $local_url ?: $url; | |
| 139 | + \update_post_meta( $entity_id, '_activitypub_avatar_url', \esc_url_raw( $avatar_url ) ); | |
| 140 | + | |
| 141 | + return $avatar_url; | |
| 126 | 142 | } |
| 127 | 143 | |
| 128 | 144 | /** |
| 129 | 145 | * Maybe clean up cached avatar when actor is deleted. |