@@ -48,10 +48,56 @@ public function id(): string {
4848 /**
4949 * Config defaults.
5050 *
51+ * The options default to a callable that reads the site's presets, the
52+ * way nav_menu's read its menus, rather than to nothing with the presets
53+ * read as a fallback. The difference is what `Field::options()` answers:
54+ * with nothing configured it answered an empty list, and the kit's own
55+ * missing-options check reported every gradient field as having nothing
56+ * to choose from while it drew a dozen swatches.
57+ *
5158 * @return array<string, mixed>
5259 */
5360 public function defaults (): array {
54- return [ 'columns ' => 4 ];
61+ return [
62+ 'columns ' => 4 ,
63+ 'options ' => [ self ::class, 'presets ' ],
64+ ];
65+ }
66+
67+ /**
68+ * The gradient presets the site offers, as CSS => name.
69+ *
70+ * Core's dozen plus the theme's, from the same global settings the block
71+ * editor reads. Public and static because it is the options default, and
72+ * a default is resolved by the field rather than by this type.
73+ *
74+ * @param Field $field The field asking. Unused: the presets are the
75+ * site's, not the field's.
76+ *
77+ * @return array<string, string>
78+ */
79+ public static function presets ( Field $ field ): array {
80+ unset( $ field );
81+
82+ if ( ! function_exists ( 'wp_get_global_settings ' ) ) {
83+ return [];
84+ }
85+
86+ $ found = [];
87+
88+ // Grouped by origin — default, theme, custom — and a later origin
89+ // overriding an earlier one is the point of the grouping.
90+ foreach ( (array ) wp_get_global_settings ( [ 'color ' , 'gradients ' ] ) as $ set ) {
91+ foreach ( (array ) $ set as $ preset ) {
92+ if ( empty ( $ preset ['gradient ' ] ) ) {
93+ continue ;
94+ }
95+
96+ $ found [ (string ) $ preset ['gradient ' ] ] = (string ) ( $ preset ['name ' ] ?? $ preset ['slug ' ] ?? '' );
97+ }
98+ }
99+
100+ return $ found ;
55101 }
56102
57103 /**
@@ -158,38 +204,14 @@ public function render( Field $field, Attributes $attributes ): string {
158204 * The site's own by default — core's dozen plus the theme's — because a
159205 * plugin that offers its own palette beside the theme's is a plugin that
160206 * looks like a plugin. A caller with a reason can pass `options` and get
161- * exactly those instead.
207+ * exactly those instead; either way it is the field that resolves them .
162208 *
163209 * @param Field $field The field.
164210 *
165211 * @return array<string, string>
166212 */
167213 private function gradients ( Field $ field ): array {
168- $ options = $ field ->options ();
169-
170- if ( [] !== $ options ) {
171- return $ options ;
172- }
173-
174- if ( ! function_exists ( 'wp_get_global_settings ' ) ) {
175- return [];
176- }
177-
178- $ found = [];
179-
180- // Grouped by origin — default, theme, custom — and a later origin
181- // overriding an earlier one is the point of the grouping.
182- foreach ( (array ) wp_get_global_settings ( [ 'color ' , 'gradients ' ] ) as $ set ) {
183- foreach ( (array ) $ set as $ preset ) {
184- if ( empty ( $ preset ['gradient ' ] ) ) {
185- continue ;
186- }
187-
188- $ found [ (string ) $ preset ['gradient ' ] ] = (string ) ( $ preset ['name ' ] ?? $ preset ['slug ' ] ?? '' );
189- }
190- }
191-
192- return $ found ;
214+ return $ field ->options ();
193215 }
194216
195217 /**
0 commit comments