<?php
namespace ERROPiX\AIOW;

// don't load directly
if ( ! defined( 'ABSPATH' ) ) die( '-1' );

/**
 * The Widget
*/
class All_In_One_Widget extends \WP_Widget {
	
	static $defaults = array(
		'post_id' => '0',
		'show_title' => '1',
		'title' => '',
		'alt_title' => '',
	);
	
	function __construct() {
		parent::__construct('erropix-all-in-one', 'All In One Widget');
	}
	
	// Widget output
	function widget( $args, $instance ) {
		global $post;
		$instance = wp_parse_args($instance, self::$defaults);
		
		extract( $args );
		extract( $instance );
		
		if( !$post_id )
			return;
		
		$post = Post::get_post( $post_id );
		if( $post && $post->post_type == Post::type ) {
			$output = $before_widget;
			
			if( $show_title ) {
				if( $alt_title ) {
					$title = $alt_title;
				} else {
					$title = $post->post_title;
				}
				$title = apply_filters('widget_title', $title);
				if ( $title ) {
					$output.= $before_title.$title.$after_title;
				}
			}
			
			$content = apply_filters('the_content', $post->post_content);
			$output.= $content;
			
			$css_id = "vc_custom_css_$post_id";
			if( !CSS::exists($css_id) ) {
				$custom_css = Post::get_meta($post_id, '_wpb_post_custom_css');
				$custom_css.= Post::get_meta($post_id, '_wpb_shortcodes_custom_css');
				CSS::add( $custom_css, $css_id );
			}
			
			$output.= $after_widget;
			echo $output;
		}
		wp_reset_postdata();
	}
	
	// Output admin widget options form
	function form( $instance ) {
		$instance = wp_parse_args($instance, self::$defaults);
		$title = '';
		
		// Field: widget content
		$label = "Widget Content";
		$key = 'post_id';
		$id = $this->get_field_id( $key );
		$name = $this->get_field_name( $key );
		$value = $instance[$key];
		$options = '<option></option>';
		
		$posts = get_posts(array(
			'posts_per_page'	=> -1,
			'post_type'			=> Post::type,
			'orderby'			=> 'post_title',
			'order'				=> 'ASC',
		));
		
		foreach ( $posts as $p ) {
			if( !$p->post_title ) continue;
			if( $value == $p->ID ) {
				$sel = ' selected';
				$title = $p->post_title;
				$label.= ' - <a href="'.get_edit_post_link($p->ID).'">Edit</a>';
			} else {
				$sel = '';
			}
			$options.= sprintf( '<option value="%s"%s>%s</option>', $p->ID, $sel, $p->post_title );
		}
		
		printf('
			<p>
				<label for="%1$s">%2$s</label>
				<select id="%1$s" name="%3$s" class="widefat">%4$s</select>
			</p>
			', $id, $label, $name, $options
		);
		
		
		// Field: widget title
		$key = 'title';
		$id = $this->get_field_id( $key );
		$name = $this->get_field_name( $key );
		
		printf('
			<p>
				<input type="hidden" id="%1$s" name="%2$s" value="%3$s"/>
			</p>
			', $id, $name, $title
		);
		
		
		// Field: Alternative widget title
		$label = "Alternative Widget Title";
		$key = 'alt_title';
		$id = $this->get_field_id( $key );
		$name = $this->get_field_name( $key );
		$value = $instance[$key];
		
		printf('
			<p>
				<label for="%1$s">%2$s</label>
				<input type="text" id="%1$s" name="%3$s" value="%4$s" class="widefat"/>
			</p>
			', $id, $label, $name, $value
		);
		
		
		// Field: show widget title
		$label = "show Widget Title";
		$key = 'show_title';
		$id = $this->get_field_id( $key );
		$name = $this->get_field_name( $key );
		$value = $instance[$key];
		$checked = $value=='1' ? ' checked':'';
		
		printf('
			<p>
				<input name="%3$s" type="hidden" value="0"/>
				<input id="%1$s" name="%3$s" type="checkbox" value="1"%4$s/>
				<label for="%1$s">%2$s</label>
			</p>
			', $id, $label, $name, $checked
		);
	}
}