<?php
/**
 * featured_products section
 *
 * This is the template for the content of featured_products section
 *
 * @package Theme Palace
 * @subpackage Enlightme Pro
 * @since Enlightme Pro 1.0.0
 */
if ( ! function_exists( 'enlightme_pro_add_featured_products_section' ) ) :
    /**
    * Add featured_products section
    *
    *@since Enlightme Pro 1.0.0
    */
    function enlightme_pro_add_featured_products_section() {
    	$options = enlightme_pro_get_theme_options();
        // Check if featured_products is enabled on frontpage
        $featured_products_enable = apply_filters( 'enlightme_pro_section_status', true, 'featured_products_section_enable' );

        if ( ( true !== $featured_products_enable) || ! class_exists( 'WooCommerce' ) ) {
            return false;
        }

        // Render featured_products section now.
        enlightme_pro_render_featured_products_section();
    }
endif;

if ( ! function_exists( 'enlightme_pro_render_featured_products_section' ) ) :
  /**
   * Start featured_products section
   *
   * @return string featured_products content
   * @since Enlightme Pro 1.0.0
   *
   */
   function enlightme_pro_render_featured_products_section() {
        $options = enlightme_pro_get_theme_options();

    ?>

    <div id="enlightme_pro_featured_products_section" class="relative page-section same-background">

        <div id="featured-products" class="relative">
            <div class="wrapper">

                <?php if ( is_customize_preview()):
                enlightme_pro_section_tooltip( 'featured-products' );
                endif; ?>
                <div class="col-3 clear">

                    <?php for ( $i=1; $i <= $options['featured_products_count']; $i++ ) { 
                        if ( empty($options['featured_products_content_featured_products_category_'.$i]) ) {
                            continue;
                        }
                        $thumbnail_id = get_term_meta( $options['featured_products_content_featured_products_category_'.$i], 'thumbnail_id', true );
                        $image = wp_get_attachment_url( $thumbnail_id );
                        $cat_name = get_term_by( 'id', $options['featured_products_content_featured_products_category_'.$i], 'product_cat' )->name;
                        $cat_link = get_category_link( $options['featured_products_content_featured_products_category_'.$i] );
                        ?>

                        <article>
                            <div class="featured-image" style="background-image: url('<?php echo esc_url( $image ); ?>');">
                                <div class="entry-container">
                                    <header class="entry-header">
                                        <h2 class="entry-title"><a href="<?php echo esc_url( $cat_link ) ?>"><?php echo esc_html( $cat_name ); ?></a></h2>

                                        <?php if( !empty( $options['featured_products_sub_title_'.$i] ) ): ?>
                                            <span><?php echo esc_html( $options['featured_products_sub_title_'.$i] ); ?></span>
                                        <?php endif; ?>
                                    </header>
                                </div><!-- .entry-container -->
                            </div><!-- .featured-image -->
                        </article>

                        <?php } ?>

                    </div><!-- .grid --> 
                </div><!-- .wrapper -->
            </div>

        </div>

    <?php }
endif;