<?php
/**
 * Custom template tags for this theme
 * 
 * @package bootstrap-basic
 * 
 * phpcs:disable Generic.WhiteSpace.ScopeIndent.Incorrect, Generic.WhiteSpace.ScopeIndent.IncorrectExact
 */


if (!function_exists('bootstrapBasicCategoriesList')) {
    /**
     * Display categories list with bootstrap icon
     * 
     * @param string $categories_list list of categories.
     * @return string
     */
    function bootstrapBasicCategoriesList($categories_list = '') 
    {
        return sprintf('<span class="categories-icon glyphicon glyphicon-th-list" title="' . __('Posted in', 'bootstrap-basic') . '"></span> %1$s', $categories_list);
    }// bootstrapBasicCategoriesList
}


if (!function_exists('bootstrapBasicComment')) {
    /**
     * Displaying a comment
     * 
     * @param \WP_Comment $comment Comment object.
     * @param array $args Arguments.
     * @param int $depth Depth.
     */
    function bootstrapBasicComment($comment, $args, $depth) {
        if ('pingback' === $comment->comment_type || 'trackback' === $comment->comment_type) { 
            echo '<li id="comment-';
                comment_ID();
                echo '" ';
                comment_class('comment-type-pt');
            echo '>';
            echo '<div class="comment-body media">';
                echo '<div class="media-body">';
                    esc_html_e('Pingback:', 'bootstrap-basic');
                    comment_author_link(); 
                    edit_comment_link(__('Edit', 'bootstrap-basic'), '<span class="edit-link">', '</span>');
                echo '</div>';
            echo '</div>';
        } else {
            echo '<li id="comment-';
                comment_ID();
                echo '" ';
                comment_class(empty($args['has_children']) ? '' : 'parent media');
            echo '>';

            echo '<article id="div-comment-';
                comment_ID();
            echo '" class="comment-body media">';

                // footer
                echo '<footer class="comment-meta pull-left">';
                    if (0 !== intval($args['avatar_size'])) {
                        echo get_avatar($comment, $args['avatar_size']);
                    }
                echo '</footer><!-- .comment-meta -->';
                // end footer

                // comment content
                echo '<div class="comment-content media-body">';
                    echo '<div class="comment-author vcard">';
                        echo '<div class="comment-metadata">';

                        // date-time
                        echo '<a href="';
                            echo esc_url(get_comment_link($comment->comment_ID));
                        echo '">';
                        echo '<time datetime="';
                            comment_time('c');
                        echo '">';
                        echo wp_kses_post(
                            /* translators: %1$s: Comment date, %2$s: Comment time. */
                            sprintf(_x('%1$s at %2$s', '1: date, 2: time', 'bootstrap-basic'), get_comment_date(), get_comment_time())
                        );
                        echo '</time>';
                        echo '</a>';
                        // end date-time

                        echo ' ';

                        edit_comment_link('<span class="fa fa-pencil-square-o "></span>' . __('Edit', 'bootstrap-basic'), '<span class="edit-link">', '</span>');

                        echo '</div><!-- .comment-metadata -->';

                        // if comment was not approved
                        if ('0' === strval($comment->comment_approved)) {
                            echo '<div class="comment-awaiting-moderation text-warning"> <span class="glyphicon glyphicon-info-sign"></span> ';
                                esc_html_e('Your comment is awaiting moderation.', 'bootstrap-basic');
                            echo '</div>';
                        } //endif;

                        // comment author says
                        echo wp_kses_post(
                            /* translators: %s: Comment author link. */
                            sprintf(__('%s <span class="says">says:</span>', 'bootstrap-basic'), sprintf('<cite class="fn">%s</cite>', get_comment_author_link()))
                        );
                    echo '</div><!-- .comment-author -->';

                    // comment content body
                    comment_text();
                    // end comment content body

                    // reply link
                    comment_reply_link(array_merge($args, array(
                        'add_below' => 'div-comment',
                        'depth'     => $depth,
                        'max_depth' => $args['max_depth'],
                        'reply_text' => '<span class="fa fa-reply"></span> ' . __('Reply', 'bootstrap-basic'),
                        'login_text' => '<span class="fa fa-reply"></span> ' . __('Log in to Reply', 'bootstrap-basic'),
                    )));
                    // end reply link
                echo '</div><!-- .comment-content -->';
                // end comment content

            echo '</article><!-- .comment-body -->';
        } //endif;
    }// bootstrapBasicComment
}


