<?php
add_action( 'elementor/element/before_section_start', function( $element, $section_id, $args ) {

	$type = get_class( $element );

	switch ( $type ) {
		case 'Elementor\Element_Column':
		case 'Elementor\Element_Section':
			$show_before_id = 'section_advanced';
			break;
		default:
			// Section '_section_style' is common to all widgets, registered in Widget_Common::register_controls()
			$show_before_id = '_section_style';
	}

	if ( $show_before_id === $section_id ) {
		ignition_elementor_scheduled_controls( $element );
	}

}, 10, 3 );

function ignition_elementor_scheduled_controls( $element ) {
	$element->start_controls_section(
		'ignition_schedule',
		[
			'tab'   => \Elementor\Controls_Manager::TAB_ADVANCED,
			'label' => __( 'Scheduled visibility', 'ignition-elementor-widgets' ),
		]
	);

	$element->add_control(
		'ignition_enable_schedule_from',
		[
			'label'        => __( 'Visible from:', 'ignition-elementor-widgets' ),
			'type'         => \Elementor\Controls_Manager::SWITCHER,
			'default'      => '',
			'label_on'     => __( 'On', 'ignition-elementor-widgets' ),
			'label_off'    => __( 'Off', 'ignition-elementor-widgets' ),
			'return_value' => 'yes',
			'separator'    => 'none',
		]
	);

	$element->add_control(
		'ignition_schedule_from_date',
		[
			'type'       => \Elementor\Controls_Manager::DATE_TIME,
			'label'      => __( 'From Date/Time', 'ignition-elementor-widgets' ),
			'show_label' => false,
			'default'    => current_time( 'mysql' ),
			'condition'  => [
				'ignition_enable_schedule_from!' => '',
			],
			'separator'  => 'none',
		]
	);

	$element->add_control(
		'ignition_enable_schedule_to',
		[
			'label'        => __( 'Visible until:', 'ignition-elementor-widgets' ),
			'type'         => \Elementor\Controls_Manager::SWITCHER,
			'default'      => '',
			'label_on'     => __( 'On', 'ignition-elementor-widgets' ),
			'label_off'    => __( 'Off', 'ignition-elementor-widgets' ),
			'return_value' => 'yes',
			'separator'    => 'none',
		]
	);

	$element->add_control(
		'ignition_schedule_to_date',
		[
			'type'       => \Elementor\Controls_Manager::DATE_TIME,
			'label'      => __( 'Until Date/Time', 'ignition-elementor-widgets' ),
			'show_label' => false,
			'default'    => current_time( 'mysql' ),
			'condition'  => [
				'ignition_enable_schedule_to!' => '',
			],
			'separator'  => 'none',
		]
	);

	$element->end_controls_section();
}

add_action( 'elementor/frontend/widget/before_render', 'ignition_elementor_schedule_before_render' );
add_action( 'elementor/frontend/section/before_render', 'ignition_elementor_schedule_before_render' );
add_action( 'elementor/frontend/column/before_render', 'ignition_elementor_schedule_before_render' );
function ignition_elementor_schedule_before_render( \Elementor\Element_Base $element ) {
	if ( ! ignition_elementor_scheduled_show_element( $element ) ) {
		ob_start();

		// This isn't really needed, as the whole HTML markup is discarded by ob_end_clean()
		// It's left as a safety net however, in case other plugins fiddle incorrectly the the output buffers.
		$element->add_render_attribute( '_wrapper', [
			'style' => 'display:none',
		] );
	}
}


add_action( 'elementor/frontend/widget/after_render', 'ignition_elementor_schedule_after_render' );
add_action( 'elementor/frontend/section/after_render', 'ignition_elementor_schedule_after_render' );
add_action( 'elementor/frontend/column/after_render', 'ignition_elementor_schedule_after_render' );
function ignition_elementor_schedule_after_render( \Elementor\Element_Base $element ) {
	if ( ! ignition_elementor_scheduled_show_element( $element ) ) {
		ob_end_clean();
	}
}

function ignition_elementor_scheduled_show_element( \Elementor\Element_Base $element ) {
	// Only affect elements where scheduling is enabled.
	if ( 'yes' !== $element->get_settings( 'ignition_enable_schedule_from' ) && 'yes' !== $element->get_settings( 'ignition_enable_schedule_to' ) ) {
		return true;
	}

	$from = 'yes' === $element->get_settings( 'ignition_enable_schedule_from' ) ? $element->get_settings( 'ignition_schedule_from_date' ) : '';
	$to   = 'yes' === $element->get_settings( 'ignition_enable_schedule_to' ) ? $element->get_settings( 'ignition_schedule_to_date' ) : '';

	$now     = current_time( 'timestamp' );
	$from_dt = ! empty( $from ) ? strtotime( $from ) : false;
	$to_dt   = ! empty( $to ) ? strtotime( $to ) : false;

	$show = false;

	if ( ! empty( $from_dt ) && ! empty( $to_dt ) ) {
		if ( $now >= $from_dt && $now <= $to_dt ) {
			$show = true;
		} else {
			$show = false;
		}
	} elseif ( ! empty( $from_dt ) && $now >= $from_dt ) {
		$show = true;
	} elseif ( ! empty( $to_dt ) && $now <= $to_dt ) {
		$show = true;
	} else {
		$show = false;
	}

	return $show;
}
