<?php

/**
 * @Packge     : Daltech
 * @Version    : 1.0
 * @Author     : Daltech
 * @Author URI : https://themeforest.net/user/ModinaTheme/portfolio
 *
 */


// Block direct access
if (!defined('ABSPATH')) {
    exit;
}

// theme option callback
function daltech_opt($id = null, $url = null)
{
    global $daltech_opt;

    if ($id && $url) {

        if (isset($daltech_opt[$id][$url]) && $daltech_opt[$id][$url]) {
            return $daltech_opt[$id][$url];
        }
    } else {
        if (isset($daltech_opt[$id])  && $daltech_opt[$id]) {
            return $daltech_opt[$id];
        }
    }
}


// theme logo withour moble device

function daltech_theme_logo()
{
    // escaping allow html
    $allowhtml = array(
        'a'    => array(
            'href' => array()
        ),
        'span' => array(),
        'i'    => array(
            'class' => array()
        )
    );
    $siteUrl = home_url('/');
    if (has_custom_logo()) {
        $custom_logo_id = get_theme_mod('custom_logo');
        $siteLogo = '';
        $siteLogo .= '<a class="navbar-brand" href="' . esc_url($siteUrl) . '">';
        $siteLogo .= daltech_img_tag(array(
            "class" => "logo",
            "url"   => esc_url(wp_get_attachment_image_url($custom_logo_id, ''))
        ));
        $siteLogo .= '</a>';

        return $siteLogo;
    } elseif (!daltech_opt('daltech_text_title') && daltech_opt('daltech_site_logo', 'url')) {
        $siteLogo = '<img class="logo" src="' . esc_url(daltech_opt('daltech_site_logo', 'url')) . '" alt="' . esc_attr(get_bloginfo('name')) . '" />';
        return '<a class="navbar-brand" href="' . esc_url($siteUrl) . '">' . $siteLogo . '</a>';
    } elseif (daltech_opt('daltech_text_title')) {
        return '<h2 class="logo-text"><a class="logo" href="' . esc_url($siteUrl) . '">' . wp_kses(daltech_opt('daltech_text_title'), $allowhtml) . '</a></h2>';
    } else {
        return '<h2 class="logo-text"><a class="logo" href="' . esc_url($siteUrl) . '">' . esc_html(get_bloginfo('name')) . '</a></h2>';
    }
}

// theme logo for mobile device
function daltech_mobile_theme_logo()
{
    // escaping allow html
    $allowhtml = array(
        'a'    => array(
            'href' => array()
        ),
        'span' => array(),
        'i'    => array(
            'class' => array()
        )
    );
    $siteUrl = home_url('/');
    if (has_custom_logo()) {
        $custom_logo_id = get_theme_mod('custom_logo');
        $siteLogo = '';
        $siteLogo .= daltech_img_tag(array(
            "url"   => esc_url(wp_get_attachment_image_url($custom_logo_id, 'full'))
        ));

        return $siteLogo;
    } elseif (!daltech_opt('daltech_text_title') && daltech_opt('daltech_site_logo', 'url')) {

        $siteLogo = '<img class="logo" src="' . esc_url(daltech_opt('daltech_site_logo', 'url')) . '" alt="' . esc_attr(get_bloginfo('name')) . '" />';
        return '<a class="mob-logo" href="' . esc_url($siteUrl) . '">' . $siteLogo . '</a>';
    } elseif (daltech_opt('daltech_text_title')) {
        return '<h2 class="logo-text"><a class="logo" href="' . esc_url($siteUrl) . '">' . wp_kses(daltech_opt('daltech_text_title'), $allowhtml) . '</a></h2>';
    } else {
        return '<h2 class="logo-text"><a class="logo" href="' . esc_url($siteUrl) . '">' . esc_html(get_bloginfo('name')) . '</a></h2>';
    }
}
// custom meta id callback
function daltech_meta($id = '')
{
    $value = get_post_meta(get_the_ID(), '_daltech_' . $id, true);
    return $value;
}


