PluginProbe
Extendify / 3.2.1
Extendify v3.2.1
3.2.1 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 All 127 releases
extendify / app / Shared / Services / HeroDescription.php

HeroDescription.php in Extendify 3.2.1, at app/Shared/Services/HeroDescription.php

81 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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