<?php /**
 * Returns word count of the sentences.
 *
 * @since United Blog 1.0.0
 */
if (!function_exists('united_blog_words_count')) :
    function united_blog_words_count($length = 25, $united_blog_content = null)
    {
        $length = absint($length);
        $source_content = preg_replace('`\[[^\]]*\]`', '', $united_blog_content);
        $trimmed_content = wp_trim_words($source_content, $length, '');
        return $trimmed_content;
    }
endif;


if (!function_exists('united_blog_body_class')) :

    /**
     * body class.
     *
     * @since 1.0.0
     */
    function united_blog_body_class($united_blog_body_class)
    {
        $select_site_mode = united_blog_get_option('select_site_mode');
        $united_blog_body_class[] = $select_site_mode;
        $global_layout = united_blog_get_option('global_layout');
        if ($global_layout == 'left-sidebar') {
            $united_blog_body_class[] = ' left-sidebar ';
        } elseif ($global_layout == 'no-sidebar') {
            $united_blog_body_class[] = ' no-sidebar ';
        } else {
            $united_blog_body_class[] = ' right-sidebar ';

        }
        return $united_blog_body_class;
    }
endif;

add_action('body_class', 'united_blog_body_class');


/**
 * Get the information about the logo.
 *
 * @param string $html The HTML output from get_custom_logo (core function).
 * @return string
 * @since Twenty Twenty 1.0
 *
 */
function united_blog_get_custom_logo($html)
{

    $logo_id = united_blog_get_option('custom_logo');

    if (!$logo_id) {
        return $html;
    }

    $logo = wp_get_attachment_image_src($logo_id, 'full');

    if ($logo) {
        // For clarity.
        $logo_width = esc_attr($logo[1]);
        $logo_height = esc_attr($logo[2]);

        // If the retina logo setting is active, reduce the width/height by half.
        if (united_blog_get_option('retina_logo', false)) {
            $logo_width = floor($logo_width / 2);
            $logo_height = floor($logo_height / 2);

            $search = array(
                '/width=\"\d+\"/iU',
                '/height=\"\d+\"/iU',
            );

            $replace = array(
                "width=\"{$logo_width}\"",
                "height=\"{$logo_height}\"",
            );

            // Add a style attribute with the height, or append the height to the style attribute if the style attribute already exists.
            if (false === strpos($html, ' style=')) {
                $search[] = '/(src=)/';
                $replace[] = "style=\"height: {$logo_height}px;\" src=";
            } else {
                $search[] = '/(style="[^"]*)/';
                $replace[] = "$1 height: {$logo_height}px;";
            }

            $html = preg_replace($search, $replace, $html);

        }
    }

    return $html;
}

add_filter('get_custom_logo', 'united_blog_get_custom_logo');


if (!function_exists('united_blog_excerpt_length')):

    /**
     * Excerpt length
     *
     * @param null
     * @return int
     * @since  united_blog 1.0.0
     *
     */
    function united_blog_excerpt_length($length)
    {
        if (is_admin()) {
            return $length;
        }
        $excerpt_length = united_blog_get_option('excerpt_length_global');
        if (absint($excerpt_length) > 0) {
            $excerpt_length = absint($excerpt_length);
        }

        return absint($excerpt_length);

    }

endif;
add_filter('excerpt_length', 'united_blog_excerpt_length', 999);

if (!function_exists('united_blog_excerpt_more')):

    /**
     * Implement read more in excerpt.
     *
     * @param string $more The string shown within the more link.
     * @return string The excerpt.
     * @since 1.0.0
     *
     */
    function united_blog_excerpt_more($more)
    {

        if (is_admin()) {
            return $more;
        }
        $flag_apply_excerpt_read_more = apply_filters('united_blog_filter_excerpt_read_more', true);
        if (true !== $flag_apply_excerpt_read_more) {
            return $more;
        }

        $output = $more;
        $read_more_text = esc_html(united_blog_get_option('read_more_button_text'));
        if (!empty($read_more_text)) {
            $output = ' <a href="' . esc_url(get_permalink()) . '" class="btn btn-link">' . esc_html($read_more_text) . '</a>';
            $output = apply_filters('united_blog_filter_read_more_link', $output);
        }
        return $output;

    }

    add_filter('excerpt_more', 'united_blog_excerpt_more');
endif;


