Skip to content

Commit eafdde5

Browse files
committed
only set css grid option if bootstrap version is >= 5
1 parent 9a175a8 commit eafdde5

2 files changed

Lines changed: 80 additions & 1 deletion

File tree

phpunit/settings/class-wp-bootstrap-blocks-settings-test.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,66 @@ public function test_pre_update_option_bootstrap_version() {
8585
$bootstrap_version_option_value = get_option( Settings::BOOTSTRAP_VERSION_OPTION_NAME );
8686
$this->assertEquals( $constant_value, $bootstrap_version_option_value );
8787
}
88+
89+
/**
90+
* Tests pre_update_option_enable_css_grid() filter. Option should be saved to constant value if set and always be false if Bootstrap < 5.
91+
*
92+
* This test must run in a separate process as it defines constants!
93+
*
94+
* @runInSeparateProcess
95+
* @preserveGlobalState disabled
96+
*/
97+
public function test_pre_update_option_enable_css_grid() {
98+
// Option value should always be set to false if Bootstrap version < 5
99+
update_option( Settings::BOOTSTRAP_VERSION_OPTION_NAME, '4' );
100+
update_option( Settings::ENABLE_CSS_GRID_OPTION_NAME, true );
101+
$this->assertEquals( false, get_option( Settings::ENABLE_CSS_GRID_OPTION_NAME ) );
102+
103+
// Option value should be set if constant is not set and Bootstrap version is >= 5
104+
update_option( Settings::BOOTSTRAP_VERSION_OPTION_NAME, '5' );
105+
$option_value = true;
106+
update_option( Settings::ENABLE_CSS_GRID_OPTION_NAME, $option_value );
107+
$enable_css_grid_option_value = get_option( Settings::ENABLE_CSS_GRID_OPTION_NAME );
108+
$this->assertEquals( $option_value, $enable_css_grid_option_value );
109+
110+
// Constant value should be saved as option value if available.
111+
$constant_value = true;
112+
define( Settings::ENABLE_CSS_GRID_CONSTANT_NAME, $constant_value );
113+
$option_value = false;
114+
update_option( Settings::ENABLE_CSS_GRID_OPTION_NAME, $option_value );
115+
$enable_css_grid_option_value = get_option( Settings::ENABLE_CSS_GRID_OPTION_NAME );
116+
$this->assertEquals( $constant_value, $enable_css_grid_option_value );
117+
118+
// Option value should be set to false if Bootstrap version < 5 even if constant is set to true
119+
update_option( Settings::BOOTSTRAP_VERSION_OPTION_NAME, '4' );
120+
$this->assertEquals( false, get_option( Settings::ENABLE_CSS_GRID_OPTION_NAME ) );
121+
}
122+
123+
/**
124+
* Tests test_is_bootstrap_5_active() helper function.
125+
*
126+
* This test must run in a separate process as it defines constants!
127+
*
128+
* @runInSeparateProcess
129+
* @preserveGlobalState disabled
130+
*/
131+
public function test_is_css_grid_enabled() {
132+
// CSS grid shouldn't be enabled by default
133+
$this->assertEquals( false, Settings::is_css_grid_enabled() );
134+
135+
// Should be true if Bootstrap version 5 is selected and css grid option is enabled.
136+
update_option( Settings::BOOTSTRAP_VERSION_OPTION_NAME, '5' );
137+
update_option( Settings::ENABLE_CSS_GRID_OPTION_NAME, true );
138+
$this->assertEquals( true, Settings::is_css_grid_enabled() );
139+
140+
// Should be true if Bootstrap version 5 is selected and css grid constant is set to true even if option is set to false.
141+
define( Settings::ENABLE_CSS_GRID_CONSTANT_NAME, true );
142+
update_option( Settings::ENABLE_CSS_GRID_OPTION_NAME, false );
143+
$this->assertEquals( true, Settings::is_css_grid_enabled() );
144+
145+
// Should be always false when Bootstrap version is < 5
146+
update_option( Settings::BOOTSTRAP_VERSION_OPTION_NAME, '4' );
147+
update_option( Settings::ENABLE_CSS_GRID_OPTION_NAME, true );
148+
$this->assertEquals( false, Settings::is_css_grid_enabled() );
149+
}
88150
}

src/settings/class-settings.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ public static function init( $assets_dir, $assets_url ) {
124124
// Filter saving of bootstrap version
125125
add_filter( 'pre_update_option_' . self::BOOTSTRAP_VERSION_OPTION_NAME, array( __CLASS__, 'pre_update_option_bootstrap_version' ), 10, 2 );
126126

127+
// Filter saving of enable css grid option
128+
add_filter( 'pre_update_option_' . self::ENABLE_CSS_GRID_OPTION_NAME, array( __CLASS__, 'pre_update_option_css_grid_enabled' ), 10, 2 );
129+
127130
// Enqueue settings stylesheet
128131
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_styles' ) );
129132

@@ -263,7 +266,7 @@ public static function settings_page() {
263266
<div class="wrap" id="<?php echo esc_attr( self::MENU_SLUG ); ?>">
264267
<h1><?php esc_html_e( 'Bootstrap Blocks Settings', 'wp-bootstrap-blocks' ); ?></h1>
265268

266-
<form method="post" action="options.php" enctype="multipart/form-data">
269+
<form method="post" action="options.php" enctype="multipart/form-data" data-cy="wp-bootstrap-blocks-settings-form">
267270
<?php
268271
// Get settings fields
269272
settings_fields( self::MENU_SLUG );
@@ -369,6 +372,20 @@ public static function pre_update_option_bootstrap_version( $new_value, $old_val
369372
return defined( self::BOOTSTRAP_VERSION_CONSTANT_NAME ) ? strval( constant( self::BOOTSTRAP_VERSION_CONSTANT_NAME ) ) : $new_value;
370373
}
371374

375+
/**
376+
* Only enable CSS grid if bootstrap version is >= 5 and always use constant value if set.
377+
*
378+
* @param string $new_value The new, unserialized option value.
379+
* @param string $old_value The old option value.
380+
*
381+
* @return string
382+
*/
383+
public static function pre_update_option_css_grid_enabled( $new_value, $old_value ) {
384+
return self::is_bootstrap_5_active()
385+
? defined( self::ENABLE_CSS_GRID_CONSTANT_NAME ) ? boolval( constant( self::ENABLE_CSS_GRID_CONSTANT_NAME ) ) : $new_value
386+
: false;
387+
}
388+
372389
/**
373390
* Get bootstrap version option.
374391
*

0 commit comments

Comments
 (0)