<?php

class Intense_Widget extends Intense_Shortcode {

  function __construct() {
    parent::__construct();

    global $intense_visions_options;

    $this->title = __( 'Widget', 'intense' );
    $this->description = __( 'Adds a widget to the page or post.', 'intense' );
    $this->category = __( 'Elements', 'intense' );
    $this->show_preview = true;
    $this->map_visual_composer = false;
  }

  function shortcode( $atts, $content = null ) {
    global $intense_visions_options, $wp_widget_factory;
    extract( shortcode_atts( $this->get_shortcode_defaults( null, $atts ), $atts ) );   

    $output = '';

    switch ($type) {
      case 'sidebar':
        ob_start();
        if ( function_exists( 'smk_sidebar' ) ) {          
          smk_sidebar( $sidebar );
        } else {
          dynamic_sidebar( $sidebar );  
        }        
        $output = ob_get_contents();
        ob_end_clean();
        break;       
      default:
        if ( !empty( $widget ) ) {
          $widget_factory = $wp_widget_factory->widgets[ $widget ];
          $widget_class = get_class( $widget_factory );
          $widget_instance = new $widget_class( $widget_factory->id_base, $widget_factory->name, $widget_factory->widget_options );
              
          $settings = array();

          if ( !empty( $widget_settings ) ) {      
            $split_settings = explode( '&', $widget_settings );      

            foreach ($split_settings as $key => $setting) {
              $key_value = explode("=", $setting);
              
              $settings[ str_replace( "#038;", "", $key_value[0] ) ] = $key_value[1];
            }
          }    
          
          ob_start();
          the_widget( $widget, $settings, array( 'widget_id' => $id ) );
          $output = ob_get_contents();
          ob_end_clean();
        }
         
        break;
    }     

    return $output;
  }
}
