| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* The SiteImages class |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace Extendify\Shared\Services; |
| 8 |
|
| 9 |
defined('ABSPATH') || die('No direct access.'); |
| 10 |
|
| 11 |
/** |
| 12 |
* Reads the site images cache, which holds two shapes across installs. |
| 13 |
*/ |
| 14 |
|
| 15 |
class SiteImages |
| 16 |
{ |
| 17 |
/** |
| 18 |
* Split stored images into the banner picks and the section photos. |
| 19 |
* |
| 20 |
* @param mixed $siteImages - The stored or posted images. |
| 21 |
* @return array |
| 22 |
*/ |
| 23 |
public static function normalize($siteImages) |
| 24 |
{ |
| 25 |
if (!is_array($siteImages)) { |
| 26 |
return ['hero' => [], 'general' => []]; |
| 27 |
} |
| 28 |
|
| 29 |
// Sites launched before the banner picks stored one flat list of urls. |
| 30 |
if (!isset($siteImages['hero']) && !isset($siteImages['general'])) { |
| 31 |
return ['hero' => [], 'general' => self::images($siteImages)]; |
| 32 |
} |
| 33 |
|
| 34 |
return [ |
| 35 |
'hero' => self::images($siteImages['hero'] ?? []), |
| 36 |
'general' => self::images($siteImages['general'] ?? []), |
| 37 |
]; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Every stored url, banner picks first. |
| 42 |
* |
| 43 |
* @param mixed $siteImages - The stored or posted images. |
| 44 |
* @return array |
| 45 |
*/ |
| 46 |
public static function urls($siteImages) |
| 47 |
{ |
| 48 |
$siteImages = self::normalize($siteImages); |
| 49 |
|
| 50 |
return array_column( |
| 51 |
array_merge($siteImages['hero'], $siteImages['general']), |
| 52 |
'url' |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Keep the entries carrying a url, as objects. |
| 58 |
* |
| 59 |
* @param mixed $images - One set of images. |
| 60 |
* @return array |
| 61 |
*/ |
| 62 |
private static function images($images) |
| 63 |
{ |
| 64 |
if (!is_array($images)) { |
| 65 |
return []; |
| 66 |
} |
| 67 |
|
| 68 |
$normalized = []; |
| 69 |
foreach ($images as $image) { |
| 70 |
$url = is_array($image) ? ($image['url'] ?? null) : $image; |
| 71 |
if (!is_string($url) || $url === '') { |
| 72 |
continue; |
| 73 |
} |
| 74 |
|
| 75 |
$normalized[] = is_array($image) ? $image : ['url' => $url]; |
| 76 |
} |
| 77 |
|
| 78 |
return $normalized; |
| 79 |
} |
| 80 |
} |
| 81 |
|