<?php

/**
 * chic-lifestyle functions and definitions
 *
 * @package Chic Lifestyle
 */
// freemius integration start
if ( !function_exists( 'fs_chic_lifestyle' ) ) {
    // Create a helper function for easy SDK access.
    function fs_chic_lifestyle() {
        global $fs_chic_lifestyle;
        if ( !isset( $fs_chic_lifestyle ) ) {
            // Include Freemius SDK.
            require_once dirname( __FILE__ ) . '/freemius/start.php';
            $fs_chic_lifestyle = fs_dynamic_init( array(
                'id'               => '6394',
                'slug'             => 'chic-lifestyle',
                'premium_slug'     => 'chic-lifestyle-pro',
                'type'             => 'theme',
                'public_key'       => 'pk_41de92096a076c4b1e1ba198fbcb7',
                'is_premium'       => false,
                'premium_suffix'   => 'pro',
                'has_addons'       => false,
                'has_paid_plans'   => true,
                'navigation'       => 'tabs',
                'menu'             => array(
                    'slug'        => 'chic-lifestyle',
                    'parent'      => array(
                        'slug' => 'themes.php',
                    ),
                    'first-path'  => 'themes.php?page=chic-lifestyle',
                    'contact'     => true,
                    'account'     => true,
                    'support'     => false,
                    'addons'      => false,
                    'affiliation' => false,
                    'pricing'     => false,
                ),
                'is_live'          => true,
                'is_org_compliant' => true,
            ) );
        }
        return $fs_chic_lifestyle;
    }

    // Init Freemius.
    fs_chic_lifestyle();
    // Signal that SDK was initiated.
    do_action( 'fs_chic_lifestyle_loaded' );
}
// freemius integration end
if ( !defined( 'CHIC_LIFESTYLE_VERSION' ) ) {
    // Replace the version number of the theme on each release.
    define( 'CHIC_LIFESTYLE_VERSION', '11.3.0' );
}
if ( !function_exists( 'chic_lifestyle_setup' ) ) {
    /**
     * Sets up theme defaults and registers support for various WordPress features.
     *
     * Note that this function is hooked into the after_setup_theme hook, which
     * runs before the init hook. The init hook is too late for some features, such
     * as indicating support for post thumbnails.
     */
    function chic_lifestyle_setup() {
        /*
         * Make theme available for translation.
         * Translations can be filed in the /languages/ directory.
         * If you're building a theme based on chic-lifestyle, use a find and replace
         * to change 'chic-lifestyle' to the name of your theme in all the template files
         */
        load_theme_textdomain( 'chic-lifestyle', get_template_directory() . '/languages' );
        add_theme_support( 'automatic-feed-links' );
        add_theme_support( 'title-tag' );
        add_theme_support( 'post-thumbnails' );
        add_theme_support( 'post-templates' );
        add_theme_support( 'wp-block-styles' );
        add_theme_support( 'responsive-embeds' );
        add_theme_support( 'align-wide' );
        add_theme_support( 'register_block_style' );
        add_theme_support( 'register_block_pattern' );
        // This theme uses wp_nav_menu() in one location.
        register_nav_menus( array(
            'primary' => esc_html__( 'Primary Menu', 'chic-lifestyle' ),
        ) );
        /*
         * Switch default core markup for search form, comment form, and comments
         * to output valid HTML5.
         */
        add_theme_support( 'html5', array(
            'search-form',
            'comment-form',
            'comment-list',
            'gallery',
            'caption'
        ) );
        /*
         * Enable support for Post Formats.
         * See http://codex.wordpress.org/Post_Formats
         */
        add_theme_support( 'custom-logo' );
        // Add theme support for selective refresh for widgets.
        add_theme_support( 'customize-selective-refresh-widgets' );
        // Set up the WordPress core custom background feature.
        add_theme_support( 'custom-background', apply_filters( 'chic_lifestyle_custom_background_args', array(
            'default-color' => 'ffffff',
            'default-image' => '',
        ) ) );
        add_theme_support( "custom-header", array(
            'default-color' => 'ffffff',
        ) );
        add_editor_style();
        // Define Image Sizes:
        add_image_size(
            'chic_lifestyle_slider',
            717,
            401,
            true
        );
    }

    // chic_lifestyle_setup
}
add_action( 'after_setup_theme', 'chic_lifestyle_setup' );
/**
 * Enqueue scripts and styles.
 */
