<?php
/**
 * Additional features to allow styling of the templates
 *
 * @package WordPress
 * @subpackage Archi
 * @since 1.0
 */

// Add specific CSS class by filter
add_filter( 'body_class', 'archi_body_class_names' );
function archi_body_class_names( $classes ) {
    global $archi_option;
	$theme = wp_get_theme();

    // add 'class-name' to the $classes array
    if($archi_option['version_type']=='light'){
        $classes[] = 'de_light';
    }

    if($archi_option['subpage-switch']==false && !is_page_template('page-templates/template-canvas.php') && !is_page_template('page-templates/template-landing.php') && !is_page_template('page-templates/template-onepage.php') ){ 
        $classes[] = 'no-subheader';
    }

    if($archi_option['topbar-allpage']==true && $archi_option['header_layout']=="htop_page"){
        $classes[] = 'has-topbar';
    } 

    if ($archi_option['desktop-sticky']==false) {
        $classes[] = 'header-no-sticky-subheader';
    }   

    if ($archi_option['mobile-sticky']==true){
        $classes[] = 'header-mobile-sticky-subheader';
    }

    if(isset($archi_option['header_layout']) and $archi_option['header_layout']!="htop_page" ){
        $classes[] = 'de-navbar-left';
    } 

    if(isset($archi_option['preloader_mode']) and $archi_option['preloader_mode']=="preloader_logo"){
        $classes[] = 'royal_preloader';
    }
	
	$classes[] = 'archi-theme-ver-'.$theme->version;
    $classes[] = 'wordpress-version-'.get_bloginfo( 'version' );
    
    // return the $classes array
    return $classes;
}

// Add specific CSS class by filter
function archi_header_class() {
    global $archi_option;

    $header_classes = array();

    if(( isset($archi_option['header_position']) and $archi_option['header_position']!="onbottom_page" and $archi_option['topbar-onepage']==true and is_page_template('page-templates/template-onepage.php')) || ($archi_option['topbar-allpage']==true and !is_page_template('page-templates/template-onepage.php'))){ 
        $header_classes[] = 'de_header_2';
    }

    if ($archi_option['mobile-sticky']==true) {
        $header_classes[] = 'header-mobile-sticky';
    }  

    if ($archi_option['desktop-sticky']==false and $archi_option['hideshow_header']==false) {
        $header_classes[] = 'header-desktop-nosticky';
    }   

    if(isset($archi_option['header_style']) and $archi_option['header_style']=="header_light" and is_page_template( array( 'page-templates/template-canvas.php', 'page-templates/template-onepage.php' ) )){
        $header_classes[] = 'header-light';
    }elseif (isset($archi_option['header_style']) and $archi_option['header_style']=="header_overlay" and is_page_template( array( 'page-templates/template-canvas.php', 'page-templates/template-onepage.php' ) )){
        $header_classes[] = 'header-bg';
    }elseif (isset($archi_option['header_style']) and $archi_option['header_style']=="header_transparent" and is_page_template( array( 'page-templates/template-canvas.php', 'page-templates/template-onepage.php' ) )){
        $header_classes[] = 'transparent';
    }else{
        $header_classes[] = '';
    }

    if( !is_page_template('page-templates/template-canvas.php') and !is_page_template('page-templates/template-onepage.php') ){
        $header_classes[] = 'header-bg';
    }

    if(isset($archi_option['header_position']) and $archi_option['header_position']=="onbottom_page" and is_page_template( array( 'page-templates/template-canvas.php', 'page-templates/template-onepage.php' ) ) ){
        $header_classes[] = 'header-bottom';
    }    		

    if ($archi_option['hideshow_header']==true and $archi_option['header_position']!="onbottom_page" and $archi_option['header_layout']=="htop_page" and is_page_template('page-templates/template-canvas.php') ) {
        $header_classes[] = 'autoshow scrollOff';
    } 
    
    // return the $classes array
    echo implode( ' ', $header_classes );
}

add_action('wp_footer', 'archi_add_custom_script_footer');
function archi_add_custom_script_footer(){ 
    global $archi_option;
    if ($archi_option['js_editor'] != '') {
?>
    <script type="text/javascript">
        <?php echo $archi_option['js_editor']; ?>
    </script>
<?php
    }
} 
 