if (!function_exists('bootstrapBasicCommentsPopupLink')) {
    /**
     * Custom comment popup link
     * 
     * @return string
     */
    function bootstrapBasicCommentsPopupLink() 
    {
        $comment_icon = '<span class="comment-icon glyphicon glyphicon-comment"><small class="comment-total">%d</small></span>';
        $comments_icon = '<span class="comment-icon glyphicon glyphicon-comment"><small class="comment-total">%s</small></span>';
        return comments_popup_link(sprintf($comment_icon, ''), sprintf($comment_icon, '1'), sprintf($comments_icon, '%'), 'btn btn-default btn-xs');
    }// bootstrapBasicCommentsPopupLink
}


if (!function_exists('bootstrapBasicEditPostLink')) {
    /**
     * Display edit post link
     */
    function bootstrapBasicEditPostLink() 
    {
        $edit_post_link = get_edit_post_link();
        if (!empty($edit_post_link)) {
            $edit_btn = '<a class="post-edit-link btn btn-default btn-xs" href="' . esc_url($edit_post_link) . '" title="' . esc_attr__('Edit', 'bootstrap-basic') . '"><i class="edit-post-icon glyphicon glyphicon-pencil" title="' . esc_attr__('Edit', 'bootstrap-basic') . '"></i></a>';
            unset($edit_post_link);
            echo wp_kses_post($edit_btn);
        }
    }// bootstrapBasicEditPostLink
}


if (!function_exists('bootstrapBasicFullPageSearchForm')) {
    /**
     * Display full page search form
     * 
     * @return string the search form element
     */
    function bootstrapBasicFullPageSearchForm() 
    {
        return get_search_form(array(
            'echo' => false,
        ));
    }// bootstrapBasicFullPageSearchForm
}


if (!function_exists('bootstrapBasicGetLinkInContent')) {
    /**
     * Get the link in content.
     * 
     * @return string
     */
    function bootstrapBasicGetLinkInContent() 
    {
        $content = get_the_content();
        $has_url = get_url_in_content($content);

        if ($has_url) {
            return $has_url;
        } else {
            return apply_filters('the_permalink', get_permalink());// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
        }
    }// bootstrapBasicGetLinkInContent
}


if (!function_exists('bootstrapBasicMoreLinkText')) {
    /**
     * Custom more link (continue reading) text.
     * 
     * @return string
     */
    function bootstrapBasicMoreLinkText() 
    {
        return __('Continue reading <span class="meta-nav">&rarr;</span>', 'bootstrap-basic');
    }// bootstrapBasicMoreLinkText
}


if (!function_exists('bootstrapBasicPagination')) {
    /**
     * Display pagination (1 2 3 ...) instead of previous, next of WordPress style.
     * 
     * @param string $pagination_align_class Pagination HTML class.
     */
    function bootstrapBasicPagination($pagination_align_class = 'pagination-center pagination-row') 
    {
        global $wp_query;
        $big = 999999999;
        $pagination_array = paginate_links(array(
            'base' => str_replace($big, '%#%', get_pagenum_link($big)),
            'format' => '/page/%#%',
            'current' => max(1, get_query_var('paged')),
            'total' => $wp_query->max_num_pages,
            'prev_text' => '&laquo;',
            'next_text' => '&raquo;',
            'type' => 'array',
        ));

        unset($big);

        if (is_array($pagination_array) && !empty($pagination_array)) {
            echo '<nav class="' . esc_attr($pagination_align_class) . '">';
            echo '<ul class="pagination">';
            foreach ($pagination_array as $page) {
                if (!is_scalar($page)) {
                    echo '<!-- $page is not scalar type: ' . esc_html(gettype($page)) . ' -->' . PHP_EOL;
                    continue;
                }

                echo '<li';
                if (strpos($page, '<a') === false && strpos($page, '&hellip;') === false) {
                    echo ' class="active"';
                }
                echo '>';
                if (strpos($page, '<a') === false && strpos($page, '&hellip;') === false) {
                    echo '<span>' . wp_kses_post($page) . '</span>';
                } else {
                    echo wp_kses_post($page);
                }
                echo '</li>';
            }
            echo '</ul>';
            echo '</nav>';
        }

        unset($page, $pagination_array);
    }// bootstrapBasicPagination
}


