<?php
/**
 * Maksitto Light functions and definitions 
 *
 * @package Maksitto Light
 */


/* Check if access from functions file */
if ( ! defined('PHP')) {
    die;
}


/* Load TGM */
require_once get_template_directory() . '/inc/plugins/tgm.php';

function maskitto_light_register_required_plugins() {

    $plugins = array(
        array(
            'name'      => __( 'Redux Framework', 'maskitto-light' ),
            'slug'      => 'redux-framework',
            'required'  => false,
        ),

        array(
            'name'      => __( 'Page Builder by SiteOrigin', 'maskitto-light' ),
            'slug'      => 'siteorigin-panels',
            'required'  => false,
        ),

        array(
            'name'      => __( 'Types - Custom Fields and Custom Post Types Management', 'maskitto-light' ),
            'slug'      => 'types',
            'required'  => false,
        ),
    );

    $config = array(
        'domain'            => 'maskitto-light',           // Text domain - likely want to be the same as your theme.
        'default_path'      => '',                          // Default absolute path to pre-packaged plugins
        'parent_menu_slug'  => 'plugins.php',               // Default parent menu slug
        'parent_url_slug'   => 'plugins.php',               // Default parent URL slug
        'menu'              => 'install-required-plugins',  // Menu slug
        'has_notices'       => true,                        // Show admin notices or not
        'is_automatic'      => true,                        // Automatically activate plugins after installation or not
    );

    tgmpa( $plugins, $config );

}
add_action( 'tgmpa_register', 'maskitto_light_register_required_plugins' );


/**
 * Initialize framework
 */
require get_template_directory() . '/inc/plugins/redux.php';
global $maskitto;


/**
 * Initialize basic theme functionality
 */
if ( ! function_exists( 'maskitto_light_setup' ) ) :

    function maskitto_light_setup() {

        /* Add theme text domain support */
        load_theme_textdomain('maskitto-light', get_template_directory() . '/languages');

    	/* Add default posts and comments RSS feed links to head. */
    	add_theme_support( 'automatic-feed-links' );

    	/* Enable support for Post Thumbnails on posts and pages. */
    	add_theme_support( 'post-thumbnails' );

        /* Enable support for custom header */
        add_theme_support( "custom-header", array( 'default-image' => get_template_directory_uri() . '/img/logo.png', 'header-text' => false, ) );

        /* Enables plugins and themes to manage the document title */
        add_theme_support( 'title-tag' );

    	/* This theme uses wp_nav_menu() in one location. */
    	register_nav_menus( array(
    		'primary' => __( 'Primary navigation', 'maskitto-light' ),
    	) );

        /* Bootstrap navigation */
        require_once get_template_directory() . '/inc/plugins/navwalker.php';

    	/* Switch default core markup for search form, comment form, and comments. */
    	add_theme_support( 'html5', array(
    		'search-form', 'comment-form', 'comment-list', 'gallery', 'caption',
    	) );

    	/* Set up the WordPress core custom background feature. */
    	add_theme_support( 'custom-background', apply_filters( 'maskitto_custom_background_args', array(
    		'default-color' => 'ffffff',
    		'default-image' => '',
    	) ) );


        /* Custom editor style */
        add_editor_style( );
    }

    add_action( 'after_setup_theme', 'maskitto_light_setup' );

endif;



/**
 * Initialize widget area
 */
function maskitto_light_widgets_init() {
	register_sidebar( array(
		'name'          => __( 'Sidebar', 'maskitto-light' ),
		'id'            => 'sidebar-1',
		'description'   => '',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h1 class="widget-title">',
		'after_title'   => '</h1>',
	) );
}
add_action( 'widgets_init', 'maskitto_light_widgets_init' );


/**
 * Load custom template tags
 */
require get_template_directory() . '/inc/template-tags.php';

/**
 * Load functions that act independently of the theme templates
 */
require get_template_directory() . '/inc/extras.php';

/**
 * Load customizer additions
 */
require get_template_directory() . '/inc/customizer.php';

/**
 * Load Jetpack compatibility file
 */
require get_template_directory() . '/inc/jetpack.php';


/** 
 * Load media files
 */

