diff --git a/globals/sanitize-functions.php b/globals/sanitize-functions.php index 9a0990f50d..1110624ae2 100644 --- a/globals/sanitize-functions.php +++ b/globals/sanitize-functions.php @@ -102,21 +102,19 @@ function neve_sanitize_colors( $value ) { * * @param mixed $value Color in rgba format. * - * @return string + * @return string Normalized rgba() color, or '' when the color is incomplete. */ function neve_sanitize_rgba( $value ) { if ( ! is_string( $value ) ) { return 'rgba(0,0,0,0)'; } - $red = 'rgba(0,0,0,0)'; - $green = 'rgba(0,0,0,0)'; - $blue = 'rgba(0,0,0,0)'; - $alpha = 'rgba(0,0,0,0)'; // If empty or an array return transparent + $parts = sscanf( str_replace( ' ', '', $value ), 'rgba(%d,%d,%d,%f)' ); + if ( ! is_array( $parts ) || in_array( null, $parts, true ) || ! is_finite( $parts[3] ) ) { + return ''; + } - // By now we know the string is formatted as an rgba color so we need to further sanitize it. - $value = str_replace( ' ', '', $value ); - sscanf( $value, 'rgba(%d,%d,%d,%f)', $red, $green, $blue, $alpha ); + list( $red, $green, $blue, $alpha ) = $parts; return 'rgba(' . $red . ',' . $green . ',' . $blue . ',' . $alpha . ')'; } diff --git a/header-footer-grid/Core/Builder/Abstract_Builder.php b/header-footer-grid/Core/Builder/Abstract_Builder.php index 24173fec99..9821e544c1 100644 --- a/header-footer-grid/Core/Builder/Abstract_Builder.php +++ b/header-footer-grid/Core/Builder/Abstract_Builder.php @@ -45,6 +45,7 @@ abstract class Abstract_Builder implements Builder { const TEXT_COLOR = 'new_text_color'; const BACKGROUND_SETTING = 'background'; const WIDTH = 'width'; + const RENDER_SLOTS = [ 'left', 'c-left', 'center', 'c-right', 'right' ]; const CONTENT_ALIGNMENT = 'content_alignment'; const VERTICAL_ALIGN = 'vertical_align'; /** @@ -1281,13 +1282,7 @@ public function new_render_components( $device ) { $data = [ 'sidebar' => $data ]; } - $render_buffer = [ - 'left' => [], - 'c-left' => [], - 'center' => [], - 'c-right' => [], - 'right' => [], - ]; + $render_buffer = array_fill_keys( self::RENDER_SLOTS, [] ); $slot = null; $component_index = null; @@ -1490,16 +1485,7 @@ public function render_components( $device = null ) { $max_columns = 12; $last_item = null; - usort( - $data, - function ( $item1, $item2 ) { - if ( $item1['x'] === $item2['x'] ) { - return 0; - } - - return $item1['x'] < $item2['x'] ? - 1 : 1; - } - ); + usort( $data, [ self::class, 'compare_legacy_position' ] ); $collection = new \CachingIterator( new \ArrayIterator( @@ -1717,6 +1703,54 @@ public function get_component( $id = null ) { return $this->builder_components[ $id ]; } + /** + * Component ids of a device's rows in render order, without the sidebar row. + * + * @param string $device Device id. + * + * @return list + */ + public function get_device_component_ids( string $device ): array { + $layout = $this->get_layout_data(); + $ids = []; + foreach ( array_keys( $this->get_rows() ) as $row_index ) { + $row = isset( $layout[ $device ][ $row_index ] ) && is_array( $layout[ $device ][ $row_index ] ) ? $layout[ $device ][ $row_index ] : []; + if ( $row_index === 'sidebar' || empty( $row ) ) { + continue; + } + + if ( ! neve_is_new_builder() ) { + usort( $row, [ self::class, 'compare_legacy_position' ] ); + $ids = array_merge( $ids, array_column( $row, 'id' ) ); + continue; + } + + foreach ( self::RENDER_SLOTS as $slot ) { + if ( isset( $row[ $slot ] ) && is_array( $row[ $slot ] ) ) { + $ids = array_merge( $ids, array_column( $row[ $slot ], 'id' ) ); + } + } + } + + return $ids; + } + + /** + * Orders legacy builder items by their column position. + * + * @param array{x: int} $item1 First item. + * @param array{x: int} $item2 Second item. + * + * @return int + */ + private static function compare_legacy_position( array $item1, array $item2 ): int { + if ( $item1['x'] === $item2['x'] ) { + return 0; + } + + return $item1['x'] < $item2['x'] ? - 1 : 1; + } + /** * Returns the builder components. * diff --git a/header-footer-grid/Core/Components/MenuIcon.php b/header-footer-grid/Core/Components/MenuIcon.php index 26ce6369ee..7fdde9108a 100644 --- a/header-footer-grid/Core/Components/MenuIcon.php +++ b/header-footer-grid/Core/Components/MenuIcon.php @@ -11,6 +11,7 @@ namespace HFG\Core\Components; +use HFG\Core\Builder\Header as HeaderBuilder; use HFG\Core\Script_Register; use HFG\Core\Settings\Manager as SettingsManager; use HFG\Main; @@ -151,6 +152,45 @@ public function add_menu_icon_classes( $class = '', $icon = '' ) { return $class; } + /** + * Icon the sidebar close button mirrors: the first menu icon rendered in the mobile header, else the first registered one in the layout. + * + * Only an icon in the layout enqueues its styles, so an unused one must not decide the close button markup. + * + * @return array{icon: 'default'|'donner'|'dots'|'wave'|'svg', svg: string} + */ + public static function get_close_button_icon(): array { + $default = [ + 'icon' => 'default', + 'svg' => '', + ]; + $header = Main::get_instance()->get_builder( HeaderBuilder::BUILDER_NAME ); + if ( ! $header instanceof HeaderBuilder ) { + return $default; + } + + $candidates = array_merge( $header->get_device_component_ids( 'mobile' ), array_keys( $header->get_components() ) ); + foreach ( $candidates as $component_id ) { + $component = $header->get_component( $component_id ); + if ( ! $component instanceof self || ! $header->is_component_active( $component_id ) ) { + continue; + } + + $icon = Mods::get( $component->get_id() . '_' . self::MENU_ICON, 'default' ); + $svg = (string) Mods::get( $component->get_id() . '_' . self::MENU_SVG, '' ); + if ( ! in_array( $icon, [ 'donner', 'dots', 'wave', 'svg' ], true ) || ( $icon === 'svg' && $svg === '' ) ) { + return $default; + } + + return [ + 'icon' => $icon, + 'svg' => $svg, + ]; + } + + return $default; + } + /** * Load Component Scripts * diff --git a/header-footer-grid/templates/row-wrapper-mobile.php b/header-footer-grid/templates/row-wrapper-mobile.php index 2ee4f8a35d..0697a47e26 100644 --- a/header-footer-grid/templates/row-wrapper-mobile.php +++ b/header-footer-grid/templates/row-wrapper-mobile.php @@ -22,16 +22,10 @@ $item_attributes = apply_filters( 'neve_nav_toggle_data_attrs', '' ); $close_classes = 'close-sidebar-panel navbar-toggle-wrapper' . ( $close_contained ? ' container' : '' ); $submenu_style = row_setting( 'layout', 'slide_left' ); -$menu_icon_class = apply_filters( 'neve_menu_icon_classes', 'hamburger is-active ' ); - -$menu_icon = component_setting( MenuIcon::MENU_ICON ); -$menu_svg = component_setting( MenuIcon::MENU_SVG ); -if ( ! in_array( $menu_icon, [ 'donner', 'dots', 'svg', 'wave' ], true ) ) { - $menu_icon = 'default'; -} -if ( $menu_icon === 'svg' && empty( $menu_svg ) ) { - $menu_icon = 'default'; -} +$close_icon = MenuIcon::get_close_button_icon(); +$menu_icon = $close_icon['icon']; +$menu_svg = $close_icon['svg']; +$menu_icon_class = apply_filters( 'neve_menu_icon_classes', 'hamburger is-active ', $menu_icon ); ?>
> diff --git a/tests/test-neve-hfg-menu-icon-close-button.php b/tests/test-neve-hfg-menu-icon-close-button.php new file mode 100644 index 0000000000..10581633da --- /dev/null +++ b/tests/test-neve-hfg-menu-icon-close-button.php @@ -0,0 +1,355 @@ +} + */ + private $previous_state; + + /** + * Store the render state and start from an uncached layout. + */ + public function set_up(): void { + parent::set_up(); + + $this->previous_state = [ + 'statics' => [ + 'builder' => Abstract_Builder::$current_builder, + 'builder_component' => Abstract_Builder::$current_component, + 'component' => Abstract_Component::$current_component, + 'device' => Abstract_Builder::$current_device, + 'row' => Abstract_Builder::$current_row, + 'no_cache' => Mods::$no_cache, + ], + 'builder' => [], + ]; + foreach ( self::BUILDER_PROPERTIES as $property ) { + $this->previous_state['builder'][ $property ] = $this->builder_property( $property )->getValue( $this->header_builder() ); + } + + Mods::$no_cache = true; + } + + /** + * Restore the render state and builder properties. + */ + public function tear_down(): void { + $statics = $this->previous_state['statics']; + Abstract_Builder::$current_builder = $statics['builder']; + Abstract_Builder::$current_component = $statics['builder_component']; + Abstract_Component::$current_component = $statics['component']; + Abstract_Builder::$current_device = $statics['device']; + Abstract_Builder::$current_row = $statics['row']; + Mods::$no_cache = $statics['no_cache']; + + foreach ( $this->previous_state['builder'] as $property => $value ) { + $this->builder_property( $property )->setValue( $this->header_builder(), $value ); + } + + parent::tear_down(); + } + + /** + * Get the header builder instance. + * + * @return Abstract_Builder + */ + private function header_builder(): Abstract_Builder { + $builder = Main::get_instance()->get_builder( 'header' ); + + $this->assertInstanceOf( Abstract_Builder::class, $builder ); + + return $builder; + } + + /** + * Accessible reflection of a builder property. + * + * @param string $property Property name. + * + * @return ReflectionProperty + */ + private function builder_property( string $property ): ReflectionProperty { + $reflection = new ReflectionProperty( Abstract_Builder::class, $property ); + $reflection->setAccessible( true ); + + return $reflection; + } + + /** + * Place components in the main rows and drop the cached layout. + * + * @param list $component_ids Components in the mobile row. + * @param list $desktop_component_ids Components in the desktop row. + */ + private function set_mobile_layout( array $component_ids, array $desktop_component_ids = [] ): void { + $to_slot = function ( array $ids ): array { + return array_map( + function ( string $id ): array { + return [ 'id' => $id ]; + }, + $ids + ); + }; + $this->set_header_layout( + [ + 'desktop' => [ 'main' => [ 'right' => $to_slot( $desktop_component_ids ) ] ], + 'mobile' => [ + 'main' => [ 'right' => $to_slot( $component_ids ) ], + 'sidebar' => [], + ], + ] + ); + } + + /** + * Save a header layout for the given builder version and drop the cached layout. + * + * @param array> $layout Layout data per device. + * @param bool $is_new_builder Whether the layout uses the new builder format. + */ + private function set_header_layout( array $layout, bool $is_new_builder = true ): void { + $control_id = 'hfg_header_layout' . ( $is_new_builder ? '_v2' : '' ); + set_theme_mod( 'neve_migrated_builders', $is_new_builder ); + set_theme_mod( $control_id, wp_json_encode( $layout ) ); + + $this->builder_property( 'layout_control_id' )->setValue( $this->header_builder(), $control_id ); + $this->builder_property( 'layout_data' )->setValue( $this->header_builder(), [] ); + $this->builder_property( 'active_components' )->setValue( $this->header_builder(), [] ); + } + + /** + * Register the additional menu icon in the header builder. + */ + private function register_additional_menu_icon(): void { + $icon = ( new ReflectionClass( Neve_Test_Additional_Menu_Icon::class ) )->newInstanceWithoutConstructor(); + $icon->init(); + + $components = $this->builder_property( 'builder_components' )->getValue( $this->header_builder() ); + $components[ $icon->get_id() ] = $icon; + $this->builder_property( 'builder_components' )->setValue( $this->header_builder(), $components ); + } + + /** + * Render the mobile sidebar with the given component last in render context. + * + * @param string $last_component Component rendered before the sidebar. + * + * @return string + */ + private function render_sidebar( string $last_component ): string { + Abstract_Builder::$current_builder = 'header'; + Abstract_Builder::$current_device = 'mobile'; + Abstract_Builder::$current_row = 'sidebar'; + Abstract_Builder::$current_component = $last_component; + + ob_start(); + Main::get_instance()->load( 'row-wrapper', 'mobile' ); + + return (string) ob_get_clean(); + } + + /** + * Close button markup, without the sidebar content. + * + * @param string $html Rendered sidebar. + * + * @return string + */ + private function close_button( string $html ): string { + $this->assertSame( 1, preg_match( '#