if ( ! function_exists( 'topbar_loginout_link' ) ) :
    function topbar_loginout_link() {
        $current_user = wp_get_current_user();
        if (is_user_logged_in()) {
            echo '<li><i class="fa fa-user-o"></i> <a href="'. wp_logout_url( get_permalink( wc_get_page_id( 'myaccount' ) ) ) .'">' . $current_user->user_login . '( ' . esc_html__( 'Log Out', 'archi' ) . ' )</a> <span>/</span> <a href="'. get_permalink( wc_get_page_id( 'myaccount' ) ) .'">' . esc_html__( 'My account', 'archi' ) . '</a></li>';
        }elseif (!is_user_logged_in()) {
            echo '<li><i class="fa fa-lock"></i> <a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">' . esc_html__( 'Login', 'archi' ) . '</a> <span>/</span> <a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">' . esc_html__( 'Register', 'archi' ) . '</a></li>';
        }
    }
endif;

// [oceanthemes_date time_custom="F j, Y"]
function oceanthemes_date_func( $atts ) {
    $date_format = shortcode_atts( array(
        'time_custom' => 'Y',        
    ), $atts );

    $dt = new DateTime("now");
    $gmt_timestamp = $dt->format("{$date_format['time_custom']}");

    return $gmt_timestamp;
}
add_shortcode( 'oceanthemes_date', 'oceanthemes_date_func' );

// Add Title Attribute to WordPress Image the_post_thumbnail
function archi_add_img_title( $attr, $attachment = null ) {
    $img_title = trim( strip_tags( $attachment->post_title ) );
    $attr['title'] = $img_title;
 
    return $attr;
}
add_filter( 'wp_get_attachment_image_attributes','archi_add_img_title', 10, 2 );

// Remove Title Attribute to WordPress Image the_post_thumbnail
/*add_filter( 'wp_get_attachment_image_attributes', 'archi_remove_image_text' );
function archi_remove_image_text( $attr ) {
    unset( $attr['alt'] );
    unset( $attr['title']);

    return $attr;
}*/

// Add default custom post type using WPBakery Page Builder
if( class_exists( 'Vc_Manager' ) ) {
    $list = array(
        'post',
        'page',
        'portfolio',        
        'service',
        'process',        
    );
    vc_set_default_editor_post_types( $list );
    //vc_disable_frontend();
}

if ( ! function_exists( 'archi_get_layout' ) ) :
    function archi_get_layout() {
        global $archi_option;
        
        $page_layout = $archi_option['blog-layout'];

        return $page_layout;
    }
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
 */
if ( ! function_exists( 'archi_content_columns' ) ) :
    function archi_content_columns() {
        global $archi_option;
        $blog_content_width = array();

        // Check if layout is one column.
        if ( $archi_option['blog-layout'] == 3 && is_active_sidebar( 'sidebar-1' ) ) {
            $blog_content_width[] = 'col-lg-'.esc_attr($archi_option['blog_col_left']).' col-md-'.esc_attr($archi_option['blog_col_left']).' col-sm-12 col-xs-12';
        }elseif ( $archi_option['blog-layout'] == 2 && is_active_sidebar( 'sidebar-1' ) ) {
            $blog_content_width[] = 'col-lg-'.esc_attr($archi_option['blog_col_left']).' col-md-'.esc_attr($archi_option['blog_col_left']).' col-sm-12 col-xs-12 pull-right';
        }else{
            $blog_content_width[] = 'col-lg-12 col-md-12 col-sm-12 col-xs-12';
        }

        // return the $classes array
        echo implode( ' ', $blog_content_width );
    }
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
 */
if ( ! function_exists( 'archi_shop_columns' ) ) :
    function archi_shop_columns() {
        global $archi_option;
        $shop_content_width = array();

        // Check if layout is one column.
        if ( $archi_option['shop-layout'] == 3 && is_active_sidebar( 'shop-sidebar' ) ) {
            $shop_content_width[] = 'col-lg-9 col-md-9 col-sm-12 col-xs-12';
        }elseif ( $archi_option['shop-layout'] == 2 && is_active_sidebar( 'shop-sidebar' ) ) {
            $shop_content_width[] = 'col-lg-9 col-md-9 col-sm-12 col-xs-12 pull-right';
        }else{
            $shop_content_width[] = 'col-lg-12 col-md-12 col-sm-12 col-xs-12';
        }

        // return the $classes array
        echo implode( ' ', $shop_content_width );
    }
endif;