function chic_lifestyle_scripts() {
    $font_family = get_theme_mod( 'font_family', 'Poppins' );
    $heading_font_family = get_theme_mod( 'heading_font_family', 'Poppins' );
    $site_title_font_family = get_theme_mod( 'site_title_font_family', 'Rock Salt' );
    wp_enqueue_style(
        'bootstrap',
        get_template_directory_uri() . '/css/bootstrap.css',
        array(),
        CHIC_LIFESTYLE_VERSION
    );
    wp_enqueue_style(
        'fontawesome',
        get_template_directory_uri() . '/css/font-awesome.css',
        array(),
        '7.2.0'
    );
    wp_enqueue_style(
        'fontawesome-v4-shims',
        get_template_directory_uri() . '/css/v4-shims.min.css',
        array('fontawesome'),
        '7.2.0'
    );
    wp_enqueue_style(
        'animate',
        get_template_directory_uri() . '/css/animate.css',
        array(),
        '1.0.0'
    );
    wp_enqueue_style(
        'owl',
        get_template_directory_uri() . '/css/owl.carousel.css',
        array(),
        '2.2.1'
    );
    wp_enqueue_style( 'chic-lifestyle-googlefonts', 'https://fonts.googleapis.com/css?family=' . esc_attr( $font_family ) . ':200,300,400,500,600,700,800,900|' . esc_attr( $heading_font_family ) . ':200,300,400,500,600,700,800,900|' . esc_attr( $site_title_font_family ) . ':200,300,400,500,600,700,800,900' );
    wp_enqueue_style(
        'chic-lifestyle-style',
        get_stylesheet_uri(),
        array(),
        CHIC_LIFESTYLE_VERSION
    );
    wp_enqueue_script(
        'bootstrap',
        get_template_directory_uri() . '/js/bootstrap.js',
        array('jquery'),
        '5.0.0',
        true
    );
    wp_enqueue_script(
        'owl',
        get_template_directory_uri() . '/js/owl.carousel.js',
        array('jquery'),
        '1.0.0',
        true
    );
    wp_enqueue_script(
        'chic-lifestyle-scripts',
        get_template_directory_uri() . '/js/script.js',
        array('jquery'),
        CHIC_LIFESTYLE_VERSION,
        true
    );
    if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
        wp_enqueue_script( 'comment-reply' );
    }
}

add_action( 'wp_enqueue_scripts', 'chic_lifestyle_scripts' );
/**
 * 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
 */
if ( !isset( $content_width ) ) {
    $content_width = 900;
}
function chic_lifestyle_content_width() {
    $GLOBALS['content_width'] = apply_filters( 'chic_lifestyle_content_width', 640 );
}

add_action( 'after_setup_theme', 'chic_lifestyle_content_width', 0 );
/**
 * Call Widget page
 **/
require get_template_directory() . '/inc/widgets/widgets.php';
/**
 * Custom template tags for this theme.
 */
require get_template_directory() . '/inc/template-tags.php';
/**
 * Custom functions that act independently of the theme templates.
 */
require get_template_directory() . '/inc/extras.php';
/**
 * Custom controls..
 */
require_once get_template_directory() . '/inc/custom-controls/custom-control.php';
/**
 * Customizer additions.
 */
require get_template_directory() . '/inc/customizer/customizer.php';
/**
 * Load Jetpack compatibility file.
 */
require get_template_directory() . '/inc/jetpack.php';
/**
 * Register Custom Navigation Walker
 */
require get_template_directory() . '/inc/wp_bootstrap_navwalker.php';
/**
 * Custom comment walker
 */
require get_template_directory() . '/inc/wp_bootstrap_comment_walker.php';
/**
 * Demo Content Section
 */
require get_template_directory() . '/inc/demo-content.php';
/**
 * Dynamic css
 */