// Blog Date Permalink
function daltech_blog_date_permalink()
{
    $year  = get_the_time('Y');
    $month_link = get_the_time('m');
    $day   = get_the_time('d');
    $link = get_day_link($year, $month_link, $day);
    return $link;
}

//audio format iframe match
function daltech_iframe_match()
{
    $audio_content = daltech_embedded_media(array('audio', 'iframe'));
    $iframe_match = preg_match("/\iframe\b/i", $audio_content, $match);
    return $iframe_match;
}


//Post embedded media
function daltech_embedded_media($type = array())
{
    $content = do_shortcode(apply_filters('the_content', get_the_content()));
    $embed   = get_media_embedded_in_content($content, $type);


    if (in_array('audio', $type)) {
        if (count($embed) > 0) {
            $output = str_replace('?visual=true', '?visual=false', $embed[0]);
        } else {
            $output = '';
        }
    } else {
        if (count($embed) > 0) {
            $output = $embed[0];
        } else {
            $output = '';
        }
    }
    return $output;
}


// WP post link pages
function daltech_link_pages()
{
    wp_link_pages(array(
        'before'      => '<div class="page-links"><span class="page-links-title">' . esc_html__('Pages:', 'daltech') . '</span>',
        'after'       => '</div>',
        'link_before' => '<span>',
        'link_after'  => '</span>',
        'pagelink'    => '<span class="screen-reader-text">' . esc_html__('Page', 'daltech') . ' </span>%',
        'separator'   => '<span class="screen-reader-text">, </span>',
    ));
}

// image alt tag
function daltech_image_alt($url = '')
{
    if ($url != '') {
        // attachment id by url
        $attachmentid = attachment_url_to_postid(esc_url($url));
        // attachment alt tag
        $image_alt = get_post_meta(esc_html($attachmentid), '_wp_attachment_image_alt', true);
        if ($image_alt) {
            return $image_alt;
        } else {
            $filename = pathinfo(esc_url($url));
            $alt = str_replace('-', ' ', $filename['filename']);
            return $alt;
        }
    } else {
        return;
    }
}


// Flat Content wysiwyg output with meta key and post id

function daltech_get_textareahtml_output($content)
{
    global $wp_embed;

    $content = $wp_embed->autoembed($content);
    $content = $wp_embed->run_shortcode($content);
    $content = wpautop($content);
    $content = do_shortcode($content);

    return $content;
}

/**
 * Add a pingback url auto-discovery header for single posts, pages, or attachments.
 */

function daltech_pingback_header()
{
    if (is_singular() && pings_open()) {
        echo '<link rel="pingback" href="', esc_url(get_bloginfo('pingback_url')), '">';
    }
}
add_action('wp_head', 'daltech_pingback_header');


// Excerpt More
function daltech_excerpt_more($more)
{
    return '...';
}

add_filter('excerpt_more', 'daltech_excerpt_more');


// daltech comment template callback
function daltech_comment_callback($comment, $args, $depth)
{

    $GLOBALS['comment'] = $comment; ?>

    <div class="single-comment-item" id="comment-<?php comment_ID(); ?>">
        <?php if ($avarta = get_avatar($comment)) :
            printf('<div class="author-img">%1$s</div>', $avarta);
        endif; ?>
        <div class='author-info-comment'>
            <div class="info">
                <?php
                if ($comment->user_id != '0') {
                    printf('<h5>%1$s</h5>', get_user_meta($comment->user_id, 'nickname', true));
                } else {
                    printf('<h5>%1$s</h5>', get_comment_author_link());
                }
                ?>
                <span><?php echo get_comment_date('j M Y'); ?></span>
                <span><?php $edit_reply_text = esc_html__('Edit', 'daltech');
                        edit_comment_link('<i class="fal fa-pencil"></i>' . $edit_reply_text, '  ', ''); ?></span>
                <?php comment_reply_link(array_merge($args, array('depth' => $depth, 'max_depth' => $args['max_depth'], 'reply_text' => '<i class="fal fa-reply"></i>Reply'))) ?>
            </div>

            <?php comment_text() ?>
            <div class='comments-info'>
                <?php if ($comment->comment_approved == '0') : ?>
                    <span class="unapproved"><?php esc_html_e('Your comment is awaiting moderation.', 'daltech'); ?></span>
                <?php endif; ?>

            </div>

        </div>
    </div>
<?php
}