if (!function_exists('united_blog_get_first_instance_of_block')):

    /** Print the first instance of a block in the content, and then break away.
     * @param string $block_name The full block type name, or a partial match.
     *                                Example: `core/image`, `core-embed/*`.
     * @param string|null $content The content to search in. Use null for get_the_content().
     * @param int $instances How many instances of the block will be printed (max). Default  1.
     * @return bool Returns true if a block was located & printed, otherwise false.
     */
    function united_blog_get_first_instance_of_block($block_name, $content = null, $instances = 1)
    {
        $instances_count = 0;
        $blocks_content = '';

        if (!$content) {
            $content = get_the_content();
        }

        // Parse blocks in the content.
        $blocks = parse_blocks($content);

        // Loop blocks.
        foreach ($blocks as $block) {

            // Sanity check.
            if (!isset($block['blockName'])) {
                continue;
            }

            // Check if this the block matches the $block_name.
            $is_matching_block = false;

            // If the block ends with *, try to match the first portion.
            if ('*' === $block_name[-1]) {
                $is_matching_block = 0 === strpos($block['blockName'], rtrim($block_name, '*'));
            } else {
                $is_matching_block = $block_name === $block['blockName'];
            }

            if ($is_matching_block) {
                // Increment count.
                $instances_count++;

                // Add the block HTML.
                $blocks_content .= render_block($block);

                // Break the loop if the $instances count was reached.
                if ($instances_count >= $instances) {
                    break;
                }
            }
        }

        if ($blocks_content) {
            /** This filter is documented in wp-includes/post-template.php */
            echo apply_filters('the_content', $blocks_content); // phpcs:ignore WordPress.Security.EscapeOutput
            return true;
        }

        return false;
    }
endif; 
if (!function_exists('united_blog_posts_navigations')):
    /**
     * Posts navigation.
     *
     * @since 1.0.0
     */
    function united_blog_posts_navigations()
    {

        $pagination_type = united_blog_get_option('pagination_type');

        switch ($pagination_type) {

            case 'default':
                the_posts_navigation();
                break;

            case 'numeric':
                the_posts_pagination();
                break;

            default:
                break;
        }

    }
endif;

add_action('united_blog_posts_navigation', 'united_blog_posts_navigations');

if (!function_exists('united_blog_post_floating_nav')):

    function united_blog_post_floating_nav()
    {

        $united_floating_post_navigation = united_blog_get_option('united_floating_post_navigation');
        if ('post' === get_post_type() && $united_floating_post_navigation) {

            $next_post = get_next_post();
            $prev_post = get_previous_post();

            if (isset($prev_post->ID)) {

                $prev_link = get_permalink($prev_post->ID); ?>

                <div class="floating-post-navigation floating-navigation-prev">

                    <?php if (get_the_post_thumbnail($prev_post->ID, 'medium')) { ?>
                        <?php echo wp_kses_post(get_the_post_thumbnail($prev_post->ID, 'medium_large')); ?>
                    <?php } ?>

                    <a href="<?php echo esc_url($prev_link); ?>">
                        <span class="floating-navigation-label"><?php echo esc_html__('Prev', 'united-blog'); ?></span>
                        <span class="entry-title entry-title-medium"><?php echo esc_html(get_the_title($prev_post->ID)); ?></span>
                    </a>

                </div>

            <?php }

            if (isset($next_post->ID)) {

                $next_link = get_permalink($next_post->ID); ?>

                <div class="floating-post-navigation floating-navigation-next">

                    <?php if (get_the_post_thumbnail($next_post->ID, 'medium')) { ?>
                        <?php echo wp_kses_post(get_the_post_thumbnail($next_post->ID, 'medium_large')); ?>
                    <?php } ?>

                    <a href="<?php echo esc_url($next_link); ?>">
                        <span class="floating-navigation-label"><?php echo esc_html__('Next', 'united-blog'); ?></span>
                        <span class="entry-title entry-title-medium"><?php echo esc_html(get_the_title($next_post->ID)); ?></span>
                    </a>

                </div>

                <?php
            }

        }

    }

endif;

add_action('united_blog_navigation_action', 'united_blog_post_floating_nav', 10);

function united_blog_archive_title($title)
{
    if (is_category()) {
        $title = single_cat_title('', false);
    } elseif (is_tag()) {
        $title = single_tag_title('', false);
    } elseif (is_author()) {
        $title = '<span class="vcard">' . get_the_author() . '</span>';
    } elseif (is_post_type_archive()) {
        $title = post_type_archive_title('', false);
    } elseif (is_tax()) {
        $title = single_term_title('', false);
    } elseif (is_date()) {
        $title = single_term_title('', false);
    }
    return $title;
}