if( !function_exists('maskitto_light_enqueue_styles') ) :
        
    function maskitto_light_enqueue_styles() {

        global $maskitto_light;

        /* Without this line, jQuery not allways load from WordPress as it should */
        wp_enqueue_script('jquery');
        wp_enqueue_script('masonry');

        if( isset( $maskitto_light['minity-status'] ) && $maskitto_light['minity-status'] == 1 ) {

            wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/css/bootstrap.min.css' );
            wp_enqueue_style( 'default-style', get_template_directory_uri() . '/css/style.css' );
            wp_enqueue_style( 'font-awesome', get_template_directory_uri() . '/css/font-awesome.min.css' );
            wp_enqueue_style( 'owl-carousel', get_template_directory_uri() . '/css/slick.css' );
            wp_enqueue_style( 'animate', get_template_directory_uri() . '/css/animate.min.css' );
            wp_enqueue_style( 'jquery-tosrus', get_template_directory_uri() . '/css/jquery.tosrus.all.css' );

            wp_enqueue_script( 'bootstrap', get_template_directory_uri() . '/js/bootstrap.min.js', array( 'jquery' ) );
            wp_enqueue_script( 'slick', get_template_directory_uri() . '/js/slick.min.js', array( 'jquery' ) );
            wp_enqueue_script( 'smoothscroll', get_template_directory_uri() . '/js/smoothscroll.js', array( 'jquery' ) );
            wp_enqueue_script( 'jquery-tosrus', get_template_directory_uri() . '/js/jquery.tosrus.min.all.js', array( 'jquery' ));

            wp_enqueue_script( 'waypoint', get_template_directory_uri() . '/js/jquery.waypoints.min.js', array( 'jquery' ));
            wp_enqueue_script( 'counterup', get_template_directory_uri() . '/js/jquery.counterup.min.js', array( 'jquery' ));

        } else {

            wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/css/bootstrap.css' );
            wp_enqueue_style( 'default-style', get_template_directory_uri() . '/css/style.css' );
            wp_enqueue_style( 'font-awesome', get_template_directory_uri() . '/css/font-awesome.css' );
            wp_enqueue_style( 'owl-carousel', get_template_directory_uri() . '/css/slick.css' );
            wp_enqueue_style( 'animate', get_template_directory_uri() . '/css/animate.css' );
            wp_enqueue_style( 'jquery-tosrus', get_template_directory_uri() . '/css/jquery.tosrus.all.css' );

            wp_enqueue_script( 'bootstrap', get_template_directory_uri() . '/js/bootstrap.js' );
            wp_enqueue_script( 'slick', get_template_directory_uri() . '/js/slick.min.js' );
            wp_enqueue_script( 'smoothscroll', get_template_directory_uri() . '/js/smoothscroll.js' );
            wp_enqueue_script( 'jquery-tosrus', get_template_directory_uri() . '/js/jquery.tosrus.min.all.js' );

            wp_enqueue_script( 'waypoint', get_template_directory_uri() . '/js/jquery.waypoints.js', array( 'jquery' ));
            wp_enqueue_script( 'counterup', get_template_directory_uri() . '/js/jquery.counterup.js', array( 'jquery' ));

        }

        //if( isset( $maskitto_light['responsive-status'] ) && $maskitto_light['responsive-status'] == 1 ){
            wp_enqueue_style( 'responsive-style', get_template_directory_uri() . '/css/responsive.css' );
        //}

        wp_enqueue_script( 'scripts', get_template_directory_uri() . '/js/scripts.js' );



    if( isset($maskitto_light['body-font']['font-family']) && $maskitto_light['body-font']['font-family'] ) {
        wp_enqueue_style( 'google-fonts', '//fonts.googleapis.com/css?family='.preg_replace("/ /", "+", $maskitto_light['body-font']['font-family']).':300italic,400italic,300,400,600,700' );
    } else {
        wp_enqueue_style( 'google-fonts', '//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,300,400,600,700' );
    }


    }

    add_action( 'wp_enqueue_scripts', 'maskitto_light_enqueue_styles' ); 

endif;


/** 
 * Load admin media files
 */

