<?php
/**
* Body Classes.
*
* @package NewsNation
*/
 
 if (!function_exists('newsnation_body_classes')) :

    function newsnation_body_classes($classes) {

        $newsnation_default = newsnation_get_default_theme_options();
        global $post;

        // Adds a class of hfeed to non-singular pages.
        if ( !is_singular() ) {
            $classes[] = 'hfeed';
        }

        // Adds a class of no-sidebar when there is no sidebar present.
        if ( !is_active_sidebar( 'sidebar-1' ) ) {
            $classes[] = 'no-sidebar';
        }
        
        if ( is_active_sidebar( 'sidebar-1' ) ) {

            $global_sidebar_layout = esc_html( get_theme_mod( 'global_sidebar_layout',$newsnation_default['global_sidebar_layout'] ) );

            if( is_single() || is_page() ){

                $newsnation_post_sidebar = esc_html( get_post_meta( $post->ID, 'newsnation_post_sidebar_option', true ) );

                if( $newsnation_post_sidebar == 'global-sidebar' || empty( $newsnation_post_sidebar ) ){

                    if( class_exists('WooCommerce') && ( is_cart() || is_checkout() ) ){
                        
                        $classes[] = 'no-sidebar';

                    }else{

                        $classes[] = esc_attr( $global_sidebar_layout );

                    }

                }else{

                    if( class_exists('WooCommerce') && ( is_cart() || is_checkout() ) ){
                        
                        $classes[] = 'no-sidebar';

                    }else{

                        $classes[] = esc_attr( $newsnation_post_sidebar );

                    }
                }
                
            }elseif( is_404() ){

                $classes[] = 'no-sidebar';

            }else{
                
                $classes[] = esc_attr( $global_sidebar_layout );
            }

        }
        if (is_active_sidebar('newsnation-offcanvas-widget')){
            $classes[] = 'offcanvas-on';
        }
        if( is_page() ){

            $newsnation_header_trending_page = get_theme_mod( 'newsnation_header_trending_page' );
            $newsnation_header_popular_page = get_theme_mod( 'newsnation_header_popular_page' );

            if( $newsnation_header_trending_page == $post->ID || $newsnation_header_popular_page == $post->ID ){

                $newsnation_archive_layout = get_theme_mod( 'newsnation_archive_layout',$newsnation_default['newsnation_archive_layout'] );
                $ed_image_content_inverse = get_theme_mod( 'ed_image_content_inverse',$newsnation_default['ed_image_content_inverse'] );
                if( $newsnation_archive_layout == 'default' && $ed_image_content_inverse ){

                    $classes[] = 'wpi-archive-alternative';

                }

                $classes[] = 'wpi-archive-'.esc_attr( $newsnation_archive_layout );
                
            }

        }

        if( is_singular('post') ){

            $newsnation_post_layout = esc_html( get_post_meta( $post->ID, 'newsnation_post_layout', true ) );

            if( $newsnation_post_layout == '' || $newsnation_post_layout == 'global-layout' ){
                
                $newsnation_post_layout = get_theme_mod( 'newsnation_single_post_layout',$newsnation_default['newsnation_archive_layout'] );

            }

            $classes[] = 'wpi-single-'.esc_attr( $newsnation_post_layout );

            if( $newsnation_post_layout == 'layout-2' ){
                
                $newsnation_header_overlay = esc_html( get_post_meta( $post->ID, 'newsnation_header_overlay', true ) );

                if( $newsnation_header_overlay == '' || $newsnation_header_overlay == 'global-layout' ){

                    $newsnation_post_layout2 = get_theme_mod( 'newsnation_single_post_layout',$newsnation_default['newsnation_archive_layout'] );

                    if( $newsnation_post_layout2 == 'layout-2' ){

                        $ed_header_overlay = esc_html( get_post_meta( $post->ID, 'ed_header_overlay', true ) );
                        if( $ed_header_overlay == '' || $ed_header_overlay == 'global-layout' ){
                            
                            $ed_header_overlay = get_theme_mod( 'ed_header_overlay',$newsnation_default['ed_header_overlay'] );
                        }

                    }else{

                        $ed_header_overlay = false;

                    }

                }else{

                    $ed_header_overlay = true;

                }
                if( $ed_header_overlay ){

                    $classes[] = 'wpi-single-header-overlay';

                }

            }

        }

        if( is_singular('page') ){

            $newsnation_page_layout = get_post_meta( $post->ID, 'newsnation_page_layout', true );

            if( $newsnation_page_layout == ''  ){
                
                $newsnation_page_layout = 'layout-1';

            }

            $classes[] = 'theme-single-'.esc_attr( $newsnation_page_layout );

            if( $newsnation_page_layout == 'layout-2' ){
                
                $newsnation_ed_header_overlay = get_post_meta( $post->ID, 'newsnation_ed_header_overlay', true );
                if( $newsnation_ed_header_overlay ){
                    $classes[] = 'theme-single-header-overlay';
                }

            }

        }

        if( is_archive() || is_home() || is_search() ){

            $newsnation_archive_layout = get_theme_mod( 'newsnation_archive_layout',$newsnation_default['newsnation_archive_layout'] );
            $ed_image_content_inverse = get_theme_mod( 'ed_image_content_inverse',$newsnation_default['ed_image_content_inverse'] );
            if( $newsnation_archive_layout == 'default' && $ed_image_content_inverse ){

                $classes[] = 'wpi-archive-alternative';

            }

            $classes[] = 'wpi-archive-'.esc_attr( $newsnation_archive_layout );
            
        }

        if( is_singular('post') ){

            $newsnation_ed_post_reaction = esc_html( get_post_meta( $post->ID, 'newsnation_ed_post_reaction', true ) );
            if( $newsnation_ed_post_reaction ){
                $classes[] = 'hide-comment-rating';
            }

        }

        return $classes;
    }

endif;

add_filter('body_class', 'newsnation_body_classes');