<?php

if (!class_exists('crum_single_page')){

	class crum_single_page extends WP_Widget{

		function __construct(){
			parent::WP_Widget('crum_single_post', 'Box: Single Page', array('description' => 'Single Page'));
		}

		function widget($args, $instance){

			$selected_page = $instance['single_page'];

			echo $before_widget;

			$the_post = get_post($selected_page);

			echo $the_post->post_content;

			echo $after_widget;

		}

		function update ($new_instance, $old_instance){

			$instance = $old_instance;
			$instance['single_page'] = $new_instance['single_page'];

			return $instance;

		}

		function form ($instance){

			$selected_page = $instance['single_page'];

			?>

			<p>
				<label for="<?php echo $this->get_field_id('single_page'); ?>"><?php _e('Select page:', 'crum'); ?></label>
				<select id="<?php echo $this->get_field_id('single_page'); ?>" name="<?php echo $this->get_field_name('single_page'); ?>">
					<?php
					$products_array = new WP_Query( array(
						'post_type'      => 'page',
						'posts_per_page' => - 1,
						'post_status'    => 'publish'
					) );
					while ( $products_array->have_posts() ) : $products_array->the_post();

						$selected = (get_the_ID() == $selected_page) ? 'selected=selected' : '';

						?>
						<option class="widefat" <?php echo $selected;?> value="<?php echo get_the_ID();?>"><?php echo get_the_title();?></option>
					<?php endwhile;
					?>
				</select>
			</p>

		<?php }

	}

}

if ( class_exists('crum_single_page') ) {
	add_action( 'widgets_init', create_function( '', 'register_widget("crum_single_page");' ) );
}