//body class
add_filter('body_class', 'daltech_body_class');
function daltech_body_class($classes)
{
    if (class_exists('ReduxFramework')) {
        $daltech_blog_single_sidebar = daltech_opt('daltech_blog_single_sidebar');


        if (is_active_sidebar('daltech-blog-sidebar')) {
            if ($daltech_blog_single_sidebar == '2') {
                $classes[] = 'left-sidebar';
            } elseif ($daltech_blog_single_sidebar == '3') {
                $classes[] = 'right-sidebar';
            } else {
                $classes[] = 'blog-standard';
            }
        } else {
            $classes[] = 'blog-standard';
        }

        $new_class = is_page() ? daltech_meta('custom_body_class') : null;

        if ($new_class) {
            $classes[] = $new_class;
        }
    } else {
        if (!is_active_sidebar('daltech-blog-sidebar')) {
            $classes[] = 'blog-standard';
        } else {
            $classes[] = 'right-sidebar';
        }
    }
    return $classes;
}


function daltech_footer_global_option()
{

    // Daltech Footer Bottom Enable Disable
    if (class_exists('ReduxFramework')) {
        $daltech_footer_bottom_active = daltech_opt('daltech_disable_footer_bottom');
        $daltech_footerwidget_enable = daltech_opt('daltech_footerwidget_enable');
    } else {
        $daltech_footer_bottom_active = '1';
        $daltech_footerwidget_enable = '1';
    }

    $allowhtml = array(
        'p'         => array(
            'class'     => array()
        ),
        'span'      => array(
            'class'     => array(),
        ),
        'a'         => array(
            'href'      => array(),
            'title'     => array()
        ),
        'br'        => array(),
        'em'        => array(),
        'strong'    => array(),
        'b'         => array(),
    );
    echo '<footer class="bg-dark text-light footer-custom-style">';
    echo '<div class="container">';
    if ($daltech_footerwidget_enable == '1') {
        if ((is_active_sidebar('daltech-footer-1') || is_active_sidebar('daltech-footer-2') || is_active_sidebar('daltech-footer-3') || is_active_sidebar('daltech-footer-4'))) {

            echo '<div class="f-items default-padding-bottom pt-70 pt-xs-0">';
            echo '<div class="row">';
            if (is_active_sidebar('daltech-footer-1')) {
                dynamic_sidebar('daltech-footer-1');
            }
            if (is_active_sidebar('daltech-footer-2')) {
                dynamic_sidebar('daltech-footer-2');
            }
            if (is_active_sidebar('daltech-footer-3')) {
                dynamic_sidebar('daltech-footer-3');
            }
            if (is_active_sidebar('daltech-footer-4')) {
                dynamic_sidebar('daltech-footer-4');
            }
            echo '</div>';
            echo '</div>';
        }
    }
    echo '</div>';
    if ($daltech_footer_bottom_active == '1') {
        if (!empty(daltech_opt('daltech_copyright_text'))) {
            echo '<!-- Start Footer Bottom -->';
            echo '<div class="footer-bottom bg-dark text-light text-center">';
            echo '<div class="container">';
            echo '<div class="row">';
            echo '<div class="col-lg-12">';
            echo '<p>' . wp_kses(daltech_opt('daltech_copyright_text'), $allowhtml) . '</p>';
            echo '</div>';
            echo '</div>';
            echo '</div>';
            echo '</div>';
        }
    }
    echo '</footer>';
}

function daltech_social_icon()
{
    $daltech_social_icon = daltech_opt('daltech_social_links');
    if (!empty($daltech_social_icon) && isset($daltech_social_icon)) {
        foreach ($daltech_social_icon as $social_icon) {
            if (!empty($social_icon['title'])) {
                echo '<a href="' . esc_url($social_icon['url']) . '"><i class="' . esc_attr($social_icon['title']) . '"></i>' . esc_html($social_icon['description']) . '</a>';
            }
        }
    }
}

