content->get_current_language_code(); $lang = is_string( $lang ) ? sanitize_key( $lang ) : ''; if ( '' === $lang ) { return $args; } // Resolve current menu term id from args. if ( empty( $args['menu'] ) ) { return $args; } $current_menu_id = 0; $menu = $args['menu']; if ( is_numeric( $menu ) ) { $current_menu_id = absint( $menu ); } elseif ( is_object( $menu ) && isset( $menu->term_id ) ) { $current_menu_id = absint( $menu->term_id ); } elseif ( is_string( $menu ) ) { $obj = wp_get_nav_menu_object( $menu ); if ( $obj && isset( $obj->term_id ) ) { $current_menu_id = absint( $obj->term_id ); } } if ( $current_menu_id <= 0 ) { return $args; } // Find the mapping "location" whose any-language menu matches the current menu. $matched_loc = ''; foreach ( $map as $loc => $langs ) { if ( ! is_array( $langs ) ) { continue; } foreach ( $langs as $code => $mid ) { if ( absint( $mid ) === $current_menu_id ) { $matched_loc = sanitize_key( (string) $loc ); break 2; } } } if ( '' === $matched_loc ) { return $args; } if ( empty( $map[ $matched_loc ][ $lang ] ) ) { return $args; } $target_menu_id = absint( $map[ $matched_loc ][ $lang ] ); if ( $target_menu_id > 0 && $target_menu_id !== $current_menu_id ) { $args['menu'] = $target_menu_id; } return $args; } /** * @param array $locations location => menu_id * @return array */ public function filter_locations( $locations ) { if ( ! is_array( $locations ) ) { return $locations; } $opt = get_option( 'abdiel_amm_menu_switching', [] ); if ( ! is_array( $opt ) ) { return $locations; } if ( empty( $opt['enabled'] ) ) { return $locations; } $map = isset( $opt['map'] ) && is_array( $opt['map'] ) ? $opt['map'] : []; if ( empty( $map ) ) { return $locations; } $lang = abdiel_amm_boot()->content->get_current_language_code(); $lang = is_string( $lang ) ? sanitize_key( $lang ) : ''; if ( '' === $lang ) { return $locations; } // If the current language has no explicit menu assignments, do nothing. foreach ( $locations as $loc => $menu_id ) { $loc = sanitize_key( (string) $loc ); if ( ! isset( $map[ $loc ] ) || ! is_array( $map[ $loc ] ) ) { continue; } if ( ! empty( $map[ $loc ][ $lang ] ) ) { $locations[ $loc ] = absint( $map[ $loc ][ $lang ] ); } } return $locations; } }