<?php 

/**
 * navigation menu add class
 */
function mpwbpk_func__add_additional_class_on_li($classes, $item, $args) {
    if(isset($args->add_li_class)) {
        $classes[] = $args->add_li_class;
    }
    return $classes;
}
add_filter('nav_menu_css_class', 'mpwbpk_func__add_additional_class_on_li', 1, 3);

add_filter( 'nav_menu_link_attributes', 'mpwbpk_func__add_link_atts');
function mpwbpk_func__add_link_atts($atts) {
    $atts['class'] = "nav-link text-uppercase dropdown-toggle";
    return $atts;
}

// add_filter( 'render_block', 'mpwbpk_func__wrap_table_block', 10, 2 );
function mpwbpk_func__wrap_table_block( $block_content, $block ) {
    // if ( 'core/table' === $block['blockName'] ) {
    // }
    if(!empty(trim($block_content))){
    $block_content = '<div class="block-section"><div class="container">' . $block_content . '</div></div>';
    }
    return $block_content;
}

function mpwbpk_func__language_switcher() {
    ob_start();
    $languages = apply_filters( 'wpml_active_languages', NULL, 'orderby=id&order=desc' );

    if ( !empty( $languages ) ) {
        foreach( $languages as $l ) {
            // var_dump($l);
            ?>
            <?php if ( !$l['active'] ): ?>
                <li data-lang="<?php echo esc_attr( $l['language_code'] ); ?>">
                    <a class="btn btn-light rounded-circle p-0 d-flex justify-content-center align-items-center"
                        style="height: 2.625rem; width: 2.625rem;"
                        href="<?php echo esc_url( $l['url'] ); ?>">
                        <span class="text-uppercase fw-semibold small"><?php echo esc_attr( $l['language_code'] ); ?></span>
                    </a>
                </li>
            <?php endif;
        }
    }
    return ob_get_clean();
}
/**
 * Add a parent CSS class for nav menu items.
 *
 * @param array  $items The menu items, sorted by each menu item's menu order.
 * @return array (maybe) modified parent CSS class.
 */
function wpdocs_add_menu_parent_class( $items ) {
	$parents = array();

	// Collect menu items with parents.
	foreach ( $items as $item ) {
		if ( $item->menu_item_parent && $item->menu_item_parent > 0 ) {
			$parents[] = $item->menu_item_parent;
		}
	}

	// Add class.
	foreach ( $items as $item ) {
		if ( in_array( $item->ID, $parents ) ) {
			$item->classes[] = 'dropdown';
		}
	}
	return $items;
}
add_filter( 'wp_nav_menu_objects', 'wpdocs_add_menu_parent_class' );

function my_nav_menu_submenu_css_class( $classes ) {
    $classes[] = 'dropdown-menu';
    return $classes;
}
add_filter( 'nav_menu_submenu_css_class', 'my_nav_menu_submenu_css_class' );

/**
 * Redirect pages
 */
if ( !is_admin() ) {
	$http_host = $_SERVER['HTTP_HOST'];
	$request_uri = $_SERVER['REQUEST_URI'];
	$protocol = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != "off") ? "https://" : "http://";
	$url = $protocol . $http_host . $request_uri;

	if (in_array(strtolower($request_uri), array('/lv', '/lv/', 'lv'))) {
		wp_redirect($protocol . $http_host, 301);
		exit;
	}
}

/**
 * Remove admin bar from frontend
 */
add_filter('show_admin_bar', '__return_false');