require get_template_directory() . '/inc/dynamic-css.php';
/**
 * Add theme compatibility function for woocommerce if active
*/
if ( class_exists( 'woocommerce' ) ) {
    require get_template_directory() . '/inc/woocommerce-functions.php';
}
// Remove default "Category or Tags" from title
add_filter( 'get_the_archive_title', 'remove_default_tax_title' );
function remove_default_tax_title(  $title  ) {
    if ( is_category() ) {
        $title = single_cat_title( '', false );
    } elseif ( is_tag() ) {
        $title = single_tag_title( '', false );
    }
    return $title;
}

// add classes for post_class function
add_filter(
    'post_class',
    'chic_lifestyle_post_classes',
    10,
    3
);
function chic_lifestyle_post_classes(  $classes, $class, $post_id  ) {
    // $classes[] = 'col-sm-12';
    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), 'medium' );
    if ( empty( $image ) ) {
        $classes[] = 'no-image';
    }
    return $classes;
}

function chic_lifestyle_load_more_scripts() {
    $archive_cat = '';
    if ( is_front_page() && !is_home() ) {
        $archive_cat = get_theme_mod( 'archive_category' );
    }
    $args = array(
        'post_type' => 'post',
        'cat'       => absint( $archive_cat ),
        'paged'     => ( get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1 ),
    );
    $wp_query = new WP_Query($args);
    wp_register_script( 'chic_lifestyle_loadmore', get_template_directory_uri() . '/js/loadmore.js', array('jquery') );
    wp_localize_script( 'chic_lifestyle_loadmore', 'chic_lifestyle_loadmore_params', array(
        'ajaxurl'      => site_url() . '/wp-admin/admin-ajax.php',
        'current_page' => ( get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1 ),
        'max_page'     => $wp_query->max_num_pages,
        'cat'          => absint( $archive_cat ),
        'context'      => ( is_page_template( 'template-home.php' ) ? 'home' : 'index' ),
    ) );
    wp_enqueue_script( 'chic_lifestyle_loadmore' );
}

add_action( 'wp_enqueue_scripts', 'chic_lifestyle_load_more_scripts' );
function chic_lifestyle_load_more_ajax() {
    $args = array(
        'post_type'   => 'post',
        'post_status' => 'publish',
        'cat'         => absint( $_POST['cat'] ),
        'paged'       => ( isset( $_POST['page'] ) ? absint( $_POST['page'] ) : 1 ),
    );
    $context = ( isset( $_POST['context'] ) ? sanitize_text_field( $_POST['context'] ) : 'index' );
    $wp_query = new WP_Query($args);
    if ( $wp_query->have_posts() ) {
        // Decide post details based on template
        if ( $context === 'home' ) {
            $post_details = get_theme_mod( 'archive_list_details', ['date', 'categories'] );
        } elseif ( $context === 'index' ) {
            // index.php or anything else
            $post_details = get_theme_mod( 'chic_lifestyle_post_details', ['date', 'categories'] );
        }
        // $post_details = get_theme_mod('chic_lifestyle_post_details', array('date', 'categories'));
        while ( $wp_query->have_posts() ) {
            $wp_query->the_post();
            get_template_part( 'template-parts/content', null, array(
                'view'         => $_POST['view'],
                'post_details' => $post_details,
            ) );
        }
    }
    wp_reset_postdata();
    wp_die();
}

add_action( 'wp_ajax_chic_lifestyle_loadmore', 'chic_lifestyle_load_more_ajax' );
add_action( 'wp_ajax_nopriv_chic_lifestyle_loadmore', 'chic_lifestyle_load_more_ajax' );
/**
 * Required Plugins
 */
add_theme_support( 'required-plugins', array(array(
    'slug'            => 'tbthemes-demo-import',
    'name'            => 'TBThemes Theme Import',
    'active_filename' => 'tbthemes-demo-import/tbthemes-demo-import.php',
)) );
/**
 * Theme Admin Dashboard
 */
require get_template_directory() . '/inc/admin/dashboard.php';
/**
 * Sidebar panel
 */
