<?php
/**
 * CRT Builder With Elementor functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package CRT_Builder_With_Elementor
 */

if ( ! defined( 'CRT_BUILDER_WITH_ELEMENTOR_VERSION' ) ) {
	define( 'CRT_BUILDER_WITH_ELEMENTOR_VERSION', wp_get_theme()->get( 'Version' ) );
}
if ( ! defined( 'CRT_BUILDER_WITH_ELEMENTOR_NAME' ) ) {
    define( 'CRT_BUILDER_WITH_ELEMENTOR_NAME', wp_get_theme()->get( 'Name' ) );
}
if ( ! defined( 'CRT_BUILDER_WITH_ELEMENTOR_URL_DEMO' ) ) {
    define( 'CRT_BUILDER_WITH_ELEMENTOR_URL_DEMO', wp_get_theme()->get( 'ThemeURI' ) );
}

if ( ! function_exists( 'crt_builder_with_elementor_setup' ) ) :
    function crt_builder_with_elementor_setup() {

        // Add default posts and comments RSS feed links to head.
        add_theme_support( 'automatic-feed-links' );

        add_theme_support( 'title-tag' );

        add_theme_support( 'wp-block-styles' );

        add_theme_support( 'register_block_style' );

        add_theme_support( 'register_block_pattern' );

        add_theme_support( 'post-thumbnails' );

        add_post_type_support( 'attachment', 'post-formats' );

        add_theme_support( 'post-formats', array( 'video', 'gallery', 'audio', 'link' ) );

        // Add theme support for selective refresh for widgets.
        add_theme_support( 'customize-selective-refresh-widgets' );

        /**
         * Add support for core custom logo.
         *
         * @link https://codex.wordpress.org/Theme_Logo
         */
        add_theme_support(
            'custom-logo',
            array(
                'height'      => 250,
                'width'       => 250,
                'flex-width'  => true,
                'flex-height' => true,
            )
        );

        add_theme_support( 'align-wide' );
        add_theme_support( 'responsive-embeds' );

        add_theme_support( 'html5', array(
            'comment-list',
            'comment-form',
            'search-form',
            'gallery',
            'caption',
        ) );

        // This theme uses wp_nav_menu() in one location.
        register_nav_menus(
            array(
                'primary' => esc_html__( 'Primary','crt-builder-with-elementor' ),
            )
        );
        register_nav_menus(
            array(
                'footer' => esc_html__( 'Footer','crt-builder-with-elementor' ),
            )
        );
    }
endif;
add_action( 'after_setup_theme', 'crt_builder_with_elementor_setup' );

add_image_size( 'crt-builder-with-elementor-image-small', 300, 9999 );
add_image_size( 'crt-builder-with-elementor-image-medium', 600, 9999 );
add_image_size( 'crt-builder-with-elementor-image-large', 1200, 9999 );

if ( ! function_exists( 'crt_builder_with_elementor_after_active' ) ) :
    function crt_builder_with_elementor_after_active() {
        $crt_builder_with_elementor_theme_active = get_option('stylesheet');

    }
endif;
add_action('after_switch_theme', 'crt_builder_with_elementor_after_active');


if ( ! function_exists( 'crt_builder_with_elementor_header_style' ) ) :
    /**
     * Styles the header image and text displayed on the blog.
     *
     * @see crt_builder_with_elementor_header_style().
     */
    function crt_builder_with_elementor_header_style() {
        $crt_builder_with_elementor_header_text_color = get_header_textcolor();

        /*
         * If no custom options for text are set, let's bail.
         * get_header_textcolor() options: Any hex value, 'blank' to hide text. Default: add_theme_support( 'custom-header' ).
         */
        if ( get_theme_support( 'custom-header', 'default-text-color' ) === $crt_builder_with_elementor_header_text_color ) {
            return;
        }

        // If we get this far, we have custom styles. Let's do this.
        ?>
        <style type="text/css">
            <?php
            // Has the text been hidden?
            if ( ! display_header_text() ) :
                ?>
            .site-title,
            .site-description {
                position: absolute;
                clip: rect(1px, 1px, 1px, 1px);
                color: red !important;
            }
            <?php
            // If the user has set a custom color for the text use that.
        else :
            ?>
            .site-title a,
            .site-description {
                color: #<?php echo esc_attr( $crt_builder_with_elementor_header_text_color ); ?>;
            }
            <?php endif; ?>
        </style>
        <?php
    }
