<?php
/**
 * Mega menu support (Ultimate Bundle).
 *
 * @package EasySite_Pro
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Add mega menu fields to nav menu items.
 *
 * @param int      $item_id Menu item ID.
 * @param WP_Post  $item    Menu item.
 */
function easysite_mega_menu_fields( $item_id, $item ) {
	$enabled = get_post_meta( $item_id, '_easysite_mega_menu', true );
	$columns = get_post_meta( $item_id, '_easysite_mega_columns', true ) ?: 3;
	?>
	<p class="field-easysite-mega description description-wide">
		<label>
			<input type="checkbox" name="easysite_mega_menu[<?php echo esc_attr( $item_id ); ?>]" value="1" <?php checked( $enabled, '1' ); ?> />
			<?php esc_html_e( 'Enable mega menu', 'easysite-pro' ); ?>
		</label>
	</p>
	<p class="field-easysite-mega-cols description description-wide">
		<label>
			<?php esc_html_e( 'Columns', 'easysite-pro' ); ?>
			<select name="easysite_mega_columns[<?php echo esc_attr( $item_id ); ?>]">
				<?php for ( $i = 1; $i <= 4; $i++ ) : ?>
					<option value="<?php echo esc_attr( $i ); ?>" <?php selected( $columns, $i ); ?>><?php echo esc_html( $i ); ?></option>
				<?php endfor; ?>
			</select>
		</label>
	</p>
	<p class="description"><?php esc_html_e( 'Mega menus show child links in a wide dropdown with multiple columns.', 'easysite-pro' ); ?></p>
	<?php
}
add_action( 'wp_nav_menu_item_custom_fields', 'easysite_mega_menu_fields', 10, 2 );

/**
 * Save mega menu meta.
 *
 * @param int $menu_id         Menu ID.
 * @param int $menu_item_db_id Item ID.
 */
function easysite_save_mega_menu_meta( $menu_id, $menu_item_db_id ) {
	$enabled = isset( $_POST['easysite_mega_menu'][ $menu_item_db_id ] ) ? '1' : '';
	update_post_meta( $menu_item_db_id, '_easysite_mega_menu', $enabled );

	if ( isset( $_POST['easysite_mega_columns'][ $menu_item_db_id ] ) ) {
		update_post_meta( $menu_item_db_id, '_easysite_mega_columns', absint( $_POST['easysite_mega_columns'][ $menu_item_db_id ] ) );
	}
}
add_action( 'wp_update_nav_menu_item', 'easysite_save_mega_menu_meta', 10, 2 );

/**
 * Add mega menu class to menu items.
 *
 * @param array    $classes CSS classes.
 * @param WP_Post  $item    Menu item.
 * @return array
 */
function easysite_mega_menu_classes( $classes, $item ) {
	if ( get_post_meta( $item->ID, '_easysite_mega_menu', true ) ) {
		$classes[] = 'easysite-mega-menu-item';
		$cols      = get_post_meta( $item->ID, '_easysite_mega_columns', true ) ?: 3;
		$classes[] = 'easysite-mega-cols-' . absint( $cols );
	}
	return $classes;
}
add_filter( 'nav_menu_css_class', 'easysite_mega_menu_classes', 10, 2 );

/**
 * Custom walker for mega menus.
 */
class EasySite_Mega_Menu_Walker extends Walker_Nav_Menu {
	/**
	 * Start submenu level.
	 *
	 * @param string $output Output.
	 * @param int    $depth  Depth.
	 * @param array  $args   Args.
	 */
	public function start_lvl( &$output, $depth = 0, $args = null ) {
		$indent  = str_repeat( "\t", $depth );
		$output .= "\n{$indent}<ul class=\"sub-menu easysite-mega-panel\">\n";
	}
}

/**
 * Use mega walker for primary menu.
 *
 * @param array $args Menu args.
 * @return array
 */
function easysite_mega_menu_walker( $args ) {
	if ( 'primary' === $args['theme_location'] ) {
		$args['walker'] = new EasySite_Mega_Menu_Walker();
	}
	return $args;
}
add_filter( 'wp_nav_menu_args', 'easysite_mega_menu_walker' );