// global header
function daltech_global_header_option()
{
?>
    <header class="header-section-1 default-header">
        <div id="header-sticky" class="header-1">
            <div class="container">
                <div class="mega-menu-wrapper">
                    <div class="header-main">
                        <div class="logo">
                            <?php echo daltech_theme_logo(); ?>
                        </div>
                        <div class="mean__menu-wrapper">
                            <div class="main-menu">
                                <nav id="mobile-menu">
                                    <?php
                                    if (has_nav_menu('primary-menu')) {
                                        wp_nav_menu(array(
                                            'theme_location'  => 'primary-menu',
                                            'container'       => 'ul',
                                            'fallback_cb'     => 'Daltech_Bootstrap_Navwalker::fallback',
                                            'walker'          => new Daltech_Bootstrap_Navwalker(),
                                        ));
                                    }
                                    ?>
                                </nav>
                            </div>
                        </div>
                        <div class="header__hamburger d-lg-none my-auto">
                            <div class="sidebar__toggle">
                                <i class="far fa-bars"></i>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </header>

    <div class="fix-area">
        <div class="offcanvas__info">
            <div class="offcanvas__wrapper">
                <div class="offcanvas__content">
                    <div class="offcanvas__top mb-5 d-flex justify-content-between align-items-center">
                        <div class="offcanvas__logo">
                            <?php echo daltech_theme_logo(); ?>
                        </div>
                        <div class="offcanvas__close">
                            <button>
                                <i class="fas fa-times"></i>
                            </button>
                        </div>
                    </div>
                    <div class="mobile-menu fix mb-3"></div>
                </div>
            </div>
        </div>
    </div>
    <div class="offcanvas__overlay"></div>
<?php
}

//Fire the wp_body_open action.
if (!function_exists('wp_body_open')) {
    function wp_body_open()
    {
        do_action('wp_body_open');
    }
}

//Remove Tag-Clouds inline style
add_filter('wp_generate_tag_cloud', 'daltech_remove_tagcloud_inline_style', 10, 1);
function daltech_remove_tagcloud_inline_style($input)
{
    return preg_replace('/ style=("|\')(.*?)("|\')/', '', $input);
}

