PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / trunk
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler vtrunk
1.6.6 1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 All 49 releases
fluent-cart / app / Services / Theme / Readers / AstraSettingsReader.php

AstraSettingsReader.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler trunk, at app/Services/Theme/Readers/AstraSettingsReader.php

161 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Services\Theme\Readers;
4
5 use FluentCart\App\Services\Theme\ColorMath;
6 use FluentCart\App\Services\Theme\ThemePalette;
7 use FluentCart\Framework\Support\Arr;
8
9 /**
10 * Astra's Customizer colours, read from its settings rather than its slots.
11 *
12 * Astra keeps its Theme Colors as settings that point at a palette swatch
13 * (`var(--ast-global-color-N)`) or hold a custom colour, and its primary
14 * button colours as four settings that are empty until the owner sets them.
15 * `astra_get_option()` merges what the owner saved over Astra's defaults, so
16 * an untouched install reads its defaults and a customised one reads the
17 * customisation — the same values Astra's own dynamic CSS prints from.
18 *
19 * Empty button settings follow Astra's own chain, not a FluentCart default:
20 * background → Accent (`theme-color`), hover background → Link Hover
21 * (`link-h-color`), and each text partner → the colour that reads on its
22 * background.
23 *
24 * Verified against Astra 4.13.11.
25 */
26 class AstraSettingsReader implements ThemeSettingsReader
27 {
28 /**
29 * Astra's outline button presets: a transparent background with a border.
30 *
31 * @var array
32 */
33 protected static $outlinePresets = ['button_04', 'button_05', 'button_06'];
34
35 /**
36 * @return bool
37 */
38 public static function applies(): bool
39 {
40 return function_exists('astra_get_option');
41 }
42
43 /**
44 * @return array Role => hex or `var(--ast-global-color-N, #hex)`.
45 */
46 public static function roles(): array
47 {
48 if (!self::applies()) {
49 return [];
50 }
51
52 $roles = [];
53
54 $accent = self::read('theme-color');
55 $roles['accent'] = $accent;
56 $roles['text'] = self::read('text-color');
57 $roles['border'] = self::read('border-color');
58
59 // Astra paints the content area with the content background and falls
60 // back to the site background outside it. Tablet and mobile inherit
61 // desktop when empty, so desktop is the value that speaks for all.
62 $surface = self::readResponsive('content-bg-obj-responsive');
63
64 if ($surface === '') {
65 $surface = self::readResponsive('site-layout-outside-bg-obj-responsive');
66 }
67
68 $roles['surface'] = $surface;
69
70 // An outline preset turns the primary button transparent: there is no
71 // filled button to report, so the button roles are left to the
72 // palette-slot fallback.
73 $preset = (string)astra_get_option('button-preset-style');
74
75 if (!in_array($preset, self::$outlinePresets, true)) {
76 $roles = array_merge($roles, self::buttonPair(
77 astra_get_option('button-bg-color'),
78 $accent,
79 astra_get_option('button-color'),
80 'button_bg',
81 'button_text'
82 ));
83
84 $roles = array_merge($roles, self::buttonPair(
85 astra_get_option('button-bg-h-color'),
86 self::read('link-h-color'),
87 astra_get_option('button-h-color'),
88 'button_hover_bg',
89 'button_hover_text'
90 ));
91 }
92
93 return array_filter($roles, function ($value) {
94 return $value !== '';
95 });
96 }
97
98 /**
99 * One background + text pair, following Astra's fallback chain.
100 *
101 * A stated background wins; an empty one follows `$fallbackBg`. A value
102 * that is set but unreadable is not replaced by the fallback — Astra is
103 * painting it, just not in a form FluentCart can write, so the pair is
104 * omitted rather than reported as a colour Astra is not showing.
105 *
106 * @param mixed $rawBg The background setting as saved.
107 * @param string $fallbackBg Already-normalised fallback background.
108 * @param mixed $rawText The text setting as saved.
109 * @param string $bgRole
110 * @param string $textRole
111 * @return array
112 */
113 protected static function buttonPair($rawBg, string $fallbackBg, $rawText, string $bgRole, string $textRole): array
114 {
115 $isSet = is_string($rawBg) && trim($rawBg) !== '';
116 $background = $isSet ? ThemePalette::settingValue($rawBg) : $fallbackBg;
117
118 if ($background === '') {
119 return [];
120 }
121
122 $text = ThemePalette::settingValue($rawText);
123
124 if ($text === '') {
125 // Astra's own rule for an empty text: the foreground that reads on
126 // the background.
127 $bgHex = ThemePalette::measurable($background);
128 $text = $bgHex !== '' ? ColorMath::readableText($bgHex) : '';
129 }
130
131 return [
132 $bgRole => $background,
133 $textRole => $text,
134 ];
135 }
136
137 /**
138 * @param string $key
139 * @return string
140 */
141 protected static function read(string $key): string
142 {
143 return ThemePalette::settingValue(astra_get_option($key));
144 }
145
146 /**
147 * @param string $key
148 * @return string
149 */
150 protected static function readResponsive(string $key): string
151 {
152 $value = astra_get_option($key);
153
154 if (!is_array($value)) {
155 return '';
156 }
157
158 return ThemePalette::settingValue(Arr::get($value, 'desktop.background-color', ''));
159 }
160 }
161