/*
 * Custom Shortcode to display taxonomy terms in a grid with pagination.
 */
function wpac_authors_grid( $atts ) {

    $taxonomy = 'book_author';

    $atts = shortcode_atts(
        array(
            'per_page' => 12,
        ),
        $atts,
        'WPAC_AUTHORS_GRID'
    );

    $paged = max( 1, get_query_var( 'paged' ) );
    $per_page = (int) $atts['per_page'];

    $total_terms = wp_count_terms(
        array(
            'taxonomy'   => $taxonomy,
            'hide_empty' => false,
        )
    );

    $offset = ( $paged - 1 ) * $per_page;

    $terms = get_terms(
        array(
            'taxonomy'   => $taxonomy,
            'hide_empty' => false,
            'orderby'    => 'name',
            'order'      => 'ASC',
            'number'     => $per_page,
            'offset'     => $offset,
        )
    );

    if ( empty( $terms ) || is_wp_error( $terms ) ) {
        return '<p>No authors found.</p>';
    }

    $output = '<div class="wpac-authors-grid">';

    foreach ( $terms as $term ) {

        $term_id     = $term->term_id;
        $author_name = $term->name;
        $author_url  = get_term_link( $term );
        $book_count  = $term->count;

        // ACF Field Name
        $image = get_field( 'author_picture', $taxonomy . '_' . $term_id );

        $output .= '<div class="grid-item">';

        if ( ! empty( $image ) ) {

            $image_url = is_array( $image ) ? $image['url'] : $image;

            $output .= '<div class="author-image">';
            $output .= '<a href="' . esc_url( $author_url ) . '">';
            $output .= '<img src="' . esc_url( $image_url ) . '" alt="' . esc_attr( $author_name ) . '">';
            $output .= '</a>';
            $output .= '</div>';
        }

        $output .= '<div class="author-name">';
        $output .= '<a href="' . esc_url( $author_url ) . '">' . esc_html( $author_name ) . '</a>';
        $output .= '</div>';

        $output .= '<div class="author-book-count">';
        $output .= esc_html( $book_count ) . ' ' . ( $book_count == 1 ? 'Book' : 'Books' ) . ' Published';
        $output .= '</div>';

        $output .= '</div>';
    }

    $output .= '</div>';

    $output .= '<div class="wpac-taxonomy-pagination">';

    $output .= paginate_links(
        array(
            'base'      => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
            'format'    => '?paged=%#%',
            'current'   => $paged,
            'total'     => ceil( $total_terms / $per_page ),
            'prev_text' => '&laquo; Previous',
            'next_text' => 'Next &raquo;',
        )
    );

    $output .= '</div>';

    return $output;
}
add_shortcode( 'WPAC_AUTHORS_GRID', 'wpac_authors_grid' );<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="https://hafsa.sphonix.com/wp-sitemap-index.xsl" ?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><sitemap><loc>https://hafsa.sphonix.com/wp-sitemap-posts-post-1.xml</loc></sitemap><sitemap><loc>https://hafsa.sphonix.com/wp-sitemap-posts-page-1.xml</loc></sitemap><sitemap><loc>https://hafsa.sphonix.com/wp-sitemap-posts-product-1.xml</loc></sitemap><sitemap><loc>https://hafsa.sphonix.com/wp-sitemap-posts-mailpoet_page-1.xml</loc></sitemap><sitemap><loc>https://hafsa.sphonix.com/wp-sitemap-taxonomies-category-1.xml</loc></sitemap><sitemap><loc>https://hafsa.sphonix.com/wp-sitemap-taxonomies-book-author-1.xml</loc></sitemap><sitemap><loc>https://hafsa.sphonix.com/wp-sitemap-taxonomies-product_cat-1.xml</loc></sitemap><sitemap><loc>https://hafsa.sphonix.com/wp-sitemap-users-1.xml</loc></sitemap></sitemapindex>