function daltech_setPostViews($postID)
{
    $count_key  = 'post_views_count';
    $count      = get_post_meta($postID, $count_key, true);
    if ($count == '') {
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    } else {
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}

function daltech_getPostViews($postID)
{
    $count_key  = 'post_views_count';
    $count      = get_post_meta($postID, $count_key, true);
    if ($count == '') {
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return __('0', 'daltech');
    }
    return $count;
}

// password protected form
add_filter('the_password_form', 'daltech_password_form', 10, 1);
function daltech_password_form($output)
{
    $output = '<form action="' . esc_url(site_url('wp-login.php?action=postpass', 'login_post')) . '" class="post-password-form" method="post"><div class="theme-input-group">
        <input name="post_password" type="password" class="theme-input-style" placeholder="' . esc_attr__('Enter Password', 'daltech') . '">
        <button type="submit" class="submit-btn btn-fill">' . esc_html__('Enter', 'daltech') . '</button></div></form>';
    return $output;
}

/* This code filters the Categories archive widget to include the post count inside the link */
add_filter('wp_list_categories', 'daltech_cat_count_span');
function daltech_cat_count_span($links)
{
    $links = str_replace('</a> (', '</a> <span>', $links);
    $links = str_replace(')', '</span>', $links);
    return $links;
}

/* This code filters the Archive widget to include the post count inside the link */
add_filter('get_archives_link', 'daltech_archive_count_span');
function daltech_archive_count_span($links)
{
    $links = str_replace('</a>&nbsp;(', '</a> <span class="category-number">(', $links);
    $links = str_replace(')', ')</span>', $links);
    return $links;
}


if (!function_exists('daltech_blog_category')) {
    function daltech_blog_category()
    {
        if (class_exists('ReduxFramework')) {
            $daltech_display_post_category =  daltech_opt('daltech_display_post_category');
        } else {
            $daltech_display_post_category = '1';
        }

        if ($daltech_display_post_category) {
            $daltech_post_categories = get_the_category();
            if (is_array($daltech_post_categories) && !empty($daltech_post_categories)) {
                if (daltech_opt('daltech_blog_style') == '2') {
                    $padding_class = 'mb-20';
                } else {
                    $padding_class = '';
                }
                echo '<div class="blog-category ' . esc_attr($padding_class) . '">';
                echo ' <a href="' . esc_url(get_term_link($daltech_post_categories[0]->term_id)) . '">' . esc_html($daltech_post_categories[0]->name) . '</a> ';
                echo '</div>';
            }
        }
    }
}

// Add Extra Class On Comment Reply Button
function daltech_custom_comment_reply_link($content)
{
    $extra_classes = 'replay-btn';
    return preg_replace('/comment-reply-link/', 'comment-reply-link ' . $extra_classes, $content);
}

add_filter('comment_reply_link', 'daltech_custom_comment_reply_link', 99);

// Add Extra Class On Edit Comment Link
function daltech_custom_edit_comment_link($content)
{
    $extra_classes = 'replay-btn';
    return preg_replace('/comment-edit-link/', 'comment-edit-link ' . $extra_classes, $content);
}

add_filter('edit_comment_link', 'daltech_custom_edit_comment_link', 99);


function daltech_post_classes($classes, $class, $post_id)
{
    if (get_post_type() === 'post') {
        if (!is_single()) {
            if (daltech_opt('daltech_blog_style') == '3') {
                $classes[] = "item grid-wide";
            } else {
                $classes[] = "item-inner";
            }
        } else {
            $classes[] = "item";
        }
    } elseif (get_post_type() === 'page') {
        $classes[] = "page--item";
    }

    return $classes;
}
add_filter('post_class', 'daltech_post_classes', 10, 3);

function daltech_kses_allowed_html($tags, $context)
{
    switch ($context) {
        case 'daltech_allowed_tags':
            $tags = array(
                'a' => array('href' => array(), 'class' => array()),
                'b' => array(),
                'br' => array(),
                'span' => array('class' => array(), 'data-count' => array()),
                'del' => array('class' => array(), 'data-count' => array()),
                'ins' => array('class' => array(), 'data-count' => array()),
                'bdi' => array('class' => array(), 'data-count' => array()),
                'img' => array('class' => array()),
                'i' => array('class' => array()),
                'p' => array('class' => array()),
                'ul' => array('class' => array()),
                'li' => array('class' => array()),
                'div' => array('class' => array()),
                'strong' => array(),
                'sup' => array(),
                'sub' => array(),
                'h5' => array(),
            );
            return $tags;
        default:
            return $tags;
    }
}

add_filter('wp_kses_allowed_html', 'daltech_kses_allowed_html', 10, 2);

if (!function_exists('daltech_get_nav_menu')) :
    function daltech_get_nav_menu()
    {
        $menu_list = get_terms(array(
            'taxonomy' => 'nav_menu',
            'hide_empty' => true,
        ));
        $options = [];
        if (!empty($menu_list) && !is_wp_error($menu_list)) {
            foreach ($menu_list as $menu) {
                $options[$menu->slug] = $menu->name;
            }
            return $options;
        }
    }
endif;

if (!function_exists('daltech_comment_count')) {
    function daltech_comment_count()
    {
        if (!post_password_required() && (comments_open() || get_comments_number())) {
            echo '<span class="comments-link"><i class="fal fa-comments"></i> ';
            comments_popup_link(
                sprintf(
                    wp_kses(
                        /* translators: %s: post title */
                        esc_html__('Leave a Comment', 'daltech') . '<span class="screen-reader-text">' . esc_html__('on', 'daltech') . ' %s</span>',
                        array(
                            'span' => array(
                                'class' => array(),
                            ),
                        )
                    ),
                    wp_kses_post(get_the_title())
                )
            );
            echo '</span>';
        }
    }
}