if( !function_exists('maskitto_light_admin_enqueue_styles') ) :

    function maskitto_light_admin_enqueue_styles() {
        wp_enqueue_style( 'default-admin-style', get_template_directory_uri() . '/css/options.css' );
    }

    add_action( 'admin_enqueue_scripts', 'maskitto_light_admin_enqueue_styles' );

endif;


/**
 * Initialize custom post types if Redux framework activated
 */

if (class_exists('ReduxFramework')) {

    /**
     * Initialize slider columns
     */

    if ( ! function_exists( 'maskitto_light_slider_type_init' ) ) :

        add_filter('manage_edit-slider_columns', 'maskitto_light_add_slider_columns');

        function maskitto_light_add_slider_columns($slider_columns) {
            $new_columns['cb'] = '<input type="checkbox" />';
             
            $new_columns['title'] = __( 'Slide name', 'maskitto-light' );
            $new_columns['caption'] = __( 'Caption', 'maskitto-light' );
            $new_columns['image'] = __( 'Image', 'maskitto-light' );
            $new_columns['author'] = __( 'Author', 'maskitto-light' );
            $new_columns['date'] = __( 'Date', 'maskitto-light' );
         
            return $new_columns;
        }

        add_action('manage_slider_posts_custom_column', 'maskitto_light_manage_slider_columns', 10, 2);
         
        function maskitto_light_manage_slider_columns($column_name, $id) {
            global $wpdb;
            switch ($column_name) {
            case 'caption':
                $caption = get_post_meta( $id, "wpcf-caption", true );

                if($caption)echo $caption;
                break;
            case 'image':
                $image = get_post_meta( $id, 'wpcf-background-image', true );
                if($image)echo '<img src="'.$image.'" alt="" height="85" />';
                break;
            default:
                break;
            }
        }  

    endif;


    /**
     * Initialize service columns
     */

    if ( ! function_exists( 'maskitto_light_services_type_init' ) ) :

        add_filter('manage_edit-services_columns', 'maskitto_light_add_services_columns');

        function maskitto_light_add_services_columns($services_columns) {
            $new_columns['cb'] = '<input type="checkbox" />';
             
            $new_columns['title'] = __( 'Slide name', 'maskitto-light' );
            $new_columns['icon'] = __( 'Icon', 'maskitto-light' );
            $new_columns['author'] = __( 'Author', 'maskitto-light' );
            $new_columns['date'] = __( 'Date', 'maskitto-light' );
         
            return $new_columns;
        }

        add_action('manage_services_posts_custom_column', 'maskitto_light_manage_services_columns', 10, 2);
         
        function maskitto_light_manage_services_columns($column_name, $id) {
            global $wpdb;
            switch ($column_name) {
            case 'icon':
                $icon = get_post_meta( $id, 'wpcf-icon', true );
                if($icon)echo $icon;
                break;
            default:
                break;
            }
        }

    endif;


    /**
     * Initialize portfolio columns
     */

    if ( ! function_exists( 'maskitto_light_projects_type_init' ) ) :

        $count_portfolio = wp_count_posts( 'portfolio-item' );
        if( isset( $count_portfolio->publish ) && $count_portfolio->publish > 0) :
            add_filter('manage_edit-portfolio-item_columns', 'maskitto_light_add_projects_columns');
        else :
            add_filter('manage_edit-portfolio_columns', 'maskitto_light_add_projects_columns');
        endif;

        function maskitto_light_add_projects_columns($projects_columns) {
            $new_columns['cb'] = '<input type="checkbox" />';
             
            $new_columns['title'] = __( 'Slide name', 'maskitto-light' );
            $new_columns['caption'] = __( 'Caption', 'maskitto-light' );
            $new_columns['image'] = __( 'Image', 'maskitto-light');
            $new_columns['author'] = __( 'Author', 'maskitto-light' );
            $new_columns['categories'] = __( 'Categories', 'maskitto-light' );
            $new_columns['date'] = __( 'Date', 'maskitto-light' );
         
            return $new_columns;
        }


        if( isset( $count_portfolio->publish ) && $count_portfolio->publish > 0) :
            add_action('manage_portfolio-item_posts_custom_column', 'maskitto_light_manage_projects_columns', 10, 2);
        else :
            add_action('manage_portfolio_posts_custom_column', 'maskitto_light_manage_projects_columns', 10, 2);
        endif;

         
        function maskitto_light_manage_projects_columns($column_name, $id) {
            global $wpdb;
            switch ($column_name) {
            case 'caption':
                $caption = get_post_meta( $id, 'wpcf-caption', true );
                if($caption)echo $caption;
                break;
            case 'image':
                $image = get_post_meta( $id, 'wpcf-background-image', true );
                if($image)echo '<img src="'.$image.'" alt="" height="85" />';
                break;
            default:
                break;
            }
        } 

    endif;


    /**
     * Initialize partners columns
     */

    if ( ! function_exists( 'maskitto_light_partners_type_init' ) ) :

        add_filter('manage_edit-partners_columns', 'maskitto_light_add_partners_columns');

        function maskitto_light_add_partners_columns($partners_columns) {
            $new_columns['cb'] = '<input type="checkbox" />';
             
            $new_columns['title'] = __( 'Slide name', 'maskitto-light' );
            $new_columns['image'] = __( 'Image', 'maskitto-light' );
            $new_columns['author'] = __( 'Author', 'maskitto-light' );
            $new_columns['date'] = __( 'Date', 'maskitto-light' );
         
            return $new_columns;
        }


        add_action('manage_partners_posts_custom_column', 'maskitto_light_manage_partners_columns', 10, 2);
         
        function maskitto_light_manage_partners_columns($column_name, $id) {
            global $wpdb;
            switch ($column_name) {
            case 'image':
                $image = get_post_meta( $id, 'wpcf-background-image', true );
                if($image)echo '<img src="'.$image.'" alt="" height="85" />';
                break;
            default:
                break;
            }
        }  

    endif;

}


