<?php 
/*****************************************************************************************************************************************************
******************************************************************************************************************************************************
 
																	Register Widget Page Builder
 
*/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
add_action( 'widgets_init', 'reza_register_widget_page_builder' );
 function reza_register_widget_page_builder() {
    register_widget( 'reza_widget_page_builder' );
}

class reza_widget_page_builder extends WP_Widget {
 	function __construct() {
		parent::__construct(
			'reza_page_builder',
			 __('rabonews','reza').' - '. esc_html__('Page builder', 'reza') 
		);
	}
public  function option(){
	  $option=array();
	$option[]= array( 
		"name"			=> esc_html__('Select a Page Builder','reza'),
 		"id"			=> "page_builder",
		"type"			=> "select",
 		"options" 		=>	 reza_category_array_options('page_builder'),
  	); 		
 
 
		return $option;
    }	
	
   /********** Update the widget info from the admin panel *******/
 	public function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
  		return	reza_widget_options_save( $new_instance, $old_instance,$this->option() );
 	}
 
	/********** Display the widget update form *******/
 	public function form( $instance ) { 
		$defaults = array( );
		$instance = wp_parse_args( (array) $instance, $defaults );
 
		reza_widget_options($instance,$this->id_base,$this->number,$this->option());
  
 	}	
    /**********  The widget For Display *******/
 	public function widget( $args, $instance ) {
		extract( $args );
  	 
 
		$reza_menu_page_builder = ! empty( $instance[ 'page_builder' ] ) ? $instance[ 'page_builder' ] : '';
		if(!empty($reza_menu_page_builder)){
			$post_name = get_page_by_path($reza_menu_page_builder);
			   if(function_exists( "sao_builder_meta") && !empty($post_name)){
 					$out = sao_section_config($post_name->ID,'output');
					$css = sao_section_config($post_name->ID,'css');
					$script = sao_section_config($post_name->ID,'script');
					if(!empty($out)){
						echo '<div class="rd-widget-page-builder rd-data-builder" data-builder="'.esc_attr($widget_id).'">'.wp_kses_post($out).'</div>';
						sao_enqueue_footer();
 					}
					if(!empty($css)){
						echo '<style>'.$css.'</style>';  	
					}
					
			   }else{
				   $out='';
				   $css='';
				   $script='';
			   }
 		}
 	
	}
 
}