From b5b73c444978532bc929f90458cdd1637335a008 Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Thu, 19 Nov 2026 17:16:45 +0530 Subject: [PATCH 1/4] fix: preserve mobile menu close icon when Wave classes are added --- .../Core/Components/MenuIcon.php | 38 +++ .../templates/row-wrapper-mobile.php | 16 +- .../test-neve-hfg-menu-icon-close-button.php | 238 ++++++++++++++++++ 3 files changed, 281 insertions(+), 11 deletions(-) create mode 100644 tests/test-neve-hfg-menu-icon-close-button.php diff --git a/header-footer-grid/Core/Components/MenuIcon.php b/header-footer-grid/Core/Components/MenuIcon.php index 26ce6369ee..b9d6a6cf7f 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,43 @@ public function add_menu_icon_classes( $class = '', $icon = '' ) { return $class; } + /** + * Icon of the first menu icon in the header layout, which the sidebar close button mirrors. + * + * 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; + } + + foreach ( $header->get_components() as $component ) { + if ( ! $component instanceof self || ! $header->is_component_active( $component->get_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..966ec3be5f --- /dev/null +++ b/tests/test-neve-hfg-menu-icon-close-button.php @@ -0,0 +1,238 @@ +} + */ + 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 mobile main row and drop the cached layout. + * + * @param list $component_ids Components in the row. + */ + private function set_mobile_layout( array $component_ids ): void { + $slot = array_map( + function ( string $id ): array { + return [ 'id' => $id ]; + }, + $component_ids + ); + set_theme_mod( 'neve_migrated_builders', true ); + set_theme_mod( + 'hfg_header_layout_v2', + wp_json_encode( + [ + 'mobile' => [ + 'main' => [ 'right' => $slot ], + 'sidebar' => [], + ], + ] + ) + ); + + $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( '#