Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions assets/admin/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,42 @@
var positionWrapper = document.getElementById('cookie-notice-position-wrapper');
var preview = document.getElementById('frcn-cookie-notice-preview');

if (layoutSelect && positionWrapper) {
layoutSelect.addEventListener('change', function () {
positionWrapper.style.display = layoutSelect.value === 'box' ? 'block' : 'none';
// Layout slug => { position slug: label }, printed by PHP so add-on
// layouts (frcn_cookie_notice_layout_positions) get their own
// positions without any change here.
var layoutPositions = {};

if (layoutSelect) {
try {
layoutPositions = JSON.parse(layoutSelect.getAttribute('data-layout-positions') || '{}') || {};
} catch (e) {
layoutPositions = {};
}
}

function syncPositionOptions() {
var positions = layoutPositions[layoutSelect.value] || {};
var slugs = Object.keys(positions);
var current = positionSelect.value;

positionWrapper.style.display = slugs.length ? 'block' : 'none';

if (!slugs.length) {
return;
}

positionSelect.innerHTML = '';
slugs.forEach(function (slug) {
var option = document.createElement('option');
option.value = slug;
option.textContent = positions[slug];
positionSelect.appendChild(option);
});
positionSelect.value = slugs.indexOf(current) !== -1 ? current : slugs[0];
}

if (layoutSelect && positionSelect && positionWrapper) {
layoutSelect.addEventListener('change', syncPositionOptions);
}

function updatePreviewLayout() {
Expand All @@ -36,6 +68,13 @@
}

var layout = layoutSelect ? layoutSelect.value : 'bar';

// An add-on layout brings its own frontend; the preview only knows
// the built-in styles, so show it as the bar (the free banner's own
// fallback for unknown layouts).
if (['bar', 'box', 'popup'].indexOf(layout) === -1) {
layout = 'bar';
}
var position = positionSelect ? positionSelect.value : 'bottom-right';

preview.className = 'frcn-cookie-notice frcn-cookie-notice-preview frcn-cookie-notice--' + layout;
Expand Down
2 changes: 1 addition & 1 deletion assets/admin/settings.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 27 additions & 8 deletions includes/Admin/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,9 @@ public function field_cookie_notice() {
$policy_page_id = (int) ( $options['cookie_notice_policy_page_id'] ?? 0 );
$layout = (string) ( $options['cookie_notice_layout'] ?? 'bar' );
$position = (string) ( $options['cookie_notice_position'] ?? 'bottom-right' );
$layouts = CookieNotice::get_layouts();
$layout_positions = CookieNotice::get_layout_positions();
$current_positions = $layout_positions[ $layout ] ?? array();
$color = (string) ( $options['cookie_notice_color'] ?? '#687df9' );
$bg_color = (string) ( $options['cookie_notice_bg_color'] ?? '#ffffff' );
$radius = (string) ( $options['cookie_notice_radius'] ?? 'small' );
Expand Down Expand Up @@ -730,13 +733,14 @@ class="frcn-input"
id="cookie_notice_layout"
name="<?php echo esc_attr( self::OPTION_NAME ); ?>[cookie_notice_layout]"
class="frcn-input"
data-layout-positions="<?php echo esc_attr( (string) wp_json_encode( $layout_positions ) ); ?>"
>
<option value="bar" <?php selected( $layout, 'bar' ); ?>><?php echo esc_html__( 'Full-width bar', 'frontconsent' ); ?></option>
<option value="box" <?php selected( $layout, 'box' ); ?>><?php echo esc_html__( 'Boxed panel', 'frontconsent' ); ?></option>
<option value="popup" <?php selected( $layout, 'popup' ); ?>><?php echo esc_html__( 'Centered popup', 'frontconsent' ); ?></option>
<?php foreach ( $layouts as $layout_slug => $layout_label ) : ?>
<option value="<?php echo esc_attr( $layout_slug ); ?>" <?php selected( $layout, $layout_slug ); ?>><?php echo esc_html( $layout_label ); ?></option>
<?php endforeach; ?>
</select>
</div>
<div id="cookie-notice-position-wrapper" style="<?php echo 'box' === $layout ? '' : 'display: none;'; ?>">
<div id="cookie-notice-position-wrapper" style="<?php echo $current_positions ? '' : 'display: none;'; ?>">
<label for="cookie_notice_position" class="frcn-label">
<?php echo esc_html__( 'Position', 'frontconsent' ); ?>
</label>
Expand All @@ -745,8 +749,9 @@ class="frcn-input"
name="<?php echo esc_attr( self::OPTION_NAME ); ?>[cookie_notice_position]"
class="frcn-input"
>
<option value="bottom-right" <?php selected( $position, 'bottom-right' ); ?>><?php echo esc_html__( 'Bottom right', 'frontconsent' ); ?></option>
<option value="bottom-left" <?php selected( $position, 'bottom-left' ); ?>><?php echo esc_html__( 'Bottom left', 'frontconsent' ); ?></option>
<?php foreach ( $current_positions as $position_slug => $position_label ) : ?>
<option value="<?php echo esc_attr( $position_slug ); ?>" <?php selected( $position, $position_slug ); ?>><?php echo esc_html( $position_label ); ?></option>
<?php endforeach; ?>
</select>
</div>
<div>
Expand Down Expand Up @@ -987,11 +992,25 @@ public function sanitize_settings( $value ) {
}

if ( array_key_exists( 'cookie_notice_layout', $value ) ) {
$sanitized['cookie_notice_layout'] = in_array( $value['cookie_notice_layout'], array( 'bar', 'box', 'popup' ), true ) ? $value['cookie_notice_layout'] : 'bar';
$layouts = CookieNotice::get_layouts();
$sanitized['cookie_notice_layout'] = is_string( $value['cookie_notice_layout'] ) && isset( $layouts[ $value['cookie_notice_layout'] ] ) ? $value['cookie_notice_layout'] : 'bar';
}

if ( array_key_exists( 'cookie_notice_position', $value ) ) {
$sanitized['cookie_notice_position'] = in_array( $value['cookie_notice_position'], array( 'bottom-right', 'bottom-left' ), true ) ? $value['cookie_notice_position'] : 'bottom-right';
// Validate against the positions of the layout being saved (or the
// stored one); a layout without positions keeps the box default so
// switching back to the box layout still has a valid value.
$layout_for_position = (string) ( $sanitized['cookie_notice_layout'] ?? 'bar' );
$layout_positions = CookieNotice::get_layout_positions();
$allowed_positions = $layout_positions[ $layout_for_position ] ?? array();

if ( ! $allowed_positions ) {
$allowed_positions = $layout_positions['box'] ?? array( 'bottom-right' => '' );
}

$sanitized['cookie_notice_position'] = is_string( $value['cookie_notice_position'] ) && isset( $allowed_positions[ $value['cookie_notice_position'] ] )
? $value['cookie_notice_position']
: (string) array_key_first( $allowed_positions );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Replace the PHP 7.3-only array_key_first call

On supported PHP 7.0–7.2 installations, saving settings for a layout with a submitted Position field (such as box) reaches this branch and fatals because array_key_first() was introduced in PHP 7.3. Use a PHP 7.0-compatible way to obtain the first allowed position so these users can save the settings page.

AGENTS.md reference: AGENTS.md:L11-L12

Useful? React with 👍 / 👎.

}

if ( array_key_exists( 'cookie_notice_color', $value ) ) {
Expand Down
52 changes: 52 additions & 0 deletions includes/Frontend/CookieNotice.php
Original file line number Diff line number Diff line change
Expand Up @@ -1621,6 +1621,58 @@ private function sanitize_tracking_type( $value ) {
return in_array( $value, self::get_tracking_types(), true ) ? $value : '';
}

/**
* Return the banner layouts offered in the Layout setting.
*
* The built-in layouts are rendered by this class. A layout added through
* the filter is stored as-is, but this class still renders its own banner
* markup as the full-width bar for it (see render_cookie_notice()), so the
* add-on that registers it is responsible for its presentation.
*
* @return array<string, string> Layout slug => translated label.
*/
public static function get_layouts() {
$layouts = array(
'bar' => __( 'Full-width bar', 'frontconsent' ),
'box' => __( 'Boxed panel', 'frontconsent' ),
'popup' => __( 'Centered popup', 'frontconsent' ),
);

/**
* Filters the banner layouts offered in the Layout setting.
*
* @param array<string, string> $layouts Layout slug => translated label.
*/
$filtered = apply_filters( 'frcn_cookie_notice_layouts', $layouts );

return is_array( $filtered ) && $filtered ? $filtered : $layouts;
}

/**
* Return the positions each layout supports. A layout without an entry
* has no Position setting.
*
* @return array<string, array<string, string>> Layout slug => (position slug => translated label).
*/
public static function get_layout_positions() {
$positions = array(
'box' => array(
'bottom-right' => __( 'Bottom right', 'frontconsent' ),
'bottom-left' => __( 'Bottom left', 'frontconsent' ),
),
);

/**
* Filters the positions each banner layout supports. The first
* position of a layout is its default.
*
* @param array<string, array<string, string>> $positions Layout slug => (position slug => translated label).
*/
$filtered = apply_filters( 'frcn_cookie_notice_layout_positions', $positions );

return is_array( $filtered ) ? $filtered : $positions;
}

/**
* Return the tracking integration types supported by FrontConsent and add-ons.
*
Expand Down
111 changes: 111 additions & 0 deletions tests/Unit/CookieNoticeLayoutsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php
/**
* Tests for the filterable banner layouts and per-layout positions
* (frcn_cookie_notice_layouts / frcn_cookie_notice_layout_positions), and
* that Settings::sanitize_settings() validates against them.
*
* @package FrontConsent
*/

use FrontConsent\Admin\Settings;
use FrontConsent\Frontend\CookieNotice;
use Yoast\WPTestUtils\WPIntegration\TestCase;

class CookieNoticeLayoutsTest extends TestCase {

/**
* @var Settings
*/
private $settings;

public function set_up() {
parent::set_up();
$this->settings = new Settings();
}

public function tear_down() {
remove_all_filters( 'frcn_cookie_notice_layouts' );
remove_all_filters( 'frcn_cookie_notice_layout_positions' );
delete_option( Settings::OPTION_NAME );
parent::tear_down();
}

/**
* Register a 'compact' layout with three positions, like an add-on would.
*/
private function add_compact_layout() {
add_filter(
'frcn_cookie_notice_layouts',
static function ( $layouts ) {
$layouts['compact'] = 'Compact';
return $layouts;
}
);
add_filter(
'frcn_cookie_notice_layout_positions',
static function ( $positions ) {
$positions['compact'] = array(
'bottom-center' => 'Bottom center',
'bottom-left' => 'Bottom left',
'bottom-right' => 'Bottom right',
);
return $positions;
}
);
}

public function test_built_in_layouts_and_positions() {
$this->assertSame( array( 'bar', 'box', 'popup' ), array_keys( CookieNotice::get_layouts() ) );
$this->assertSame( array( 'box' ), array_keys( CookieNotice::get_layout_positions() ) );
}

public function test_a_filtered_layout_is_offered_and_saved() {
$this->add_compact_layout();

$this->assertArrayHasKey( 'compact', CookieNotice::get_layouts() );

$result = $this->settings->sanitize_settings(
array(
'cookie_notice_layout' => 'compact',
'cookie_notice_position' => 'bottom-center',
)
);

$this->assertSame( 'compact', $result['cookie_notice_layout'] );
$this->assertSame( 'bottom-center', $result['cookie_notice_position'] );
}

public function test_an_unregistered_layout_falls_back_to_bar() {
$result = $this->settings->sanitize_settings( array( 'cookie_notice_layout' => 'compact' ) );

$this->assertSame( 'bar', $result['cookie_notice_layout'] );
}

public function test_a_position_not_supported_by_the_layout_falls_back_to_its_first_position() {
$result = $this->settings->sanitize_settings(
array(
'cookie_notice_layout' => 'box',
'cookie_notice_position' => 'bottom-center',
)
);

$this->assertSame( 'bottom-right', $result['cookie_notice_position'] );
}

public function test_a_layout_without_positions_keeps_a_valid_box_position() {
$result = $this->settings->sanitize_settings(
array(
'cookie_notice_layout' => 'bar',
'cookie_notice_position' => 'bottom-left',
)
);

$this->assertSame( 'bottom-left', $result['cookie_notice_position'] );
}

public function test_a_non_array_filter_result_is_ignored() {
add_filter( 'frcn_cookie_notice_layouts', '__return_false' );

$this->assertSame( array( 'bar', 'box', 'popup' ), array_keys( CookieNotice::get_layouts() ) );
}
}
Loading