'#55555e', 'text' => '#ffffff', ]; /** * Detected by GeneratePress's own API — its version constant, its option * defaults, its Global Colors, and its dynamic-CSS printer hooked on * `wp_enqueue_scripts` (inc/css-output.php) — not by theme name, so a * child theme or renamed folder keeps working. The hook is what makes the * settings real: it is the printer that paints every colour read here. * * @return bool */ public static function applies(): bool { return defined('GENERATE_VERSION') && function_exists('generate_get_defaults') && function_exists('generate_get_color_defaults') && function_exists('generate_get_global_colors') && has_action('wp_enqueue_scripts', 'generate_enqueue_dynamic_css') !== false; } /** * @return array Role => hex or `var(--slug, #hex)`. */ public static function roles(): array { if (!self::applies()) { return []; } $settings = self::settings(); $roles = [ 'accent' => self::first($settings, ['content_link_color', 'link_color']), 'text' => self::first($settings, ['content_text_color', 'text_color']), 'surface' => self::first($settings, ['content_background_color', 'background_color']), ]; $button = self::button($settings); if ($button) { $roles = array_merge($roles, $button); } return array_filter($roles, function ($value) { return $value !== ''; }); } /** * Each Global Color's property and current colour: `--{slug}` => colour, * from `generate_get_global_colors()` — the list GeneratePress prints the * properties on :root from (generate_base_css()), and publishes to the * editor palette as bare `var(--{slug})`. * * @return array Property => colour, as GeneratePress saved it * (ThemePalette drops non-hex). */ public static function paletteValues(): array { if (!self::applies()) { return []; } $values = []; try { foreach ((array)generate_get_global_colors() as $color) { $slug = (string)Arr::get((array)$color, 'slug', ''); $value = (string)Arr::get((array)$color, 'color', ''); // GeneratePress prints only a slug with a colour. if ($slug !== '' && $value !== '') { $values['--' . $slug] = $value; } } } catch (\Throwable $e) { // GeneratePress's API misbehaving means no palette values: // references stay bare and unmeasurable, which resolve() refuses. return []; } return $values; } /** * The button and its hover. * * A background saved empty is GeneratePress's stylesheet grey; one that is * set but unwritable states no button, and its hover goes with it. A text * saved empty is the stylesheet's white; one that cannot be written gets * the colour that reads on the background. A hover background is stated * only when written, and a hover text only when written — otherwise the * resting text carries over, as resolve() already does. * * @param array $settings * @return array */ protected static function button(array $settings): array { $background = self::read($settings, 'form_button_background_color', self::$stylesheetButton['background']); if ($background === '') { return []; } $text = self::read($settings, 'form_button_text_color', self::$stylesheetButton['text']); if ($text === '') { $text = ColorMath::readableText(ThemePalette::measurable($background)); } $roles = [ 'button_bg' => $background, 'button_text' => $text, ]; $hoverBg = self::read($settings, 'form_button_background_color_hover'); if ($hoverBg !== '') { $roles['button_hover_bg'] = $hoverBg; $roles['button_hover_text'] = self::read($settings, 'form_button_text_color_hover'); } return array_filter($roles, function ($value) { return $value !== ''; }); } /** * The first setting in a chain that GeneratePress prints — one saved * non-empty — as FluentCart writes it. A set value that cannot be written * ends the chain: it is what GeneratePress paints there. * * @param array $settings * @param array $keys * @return string */ protected static function first(array $settings, array $keys): string { foreach ($keys as $key) { if (self::isSet($settings, $key)) { return self::read($settings, $key); } } return ''; } /** * One setting as FluentCart writes it; empty takes `$default`. * * @param array $settings * @param string $key * @param string $default * @return string Hex, `var(--slug, #hex)`, or ''. */ protected static function read(array $settings, string $key, string $default = ''): string { $raw = self::isSet($settings, $key) ? trim((string)$settings[$key]) : $default; $value = ThemePalette::settingValue($raw); return ThemePalette::measurable($value) !== '' ? $value : ''; } /** * Whether GeneratePress prints this setting: a non-empty string. * * @param array $settings * @param string $key * @return bool */ protected static function isSet(array $settings, string $key): bool { return isset($settings[$key]) && is_string($settings[$key]) && trim($settings[$key]) !== ''; } /** * The saved `generate_settings` row over GeneratePress's defaults — both * sets, merged the way its dynamic CSS merges each. * * @return array */ protected static function settings(): array { $saved = get_option('generate_settings', []); $saved = is_array($saved) ? $saved : []; try { $defaults = array_merge((array)generate_get_color_defaults(), (array)generate_get_defaults()); } catch (\Throwable $e) { $defaults = []; } return wp_parse_args($saved, $defaults); } }