<?php
/**
 * Custom layouts
 */
add_filter( 'kopa_layout_manager_settings', 'kopa_extra_layout_manager_settings' );

function kopa_extra_layout_manager_settings( $options ) {
    $positions = array(
        'position_1' => 'Right sidebar',
        'position_2' => 'Footer 1',
        'position_3' => 'Footer 2',
        'position_4' => 'Footer 3',
        'position_5' => 'Footer 4',
        'position_6' => 'Footer 5',
        
    );

    //set layout for blog
    $blog = array(
        'title'     => 'Blog',
        'preview'   => get_template_directory_uri() . '/images/layouts/blog.jpg',
        'positions' => array(
            'position_1',
            'position_2',
            'position_3',
            'position_4',
            'position_5',
            'position_6'
        )
    );

    $blog_default = array(
        'position_1' => 'right_sidebar',
        'position_2' => 'footer_1',
        'position_3' => 'footer_2',
        'position_4' => 'footer_3',
        'position_5' => 'footer_4',
        'position_6' => 'footer_5',

    );
    //set layout for single post
    $single = array(
        'title'     => 'Single',
        'preview'   => get_template_directory_uri() . '/images/layouts/blog.jpg',
        'positions' => array(
            'position_1',
            'position_2',
            'position_3',
            'position_4',
            'position_5',
            'position_6'
        )
    );

    $single_default = array(
        'position_1' => 'right_sidebar',
        'position_2' => 'footer_1',
        'position_3' => 'footer_2',
        'position_4' => 'footer_3',
        'position_5' => 'footer_4',
        'position_6' => 'footer_5',
    );


    //set layout for page full-width
    $page = array(
        'title'     => 'Static page',
        'preview'   => get_template_directory_uri() . '/images/layouts/front-page.jpg',
        'positions' => array(
            'position_2',
            'position_3',
            'position_4',
            'position_5',
            'position_6',
        )
    );

    $page_default = array(
        'position_2' => 'footer_1',
        'position_3' => 'footer_2',
        'position_4' => 'footer_3',
        'position_5' => 'footer_4',
        'position_6' => 'footer_5',

    );


    // page layout
    $options['page-layout']['positions'] = $positions;
    $options['page-layout']['layouts'] = array(
        'page' => $page,
    );
    $options['page-layout']['default'] = array(
        'layout_id' => 'page',
        'sidebars'  => array(
            'page' => $page_default
        )
    );

    // blog layout
    $options['blog-layout']['positions'] = $positions;
    $options['blog-layout']['layouts'] = array(
        'blog' => $blog
    );
    $options['blog-layout']['default'] = array(
        'layout_id' => 'blog',
        'sidebars'  => array(
            'blog' => $blog_default,
        ),
    );

    // post layout
    $options['post-layout']['positions'] = $positions;
    $options['post-layout']['layouts'] = array(
        'single' => $single
    );
    $options['post-layout']['default'] = array(
        'layout_id' => 'single',
        'sidebars'  => array(
            'single' => $single_default
        ),
    );

    //search layout
    $options['search-layout']['positions'] = $positions;
    $options['search-layout']['layouts'] = array(
        'blog' => $blog
    );
    $options['search-layout']['default'] = array(
        'layout_id' => 'blog',
        'sidebars'  => array(
            'blog' => $blog_default
        ),
    );

    //404 layout
    $page_404 = array(
        'title'     => 'Error 404',
        'preview'   => get_template_directory_uri() . '/images/layouts/error_404.jpg',
        'positions' => array(
            'position_2',
            'position_3',
            'position_4',
            'position_5',
            'position_6'
        )
    );

    $page_404_default = array(
        'position_2' => 'footer_1',
        'position_3' => 'footer_2',
        'position_4' => 'footer_3',
        'position_5' => 'footer_4',
        'position_6' => 'footer_5',

    );
    $options['error404-layout']['positions'] = $positions;
    $options['error404-layout']['layouts'] = array(
        'page-404' => $page_404
    );
    $options['error404-layout']['default'] = array(
        'layout_id' => 'page-404',
        'sidebars'  => array(
            'page-404' => $page_404_default
        ),
    );


    return $options;
}

/**
 * Add custom layout feature for custom post types and custom taxonomies
 */
add_filter( 'kopa_custom_layout_arguments', 'kopa_edit_custom_layout_feature' );

function kopa_edit_custom_layout_feature( $args ) {
    $args[] = array(
        'screen'   => 'product',
        'taxonomy' => false,
        'layout'   => 'product-layout',
    );

    $args[] = array(
        'screen'   => 'product_cat',
        'taxonomy' => true,
        'layout'   => 'shop-layout',
    );

    $args[] = array(
        'screen'   => 'product_tag',
        'taxonomy' => true,
        'layout'   => 'shop-layout',
    );

    if( defined('GRJ_PREFIX') ){
        $args[] = array(
            'screen'   => 'project',
            'taxonomy' => true,
            'layout'   => 'portfolio-layout',
        );

        $args[] = array(
            'screen'   => 'portfolio',
            'taxonomy' => false,
            'layout'   => 'single-portfolio-layout',
        );

        $args[] = array(
            'screen'   => 'portfolio_tag',
            'taxonomy' => true,
            'layout'   => 'portfolio-layout',
        );
    }


    if ( isset($args['frontpage-layout']) ){
        unset($args['frontpage-layout']);
    }


    return $args;
}

/*
 * Custom layout manager settings
 */
add_filter('kopa_layout_manager_settings', 'kopa_remove_layout_manager_settings');
function kopa_remove_layout_manager_settings( $setting ) {
    if ( isset($setting['frontpage-layout']) ) {
        unset($setting['frontpage-layout']);
    }
    $new_setting = array();
    foreach ($setting as $v ) {
        if ( 'frontpage-title' !== $v['id'] ){
            $new_setting[] = $v;
        }
    }
    return $new_setting;
}