add_filter('get_the_archive_title', 'united_blog_archive_title');

if (!function_exists('united_blog_get_sidebar')) :
    /**
     * Get Proper sidebar based on settings
     *
     * @return string Sidebar
     * @since 1.0.0
     *
     */
    function united_blog_get_sidebar()
    {
        $class = '';
        if (is_front_page()) :
            $hide_sidebar_mobile = united_blog_get_option('hide_front_page_sidebar_mobile');
            $class .= $hide_sidebar_mobile ? ' hide-on-mobile ' : '';            
            if (united_blog_get_option('front_page_enable_sidebar')) :
                if (is_active_sidebar('home-page-sidebar')) :
                    ?>
                    <div id="secondary" class="sidebar-area <?php echo esc_attr($class); ?>">
                        <aside class="widget-area sticky-widget-area">
                            <?php dynamic_sidebar('home-page-sidebar'); ?>
                        </aside>
                    </div>
                <?php
                elseif (is_active_sidebar('sidebar-1')) :
                    ?>
                    <div id="secondary" class="sidebar-area <?php echo esc_attr($class); ?>">
                        <aside class="widget-area sticky-widget-area">
                            <?php dynamic_sidebar('sidebar-1'); ?>
                        </aside>
                    </div>
                <?php endif; ?>

            <?php elseif (is_active_sidebar('sidebar-1')) :
                ?>
                <div id="secondary" class="sidebar-area <?php echo esc_attr($class); ?>">
                    <aside class="widget-area sticky-widget-area">
                        <?php dynamic_sidebar('sidebar-1'); ?>
                    </aside>
                </div>
            <?php

            endif;
        else :
            $hide_sidebar_mobile = united_blog_get_option('hide_global_sidebar_mobile');
            $class .= $hide_sidebar_mobile ? ' hide-on-mobile ' : '';
            if (is_active_sidebar('sidebar-1')) :
                ?>
                <div id="secondary" class="sidebar-area <?php echo esc_attr($class); ?>">
                    <aside class="widget-area sticky-widget-area">
                        <?php dynamic_sidebar('sidebar-1'); ?>
                    </aside>
                </div>
            <?php
            endif;

        endif;
    }
endif;


