/**
 *  Loop through the $books array and generate the HTML output for the
 *  grid, including printing out the headings and indenting at each
 *  nested level
 *  
 *  Recursively called for each level
 *  
 *  @since 1.0
 *  @since 2.0 made responsive
 *  @since 3.0 re-factored
 *  
 *  @param [array] $mbdb_books nested array of books in grid
 *  @param [int] $l           current level to display
 *  
 *  @return Return_Description
 *  
 *  @access public
 */
function mbdb_display_grid($mbdb_books, $l)
{
    // grab the coming soon image
    $mbdb_options = get_option('mbdb_options');
    $coming_soon_image = $mbdb_options['coming-soon'];
    // indent the grid by 15px per depth level of the array
    do_action('mbdb_book_grid_pre_div', $l);
    $content = '<div class="mbm-book-grid-div" style="padding-left:' . 15 * $l . 'px;">';
    if (count($mbdb_books) > 0) {
        // because the index of the array could be a genre or series name and not a sequential index use array_keys to get the index
        // if the first element in the array is an object that means there's NOT another level in the array
        // so just print out the grid and skip the rest
        $the_key = array_keys($mbdb_books);
        if (count($the_key) > 0) {
            // this breaks the recursion
            if (gettype($mbdb_books[$the_key[0]]) == 'object') {
                foreach ($mbdb_books as $book) {
                    do_action('mbdb_book_grid_pre_div', $l);
                    $content .= mbdb_output_grid_book($book, $coming_soon_image);
                }
                $content .= '</div>';
                do_action('mbdb_book_grid_post_div', $l);
                return apply_filters('mbdb_book_grid_content', $content, $l);
            }
        }
        // loop through each book
        foreach ($mbdb_books as $key => $set) {
            // If a label is set and there's at least one book, print the label
            if ($key && count($set) > 0) {
                // set the heading level based on the depth level of the array
                do_action('mbdb_book_grid_pre_heading', $l, $key);
                // start the headings at H2
                $heading_level = $l + 2;
                // Headings can only go to H6
                if ($heading_level > 6) {
                    $heading_level = 6;
                }
                // display the heading
                $content .= '<h' . $heading_level . ' class="mbm-book-grid-heading' . ($l + 1) . '">' . esc_html($key) . '</h' . $heading_level . '>';
                do_action('mbdb_book_grid_post_heading', $l, $key);
            }
            if (gettype($set) != 'object') {
                do_action('mbdb_book_grid_pre_recursion', $set, $l + 1);
                $content .= mbdb_display_grid($set, $l + 1);
                do_action('mbdb_book_grid_post_recursion', $set, $l + 1);
            }
        }
    } else {
        do_action('mbdb_book_grid_no_books_found');
        $content = apply_filters('mbdb_book_grid_books_not_found', $content . __('Books not found', 'mooberry-book-manager'));
    }
    $content .= '</div>';
    do_action('mbdb_book_grid_post_div', $l);
    return apply_filters('mbdb_book_grid_content', $content, $l);
}
function mbdb_tax_grid_content($attr, $content)
{
    $attr = shortcode_atts(array('taxonomy' => '', 'term' => ''), $attr);
    global $wp_query;
    if (isset($wp_query->query_vars['the-term'])) {
        $term = trim(urldecode($wp_query->query_vars['the-term']), '/');
    } else {
        $term = $attr['term'];
    }
    if (isset($wp_query->query_vars['the-taxonomy'])) {
        $taxonomy = trim(urldecode($wp_query->query_vars['the-taxonomy']), '/');
    } else {
        $taxonomy = $attr['taxonomy'];
    }
    if ($taxonomy == '' || $term == '') {
        return __('There was an error!', 'mooberry-book-manager');
    }
    $selection = str_replace('mbdb_', '', $taxonomy);
    //$groups[1] = $selection;
    $groups[1] = 'none';
    $groups[2] = 'none';
    //$current_group = array($selection => 0, 'none' => 0);
    $current_group = array('none' => 0);
    $sort = mbdb_set_sort($groups, 'titleA');
    // set sort varialbles
    //list( $orderby, $order ) = MBDB()->books->get_sort_fields( $sort );
    // get id
    $term_obj = get_term_by('slug', $term, $taxonomy);
    if ($term_obj != null) {
        $selected_ids = array((int) $term_obj->term_id);
    } else {
        $selected_ids = null;
    }
    $books = apply_filters('mbdb_tax_grid_get_group', mbdb_get_group(1, $groups, $current_group, $selection, $selected_ids, $sort, null), $groups, $current_group, $selection, $selected_ids, $sort, $term);
    //$orderby, $order, null);
    /********************* term meta ***************************************/
    if (function_exists('get_term_meta')) {
        $content = '<p>' . get_term_meta($selected_ids[0], $taxonomy . '_book_grid_description', true) . '</p>';
        $content2 = '<p>' . get_term_meta($selected_ids[0], $taxonomy . '_book_grid_description_bottom', true) . '</p>';
    } else {
        $content = '';
        $content2 = '';
    }
    return $content . mbdb_display_grid($books, 0) . $content2;
}