cloudProvider = $cloudProvider; $upload_dir = wp_get_upload_dir(); $this->upload_base_url = trailingslashit($upload_dir['baseurl']); } /** * Register the observer with WordPress hooks. * * @return void */ public function register(): void { add_filter('wp_calculate_image_srcset', [$this, 'run'], 10, 5); } /** * Modify the image srcset. * @param array $sources * @param array $size_array * @param array $image_src * @param array $image_meta * @param int $attachment_id * @return array */ public function run(array $sources, array $size_array, string $image_src, array $image_meta, int $attachment_id) { if (! $this->is_offloaded($attachment_id)) { return $sources; } $domain = $this->cloudProvider->getDomain(); if (empty($domain)) { // No custom domain provided, leave the original sources intact. return $sources; } $subDir = $this->get_attachment_subdir($attachment_id); $domain = rtrim($domain, '/'); return array_map(function ($source) use ($subDir, $domain) { $source['url'] = $domain . '/' . ltrim($subDir, '/') . basename($source['url']); return $source; }, $sources); } }