if (!function_exists('united_blog_social_share')):

    /**
     * Social Share
     **/

    function united_blog_social_share()
    {

        $enable_social_share = united_blog_get_option('enable_social_share');
        if (!$enable_social_share) {
            return;
        }
        $enable_facebook = united_blog_get_option('enable_facebook');
        $enable_twitter = united_blog_get_option('enable_twitter');
        $enable_pinterest = united_blog_get_option('enable_pinterest');
        $enable_linkedin = united_blog_get_option('enable_linkedin');
        $enable_telegram = united_blog_get_option('enable_telegram');
        $enable_reddit = united_blog_get_option('enable_reddit');
        $enable_whatsapp = united_blog_get_option('enable_whatsapp');
        $enable_email = united_blog_get_option('enable_email');

        if ($enable_facebook || $enable_twitter || $enable_pinterest || $enable_linkedin || $enable_email || $enable_telegram || $enable_reddit || $enable_whatsapp) {

            $permalink = urlencode(get_the_permalink());
            $post_title = html_entity_decode(get_the_title(), ENT_COMPAT, 'UTF-8');
            $media_url = urlencode(get_the_post_thumbnail_url(get_the_ID(), 'full')); ?>
            <div class="united-social-share single-social-share">
                <button type="button" class="social-share-title" aria-expanded="false" aria-controls="social-share-content">
                    <?php echo esc_html__('Share', 'united-blog') ?>
                </button>
                <div class="social-share-content">
                    <?php if ($enable_facebook) { ?>
                        <a href="https://www.facebook.com/sharer/sharer.php?u=<?php echo esc_url($permalink); ?>"
                           target="popup" class="social-share-hyperlink facebook-share-hyperlink"
                           onclick="window.open(this.href,'<?php echo esc_html__('Facebook', 'united-blog'); ?>','width=600,height=400')">
                            <?php united_blog_theme_svg_social('facebook'); ?>
                            <?php echo esc_html__('Share on Facebook', 'united-blog'); ?>
                        </a>
                    <?php } ?>

                    <?php if ($enable_twitter) {

                        $twitter_id = united_blog_get_option('twitter_id'); ?>
                        <a href="https://twitter.com/intent/tweet?text=<?php echo esc_html($post_title); ?>&amp;url=<?php echo esc_url($permalink); ?>&amp;via=<?php echo esc_html($twitter_id); ?>"
                           target="popup" class="social-share-hyperlink twitter-share-hyperlink"
                           onclick="window.open(this.href,'<?php echo esc_html__('Twitter', 'united-blog'); ?>','width=600,height=400')">
                            <?php united_blog_theme_svg_social('twitter'); ?>
                            <?php echo esc_html__('Share on X', 'united-blog'); ?>
                        </a>
                    <?php } ?>

                    <?php if ($enable_pinterest) { ?>
                        <a href="http://pinterest.com/pin/create/button/?url=<?php echo esc_url($permalink); ?>&amp;media=<?php echo $media_url; ?>&amp;description=<?php echo esc_html($post_title); ?>"
                           target="popup" class="social-share-hyperlink pinterest-share-hyperlink"
                           onclick="window.open(this.href,'<?php echo esc_html__('Pinterest', 'united-blog'); ?>','width=600,height=400')">
                            <?php united_blog_theme_svg_social('pinterest'); ?>
                            <?php echo esc_html__('Pin it on Pinterest', 'united-blog'); ?>
                        </a>
                    <?php } ?>

                    <?php if ($enable_linkedin) { ?>
                        <a href="https://www.linkedin.com/shareArticle?mini=true&url=<?php echo esc_url($permalink); ?>&title=<?php echo esc_html($post_title); ?>"
                           target="popup" class="social-share-hyperlink linkedin-share-hyperlink"
                           onclick="window.open(this.href,'<?php echo esc_html__('LinkedIn', 'united-blog'); ?>','width=600,height=400')">
                            <?php united_blog_theme_svg_social('linkedin'); ?>
                            <?php echo esc_html__('Share on LinkedIn', 'united-blog'); ?>
                        </a>
                    <?php } ?>

                    <?php if ($enable_telegram) { ?>
                        <a href="https://telegram.me/share/url?url=<?php echo esc_attr($permalink); ?>&text=<?php echo esc_html($post_title); ?>"
                           target="popup" class="social-share-hyperlink telegram-share-hyperlink"
                           onclick="window.open(this.href,'<?php echo esc_html__('Telegram', 'united-blog'); ?>','width=600,height=400')">
                            <?php united_blog_theme_svg_social('telegram'); ?>
                            <?php echo esc_html__('Share via Telegram', 'united-blog'); ?>
                        </a>
                    <?php } ?>


                    <?php if ($enable_reddit) { ?>
                        <a href="https://reddit.com/submit?url=<?php echo esc_attr($permalink); ?>&title=<?php echo esc_html($post_title); ?>"
                           target="popup" class="social-share-hyperlink reddit-share-hyperlink"
                           onclick="window.open(this.href,'<?php echo esc_html__('Reddit', 'united-blog'); ?>','width=600,height=400')">
                            <?php united_blog_theme_svg_social('reddit'); ?>
                            <?php echo esc_html__('Post on Reddit', 'united-blog'); ?>
                        </a>
                    <?php } ?>





                    <?php if ($enable_whatsapp) { ?>
                        <a href="https://api.whatsapp.com/send?text=<?php echo esc_attr($permalink); ?>" target="popup"
                           class="social-share-hyperlink whatsapp-share-hyperlink"
                           onclick="window.open(this.href,'<?php echo esc_html__('Whatsapp', 'united-blog'); ?>','width=600,height=400')">
                            <?php united_blog_theme_svg_social('whatsapp'); ?>
                            <?php echo esc_html__('Send via WhatsApp', 'united-blog'); ?>
                        </a>
                    <?php } ?>


                    <?php if ($enable_email) { ?>
                        <a href="mailto:?subject=<?php echo esc_html($post_title); ?>&body=<?php echo esc_html($post_title) . " " . esc_url($permalink); ?>"
                           target="_blank" class="social-share-hyperlink email-share-hyperlink">
                           <?php united_blog_theme_svg_social('mail'); ?>
                           <?php echo esc_html__('Share by Email', 'united-blog'); ?>
                        </a>
                    <?php } ?>
                </div>
            </div>
            <?php
        }

    }

endif;
