Skip to content

Commit 8254dd1

Browse files
committed
fix: add default to prioritization
1 parent 787a54a commit 8254dd1

File tree

1 file changed

+47
-14
lines changed
  • src/plugins/global-settings/preset-controls

1 file changed

+47
-14
lines changed

src/plugins/global-settings/preset-controls/index.php

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515
*/
1616
class Stackable_Size_And_Spacing_Preset_Controls {
1717

18-
public $stackable_presets;
1918
public $theme_presets;
19+
public $default_presets;
20+
public $stackable_presets;
2021

2122
/**
2223
* Initialize
2324
*/
2425
function __construct() {
25-
$this->stackable_presets = $this->load_presets( __DIR__ . '/presets.json');
2626
$this->theme_presets = WP_Theme_JSON_Resolver::get_theme_data()->get_settings();
27-
// Register our settings.
28-
// add_action( 'register_stackable_global_settings', array( $this, 'register_preset_controls' ) );
27+
$this->default_presets = WP_Theme_JSON_Resolver::get_core_data()->get_settings();
28+
$this->stackable_presets = $this->load_presets( __DIR__ . '/presets.json');
2929

3030
add_filter( 'stackable_inline_styles_nodep', array( $this, 'add_preset_controls_styles' ) );
3131
add_filter( 'stackable_inline_editor_styles', array( $this, 'add_preset_controls_styles' ) );
@@ -53,7 +53,13 @@ private function load_presets( $json_path ) {
5353
return [];
5454
}
5555

56-
// Generate CSS variables based on preset type (e.g., fontSizes, spacing)
56+
/**
57+
* Generate CSS variables based on preset type (e.g., fontSizes, spacing)
58+
*
59+
* @param array $presests
60+
* @param array $prefix
61+
* @return mixed
62+
*/
5763
public function generate_css_variables( $presets, $prefix ) {
5864
$css = "";
5965
foreach ( $presets as $preset ) {
@@ -65,23 +71,50 @@ public function generate_css_variables( $presets, $prefix ) {
6571
return $css;
6672
}
6773

74+
/**
75+
* Get the value from an array deeply with an array of keys
76+
*
77+
* @param array $array
78+
* @param array $keys
79+
* @return mixed
80+
*/
81+
public function deepGet( $array, $keys ) {
82+
return array_reduce( $keys, fn( $value, $key ) => $value[ $key ] ?? null, $array );
83+
}
84+
6885
/**
6986
* Add our global preset control styles.
7087
*
7188
* @param String $current_css
7289
* @return String
7390
*/
7491
public function add_preset_controls_styles( $current_css ) {
75-
$presets = $this->stackable_presets;
76-
77-
if ( isset( $this->theme_presets ) ) {
78-
$presets[ 'spacing' ][ 'spacingSizes' ] = $this->theme_presets[ 'spacing' ][ 'spacingSizes' ][ 'theme' ] ?? $presets[ 'spacing' ][ 'spacingSizes' ];
79-
$presets[ 'typography' ][ 'fontSizes' ] = $this->theme_presets[ 'typography' ][ 'fontSizes' ][ 'theme' ] ?? $presets[ 'typography' ][ 'fontSizes' ];
80-
}
81-
92+
$preset_keys = array(
93+
'spacing-size' => array( 'spacing', 'spacingSizes' ),
94+
'font-size' => array( 'typography', 'fontSizes' ),
95+
);
96+
8297
$generated_css = ":root {\n";
83-
$generated_css .= $this->generate_css_variables( $presets[ 'spacing' ][ 'spacingSizes' ], 'spacing-size' );
84-
$generated_css .= $this->generate_css_variables( $presets[ 'typography' ][ 'fontSizes' ], 'font-size' );
98+
99+
foreach ( $preset_keys as $key => $value ) {
100+
if ( ! empty( $this->deepGet( $this->theme_presets, $value )[ 'theme' ] ) ) {
101+
$generated_css .= $this->generate_css_variables(
102+
$this->deepGet( $this->theme_presets, $value )[ 'theme' ],
103+
$key,
104+
);
105+
} elseif ( ! empty( $this->deepGet( $this->default_presets, $value )[ 'default' ] ) ) {
106+
$generated_css .= $this->generate_css_variables(
107+
$this->deepGet( $this->default_presets, $value )[ 'default' ],
108+
$key,
109+
);
110+
} else {
111+
$generated_css .= $this->generate_css_variables(
112+
$this->deepGet( $this->stackable_presets, $value ),
113+
$key,
114+
);
115+
}
116+
}
117+
85118
$generated_css .= "}\n";
86119

87120
if ( ! $generated_css ) {

0 commit comments

Comments
 (0)