if (!function_exists('bootstrapBasicPostOn')) {
    /**
     * Display post date/time and author.
     */
    function bootstrapBasicPostOn() 
    {
        $time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time>';
        if (get_the_time('U') !== get_the_modified_time('U')) {
            $time_string .= '<time class="updated" datetime="%3$s">%4$s</time>';
        }

        $time_string = sprintf($time_string,
            esc_attr(get_the_date('c')),
            esc_html(get_the_date()),
            esc_attr(get_the_modified_date('c')),
            esc_html(get_the_modified_date())
        );

        echo wp_kses_post(
            /* translators: %1$s: Link to post with date/time text, %2$s: Link to author with auth name. */
            sprintf(__('<span class="posted-on">Posted on %1$s</span><span class="byline"> by %2$s</span>', 'bootstrap-basic'),
                sprintf('<a href="%1$s" title="%2$s" rel="bookmark">%3$s</a>',
                    esc_url(get_permalink()),
                    esc_attr(get_the_time()),
                    $time_string
                ),
                sprintf('<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>',
                    esc_url(get_author_posts_url(get_the_author_meta('ID'))),
                    /* translators: %s Author name. */
                    esc_attr(sprintf(__('View all posts by %s', 'bootstrap-basic'), get_the_author())),
                    esc_html(get_the_author())
                )
            )
        );
    }// bootstrapBasicPostOn
}


if (!function_exists('bootstrapBasicTagsList')) {
    /**
     * Display tags list.
     * 
     * @param string $tags_list HTML tag list.
     * @return string
     */
    function bootstrapBasicTagsList($tags_list = '') 
    {
        return sprintf('<span class="tags-icon glyphicon glyphicon-tags" title="' . __('Tagged', 'bootstrap-basic') . '"></span>&nbsp; %1$s', $tags_list);
    }// bootstrapBasicTagsList
}


if (!function_exists('bootstrapBasicTheAttachedImage')) {
    /**
     * Display attach image with link.
     */
    function bootstrapBasicTheAttachedImage() 
    {
        $post                = get_post();
        $attachment_size     = apply_filters('bootstrap_basic_attachment_size', array(1140, 1140));
        $next_attachment_url = wp_get_attachment_url();

        /**
         * Grab the IDs of all the image attachments in a gallery so we can get the
         * URL of the next adjacent image in a gallery, or the first image (if
         * we're looking at the last image in a gallery), or, in a gallery of one,
         * just the link to that image file.
         */
        $attachment_ids = get_posts(array(
            'post_parent'    => $post->post_parent,
            'fields'         => 'ids',
            'numberposts'    => -1,
            'post_status'    => 'inherit',
            'post_type'      => 'attachment',
            'post_mime_type' => 'image',
            'order'          => 'ASC',
            'orderby'        => 'menu_order ID',
        ));

        // If there is more than 1 attachment in a gallery...
        if (count($attachment_ids) > 1) {
            foreach ($attachment_ids as $attachment_id) {
                if ($attachment_id === $post->ID) {
                    $next_id = current($attachment_ids);
                    break;
                }
            }

            if ($next_id) {
                // get the URL of the next image attachment...
                $next_attachment_url = get_attachment_link($next_id);
            } else {
                // or get the URL of the first image attachment.
                $next_attachment_url = get_attachment_link(array_shift($attachment_ids));
            }
        }

        printf('<a href="%1$s" title="%2$s" rel="attachment">%3$s</a>',
            esc_url($next_attachment_url),
            the_title_attribute(array('echo' => false)),
            wp_get_attachment_image($post->ID, $attachment_size, false, array('class' => 'img-responsive aligncenter'))
        );
    }// bootstrapBasicTheAttachedImage
}