/**
 * Initialize maskitto widgets
 */

require get_template_directory() . "/inc/widgets/sh-slider.php";
add_action('widgets_init',
    create_function('', 'return register_widget("Maskitto_Slider");')
);

require get_template_directory() . "/inc/widgets/sh-services.php";
add_action('widgets_init',
    create_function('', 'return register_widget("Maskitto_Services");')
);

require get_template_directory() . "/inc/widgets/sh-portfolio.php";
add_action('widgets_init',
    create_function('', 'return register_widget("Maskitto_Projects");')
);

require get_template_directory() . "/inc/widgets/sh-partners.php";
add_action('widgets_init',
    create_function('', 'return register_widget("Maskitto_Partners");')
);

require get_template_directory() . "/inc/widgets/sh-blog.php";
add_action('widgets_init',
    create_function('', 'return register_widget("Maskitto_Blog");')
);

require get_template_directory() . "/inc/widgets/sh-include_page.php";
add_action('widgets_init',
     create_function('', 'return register_widget("Maskitto_Include_Other_Page");')
);

require get_template_directory() . "/inc/widgets/sh-counter.php";
add_action('widgets_init',
     create_function('', 'return register_widget("Maskitto_Counter");')
);

/**
 * Initialize maskitto custom login
 */

if(isset($maskitto_light['admin-login-logo']) && isset($maskitto_light['admin-login-logo']['url']) && $maskitto_light['admin-login-logo']['url']){

    function maskitto_light_login_logo() {

        GLOBAL $maskitto_light;

        echo '<style type="text/css">
        h1 a { background-image: url('.$maskitto_light['admin-login-logo']['url'].') !important; background-position: 50% 50%!important; width: 75%!important; background-size: cover!important }
        </style>';
    }
    add_action('login_head', 'maskitto_light_login_logo');

}


/**
 * Remove WordPress.org restricted menu
 */

function maskitto_light_remove_submenu_item() {
    remove_submenu_page( 'themes.php', 'install-required-plugins' );
}
add_action( 'admin_menu', 'maskitto_light_remove_submenu_item', 999 );


/**
 * Adds theme widgets group
 */

function maskitto_light_add_widget_tabs($tabs) {
    $tabs[] = array(
        'title' => __('Theme Widgets', 'maskitto-light'),
        'filter' => array(
            'groups' => 'theme-widgets'
        )
    );

    return $tabs;
}
add_filter('siteorigin_panels_widget_dialog_tabs', 'maskitto_light_add_widget_tabs', 20);