endif;

/**
 * Set the content width in pixels, based on the theme's design and stylesheet.
 *
 * Priority 0 to make it available to lower priority callbacks.
 *
 * @global int $content_width
 */
function crt_builder_with_elementor_content_width() {
    $GLOBALS['content_width'] = apply_filters( 'nmb_content_width', 640 );
}
add_action( 'after_setup_theme', 'crt_builder_with_elementor_content_width', 0 );


/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 */

function crt_builder_with_elementor_widget_registration($name, $id, $description,$crt_builder_with_elementor_beforeWidget, $crt_builder_with_elementor_afterWidget, $crt_builder_with_elementor_beforeTitle, $crt_builder_with_elementor_afterTitle){
    register_sidebar( array(
        'name' => $name,
        'id' => $id,
        'description' => $description,
        'before_widget' => $crt_builder_with_elementor_beforeWidget,
        'after_widget' => $crt_builder_with_elementor_afterWidget,
        'before_title' => $crt_builder_with_elementor_beforeTitle,
        'after_title' => $crt_builder_with_elementor_afterTitle,
    ));
}

function crt_builder_with_elementor_widgets_init() {
    crt_builder_with_elementor_widget_registration(esc_html__('Sidebar', 'crt-builder-with-elementor'), 'sidebar-1', esc_html__('Add widgets here.', 'crt-builder-with-elementor'), '<section id="%1$s" class="widget %2$s">', '</section>', '<h2 class="widget-title"><span>', '</span></h2>');
    crt_builder_with_elementor_widget_registration(esc_html__('Footer', 'crt-builder-with-elementor'), 'footer-1', esc_html__('Add widgets here.', 'crt-builder-with-elementor'), '<div id="%1$s" class="footer %2$s">', '</div>', '<h2 class="widget-title"><span>', '</span></h2>');
}
add_action( 'widgets_init', 'crt_builder_with_elementor_widgets_init' );

/**
 * Count Widget Footer Active
 */
function crt_builder_with_elementor_footer_is_widget() {
    $crt_builder_with_elementor_widget_active = array();
    for($i = 1; $i < 5;$i++) {
        if(is_active_sidebar( 'footer-'.$i )) {
            $crt_builder_with_elementor_widget_active[$i] = $i;
        }
    }
    return $crt_builder_with_elementor_widget_active;
}

/**
 * Enqueue scripts and styles.
 */
function crt_builder_with_elementor_scripts() {

    wp_enqueue_style( 'crt-builder-with-elementor-style', get_template_directory_uri() . '/style.css', array(), CRT_BUILDER_WITH_ELEMENTOR_VERSION );
    // Main style.
    wp_enqueue_style( 'crt-builder-with-elementor-main-style', get_template_directory_uri() . '/assets/build/css/main.min.css', array(), CRT_BUILDER_WITH_ELEMENTOR_VERSION );

    // Main script.
    wp_enqueue_script( 'jquery-lazy', get_template_directory_uri() . '/assets/js/jquery.lazy.js', array(  ), CRT_BUILDER_WITH_ELEMENTOR_VERSION, true  );

    wp_enqueue_script( 'crt-builder-with-elementor-main-script', get_template_directory_uri() . '/assets/js/main.js', array( 'jquery' ), CRT_BUILDER_WITH_ELEMENTOR_VERSION, false );

    if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
        wp_enqueue_script( 'comment-reply' );
    }
}
add_action( 'wp_enqueue_scripts', 'crt_builder_with_elementor_scripts' );

/**
 * Custom template tags for this theme.
 */
require get_template_directory() . '/inc/template-tags.php';

/**
 * Functions which enhance the theme by hooking into WordPress.
 */
require get_template_directory() . '/inc/template-functions.php';

/**
 * Dynamic CSS
 */
require get_template_directory() . '/inc/dynamic-css.php';

/**
 * Breadcrumb
 */
require get_template_directory() . '/inc/class-breadcrumb-trail.php';
/**
 * Quick Setup
 */
