| @@ -10,8 +10,9 @@ | ||
| 10 | 10 | |
| 11 | 11 | use Extendify\Constants; |
| 12 | 12 | use Extendify\Shared\Services\HttpClient; |
| 13 | 13 | use Extendify\Shared\Services\Sanitizer; |
| 14 | +use Extendify\Shared\Services\SiteImages; | |
| 14 | 15 | |
| 15 | 16 | /** |
| 16 | 17 | * The controller for the persisted site images cache |
| 17 | 18 | */ |
| @@ -27,9 +28,9 @@ | ||
| 27 | 28 | { |
| 28 | 29 | $siteImages = \get_option('extendify_site_images', []); |
| 29 | 30 | |
| 30 | 31 | if (!empty($siteImages)) { |
| 31 | - return new \WP_REST_Response(['siteImages' => $siteImages]); | |
| 32 | + return new \WP_REST_Response(['siteImages' => SiteImages::normalize($siteImages)]); | |
| 32 | 33 | } |
| 33 | 34 | |
| 34 | 35 | return new \WP_REST_Response(['siteImages' => self::refresh()]); |
| 35 | 36 | } |
| @@ -41,12 +42,9 @@ | ||
| 41 | 42 | * @return \WP_REST_Response |
| 42 | 43 | */ |
| 43 | 44 | public static function store($request) |
| 44 | 45 | { |
| 45 | - $siteImages = $request->get_param('siteImages'); | |
| 46 | - if (!is_array($siteImages)) { | |
| 47 | - $siteImages = []; | |
| 48 | - } | |
| 46 | + $siteImages = SiteImages::normalize($request->get_param('siteImages')); | |
| 49 | 47 | |
| 50 | 48 | \update_option('extendify_site_images', Sanitizer::sanitizeArray($siteImages)); |
| 51 | 49 | return new \WP_REST_Response(['siteImages' => $siteImages]); |
| 52 | 50 | } |
| @@ -63,10 +61,10 @@ | ||
| 63 | 61 | } |
| 64 | 62 | |
| 65 | 63 | /** |
| 66 | 64 | * Fetch fresh images from the images service using the stored site profile, |
| 67 | - * persist them, and return the array. Returns [] when the profile is empty | |
| 68 | - * or the upstream call fails. | |
| 65 | + * persist them, and return them. Returns empty sets when the profile is | |
| 66 | + * empty or the upstream call fails. | |
| 69 | 67 | * |
| 70 | 68 | * @return array |
| 71 | 69 | */ |
| 72 | 70 | private static function refresh() |
| @@ -72,16 +70,21 @@ | ||
| 72 | 70 | private static function refresh() |
| 73 | 71 | { |
| 74 | 72 | $siteProfile = \get_option('extendify_site_profile', []); |
| 75 | 73 | if (empty($siteProfile)) { |
| 76 | - return []; | |
| 74 | + return SiteImages::normalize([]); | |
| 77 | 75 | } |
| 78 | 76 | |
| 79 | 77 | $response = HttpClient::post( |
| 80 | - Constants::IMAGES_HOST . '/api/search', | |
| 78 | + Constants::IMAGES_HOST . '/api/images', | |
| 81 | 79 | [ |
| 82 | 80 | 'params' => [ |
| 83 | 81 | 'siteProfile' => $siteProfile, |
| 82 | + 'lang' => \get_locale(), | |
| 83 | + 'imageTypes' => [ | |
| 84 | + ['type' => 'hero'], | |
| 85 | + ['type' => 'general'], | |
| 86 | + ], | |
| 84 | 87 | 'source' => 'shared', |
| 85 | 88 | ], |
| 86 | 89 | ], |
| 87 | 90 | null, |
| @@ -87,11 +90,11 @@ | ||
| 87 | 90 | null, |
| 88 | 91 | true |
| 89 | 92 | ); |
| 90 | 93 | |
| 91 | - $siteImages = $response['response']['siteImages'] ?? []; | |
| 92 | - if (!is_array($siteImages) || empty($siteImages)) { | |
| 93 | - return []; | |
| 94 | + $siteImages = SiteImages::normalize($response['response']['images'] ?? []); | |
| 95 | + if (empty($siteImages['hero']) && empty($siteImages['general'])) { | |
| 96 | + return $siteImages; | |
| 94 | 97 | } |
| 95 | 98 | |
| 96 | 99 | \update_option('extendify_site_images', Sanitizer::sanitizeArray($siteImages)); |
| 97 | 100 | return $siteImages; |