| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* The HeroDescription class |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace Extendify\Shared\Services; |
| 8 |
|
| 9 |
defined('ABSPATH') || die('No direct access.'); |
| 10 |
|
| 11 |
use Extendify\Constants; |
| 12 |
|
| 13 |
/** |
| 14 |
* Reads the sentence used as the hero paragraph of a generated design. |
| 15 |
*/ |
| 16 |
|
| 17 |
class HeroDescription |
| 18 |
{ |
| 19 |
/** |
| 20 |
* The description to write into a hero pattern, or an empty string. |
| 21 |
* |
| 22 |
* @param mixed $description - The description sent with the request. |
| 23 |
* @return string |
| 24 |
*/ |
| 25 |
public static function resolve($description = null) |
| 26 |
{ |
| 27 |
// The request reads the live hero, so it beats an option gone stale. |
| 28 |
$description = self::text($description); |
| 29 |
if ($description !== '') { |
| 30 |
return $description; |
| 31 |
} |
| 32 |
|
| 33 |
$stored = self::text(\get_option('extendify_hero_description', '')); |
| 34 |
if ($stored !== '') { |
| 35 |
return $stored; |
| 36 |
} |
| 37 |
|
| 38 |
return self::refresh(); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Build a sentence from the site profile, the only source on older sites. |
| 43 |
* |
| 44 |
* @return string |
| 45 |
*/ |
| 46 |
private static function refresh() |
| 47 |
{ |
| 48 |
$siteProfile = \get_option('extendify_site_profile', []); |
| 49 |
if (empty($siteProfile)) { |
| 50 |
return ''; |
| 51 |
} |
| 52 |
|
| 53 |
$response = HttpClient::post( |
| 54 |
Constants::AI_HOST . '/api/site-strings', |
| 55 |
['params' => ['siteProfile' => $siteProfile]], |
| 56 |
null, |
| 57 |
true |
| 58 |
); |
| 59 |
|
| 60 |
$description = self::text($response['response']['heroDescription'] ?? ''); |
| 61 |
if ($description === '') { |
| 62 |
// Leave the option unset so a failed reply retries on the next open. |
| 63 |
return ''; |
| 64 |
} |
| 65 |
|
| 66 |
\update_option('extendify_hero_description', Sanitizer::sanitizeText($description)); |
| 67 |
return $description; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* A trimmed string, whatever the value was. |
| 72 |
* |
| 73 |
* @param mixed $value - The value to read. |
| 74 |
* @return string |
| 75 |
*/ |
| 76 |
private static function text($value) |
| 77 |
{ |
| 78 |
return is_string($value) ? trim($value) : ''; |
| 79 |
} |
| 80 |
} |
| 81 |
|