if ( ! function_exists( 'archi_page_header' ) ) {
    function archi_page_header (){
        global $archi_option;

        if ( function_exists('rwmb_meta') ) {
            if( is_home() || is_singular('post') || is_archive() || is_search() || is_category() || is_tax() ){
                $pheader = rwmb_meta('pheader_switch', 'type=switch', get_option( 'page_for_posts' ));                
            }else{
                $pheader = rwmb_meta('pheader_switch', 'type=switch');
            }
            if ($archi_option['subpage-switch']==false && $pheader==0) {
                return;
            }
            if ( $pheader==0 && !is_singular('product') && !is_shop() && !is_tax() ) {
                return;
            }
        }

        if( $archi_option['subpage-switch']==false && is_shop() || $archi_option['subpage-switch']==false && is_singular('product') || is_page_template( 'page-templates/template-canvas.php' ) ) {
            return;
        }else{
            $bg     = '';
            $title  = '';
            $subtitle = '';
            $output = array();
            $output2 = array();

            if ( is_home() ) {
                $title = get_the_title(get_option('page_for_posts'));
                if ( function_exists('rwmb_meta') ) {
                    $subtitle = rwmb_meta('_cmb_page_subtitle', 'type=textarea', get_option( 'page_for_posts' ));
                }
            }elseif( is_singular('portfolio') ){
                $title = get_the_title();
                if ( function_exists('rwmb_meta') ) {
                    $subtitle = rwmb_meta('_cmb_portfolio_subtitle', 'type=textarea');
                }
            } elseif ( is_search() ) {
                $title = esc_html__('Search Results for: ', 'archi') . get_search_query();
            } elseif ( is_archive() ) {
                $title = get_the_archive_title(); 
                $subtitle =  get_the_archive_description(); 
            } else {
                $title = get_the_title();
                if ( function_exists('rwmb_meta') ) {
                    $subtitle = rwmb_meta('_cmb_page_subtitle', 'type=textarea');
                }
            }

            if (!function_exists('rwmb_meta')) {
                $bg = $archi_option['bg_allpage']['url'];
            } else {
                if( is_home() ) {
                    $images = rwmb_meta('_cmb_subheader_image', 'type=image', get_option( 'page_for_posts' ));
                }elseif( is_singular('portfolio') ){
                    $images = rwmb_meta('_cmb_portfolio_subheader', 'type=image');
                }else{
                    $images = rwmb_meta('_cmb_subheader_image', 'type=image');
                }                
                if (!$images) {                                               
                    $bg = $archi_option['bg_allpage']['url']; 
                    if( class_exists( 'WooCommerce' ) ){
                        if( is_shop() || is_product_category() || is_product_tag() ){
                            $bg = $archi_option['shop_thumbnail']['url'];
                        }
                        if( is_singular('product') ){
                            $bg = $archi_option['product_thumbnail']['url'];
                        }
                    } 
                    if( is_post_type_archive( 'portfolio' ) || is_tax('categories') ) {
                        $bg = $archi_option['portfolio_thumbnail']['url'];
                    }                  
                } else {
                    foreach ($images as $image) {
                        $bg = $image['full_url'];
                        break;
                    }
                }
            }

            if ($title) {
                $output[] = sprintf('%s', $title);
            }

            if ($subtitle) {
                $output2[] = sprintf('%s', $subtitle);
            }
        ?>

        <section id="subheader" data-speed="8" data-type="background" class="padding-top-bottom" <?php if ($bg) { ?> style="background-image: url(<?php echo esc_url($bg); ?>);" <?php } ?> >
            <div class="container">
                <div class="row">
                    <div class="col-md-12">                
                        <div class="title-wrap">
                            <?php if ( is_post_type_archive( 'portfolio' ) ) {  ?> 
                                <?php if ($archi_option['portfolio_title'] != ''){ 
                                  echo '<h1 class="page-title">' . esc_attr($archi_option['portfolio_title']) . '</h1>'; 
                                }else{ 
                                  the_archive_title( '<h1 class="page-title">', '</h1>' );                                   
                                } ?>
                            <?php } elseif ( is_post_type_archive( 'service' ) ) {  ?>   
                                <?php if ($archi_option['archive_service_title'] != ''){ 
                                    echo '<h1 class="page-title">' . esc_attr($archi_option['archive_service_title']) . '</h1>'; 
                                }else{ 
                                    the_archive_title( '<h1 class="page-title">', '</h1>' );                                     
                                } ?>
                            <?php }else{ ?>
                                <h1 class="page-title"><?php echo implode('', $output); ?></h1>
                                <?php if ($subtitle) { ?><h4><?php echo implode('', $output2); ?></h4><?php } ?>
                            <?php } ?>                    
                        </div> 
                        <?php 
                            if( function_exists('archi_breadcrumbs') ): 
                                archi_breadcrumbs();
                            endif;
                        ?> 
                    </div>
                </div>
            </div>
        </section>
        <!-- subheader close -->

        <?php echo wp_specialchars_decode( do_shortcode( $archi_option['top_page_contact_info'] ) ); ?>

        <?php
        }
    }
}