<?php /**
 * Redux Framework 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.
 *
 * Redux Framework 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 Redux Framework. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package     ReduxFramework
 * @author      Dovy Paukstys (dovy)
 * @version     3.0.0
 */

// Exit if accessed directly
if( !defined( 'ABSPATH' ) ) exit;

// Don't duplicate me!
if( !class_exists( 'ReduxFramework_extension_customizer' ) ) {


    /**
     * Main ReduxFramework customizer extension class
     *
     * @since       1.0.0
     */
    class ReduxFramework_extension_customizer extends ReduxFramework {

      // Protected vars
      protected $redux;
      private $extension_url;
      private $extension_dir;

      /**
       * Class Constructor. Defines the args for the extions class
       *
       * @since       1.0.0
       * @access      public
       * @param       array $sections Panel sections.
       * @param       array $args Class constructor arguments.
       * @param       array $extra_tabs Extra panel tabs.
       * @return      void
       */
      public function __construct( $parent ) {
        global $pagenow;
        if ($pagenow !== "customize.php" &amp;&amp; $pagenow !== "admin-ajax.php") {
          return;
        }

        if ($parent-&gt;args['customizer'] === false) {
          return;
        }
        
        parent::__construct( $parent-&gt;sections, $parent-&gt;args, $parent-&gt;extra_tabs );
      
        // Create defaults array
        $defaults = array();
        /*
          customize_controls_init
          customize_controls_enqueue_scripts
          customize_controls_print_styles
          customize_controls_print_scripts
          customize_controls_print_footer_scripts
        */

        add_action( 'admin_init', array( &amp;$this, '_enqueue' ), 30 ); // Customizer control scripts

        add_action( 'customize_register', array( &amp;$this, '_register_customizer_controls' ) ); // Create controls

        add_action( 'wp_enqueue_scripts', array( &amp;$this, '_enqueue_previewer_css' ) ); // Enqueue previewer css
        add_action( 'wp_enqueue_scripts', array( &amp;$this, '_enqueue_previewer_js' ) ); // Enqueue previewer javascript
        add_action( 'customize_save', array( &amp;$this, 'customizer_save_before' ) ); // Before save
        add_action( 'customize_save_after', array( &amp;$this, 'customizer_save_after' ) ); // After save
        if ( empty( $this-&gt;extension_dir ) ) {
          $this-&gt;extension_dir = trailingslashit( str_replace( '\\', '/', dirname( __FILE__ ) ) );
          $this-&gt;extension_url = site_url( str_replace( trailingslashit( str_replace( '\\', '/', ABSPATH ) ), '', $this-&gt;extension_dir ) );
        }

      }




        // All sections, settings, and controls will be added here
        public function _register_customizer_controls( $wp_customize ) {
          
          
          if ( $this-&gt;args['customizer'] === false ) {
            return;
          }

          $order = array(
            'heading' =&gt; -500,
            'option'  =&gt; -500,
          );
          $defaults = array(
            'default-color'          =&gt; '',
            'default-image'          =&gt; '',
            'wp-head-callback'       =&gt; '',
            'admin-head-callback'    =&gt; '',
            'admin-preview-callback' =&gt; ''
          );

          foreach( $this-&gt;sections as $key =&gt; $section ) {
            if ( empty( $section['fields'] ) ) {
              continue;
            }



            if ( empty( $section['desc'] ) &amp;&amp; !empty( $section['subtitle'] ) ) {
              $section['desc'] = $section['subtitle'];
            }

            if ( !isset( $section['desc'] ) ) {
              $section['desc'] = "";
            }            

            if ( empty( $section['id'] ) ) {
              $section['id'] = strtolower( str_replace( " ", "", $section['title'] ) ); 
            }

            if (empty($section['priority'])) {
                $section['priority'] = $order['heading'];
                $order['heading']++;              
            }

            $wp_customize-&gt;add_section($section['id'], array(
              'title'       =&gt; $section['title'],
              'priority'    =&gt; $section['priority'],
              'description' =&gt; $section['desc']
            ));


            foreach( $section['fields'] as $skey =&gt; $option ) {

              if ( isset( $option['customizer'] ) &amp;&amp; $option['customizer'] === false ) {
                //continue;
              }

              //Change the item priority if not set
              if ( $option['type'] != 'heading' &amp;&amp; !isset( $option['priority'] ) ) {
                $option['priority'] = $order['option'];
                $order['option']++;
              }   

              if ( !empty( $this-&gt;options_defaults[$option['id']] ) ) {
                $option['default'] = $this-&gt;options_defaults['option']['id'];
              }

              if (!isset($option['default'])) {
                $option['default'] = "";
              }
              if (!isset($option['title'])) {
                $option['title'] = "";
              }


              $customSetting = array(
                'type'          =&gt; 'option',
                'capabilities'  =&gt; 'manage_theme_options',
                'default'       =&gt;  $option['default']
              );     


              $option['id'] = $this-&gt;args['opt_name'].'['.$option['id'].']';

              if ($option['type'] != "heading" || !empty($option['type'])) {
                
                $wp_customize-&gt;add_setting( $option['id'], $customSetting);
              }                       

              switch( $option['type'] ) {
                case 'heading':
                  // We don't want to put up the section unless it's used by something visible in the customizer
                  $section        = $option;
                  $section['id']  = strtolower( str_replace( " ", "", $option['title'] ) );
                  $order['heading']=-500;
                  if (!empty( $option['priority'] ) ) {
                    $section['priority'] = $option['priority'];
                  } else {
                    $section['priority'] = $order['heading'];
                    $order['heading']++;          
                  }
                  break;

                case 'text':
                  $wp_customize-&gt;add_control( $option['id'], array(
                    'label'   =&gt; $option['title'],
                    'section' =&gt; $section['id'],
                    'settings'=&gt; $option['id'],
                    'priority'=&gt; $option['priority'],
                    'type'    =&gt; 'text',
                  ) );
                  break;

                case 'select':
                  $wp_customize-&gt;add_control( $option['id'], array(
                    'label'   =&gt; $option['title'],
                    'section' =&gt; $section['id'],
                    'settings'=&gt; $option['id'],
                    'priority'=&gt; $option['priority'],
                    'type'    =&gt; 'select',
                    'choices' =&gt; $option['options']
                  ) );
                  break;

                case 'radio':
                  $wp_customize-&gt;add_control( $option['id'], array(
                    'label'   =&gt; $option['title'],
                    'section' =&gt; $section['id'],
                    'settings'=&gt; $option['id'],
                    'priority'=&gt; $option['priority'],
                    'type'    =&gt; 'radio',
                    'choices' =&gt; $option['options']
                  ) );
                  break;

                case 'checkbox':
                  $wp_customize-&gt;add_control( $option['id'], array(
                    'label'   =&gt; $option['title'],
                    'section' =&gt; $section['id'],
                    'settings'=&gt; $option['id'],
                    'priority'=&gt; $option['priority'],
                    'type'    =&gt; 'checkbox',
                  ) );
                  break;

                case 'media':
                  $wp_customize-&gt;add_control( new WP_Customize_Image_Control( $wp_customize, $option['id'], array(
                    'label'   =&gt; $option['title'],
                    'section' =&gt; $section['id'],
                    'settings'=&gt; $option['id'],
                    'priority'=&gt; $option['priority']
                  ) ) );
                  break;

                case 'color':
                  $wp_customize-&gt;add_control( new WP_Customize_Color_Control( $wp_customize, $option['id'], array(
                    'label'   =&gt; $option['title'],
                    'section' =&gt; $section['id'],
                    'settings'=&gt; $option['id'],
                    'priority'=&gt; $option['priority']
                  ) ) );
                  break;

                case 'switch':
                  $wp_customize-&gt;add_control( $option['id'], array(
                    'label'   =&gt; $option['title'],
                    'section' =&gt; $section['id'],
                    'settings'=&gt; $option['id'],
                    'priority'=&gt; $option['priority'],
                    'type'    =&gt; 'checkbox',
                  ) );
                  break;

                default:
                  break;
              }

            }
          }

          
               

          /*
title_tagline - Site Title &amp; Tagline
colors - Colors
header_image - Header Image
background_image - Background Image
nav - Navigation
static_front_page - Static Front Page
          */


        }

      public function customizer_save_before($wp_customize) {
//echo "there";
  //      print_r($wp_customize);
        //exit();

      }    

      public function customizer_save_after($wp_customize) {
//echo "there";
  //      print_r($wp_customize);
        //exit();

      }              

      /**
       * Enqueue CSS/JS for preview pane
       *
       * @since       1.0.0
       * @access      public
       * @global      $wp_styles
       * @return      void
       */
      public function _enqueue_previewer() {
        wp_enqueue_script( 'redux-extension-previewer-js', $this-&gt;extension_url . 'assets/js/preview.js' );
        $localize = array(
          'save_pending'      =&gt; __( 'You have changes that are not saved. Would you like to save them now?', 'redux-framework' ), 
          'reset_confirm'     =&gt; __( 'Are you sure? Resetting will loose all custom values.', 'redux-framework' ), 
          'preset_confirm'    =&gt; __( 'Your current options will be replaced with the values of this preset. Would you like to proceed?', 'redux-framework' ), 
          'opt_name'          =&gt; $this-&gt;args['opt_name'],
          'folds'       =&gt; $this-&gt;folds,
          'options'     =&gt; $this-&gt;options,
          'defaults'      =&gt; $this-&gt;options_defaults,
        );        
        wp_localize_script( 'redux-extension-previewer-js', 'reduxPost', $localize);
      } 

      /**
       * Enqueue CSS/JS for the customizer controls
       *
       * @since       1.0.0
       * @access      public
       * @global      $wp_styles
       * @return      void
       */
      public function _enqueue() {
        global $wp_styles;

        wp_enqueue_style( 'wp-pointer' );
        wp_enqueue_script( 'wp-pointer' );
        // Remove when code is in place!
        wp_enqueue_script('redux-extension-customizer-js', $this-&gt;extension_url . 'assets/js/customizer.js');
        // Get styles
        wp_enqueue_style('redux-extension-customizer-css', $this-&gt;extension_url . 'assets/css/customizer.css');


        $localize = array(
          'save_pending'      =&gt; __( 'You have changes that are not saved. Would you like to save them now?', 'redux-framework' ), 
          'reset_confirm'     =&gt; __( 'Are you sure? Resetting will loose all custom values.', 'redux-framework' ), 
          'preset_confirm'    =&gt; __( 'Your current options will be replaced with the values of this preset. Would you like to proceed?', 'redux-framework' ), 
          'opt_name'          =&gt; $this-&gt;args['opt_name'],
          'folds'       =&gt; $this-&gt;folds,
          'options'     =&gt; $this-&gt;options,
          'defaults'      =&gt; $this-&gt;options_defaults,
        );       

        // Values used by the javascript
        wp_localize_script(
            'redux-js', 
            'redux_opts', 
            $localize
        );

        do_action( 'redux-enqueue-' . $this-&gt;args['opt_name'] );

        foreach( $this-&gt;sections as $section ) {
          if( isset( $section['fields'] ) ) {
            foreach( $section['fields'] as $field ) {
              if( isset( $field['type'] ) ) {
                $field_class = 'ReduxFramework_' . $field['type'];
                if( !class_exists( $field_class ) ) {
                  $class_file = apply_filters( 'redux-typeclass-load', $this-&gt;path . 'inc/fields/' . $field['type'] . '/field_' . $field['type'] . '.php', $field_class );
                  if( $class_file ) {
                    /** @noinspection PhpIncludeInspection */
                    require_once( $class_file );
                  }
                }
                if( class_exists( $field_class ) &amp;&amp; method_exists( $field_class, 'enqueue' ) ) {
                  $enqueue = new $field_class( '', '', $this );
                  $enqueue-&gt;enqueue();
                }
              }
            }
          }
        }
      }

      /**
       * Register Option for use
       *
       * @since       1.0.0
       * @access      public
       * @return      void
       */
      public function _register_setting() {
  

      }

      /**
       * Validate the Options options before insertion
       *
       * @since       3.0.0
       * @access      public
       * @param       array $plugin_options The options array
       * @return      
       */
      public function _validate_options( $plugin_options ) {

          return $plugin_options;
      }

      /**
       * Validate values from options form (used in settings api validate function)
       * calls the custom validation class for the field so authors can override with custom classes
       *
       * @since       1.0.0
       * @access      public
       * @param       array $plugin_options
       * @param       array $options
       * @return      array $plugin_options
       */
      public function _validate_values( $plugin_options, $options ) {


          return $plugin_options;
      }

      /**
       * HTML OUTPUT.
       *
       * @since       1.0.0
       * @access      public
       * @return      void
       */
      public function _customizer_html_output() {

              
      }

    } // class
} // if
