<?php

/**
 *
 * @link              https://sitespot.dev
 * @package           Bt_Pro
 *
 * @wordpress-plugin
 * Plugin Name:       Beaver Team Z-Index Add On for Beaver Builder
 * Plugin URI:        https://sitespot.dev/resource/beaver-builder-z-index/
 * Description:       Adds a z-index setting to the advanced tab of rows, modules and columns.
 * Version:           1.0.1
 * Author:            SiteSpot Dev
 * Author URI:        https://sitespot.dev
 * License:           GPL-2.0+
 * License URI:       https://www.gnu.org/licenses/gpl-3.0.en.html
 * Text Domain:       bt-pro
 * Domain Path:       /languages

  Beaver Team Z-Index Add On for Beaver Builder is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 2 of the License, or
  any later version.
   
  Beaver Team Z-Index Add On for Beaver Builder is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.
   
  You should have received a copy of the GNU General Public License
  along with Beaver Team Z-Index Add On for Beaver Builder. If not, see https://www.gnu.org/licenses/gpl-3.0.en.html.

 */



require 'plugin-update-checker/plugin-update-checker.php';
$myUpdateChecker = Puc_v4_Factory::buildUpdateChecker(
  'https://sitespot.dev/wp-content/uploads/bt-updates/Beaver Builder z-index.json',
  __FILE__,
  'bt-z-index'
);


if(!class_exists('BtZIndex')){
  
  class BtZIndex {
      /** 
       * Constructor function for the module. You must pass the
       * name, description, dir and url in an array to the parent class.
       *
       * @method __construct
       */  

      function __construct(){

        //filters
        add_filter( 'fl_builder_register_settings_form' , array($this,'add_z_settings'), 20 , 2 );
        add_filter( 'fl_builder_render_css',array($this,'bt_zindex'), 10,3 );
      }


      function bt_zindex($css, $nodes, $global_settings ){

        $ncss = '';

        foreach($nodes['rows'] as $row)
        {
          if(isset($row->settings->zindex) && $row->settings->zindex !== '')
          {
            $node = $row->node;
            $zindex = $row->settings->zindex;
            $ncss .= "body:not(.fl-builder-edit) .fl-node-$node{z-index:$zindex; position:relative;}";
          }
        }

        foreach($nodes['columns'] as $row)
        {
          if(isset($row->settings->zindex) && $row->settings->zindex !== '')
          {
            $node = $row->node;
            $zindex = $row->settings->zindex;
            $ncss .= "body:not(.fl-builder-edit) .fl-node-$node{z-index:$zindex; position:relative;}";
          }
        }
        foreach($nodes['modules'] as $row)
        {
          if(isset($row->settings->zindex) && $row->settings->zindex !== '')
          {
            $node = $row->node;
            $zindex = $row->settings->zindex;
            $ncss .= "body:not(.fl-builder-edit) .fl-node-$node{z-index:$zindex; position:relative;}";
          }
        }

        return $css .$ncss;
      }



      function add_z_settings( $form, $slug ) {

        if ( 'col' === $slug || 'row' === $slug ) {

          $form[ 'tabs' ][ 'advanced' ][ 'sections' ][ 'visibility' ][ 'fields' ][ 'zindex' ] = [
            'type'        => 'unit',
            'label'       => 'z-index',
          ];
        }
        
        if('module_advanced' === $slug)
        {
          $form[ 'sections' ][ 'visibility' ][ 'fields' ][ 'zindex' ] = [
            'type'        => 'unit',
            'label'       => 'z-index',
          ];
        }
        return $form;
      }

    }
    $btzi = new BtZIndex();
}
