PluginProbe
Extendify / 3.1.5
Extendify v3.1.5
3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 0.7.0 All 126 releases
extendify / app / Shared / Controllers / SiteImagesController.php

SiteImagesController.php in Extendify 3.1.5, at app/Shared/Controllers/SiteImagesController.php

103 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Controls cached site images
5 */
6
7 namespace Extendify\Shared\Controllers;
8
9 defined('ABSPATH') || die('No direct access.');
10
11 use Extendify\Constants;
12 use Extendify\Shared\Services\HttpClient;
13 use Extendify\Shared\Services\Sanitizer;
14 use Extendify\Shared\Services\SiteImages;
15
16 /**
17 * The controller for the persisted site images cache
18 */
19
20 class SiteImagesController
21 {
22 /**
23 * Return cached site images, lazy-fetching from the images service when missing.
24 *
25 * @return \WP_REST_Response
26 */
27 public static function get()
28 {
29 $siteImages = \get_option('extendify_site_images', []);
30
31 if (!empty($siteImages)) {
32 return new \WP_REST_Response(['siteImages' => SiteImages::normalize($siteImages)]);
33 }
34
35 return new \WP_REST_Response(['siteImages' => self::refresh()]);
36 }
37
38 /**
39 * Persist a provided site images array.
40 *
41 * @param \WP_REST_Request $request - The request.
42 * @return \WP_REST_Response
43 */
44 public static function store($request)
45 {
46 $siteImages = SiteImages::normalize($request->get_param('siteImages'));
47
48 \update_option('extendify_site_images', Sanitizer::sanitizeArray($siteImages));
49 return new \WP_REST_Response(['siteImages' => $siteImages]);
50 }
51
52 /**
53 * Delete the cached site images option.
54 *
55 * @return \WP_REST_Response
56 */
57 public static function clear()
58 {
59 \delete_option('extendify_site_images');
60 return new \WP_REST_Response(['siteImages' => []]);
61 }
62
63 /**
64 * Fetch fresh images from the images service using the stored site profile,
65 * persist them, and return them. Returns empty sets when the profile is
66 * empty or the upstream call fails.
67 *
68 * @return array
69 */
70 private static function refresh()
71 {
72 $siteProfile = \get_option('extendify_site_profile', []);
73 if (empty($siteProfile)) {
74 return SiteImages::normalize([]);
75 }
76
77 $response = HttpClient::post(
78 Constants::IMAGES_HOST . '/api/images',
79 [
80 'params' => [
81 'siteProfile' => $siteProfile,
82 'lang' => \get_locale(),
83 'imageTypes' => [
84 ['type' => 'hero'],
85 ['type' => 'general'],
86 ],
87 'source' => 'shared',
88 ],
89 ],
90 null,
91 true
92 );
93
94 $siteImages = SiteImages::normalize($response['response']['images'] ?? []);
95 if (empty($siteImages['hero']) && empty($siteImages['general'])) {
96 return $siteImages;
97 }
98
99 \update_option('extendify_site_images', Sanitizer::sanitizeArray($siteImages));
100 return $siteImages;
101 }
102 }
103