/**
 * Load Jetpack compatibility file.
 */
if ( defined( 'JETPACK__VERSION' ) ) {
    require get_template_directory() . '/inc/jetpack.php';
}

if ( ! function_exists( 'crt_builder_with_elementor_is_woocommerce' ) ) {
    function crt_builder_with_elementor_is_woocommerce() {
        if(class_exists( 'WooCommerce' )) {
            return true;
        }
        return false;
    }
}
/**
 * CRT Manage is active.
 */
if ( ! function_exists( 'crt_builder_with_elementor_crt_is_active' ) ) {
    function crt_builder_with_elementor_crt_is_active() {
        if(class_exists( 'CRT_Manage_Base' )) {
            return true;
        }
        return false;
    }
}
/**
 * Custom Heading Archive.
 */
add_filter('get_the_archive_title', function ($title) {
    $title  = __( 'Archives','crt-builder-with-elementor' );

    if ( is_category() ) {
        $title  = single_cat_title( '', false );
    } elseif ( is_tag() ) {
        $title  = single_tag_title( '', false );
    } elseif ( is_author() ) {
        $title  = get_the_author();
    } elseif ( is_year() ) {
        /* translators: See https://www.php.net/manual/datetime.format.php */
        $title  = get_the_date( _x( 'Y', 'yearly archives date format','crt-builder-with-elementor' ) );
    } elseif ( is_month() ) {
        /* translators: See https://www.php.net/manual/datetime.format.php */
        $title  = get_the_date( _x( 'F Y', 'monthly archives date format','crt-builder-with-elementor' ) );
    } elseif ( is_day() ) {
        /* translators: See https://www.php.net/manual/datetime.format.php */
        $title  = get_the_date( _x( 'F j, Y', 'daily archives date format','crt-builder-with-elementor' ) );
    } elseif ( is_tax( 'post_format' ) ) {
        if ( is_tax( 'post_format', 'post-format-aside' ) ) {
            $title = _x( 'Asides', 'post format archive title','crt-builder-with-elementor' );
        } elseif ( is_tax( 'post_format', 'post-format-gallery' ) ) {
            $title = _x( 'Galleries', 'post format archive title','crt-builder-with-elementor' );
        } elseif ( is_tax( 'post_format', 'post-format-image' ) ) {
            $title = _x( 'Images', 'post format archive title','crt-builder-with-elementor' );
        } elseif ( is_tax( 'post_format', 'post-format-video' ) ) {
            $title = _x( 'Videos', 'post format archive title','crt-builder-with-elementor' );
        } elseif ( is_tax( 'post_format', 'post-format-quote' ) ) {
            $title = _x( 'Quotes', 'post format archive title','crt-builder-with-elementor' );
        } elseif ( is_tax( 'post_format', 'post-format-link' ) ) {
            $title = _x( 'Links', 'post format archive title','crt-builder-with-elementor' );
        } elseif ( is_tax( 'post_format', 'post-format-status' ) ) {
            $title = _x( 'Statuses', 'post format archive title','crt-builder-with-elementor' );
        } elseif ( is_tax( 'post_format', 'post-format-audio' ) ) {
            $title = _x( 'Audio', 'post format archive title','crt-builder-with-elementor' );
        } elseif ( is_tax( 'post_format', 'post-format-chat' ) ) {
            $title = _x( 'Chats', 'post format archive title','crt-builder-with-elementor' );
        }
    } elseif ( is_post_type_archive() ) {
        $title  = post_type_archive_title( '', false );
    } elseif ( is_tax() ) {
        $queried_object = get_queried_object();
        if ( $queried_object ) {
            $tax    = get_taxonomy( $queried_object->taxonomy );
            $title  = single_term_title( '', false );
        }
    }  elseif (is_search()) {
        $value = get_search_query();
        $title = sprintf( esc_html__( 'Search: %s','crt-builder-with-elementor' ), $value);
    } elseif (crt_builder_with_elementor_is_woocommerce()) {
        if (is_cart()) {
            $title = _x( 'Cart', 'post format archive title','crt-builder-with-elementor' );
        } elseif (is_checkout()) {
            $title = _x( 'Checkout', 'post format archive title','crt-builder-with-elementor' );
        }
    }
    return $title;
});