<?php
/**
 * Registers a widget to show a page section on another page.
 *
 * @package Organic Widgets Pro
 */

// Block direct requests.
if ( ! defined( 'ABSPATH' ) ) {
	die( '-1' );
}

/**
 * Adds Organic_Widgets_Pro_Subpage_Widget widget.
 */
class Organic_Widgets_Pro_Subpage_Widget extends Organic_Widgets_Pro_Custom_Widget {

	const CUSTOM_IMAGE_SIZE_SLUG = 'organic_widgets_image_upload';

	/**
	 * Register widget with WordPress.
	 */
	function __construct() {
		parent::__construct(
			'organic_widgets_subpage_section', // Base ID.
			__( 'Organic Subpage', 'organic-widgets-pro' ), // Name.
			array(
				'description' => __( 'A subpage\'s content displayed as a section of another page.', 'organic-widgets-pro' ),
				'customize_selective_refresh' => true,
			)
		);

		$this->id_prefix = $this->get_field_id( '' );

		// BG options.
		$this->bg_options = array(
			'color'     => true,
			'image'     => true,
			'bg_fixed'  => true,
			'bg_repeat' => true,
			'video'     => true,
		);

		// Font options.
		$this->font_options = array(
			'title_heading'          => false,
			'title_font'             => true,
			'title_color'            => false,
			'title_weight'           => true,
			'title_spacing'          => true,
			'title_size'             => true,
			'content_heading_font'   => false,
			'content_heading_size'   => false,
			'content_heading_weight' => false,
			'body_font'              => true,
			'text_alignment'         => false,
		);

		// Layout options.
		$this->layout_options = array(
			'content_aligner'     => false,
			'content_orientation' => false,
			'padding_top'         => true,
			'padding_bottom'      => true,
			'custom_height'       => true,
			'custom_width'        => true,
			'content_width'       => true,
			'full_height'         => true,
		);

		// Admin Scripts.
		add_action( 'admin_enqueue_scripts', array( $this, 'admin_setup' ) );

		// Public scripts.
		add_action( 'customize_preview_init', array( $this, 'public_scripts' ) );

	}
	/**
	 * Front-end display of widget.
	 *
	 * @see WP_Widget::widget()
	 *
	 * @param array $args     Widget arguments.
	 * @param array $instance Saved values from database.
	 */
	public function widget( $args, $instance ) {

		$bg_image_id = isset( $instance['bg_image_id'] ) ? $instance['bg_image_id'] : false;
		$bg_image    = ( isset( $instance['bg_image'] ) && '' !== $instance['bg_image'] ) ? $instance['bg_image'] : false;
		$bg_color    = ( isset( $instance['bg_color'] ) && '' !== $instance['bg_color'] ) ? $instance['bg_color'] : false;
		$bg_video    = ( isset( $instance['bg_video'] ) && $instance['bg_video'] ) ? $instance['bg_video'] : false;
		if ( isset( $instance['full_window_height'] ) ) {
			$full_window_height = $instance['full_window_height'];
		} else {
			$full_window_height = false;
		}
		if ( isset( $instance['bg_image_fixed'] ) ) {
			$bg_image_fixed = $instance['bg_image_fixed'];
		} else {
			$bg_image_fixed = false;
		}
		if ( isset( $instance['bg_image_repeat'] ) ) {
			$bg_image_repeat = $instance['bg_image_repeat'];
		} else {
			$bg_image_repeat = false;
		}
		if ( isset( $instance['text_align'] ) ) {
			$text_align = $instance['text_align'];
		} else {
			$text_align = 'center';
		}

		echo $args['before_widget'];

		?>

		<?php echo '<style media="screen">'; ?>
			<?php if ( ! empty( $instance['widget_custom_width'] ) ) { ?>
				#<?php echo esc_attr( $this->id ); ?> {
					width: <?php echo absint( $instance['widget_custom_width'] ); ?>%;
				}
			<?php } ?>
			<?php if ( ! empty( $instance['content_max_width'] ) ) { ?>
				#<?php echo esc_attr( $this->id ); ?> .organic-widgets-content {
					max-width: <?php echo absint( $instance['content_max_width'] ); ?>px;
				}
			<?php } ?>
			<?php if ( ! empty( $instance['widget_custom_height'] ) || '' !== $instance['widget_padding_top'] || '' !== $instance['widget_padding_bottom'] ) { ?>
				#<?php echo esc_attr( $this->id ); ?> .organic-widgets-subpage-section {
					<?php if ( ! empty( $instance['widget_custom_height'] ) ) { ?>
					min-height: <?php echo absint( $instance['widget_custom_height'] ); ?>px;
					<?php } ?>
					<?php if ( '' !== $instance['widget_padding_top'] ) { ?>
					padding-top: <?php echo intval( $instance['widget_padding_top'] ); ?>px;
					<?php } ?>
					<?php if ( '' !== $instance['widget_padding_bottom'] ) { ?>
					padding-bottom: <?php echo intval( $instance['widget_padding_bottom'] ); ?>px;
					<?php } ?>
				}
			<?php } ?>
			<?php if ( ! empty( $instance['title_font_family'] ) || ! empty( $instance['title_font_weight'] ) || ! empty( $instance['title_font_size'] ) || ! empty( $instance['title_letter_spacing'] ) || ! empty( $instance['title_text_color'] ) ) { ?>
				#<?php echo esc_attr( $this->id ); ?> h1, #<?php echo esc_attr( $this->id ); ?> h2,
				#<?php echo esc_attr( $this->id ); ?> h3, #<?php echo esc_attr( $this->id ); ?> h4,
				#<?php echo esc_attr( $this->id ); ?> h5, #<?php echo esc_attr( $this->id ); ?> h6 {
					<?php if ( ! empty( $instance['title_font_family'] ) ) { ?>
					font-family: <?php echo esc_html( $instance['title_font_family'] ); ?>;
					<?php } ?>
					<?php if ( ! empty( $instance['title_font_weight'] ) ) { ?>
					font-weight: <?php echo esc_html( $instance['title_font_weight'] ); ?>;
					<?php } ?>
					<?php if ( ! empty( $instance['title_font_size'] ) ) { ?>
					font-size: <?php echo esc_html( $instance['title_font_size'] ); ?>px;
					<?php } ?>
					<?php if ( ! empty( $instance['title_letter_spacing'] ) ) { ?>
					letter-spacing: <?php echo esc_html( $instance['title_letter_spacing'] ); ?>px;
					<?php } ?>
				}
			<?php } ?>
			<?php if ( ! empty( $instance['body_font_family'] ) ) { ?>
				#<?php echo esc_attr( $this->id ); ?> .organic-widgets-content p {
					font-family: <?php echo esc_html( $instance['body_font_family'] ); ?>;
				}
			<?php } ?>
		<?php echo '</style>'; ?>

		<?php if ( ! empty( $instance['page_id'] ) ) { ?>

		<?php /** BEGIN .organic-widgets-section */ ?>
		<div <?php if ( ! empty( $instance['widget_custom_anchor'] ) ) { ?>id="<?php echo esc_html( $instance['widget_custom_anchor'] ); ?>"<?php } ?>
		class="organic-widgets-section organic-widgets-subpage-section organic-widgets-video-bg-section
		<?php if ( $bg_image_fixed ) echo 'organic-widgets-fixed-bg-img'; ?>
		<?php if ( $bg_image_repeat ) echo 'organic-widgets-repeat-bg-img'; ?>
		<?php if ( $full_window_height ) echo 'organic-widgets-full-height-section'; ?>
		<?php if ( ! empty( $instance['widget_custom_class'] ) ) echo esc_html( $instance['widget_custom_class'] ); ?>"
		<?php if ( 0 < $bg_image_id ) { ?>
			style="background-image:url(<?php echo esc_url( $bg_image ); ?>);"
		<?php } if ( $bg_color ) { ?>
			style="background-color:<?php echo esc_attr( $bg_color ); ?>;"
		<?php } ?>>

			<?php
			// Video Background Section.
			if ( $bg_video ) {

				// Prep arguments.
				$video_info = array(
					'video'           => $bg_video,
					'video_type'      => $this->get_video_type( $bg_video ),
					'video_id'        => $this->youtube_id_from_url( $bg_video ),
					'widget_id'       => $this->id,
					'clean_widget_id' => $this->sanitize_js_variable( $this->id ),
				);

				// Add video bg to global var.
				$this->add_video_bg( $video_info );

				// Output video HTML.
				$this->video_bg_html( $video_info );

			}

			// Get Page Info.
			$page_id        = $instance['page_id'];
			$featured_image = get_the_post_thumbnail_url( $page_id, 'organic-widgets-featured-large' );

			$page_query = new WP_Query(array(
				'post_type'      => 'page',
				'page_id'        => $page_id,
				'posts_per_page' => 1,
			) );

			if ( $page_query->have_posts() ) : while ( $page_query->have_posts() ) : $page_query->the_post(); ?>

				<?php /** BEGIN .organic-widgets-content */ ?>
				<div class="organic-widgets-content <?php if ( $text_align ) echo 'organic-widgets-text-'?><?php echo esc_attr( $text_align ); ?>">

					<?php the_content( esc_html__( 'Read More', 'organic-widgets-pro' ) ); ?>

				<?php /** END .organic-widgets-content */ ?>
				</div>

			<?php /** END .organic-widgets-section */ ?>
			</div>

					<?php

			endwhile;
			endif;
			wp_reset_postdata();

			if ( is_active_widget( false, false, $this->id_base, true ) ) {
				wp_enqueue_script( 'yt-player-api', 'https://www.youtube.com/player_api', array(), '1.0', true );
			}

			echo $args['after_widget'];

		}

	}

	/**
	 * Back-end widget form.
	 *
	 * @see WP_Widget::form()
	 *
	 * @param array $instance Previously saved values from database.
	 */
	public function form( $instance ) {

		$this->id_prefix = $this->get_field_id( '' );

		if ( isset( $instance['page_id'] ) ) {
			$page_id = $instance['page_id'];
		} else {
			$page_id = 0;
		}

		?>

		<p><b><?php _e( 'Select Existing Page:', 'organic-widgets-pro' ) ?></b></p>

		<p>
			<?php
			wp_dropdown_pages( array(
				'class'             => 'widefat',
				'selected'          => absint( $page_id ),
				'id'                => $this->get_field_id( 'page_id' ),
				'name'              => $this->get_field_name( 'page_id' ),
				'show_option_none'  => __( '— Select Existing Page —', 'organic-widgets-pro' ),
				'option_none_value' => '0',
			) );
			?>
		</p>

		<br />

		<?php $this->section_font_input_markup( $instance, $this->font_options ); ?>

		<?php $this->section_layout_input_markup( $instance, $this->layout_options ); ?>

		<?php $this->section_background_input_markup( $instance, $this->bg_options ); ?>

		<p class="organic-widgets-input">
			<label for="<?php echo $this->get_field_id( 'widget_custom_class' ); ?>"><?php _e( 'Custom Widget Class', 'organic-widgets-pro' ); ?></label>
			<input class="widefat" type="text" id="<?php echo $this->get_field_id( 'widget_custom_class' ); ?>" name="<?php echo $this->get_field_name( 'widget_custom_class' ); ?>" value="<?php if ( ! empty( $instance['widget_custom_class'] ) ) echo $instance['widget_custom_class']; ?>" />
		</p>

		<p class="organic-widgets-input">
			<label for="<?php echo $this->get_field_id( 'widget_custom_anchor' ); ?>"><?php _e( 'Custom Widget Anchor', 'organic-widgets-pro' ); ?></label>
			<input class="widefat" type="text" id="<?php echo $this->get_field_id( 'widget_custom_anchor' ); ?>" name="<?php echo $this->get_field_name( 'widget_custom_anchor' ); ?>" value="<?php if ( ! empty( $instance['widget_custom_anchor'] ) ) echo $instance['widget_custom_anchor']; ?>" />
		</p>

		<?php

	}

	/**
	 * Sanitize widget form values as they are saved.
	 *
	 * @see WP_Widget::update()
	 *
	 * @param array $new_instance Values just sent to be saved.
	 * @param array $old_instance Previously saved values from database.
	 *
	 * @return array Updated safe values to be saved.
	 */
	public function update( $new_instance, $old_instance ) {

		$instance = $old_instance;

		if ( ! isset( $old_instance['created'] ) ) {
			$instance['created'] = time();
		}

		/*--- Unique Options ----*/
		if ( isset( $new_instance['page_id'] ) && $new_instance['page_id'] > 0 ) {
			$instance['page_id'] = wp_strip_all_tags( $new_instance['page_id'] );
		}
		// Widget Title.
		if ( isset( $new_instance['page_id'] ) && $new_instance['page_id'] > 0 ) {
			$instance['title'] = wp_strip_all_tags( get_the_title( $instance['page_id'] ) );
		}
		/*--- END Unique Options ----*/

		/*--- Background ----*/
		if ( isset( $new_instance['bg_image_id'] ) ) {
			$instance['bg_image_id'] = wp_strip_all_tags( $new_instance['bg_image_id'] );
		}
		if ( isset( $new_instance['bg_image'] ) ) {
			$instance['bg_image'] = wp_strip_all_tags( $new_instance['bg_image'] );
		}
		if ( isset( $new_instance['bg_image_fixed'] ) ) {
			$instance['bg_image_fixed'] = true;
		} else {
			$instance['bg_image_fixed'] = false;
		}
		if ( isset( $new_instance['bg_image_repeat'] ) ) {
			$instance['bg_image_repeat'] = true;
		} else {
			$instance['bg_image_repeat'] = false;
		}
		if ( isset( $new_instance['bg_video'] ) && $this->check_video_url( $new_instance['bg_video'] ) ) {
			$instance['bg_video'] = wp_strip_all_tags( $new_instance['bg_video'] );
		} else {
			$instance['bg_video'] = false;
		}
		if ( isset( $new_instance['bg_color'] ) && $this->check_hex_color( $new_instance['bg_color'] ) ) {
			$instance['bg_color'] = wp_strip_all_tags( $new_instance['bg_color'] );
		} else {
			$instance['bg_color'] = false;
		}
		/*--- END Background ----*/

		/*--- Font Options ----*/
		if ( isset( $new_instance['title_font_weight'] ) ) {
			$instance['title_font_weight'] = wp_strip_all_tags( $new_instance['title_font_weight'] );
		}
		if ( isset( $new_instance['title_font_size'] ) ) {
			$instance['title_font_size'] = wp_strip_all_tags( $new_instance['title_font_size'] );
		}
		if ( isset( $new_instance['title_letter_spacing'] ) ) {
			$instance['title_letter_spacing'] = wp_strip_all_tags( $new_instance['title_letter_spacing'] );
		}
		if ( isset( $new_instance['title_font_family'] ) ) {
			$instance['title_font_family'] = wp_strip_all_tags( $new_instance['title_font_family'] );
		}
		if ( isset( $new_instance['body_font_family'] ) ) {
			$instance['body_font_family'] = wp_strip_all_tags( $new_instance['body_font_family'] );
		}
		/*--- END Font Options ----*/

		/*--- Layout ----*/
		if ( isset( $new_instance['widget_padding_top'] ) ) {
			$instance['widget_padding_top'] = wp_strip_all_tags( $new_instance['widget_padding_top'] );
		}
		if ( isset( $new_instance['widget_padding_bottom'] ) ) {
			$instance['widget_padding_bottom'] = wp_strip_all_tags( $new_instance['widget_padding_bottom'] );
		}
		if ( isset( $new_instance['widget_custom_height'] ) ) {
			$instance['widget_custom_height'] = wp_strip_all_tags( $new_instance['widget_custom_height'] );
		}
		if ( isset( $new_instance['widget_custom_width'] ) ) {
			$instance['widget_custom_width'] = wp_strip_all_tags( $new_instance['widget_custom_width'] );
		}
		if ( isset( $new_instance['content_max_width'] ) ) {
			$instance['content_max_width'] = wp_strip_all_tags( $new_instance['content_max_width'] );
		}
		if ( isset( $new_instance['widget_custom_class'] ) ) {
			$instance['widget_custom_class'] = wp_strip_all_tags( $new_instance['widget_custom_class'] );
		}
		if ( isset( $new_instance['widget_custom_anchor'] ) ) {
			$instance['widget_custom_anchor'] = wp_strip_all_tags( $new_instance['widget_custom_anchor'] );
		}
		if ( isset( $new_instance['full_window_height'] ) ) {
			$instance['full_window_height'] = true;
		} else {
			$instance['full_window_height'] = false;
		}
		/*--- END Layout ----*/

		return $instance;
	}

	/**
	 * Enqueue all the javascript.
	 */
	public function admin_setup() {

		wp_enqueue_media();

		wp_enqueue_style( 'wp-color-picker' );
		wp_enqueue_script( 'wp-color-picker' );
		wp_enqueue_script( 'organic-widgets-module-color-picker', ORGANIC_WIDGETS_PRO_ADMIN_JS_DIR . 'organic-widgets-module-color-picker.js', array( 'jquery', 'wp-color-picker' ) );

		wp_enqueue_script( 'organic-widgets-module-image-background', ORGANIC_WIDGETS_PRO_ADMIN_JS_DIR . 'organic-widgets-module-image-background.js', array( 'jquery', 'media-upload', 'media-views', 'wp-color-picker' ) );
		wp_localize_script( 'organic-widgets-module-image-background', 'OrganicWidgetBG', array(
			'frame_title'  => __( 'Select an Image', 'organic-widgets-pro' ),
			'button_title' => __( 'Insert Into Widget', 'organic-widgets-pro' ),
		) );

	}

	/**
	 * Enqueue public javascript.
	 */
	public function public_scripts() {
		if ( ! wp_script_is('organic-widgets-backgroundimagebrightness-js') ) { wp_enqueue_script( 'organic-widgets-backgroundimagebrightness-js', ORGANIC_WIDGETS_PRO_BASE_DIR . 'public/js/jquery.backgroundbrightness.js', array( 'jquery' ) ); }
	}

} // class Organic_Widgets_Pro_Subpage_Widget