require get_template_directory() . '/inc/gutenberg/sidebar/sidebar_panel.php';
if ( !function_exists( 'chic_lifestyle_get_pages_array' ) ) {
    /**
     * @array of pages
     */
    function chic_lifestyle_get_pages_array() {
        $pages = get_pages();
        $choices = [
            '' => esc_html__( 'No page', 'chic-lifestyle' ),
        ];
        foreach ( $pages as $page ) {
            $choices[$page->ID] = $page->post_title;
        }
        return $choices;
    }

}
/**
 * Generate Bootstrap 5.3 compatible pagination.
 *
 * @param WP_Query|null $query Optional. Custom WP_Query object. Defaults to global $wp_query.
 */
function chic_lifestyle_numeric_pagination(  $query = null  ) {
    if ( is_singular() ) {
        return;
    }
    // Use the global query if no custom query is passed.
    if ( !$query ) {
        global $wp_query;
        $query = $wp_query;
    }
    // Don't render pagination if there's only one page.
    if ( $query->max_num_pages <= 1 ) {
        return;
    }
    // Get the current page.
    $current_page = max( 1, get_query_var( 'paged' ) );
    // Pagination arguments.
    $pagination_links = paginate_links( array(
        'total'     => $query->max_num_pages,
        'current'   => $current_page,
        'type'      => 'array',
        'prev_text' => __( '&laquo;', 'chic-lifestyle' ),
        'next_text' => __( '&raquo;', 'chic-lifestyle' ),
    ) );
    if ( !empty( $pagination_links ) ) {
        echo '<nav id="chic_lifestyle_pagination" class="my-40 navigation" aria-label="Page navigation"><ul class="pagination justify-content-center">' . "\n";
        foreach ( $pagination_links as $link ) {
            // Add Bootstrap classes to each pagination item.
            $class = ( strpos( $link, 'current' ) !== false ? 'page-item active' : 'page-item' );
            echo '<li class="' . esc_attr( $class ) . '">' . str_replace( 'page-numbers', 'page-link', $link ) . '</li>';
        }
        /** Previous Post Link */
        /*if ( get_previous_posts_link() ) {
              printf( '<li>%s</li>' . "\n", get_previous_posts_link() );
          }*/
        /** Link to first page, plus ellipses if necessary */
        /*if ( !in_array( 1, $links ) ) {
              $class = ( 1 == $paged ? ' class="active"' : '' );
              printf(
                  '<li%s><a href="%s">%s</a></li>' . "\n",
                  $class,
                  esc_url( get_pagenum_link( 1 ) ),
                  '1'
              );
              if ( !in_array( 2, $links ) ) {
                  echo  '<li>…</li>' ;
              }
          }*/
        /** Link to current page, plus 2 pages in either direction if necessary */
        /*sort( $links );
          foreach ( (array) $links as $link ) {
              $class = ( $paged == $link ? ' class="active"' : '' );
              printf(
                  '<li%s><a href="%s">%s</a></li>' . "\n",
                  $class,
                  esc_url( get_pagenum_link( $link ) ),
                  $link
              );
          }*/
        /** Link to last page, plus ellipses if necessary */
        /*if ( !in_array( $max, $links ) ) {
              if ( !in_array( $max - 1, $links ) ) {
                  echo  '<li>…</li>' . "\n" ;
              }
              $class = ( $paged == $max ? ' class="active"' : '' );
              printf(
                  '<li%s><a href="%s">%s</a></li>' . "\n",
                  $class,
                  esc_url( get_pagenum_link( $max ) ),
                  $max
              );
          }*/
        /** Next Post Link */
        /*if ( get_next_posts_link() ) {
              printf( '<li>%s</li>' . "\n", get_next_posts_link() );
          }*/
        echo '</ul></nav>' . "\n";
    }
}

if ( !function_exists( 'chic_lifestyle_custom_excerpt' ) ) {
    /**
     * Custom post excerpt with character limit
     */
    function chic_lifestyle_custom_excerpt(  $limit = 200  ) {
        $excerpt = get_the_excerpt();
        if ( mb_strlen( $excerpt ) > $limit ) {
            $excerpt = mb_substr( $excerpt, 0, $limit );
            $excerpt = mb_substr( $excerpt, 0, mb_strrpos( $excerpt, ' ' ) );
            $excerpt .= '...';
        }
        return $excerpt;
    }

}
/**
 * Block Editor Typography
 */
require get_template_directory() . '/inc/gutenberg/typography/typography.php';