<?php
/**
 * Functions for getting values in templates
 */
if ( ! defined( 'ABSPATH' ) ) {
	return;
}

/**
 * Creating the gallery
 *
 * @param  string $gallery_images Image IDs separated by comma.
 * @param  string $size           Image Size.
 */
function the_giveasap_gallery( $gallery_images, $size = 'full' ) {
	if ( $gallery_images ) {

		$gallery_images_array = explode( ',', $gallery_images );
		$content              = '<ul class="giveasap_gallery">';
		foreach ( $gallery_images_array as $image_id ) {

			$image = wp_get_attachment_image_src( $image_id, $size );
			$html  = '';
			if ( $image ) {
				$html = '<img src="' . esc_url_raw( $image[0] ) . '" />';
			}

			if ( '' === $html ) {
				continue;
			}

			$content .= '<li>';

			$content .= $html;

			$content .= '</li>';
		}

		$content .= '</ul>';
		echo $content;

	}
}

/**
 * Applying only the core WordPress the_content hooks for rendering content
 *
 * @param  boolean $echo If false, the the content will be returned.
 *
 * @return mixed         Void if $echo true, string if $echo is false
 */
function giveasap_the_content( $echo = true, $post = null, $raw = false ) {
	$content = $raw && $post ? $post->post_content : get_the_content( '', false, $post );
	$content = apply_filters( 'giveasap_the_content', $content );
	if ( $echo ) {
		echo wp_kses_post( $content );
	} else {
		return $content;
	}
}

/**
 * Returning the first image of the gallery
 *
 * @param  string $gallery_images String of IDs separated by a comma.
 *
 * @return string                 URL of the image
 */
function the_giveasap_first_gallery_image( $gallery_images ) {
	if ( $gallery_images ) {

		$gallery_images_array = explode( ',', $gallery_images );
		$first_image          = $gallery_images_array[0];
		$image                = wp_get_attachment_image_src( $first_image, 'full' );

		return $image[0];
	}
}

/**
 * Getting the URL of the image
 *
 * @param  int    $image_id ID of the image.
 * @param  string $size     Size of the image.
 *
 * @return string            URL of the image
 */
function giveasap_get_image_url( $image_id, $size = 'full' ) {
	if ( ! is_numeric( $image_id ) ) {
		return false;
	}

	$image = wp_get_attachment_image_src( $image_id, $size );

	return $image[0];
}

/**
 * Displays an alert if a giveaway has ended
 *
 * @return void
 */
function giveasap_show_ended_alert() {
	echo '<div class="giveasap_alert giveasap_ended">';
	esc_html_e( 'Ended', 'giveasap' );
	echo '</div>';
}

/**
 * Show Prize instructions if current user is a winner.
 *
 * @param GiveASAP_Front $front
 */
function giveasap_show_prize_instructions( $front ) {
	$giveaway       = $front->get_giveaway();
	$prize_settings = $giveaway->get_prize_settings();

	if ( ! isset( $prize_settings['show_instructions'] ) ) {
		return;
	}

	if ( ! absint( $prize_settings['show_instructions'] ) ) {
		return;
	}

	$subscriber   = $front->user;
	$winners      = giveasap_get_winners( $giveaway->get_id() );
	$instructions = array();

    if ( ! $winners ) {
        return;
    }

	foreach ( $winners as $winner ) {
		$subscriber_id = absint( $winner['id'] );
		if ( $subscriber_id !== absint( $subscriber->id ) ) {
			continue;
		}

		if ( empty( $winner['prizes'] ) || ! $winner['prizes'] ) {
			continue;
		}

		$prizes = $winner['prizes'];
		foreach ( $prizes as $prize ) {
			if ( empty( $prize['instructions'] ) ) {
				continue;
			}

			$instructions[] = array(
				'title' => $prize['title'],
				'text'  => $prize['instructions'],
			);
		}
	}

	if ( ! $instructions ) {
		return;
	}

	?>
	<h3 class="giveasap-prize-instructions-title"><?php esc_html_e( 'Your Prize Instructions', 'giveasap' ); ?></h3>
	<?php
	foreach ( $instructions as $instruction ) {
		?>
		<div class="giveasap-prize-instructions sg-won-prize">
			<h3 class="sg-giveaway-prize-title"><?php echo esc_html( $instruction['title'] ); ?></h3>
			<p class="sg-giveaway-prize-text"><?php echo wp_kses_post( $instruction['text'] ); ?></p>
		</div>
		<?php
	}

}

/**
 * Display Winners for a giveaway
 *
 * @since 2.9.0
 *
 * @param integer $post_id Post ID. If empty, use global.
 * @param boolean $force If forced, it will still display the winners even if disabled in settings.
 */
function giveasap_show_winners( $post_id = 0, $force = false ) {
	if ( ! $post_id ) {
		global $post;
		$post_id = $post->ID;
	}

	$giveaway_status = get_post_status( $post_id );

	if ( 'giveasap_notified' === $giveaway_status || 'giveasap_winners' === $giveaway_status ) {
		$settings = giveasap_get_settings();
		if ( ! $force ) {
			$display_winners = isset( $settings['display_winners'] ) ? $settings['display_winners'] : '0';
			if ( '1' !== $display_winners ) {
				return false;
			}
		}
		$winners     = giveasap_get_winners( $post_id );
		$show_points = isset( $settings['display_winners_points'] ) ? $settings['display_winners_points'] : '0';

		if ( $winners ) {
			$fields        = giveasap_get_form_fields( $post_id );
			$winner_fields = array();

			foreach ( $fields as $field_key => $field ) {
				if ( isset( $field['winner'] ) && '1' === $field['winner'] ) {
					$winner_fields[] = $field_key;
				}
			}
			echo '<div class="giveasap_alert giveasap_winners">';
			echo '<p>' . esc_html( _n( 'Winner', 'Winners', count( $winners ), 'giveasap' ) ) . '</p>';
			echo '<ul>';
			foreach ( $winners as $winner ) {

				$name = '';
				if ( $winner_fields ) {
					$_name = array();
					foreach ( $winner_fields as $winner_field ) {

						if ( 'user_email' === $winner_field ) {
							$field_value = $winner['email'];
							$field_value = giveasap_mask_email( $field_value );
						} else {
							$field_value = giveasap_get_meta( $winner['id'], '_' . $winner_field, true );
						}

						if ( ! $field_value ) {
							continue;
						}

						$_name[] = $field_value;
					}

					if ( $_name ) {
						$_name = array_values( $_name );
						$name  = implode( ', ', $_name );
					}
				}

				// If we stil don't have a name.
				if ( ! $name ) {
					$user_id = giveasap_get_meta( $winner['id'], 'user_id', true );
					$f_name  = giveasap_get_meta( $winner['id'], '_user_name', true );
					$name    = $f_name ? $f_name : $winner['email'];

					if ( ! $f_name ) {
						// Let's mask the email.
						$name = giveasap_mask_email( $name );
					}

					if ( $user_id ) {
						$user = get_userdata( $user_id );
						$name = $user->display_name ? $user->display_name : $name;
					}
				}

				if ( '1' === $show_points ) {
					$subscriber = giveasap_get_user_by_id( $winner['id'] );
					if ( $subscriber ) {
						$name .= ' (' . $subscriber->entries . ')';
					}
				}

				$name = apply_filters( 'sg_show_winner_name', $name, $winner );

				echo '<li>' . esc_html( $name ) . '</li>';
			}
			echo '</ul>';
			echo '</div>';
		}
	}
}

/**
 * Displaying the Logo with the link
 *
 * @since 2.9.0
 *
 * @param array $settings Display Settings.
 */
function giveasap_show_logo( $settings ) {
	if ( isset( $settings['logo'] ) && $settings['logo'] ) {
		$image_url = giveasap_get_image_url( $settings['logo'] );
		$logo_link = isset( $settings['logo_link'] ) ? $settings['logo_link'] : '';

		echo '<div class="giveasap_logo_container">';

		if ( $logo_link ) {
			if ( false === strpos( $logo_link, 'http' ) ) {
				$logo_link = 'http://' . $logo_link;
			}
			echo '<a target="_blank" href="' . esc_url( $logo_link ) . '">';
		}

		echo '<img class="giveasap_logo" src="' . esc_url( $image_url ) . '" title="Logo" />';

		if ( $logo_link ) {
			echo '</a>';
		}

		echo '</div>';
	}
}

/**
 * Edit Form for a Subscriber.
 *
 * @param GiveASAP_Front    $giveasap_front
 * @param $giveasap_settings
 */
function giveasap_subscriber_edit_form( $giveasap_front ) {
	$giveaway   = $giveasap_front->get_giveaway();
	$subscriber = $giveasap_front->get_subscriber();
	$settings   = giveasap_get_settings();
	$fields     = giveasap_get_form_fields( $giveaway->get_id() );
	$hideForm   = true;

	if ( $fields ) {
		foreach ( $fields as $field_name => $field ) {
			if ( 'user_email' === $field['name'] ) {
				$field['value']    = $subscriber ? $subscriber->get_email() : '';
				$field['disabled'] = 1;
			} else {
				$field['value'] = $subscriber ? $subscriber->get_meta( '_' . $field['name'] ) : '';
			}
			if (
				isset( $field['required'] )
				&& (bool) $field['required']
				&& ( null === $field['value'] || '' === $field['value'] )
			) {
				$hideForm = false;
				$giveasap_front->add_error( 'field-required', sprintf( __( 'Field %s is required' ), $field['label'] ) );
			}
			$fields[ $field_name ] = $field;
		}
	}

	$allow_edit = isset( $settings['allow_subscriber_edit'] ) ? absint( $settings['allow_subscriber_edit'] ) : 0;
	// If we don't allow to edit form and the we have all required fields. We don't show it.
	if ( $hideForm && ! $allow_edit ) {
		return;
	}

	?>
	<div class="sg-subscriber-edit-data">
		<p>
			<?php echo esc_html( sprintf( __( 'Subscribed as: %s', 'giveasap' ), $subscriber->get_email() ) ); ?>.
			<?php if ( $hideForm ) { ?>
				<button type="button"
						class="sg-button-link sg-toggle"
						data-text="<?php esc_attr_e( 'Hide Details', 'giveasap' ); ?>"
						data-target="#giveasap_subscribe_form"
						data-class="sg-hidden">
					<?php esc_html_e( 'Edit Details', 'giveasap' ); ?>
				</button>
			<?php } ?>
		</p>
		<?php
		$giveasap_front->show_successes();
		$giveasap_front->show_errors();
		$giveasap_front->show_notices();
		?>
		<form id="giveasap_subscribe_form" class="giveasap_form <?php echo $hideForm ? 'sg-hidden' : ''; ?>" method="post">
		<input type="hidden" name="sg_giveaway_id" value="<?php echo esc_attr( $giveaway->get_id() ); ?>" />
		<input type="hidden" name="sg_giveaway_subscriber" value="<?php echo esc_attr( $subscriber->get_id() ); ?>" />
		<?php

		if ( $fields ) {
			foreach ( $fields as $field ) {
				giveasap_render_form_field( $field );
			}
		}

			do_action( 'sg_form_edit_before_button', $giveasap_front );
			wp_nonce_field( 'sg_form_edit_' . $subscriber->get_id(), 'sg_form_edit_nonce' );
		?>
		<button type="submit" class="giveasap_button"
			name="giveasap_edit">
			<?php esc_html_e( 'Update', 'giveasap' ); ?>
		</button>

	</form>
	</div>
	<?php
}

/**
 * Displaying the Form
 *
 * @param  GiveASAP_Front $giveasap_front
 * @param  array          $giveasap_settings
 *
 * @return void
 */
function giveasap_the_form( $giveasap_front, $giveasap_settings ) {
	$giveaway = $giveasap_front->get_giveaway();

	if ( ! $giveaway->showing_subscriber_page() || absint( $giveasap_front->get_step() ) === 1 ) {

		if ( $giveaway->has_started() ) {
			/**
			 * @since  2.5.0
			 */
			do_action( 'giveasap_form_before', $giveasap_front, $giveasap_settings );

			if ( $giveaway->is_open_to_subscribe() ) {
				?>
				<form enctype="multipart/form-data" id="giveasap_subscribe_form" class="giveasap_form" action="<?php echo esc_url( $giveasap_front->get_form_link() ); ?>" method="post">
					<input type="hidden" name="sg_giveaway_id" value="<?php echo esc_attr( $giveaway->get_id() ); ?>" />
					<input type="hidden" name="sg_giveaway_type" value="<?php echo esc_attr( $giveaway->get_type() ); ?>" />
					<?php
					if ( $giveasap_front->get_from_page() ) {
						?>
						<input type="hidden" name="sg_from_page" value="<?php echo esc_attr( $giveasap_front->get_from_page() ); ?>" />
						<?php
					}
					?>
					<?php if ( absint( $giveasap_front->shareID ) !== 0 ) { ?>
						<input type="hidden" name="user_share" value="<?php echo esc_attr( absint( $giveasap_front->shareID ) ); ?>"/>
						<?php
					}

					if ( isset( $_GET['method'] ) && \Simple_Giveaways\Helpers::unslash_and_clean( $_GET['method'] ) ) {
						?>
						<input type="hidden" name="sg_sharing_method" value="<?php echo esc_attr( \Simple_Giveaways\Helpers::unslash_and_clean( $_GET['method'] ) ); ?>"/>
						<?php
					}

                    if ( isset( $_GET['method'] ) && \Simple_Giveaways\Helpers::unslash_and_clean( $_GET['method'] ) ) {
                        ?>
                        <input type="hidden" name="sg_sharing_method" value="<?php echo esc_attr( \Simple_Giveaways\Helpers::unslash_and_clean( $_GET['method'] ) ); ?>"/>
                        <?php
                    }

					$giveasap_front->show_errors();
					$giveasap_front->show_notices();
					$giveasap_front->show_successes();

					/**
					 * Checking if a user is already logged in to use their email list
					 *
					 * @since  2.5.0
					 */
					$current_user = wp_get_current_user();

					/**
					 * @since  2.8.0
					 */
					do_action( 'giveasap_form_before_default', $giveasap_front, $giveasap_settings, $current_user );

					$fields = giveasap_get_form_fields( $giveaway->get_id() );

					if ( $fields ) {
						foreach ( $fields as $field ) {
							if ( $current_user->ID ) {
								if ( 'user_name' === $field['name'] ) {
									$field['value'] = $current_user->get( 'first_name' );
								} else {
									$field['value'] = $current_user->get( $field['name'] );
								}

								if ( 'user_email' === $field['name'] ) {
									$field['disabled'] = true;
								}
							}
							giveasap_render_form_field( $field );
						}
					}

					do_action( 'giveasap_form_after_form_fields', $giveasap_front, $giveasap_settings, $current_user );

					if ( ! $current_user->ID ) {
						if ( $giveasap_settings['google_site_key'] ) {
							?>
							<div class="g-recaptcha"
								 data-sitekey="<?php echo esc_attr( $giveasap_settings['google_site_key'] ); ?>"></div>
							 <?php
						}
					}

					do_action( 'sg_form_before_button', $giveasap_front, $giveasap_settings, $current_user );

					if ( giveasap_is_gdpr_active() ) {
						?>
						<label for="giveasap_consent" class="giveasap_consent">
							<input type="checkbox" required="required" value="1" name="giveasap_consent"
								   id="giveasap_consent">
							<?php
							$consent_text = isset( $giveasap_settings['gdpr_consent'] ) ? $giveasap_settings['gdpr_consent'] : __( 'By signing up, you are agreeing to Terms & Conditions and our Privacy Policy.', 'giveasap' );
							echo wp_kses(
								$consent_text,
								array(
									'a' => array(
										'href'   => true,
										'target' => true,
									),
								),
								''
							);
							?>

						</label>
					<?php } ?>
					<button type="submit" class="giveasap_button"
							name="giveasap_submit"><?php esc_html_e( 'Enter', 'giveasap' ); ?></button>
					<?php
					if ( is_singular( 'giveasap' ) ) {
						?>
						<button type="button" class="giveasap_button transparent center"
								id="giveasap_request_link"><?php esc_html_e( 'Forgot your subscribed link?', 'giveasap' ); ?></button>
						<?php
					}
					?>
				</form>

				<?php
			} else {
				$message = $giveaway->get_not_open_reason();
				if ( $message ) {
					echo '<div class="giveasap_form">';
					echo '<p class="giveasap_error">' . esc_html( $message ) . '</p>';
					echo '</div>';
				}
			}

			if ( is_singular( 'giveasap' ) ) {
				?>
				<form id="giveasap_request_link_form" class="giveasap_form hidden" action="" method="post">
					<div class="sg-form-field">
						<label for="sg_request_email"><?php esc_html_e( 'Email', 'giveasap' ); ?></label>
						<input required="required" class="giveasap_input" id="sg_request_email" type="email" name="request_email" value=""
							   placeholder="<?php esc_attr_e( 'Enter your email', 'giveasap' ); ?>"/>
					</div>
					<button type="submit" class="giveasap_button"
							name="giveasap_request_link"><?php esc_html_e( 'Get Link', 'giveasap' ); ?></button>

				</form>
				<?php
			}

			/**
			 * @since  2.5.0
			 */
			do_action( 'giveasap_form_after', $giveasap_front, $giveasap_settings );
		} else {
			echo '<div class="giveasap_form">';
			echo '<p>' . esc_html__( 'Giveaway has not started yet.', 'giveasap' ) . '</p>';
			echo '</div>';
		}
	}

	if ( $giveaway->showing_subscriber_page() && absint( $giveasap_front->get_step() ) === 2 ) {

		giveasap_subscriber_edit_form( $giveasap_front );

		if ( $giveaway->is_displaying_entries() ) {
			?>
			<p class="giveasap-entries">
				<?php esc_html_e( 'Your Current Entries:', 'giveasap' ); ?>
				<strong><?php $giveasap_front->current_entries(); ?></strong>
			</p>
			<?php
		}

		if ( giveasap_is_activation_required() ) {
			if ( 'pending' === $giveasap_front->user->status ) {
				echo '<div class="giveasap_form">';
				echo '<p class="giveasap_error">' . esc_html__( 'You have to activate your Account to have a chance in winning. The link should be in the email.', 'giveasap' ) . '</p>';
				echo '</div>';
			}

			if ( 'active' === $giveasap_front->user->status && isset( $_GET['activate'] ) && \Simple_Giveaways\Helpers::unslash_and_clean( $_GET['activate'] ) ) {
				echo '<div class="giveasap_form">';
				echo '<p class="giveasap_success">' . esc_html__( 'Account Activated.', 'giveasap' ) . '</p>';
				echo '</div>';
			}
		}

		do_action( 'giveasap_the_form_end', $giveasap_front, $giveasap_settings );
	}
}

add_action( 'giveasap_the_form_end', 'giveasap_display_sharing_methods', 10 );

/**
 * Display Sharing Methods
 *
 * was sg_display_sharing_methods
 *
 * @param GiveASAP_Front $giveasap_front Front object.
 *
 * @since 2.32.0
 */
function giveasap_display_sharing_methods( $giveasap_front ) {
	$giveaway = $giveasap_front->get_giveaway();
	if ( $giveaway->is_collecting_entries() ) {
		$giveasap_front->render_sharing_methods();
	}
}

add_action( 'giveasap_form_before', 'giveasap_form_before_conditionals', 20, 2 );

/**
 * Showing conditionals before the form
 *
 * @since  2.5.0
 *
 * @param  GiveASAP_Front $giveasap_front Front object.
 * @param  array          $giveasap_settings Array of settings.
 *
 * @return void
 */
function giveasap_form_before_conditionals( $giveasap_front, $giveasap_settings ) {

	$conditions = isset( $giveasap_settings['text_conditions'] ) ? $giveasap_settings['text_conditions'] : false;

	if ( $conditions ) {
		$count = giveasap_get_subscribed_count( $giveasap_front->get_giveaway_id() );

		$after = isset( $conditions[0] ) ? $conditions[0] : false;

		if ( $after && isset( $after['text'] ) && '' !== $after['text'] ) {
			$after_count = intval( $after['number'] );

			if ( $count >= $after_count ) {
				$text = str_replace( '{{count}}', $count, $after['text'] );

				if ( $text ) {
					echo '<p class="giveasap-p">' . esc_html( $text ) . '</p>';
				}

				return;
			}
		}

		do_action( 'giveasap_form_before_conditionals', $conditions, $count );

	}
}

/**
 * Compatibility wrapper.
 *
 * @param array $options
 */
function sg_show_meta_info( $options = array() ) {
	giveasap_show_meta_info( $options );
}

/**
 * Display the Simple Giveaway Meta Info
 * was sg_show_meta_info
 *
 * @since 2.12.0
 *
 * @param array $options Array of meta info.
 */
function giveasap_show_meta_info( $options = array() ) {
	if ( ( isset( $options['winners'] ) && $options['winners'] ) || ( isset( $options['value'] ) && $options['value'] ) ) {
		?>
		<ul class="giveasap_meta">
			<?php
			if ( isset( $options['winners'] ) && $options['winners'] ) {
				?>
				<li class="person">
					<span class="fal fa-user-times"></span>
					<?php
					// translators: %s - number of winners.
					echo sprintf( _n( '%s <small>winner</small>', '%s <small>winners</small>', absint( $options['winners'] ), 'giveasap' ), number_format_i18n( absint( $options['winners'] ) ) );
					?>
				</li>
			<?php } ?>
			<?php
			if ( isset( $options['value'] ) && $options['value'] ) {
				?>
				<li class="value">
					<span class="fal fa-money-bill"></span>
					<small><?php esc_html_e( 'Value:', 'giveasap' ); ?></small>
					<?php echo esc_html( $options['value'] ); ?>
				</li>
			<?php } ?>
		</ul>
		<?php
	}
	if ( isset( $options['prize'] ) && '' !== $options['prize'] ) {
		?>

		<ul class="giveasap_meta">
			<li class="value">
				<span class="fal fa-gift"></span>
				<small><?php esc_html_e( 'Prize:', 'giveasap' ); ?></small>
				<?php echo esc_html( $options['prize'] ); ?>
			</li>
		</ul>
		<?php
	}
}

add_action( 'sg_box_header', 'giveasap_header', 20, 2 );

/**
 * Displaying the header
 *
 * was sg_header
 *
 * @param SG_Giveaway    $giveaway Giveaway object.
 * @param GiveASAP_Front $front Front object.
 */
function giveasap_header( $giveaway, $front ) {
	$step = $front->get_step();

	$content         = giveasap_the_content( false, $giveaway->get_post(), true );
	$content         = get_extended( $content );
	$use_hide_button = apply_filters( 'sg_use_content_hide_button', '' !== $content['extended'] );
	?>
	<header class="giveasap_section 
	<?php
	if ( ! $use_hide_button ) {
		echo esc_attr( 'shown' ); }
	?>
	">
		<h1><?php the_title(); ?></h1>
		<?php echo wp_kses_post( $content['main'] ); ?>
		<?php
		if ( $use_hide_button && 2 === $step ) {
			echo '<div class="sg-content-extended">';
			echo wp_kses_post( $content['extended'] );
			echo '</div>';
			?>
				<button
						type="button"
						class="sg-button sg-toggle"
						data-target="header.giveasap_section"
						data-class="shown"
						data-text="<?php esc_attr_e( 'Hide the content', 'giveasap' ); ?>">
				<?php esc_html_e( 'Show the content', 'giveasap' ); ?>
				</button>
				<?php
		} else {
			echo wp_kses_post( $content['extended'] );
		}
		?>
	</header>
	<?php
}



add_action( 'sg_box_header', 'giveasap_header_show_prizes', 20 );

/**
 * Display the prizes
 *
 * was sg_header_show_prizes
 *
 * @param SG_Giveaway $giveaway
 */
function giveasap_header_show_prizes( $giveaway ) {
	$prize_settings = $giveaway->get_prize_settings();
	$prizes         = isset( $prize_settings['prizes'] ) ? $prize_settings['prizes'] : array();
	$prizes_layout  = isset( $prize_settings['prizes_layout'] ) ? $prize_settings['prizes_layout'] : 'slider';

	if ( ! $prizes ) {
		return;
	}

	?>
	<div class="sg-giveaway-prizes sg-giveaway-prizes-<?php echo esc_attr( $prizes_layout ); ?>">
		<?php
		foreach ( $prizes as $prize ) {
			?>
			<div class="sg-giveaway-prize">
				<h3 class="sg-giveaway-prize-title"><?php echo esc_html( $prize['title'] ); ?></h3>
				<?php if ( isset( $prize['value'] ) && $prize['value'] ) { ?>
				<p class="sg-giveaway-prize-text sg-giveaway-prize-value"><strong><?php echo esc_html( $prize['value'] ); ?></strong></p>
				<?php } ?>
				<?php if ( isset( $prize['desc'] ) && $prize['desc'] ) { ?>
				<p class="sg-giveaway-prize-text"><?php echo esc_html( $prize['desc'] ); ?></p>
				<?php } ?>
				<?php
				if ( $prize['image'] ) {
					echo wp_get_attachment_image( $prize['image'], 'large' );
				}
				?>
			</div>
			<?php
		}
		?>
	</div>
	<?php
}


add_action( 'sg_box_header', 'giveasap_header_show_message_per_type', 22, 2 );

/**
 * Show a message per type under the header
 *
 * was sg_header_show_message_per_type
 *
 * @param $giveaway
 * @param $front
 */
function giveasap_header_show_message_per_type( $giveaway, $front ) {
	if ( $giveaway->get_header_message( $front ) ) {
		echo '<div class="giveasap_header_message giveasap_notice">' . esc_html( $giveaway->get_header_message( $front ) ) . '</div>';
	}
}

add_filter( 'giveasap_render_form_field_atts', 'giveasap_form_field_to_hidden_for_logged_in', 20, 2 );

/**
 * Transforming form fields for hidden that can be filled with user data.
 *
 * was sg_form_field_to_hidden_for_logged_in
 *
 * @since 2.16.0
 *
 * @param array $atts
 * @param array $field
 */
function giveasap_form_field_to_hidden_for_logged_in( $atts, $field ) {
	if ( isset( $field['slug'] ) && 'user_email' === $field['slug'] ) {
		$current_user = wp_get_current_user();
		if ( $current_user->ID ) {
			$atts['type']  = 'hidden';
			$atts['value'] = $current_user->get( 'user_email' );
		}
	}

	if ( isset( $field['slug'] ) && 'user_name' === $field['slug'] ) {
		$current_user = wp_get_current_user();
		if ( $current_user->ID ) {
			$atts['type']  = 'hidden';
			$atts['value'] = $current_user->get( 'user_firstname' );
		}
	}

	return $atts;
}

/**
 * Return default form fields.
 *
 * was sg_get_default_form_fields
 *
 * @return array
 */
function giveasap_get_default_form_fields() {
	$fields = array(
		array(
			'label'    => __( 'Email', 'giveasap' ),
			'name'     => 'user_email',
			'required' => '1',
			'readonly' => true,
            'type'     => 'email'
		),
	);

	return apply_filters( 'giveasap_default_form_fields', $fields );
}
/**
 * Return Form Fields.
 *
 * was sg_get_form_fields
 *
 * @since 2.16.0
 * @since 2.19.0 - Added Giveaway ID parameter.
 *
 * @param integer $giveaway_id Giveaway ID.
 *
 * @return array
 */
function giveasap_get_form_fields( $giveaway_id = 0 ) {
	$settings    = giveasap_get_settings();
	$form_fields = isset( $settings['form_fields'] ) ? $settings['form_fields'] : giveasap_get_default_form_fields();

	/**
	 * Filtering forms fields so others can extend it.
	 *
	 * @since 2.19.0 - Passing $giveaway_id to the filter.
	 */
	$form_fields = apply_filters( 'giveasap_form_fields', $form_fields, $giveaway_id );

	$fields_with_key = array();

	// We create an array of fields with keys as their slugs.
	foreach ( $form_fields as $index => $field ) {
		if ( 'winner' === $index ) {
			$fields_with_key['winner'] = $field;
		} else {
			if ( ! $field['name'] ) {
				$field['name'] = sanitize_title( $field['label'] );
			}
			$fields_with_key[ $field['name'] ] = $field;
		}
	}

	return $fields_with_key;
}

/**
 * Rendering a single field.
 *
 * Was sg_render_form_field
 *
 * @since 2.16.0
 *
 * @param array $field Field Configuration.
 *
 * @return mixed Void if echo is true. Otherwise the field HTML or null.
 */
function giveasap_render_form_field( $field, $echo = true ) {
	$label   = isset( $field['label'] ) ? esc_html( $field['label'] ) : '';
	$name    = isset( $field['name'] ) ? $field['name'] : '';
	$value   = isset( $field['value'] ) ? $field['value'] : '';
	$type    = isset( $field['type'] ) ? $field['type'] : 'text';
	$options = isset( $field['options'] ) ? $field['options'] : array();

	if ( ! $label ) {
		return ''; }

	if ( ! $name ) {
		return ''; }

	if ( ( 'select' === $type || 'radio' === $type ) && ! $options ) {
		$type = 'text';
	}

	$required = isset( $field['required'] ) ? (bool) $field['required'] : false;
	$disabled = isset( $field['disabled'] ) ? (bool) $field['disabled'] : false;
	$id       = isset( $field['id'] ) ? $field['id'] : 'sg_field_' . $name;

	$atts = array(
		'class'       => 'giveasap_input',
		'placeholder' => sprintf( __( 'Enter %s', 'giveasap' ), $label ),
		'name'        => 'sg_form[' . $name . ']',
		'value'       => $value,
		'type'        => $type,
		'id'          => $id,
	);
	if ( $required ) {
		$atts['required'] = 'required';
	}

	if ( $disabled ) {
		$atts['disabled'] = 'disabled';
	}
    if ( 'image' === $type ) {
        $atts['type'] = 'file';
        $atts['name'] = 'sg_form_' . $name;
        $atts['accept'] = 'image/png, image/jpeg, image/jpg';
    }

	$atts = apply_filters( 'giveasap_render_form_field_atts', $atts, $field );

	if ( 'checkbox' === $atts['type'] && ! $atts['value'] ) {
		$atts['value'] = 1;
	}

	$html_atts = array();
	foreach ( $atts as $key => $att ) {
		$html_atts[] = $key . '="' . esc_attr( $att ) . '"';
	}

	switch ( $atts['type'] ) {
		case 'select':
			$field_html = '<select ' . implode( ' ', $html_atts ) . '>';
			foreach ( $options as $option ) {
				if ( '' === trim( $option ) ) {
					continue;
				}
				$field_html .= '<option value="' . esc_attr( $option ) . '">' . esc_html( $option ) . '</option>';
			}
			$field_html .= '</select>';
			break;
		case 'radio':
			$field_html = '';
			foreach ( $options as $index => $option ) {
				if ( '' === trim( $option ) ) {
					continue; }
				$radio_atts          = $atts;
				$radio_atts['id']    = $atts['id'] . '_' . $index;
				$radio_atts['value'] = $option;
				$html_atts           = array();
				foreach ( $radio_atts as $key => $att ) {
					$html_atts[] = $key . '="' . esc_attr( $att ) . '"';
				}
				if ( 0 === $index ) {
					$html_atts[] = 'checked';
				}
				$field_html .= '<div class="sg-form-field-radio-option"><input ' . implode( ' ', $html_atts ) . '/><label for="' . esc_attr( $radio_atts['id'] ) . '">' . esc_html( $option ) . '</label></div>';
			}
			break;
		default:
            $field_html = '<input ' . implode( ' ', $html_atts ) . '/>';
            if ( 'image' === $type && $atts['value'] && absint( $atts['value'] ) > 0 ) {
                $field_html = wp_kses_post( wp_get_attachment_image( absint( $atts['value'] ) ) ) . $field_html;
            }

			break;
	}

	$outer_html = '<div class="sg-form-field sg-form-field-' . esc_attr( $atts['type'] ) . '"><label for="' . esc_attr( $id ) . '">' . $label . '</label>%s</div>';

	// If hidden, we will only render the field.
	if ( 'hidden' === $atts['type'] ) {
		$outer_html = '%s';
	}

	if ( 'checkbox' === $atts['type'] ) {
		$outer_html = '<div class="sg-form-field sg-form-field-' . $atts['type'] . '">%s<label for="' . esc_attr( $id ) . '">' . esc_html( $label ) . '</label></div>';
	}

	$html = sprintf( $outer_html, $field_html );
	if ( $echo ) {
		echo $html;
	} else {
		return $html;
	}
}

/**
 * Get the current page URL.
 *
 * was: sg_get_current_page_url
 *
 * @since 2.20.0
 *
 * @return string
 */
function giveasap_get_current_page_url() {
	global $wp;
	return add_query_arg( \Simple_Giveaways\Helpers::unslash_and_clean( $_GET ), home_url( $wp->request ) );
}

add_action( 'giveasap_box_footer', 'giveasap_box_footer', 20, 3 );

/**
 * Show the Footer
 *
 * was sg_box_footer
 *
 * @param SG_Giveaway    $giveaway Giveaway.
 * @param GiveASAP_Front $giveasap_front Front.
 * @param array          $giveasap_settings Settings.
 */
function giveasap_box_footer( $giveaway, $giveasap_front, $giveasap_settings ) {
	$ga_schedule       = $giveaway->get_schedule();
	$rules_button_text = isset( $giveasap_settings['rules_button'] ) ? $giveasap_settings['rules_button'] : __( 'Show Rules', 'giveasap' );
	$schedule          = $giveaway->get_schedule_settings();
	$show_timezone     = ! isset( $schedule['sg_hide_timezone'] ) || 1 !== absint( $schedule['sg_hide_timezone'] );
	$winner_timestamp  = $ga_schedule->get_original_time( 'winner' );
	$end_timestamp     = $ga_schedule->get_original_time( 'end' );

	?>
	<footer class="giveasap_section">

		<?php
		if ( $winner_timestamp || $end_timestamp ) :
		?>
		<ul class="giveasap_meta items-2 row">
			<?php if ( $end_timestamp ) : ?>
			<li class="time icon-aside">
				<span class="fal fa-2x fa-fw fa-calendar-alt"></span>
				<div class="meta-content">
					<small><?php esc_html_e( 'Ends:', 'giveasap' ); ?></small>
					<?php giveasap_the_formatted_datetime( $end_timestamp, $show_timezone ); ?>
				</div>
			</li>
			<?php endif; ?>
			<?php if ( $winner_timestamp ) : ?>
			<li class="time icon-aside">
				<span class="fal fa-2x fa-fw fa-calendar-alt"></span>
				<div class="meta-content">
					<small><?php esc_html_e( 'Winner Announcement:', 'giveasap' ); ?></small>
					<?php giveasap_the_formatted_datetime( $winner_timestamp, $show_timezone ); ?>
				</div>
			</li>
			<?php endif; ?>
		</ul>
		<?php endif; ?>

		<?php
			$rules_text = giveasap_get_rules_text( $giveaway );
			if ( $rules_text ) :
			?>
			<div class="giveasap_rules">
				<h2 class="giveasap_heading"><span class="fal fa-exclamation-circle"></span> <?php esc_html_e( 'Rules', 'giveasap' ); ?></h2>
				<?php

				$rules_text = get_extended( $rules_text );
				echo wp_kses_post( $rules_text['main'] );

				if ( '' !== $rules_text['extended'] ) {
					echo '<button id="giveasap_show_rules" class="giveasap_button secondary">' . esc_html( $rules_button_text ) . '</button>';
					?>
					<div class="giveasap_rules_extended">
						<?php echo wp_kses_post( $rules_text['extended'] ); ?>
					</div>
				<?php } ?>

			</div>
			<?php
			endif;

		if ( giveasap_is_powered_by_enabled() ) {
			?>
			<a target="_blank" href="https://wordpress.org/plugins/giveasap/" class="giveasap_button-secondary">
				<?php esc_html_e( 'Powered by Simple Giveaways', 'giveasap' ); ?>
			</a>
			<?php
		}
		?>
	</footer>
	<?php
}

add_action( 'giveasap_box_shortcode_footer', 'giveasap_box_shortcode_footer', 20, 4 );

/**
 * Show the Shortcode Footer
 *
 * was sg_box_shortcode_footer
 *
 * @param SG_Giveaway    $giveaway Giveaway.
 * @param array          $shortcode_atts Array.
 * @param GiveASAP_Front $giveasap_front Front.
 * @param array          $giveasap_settings Settings.
 */
function giveasap_box_shortcode_footer( $giveaway, $shortcode_atts, $giveasap_front, $giveasap_settings ) {

	if ( ! giveasap_is_powered_by_enabled() && ! absint( $shortcode_atts['rules'] ) ) {
		return;
	}
	?>
	<footer class="giveasap_section">
		<?php if ( absint( $shortcode_atts['rules'] ) ) { ?>
		<div class="giveasap_rules">
			<h2 data-class="active" data-target="#sg_rules_extended_<?php echo esc_attr( $giveaway->get_id() ); ?>" class="giveasap_heading sg-toggle"><span class="fal fa-exclamation-circle"></span> <?php esc_html_e( 'Rules', 'giveasap' ); ?></h2>
		</div>
			<?php
		}

		if ( giveasap_is_powered_by_enabled() ) {
			?>
		<a aria-label="<?php esc_attr_e( 'Powered by Simple Giveaways', 'giveasap' ); ?>" target="_blank" href="https://wordpress.org/plugins/giveasap/" class="sg-powered-by giveasap_button-secondary">
			<?php include trailingslashit( GASAP_ROOT ) . 'assets/images/logo-color.svg'; ?>
		</a>
			<?php
		}

		if ( absint( $shortcode_atts['rules'] ) ) {
			?>
		<div class="giveasap_rules sg-rules-text">

			<div id="sg_rules_extended_<?php echo esc_attr( $giveaway->get_id() ); ?>" class="giveasap_rules_extended">
				<?php
				$rules_text = giveasap_get_rules_text( $giveaway );
				echo wp_kses_post( $rules_text );
				?>
			</div>

		</div>
			<?php
		}
		?>

	</footer>
	<?php
}

/**
 * Get templates in public folder
 *
 * @param string $template_name Template name.
 * @param array  $args          Arguments. (default: array).
 * @param string $template_path Template path. (default: '').
 * @param string $default_path  Default path. (default: '').
 *
 * Copied from WooCommerce
 */
function giveasap_get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
	$cache_key = sanitize_key( implode( '-', array( 'template', $template_name, $template_path, $default_path, gasap()->version() ) ) );
	$template  = (string) wp_cache_get( $cache_key, 'simple-giveaways' );

	if ( ! $template ) {
		$template = giveasap_locate_template( $template_name, $template_path, $default_path );

		// Don't cache the absolute path so that it can be shared between web servers with different paths.
		$cache_path = giveasap_tokenize_path( $template, giveasap_get_path_define_tokens() );

		giveasap_set_template_cache( $cache_key, $cache_path );
	} else {
		// Make sure that the absolute path to the template is resolved.
		$template = giveasap_untokenize_path( $template, giveasap_get_path_define_tokens() );
	}

	// Allow 3rd party plugin filter template file from their plugin.
	$filter_template = apply_filters( 'giveasap_get_template', $template, $template_name, $args, $template_path, $default_path );

	if ( $filter_template !== $template ) {
		if ( ! file_exists( $filter_template ) ) {
			/* translators: %s template */
			_doing_it_wrong( __FUNCTION__, sprintf( __( '%s does not exist.', 'giveasap' ), '<code>' . $template . '</code>' ), '2.33.0' );
			return;
		}
		$template = $filter_template;
	}

	$action_args = array(
		'template_name' => $template_name,
		'template_path' => $template_path,
		'located'       => $template,
		'args'          => $args,
	);

	if ( ! empty( $args ) && is_array( $args ) ) {
		extract( $args ); // @codingStandardsIgnoreLine
	}

	do_action( 'sg_before_template_part', $action_args['template_name'], $action_args['template_path'], $action_args['located'], $action_args['args'] );

	include $action_args['located'];

	do_action( 'sg_after_template_part', $action_args['template_name'], $action_args['template_path'], $action_args['located'], $action_args['args'] );
}

/**
 * Locate a template and return the path for inclusion.
 *
 * This is the load order:
 *
 * yourtheme/$template_path/$template_name
 * yourtheme/$template_name
 * $default_path/$template_name
 *
 * Copied from WooCommerce
 *
 * @param string $template_name Template name.
 * @param string $template_path Template path. (default: '').
 * @param string $default_path  Default path. (default: '').
 * @return string
 */
function giveasap_locate_template( $template_name, $template_path = '', $default_path = '' ) {
	if ( ! $template_path ) {
		$template_path = apply_filters( 'sg_template_path', 'giveaways/' );
	}

	if ( ! $default_path ) {
		$default_path = untrailingslashit( GASAP_ROOT ) . '/public/';
	}

	// Look within passed path within the theme - this is priority.
	$template = locate_template(
		array(
			trailingslashit( $template_path ) . $template_name,
			$template_name,
		)
	);

	// Get default template/.
	if ( ! $template || ( defined( 'SG_TEMPLATE_DEBUG_MODE' ) && SG_TEMPLATE_DEBUG_MODE ) ) {
		$template = $default_path . $template_name;
	}

	// Return what we found.
	return apply_filters( 'giveasap_locate_template', $template, $template_name, $template_path );
}

/**
 * Given a path, this will convert any of the subpaths into their corresponding tokens.
 * Copied from WooCommerce
 *
 * @param string $path The absolute path to tokenize.
 * @param array  $path_tokens An array keyed with the token, containing paths that should be replaced.
 * @return string The tokenized path.
 */
function giveasap_tokenize_path( $path, $path_tokens ) {
	// Order most to least specific so that the token can encompass as much of the path as possible.
	uasort(
		$path_tokens,
		function ( $a, $b ) {
			$a = strlen( $a );
			$b = strlen( $b );

			if ( $a > $b ) {
				return -1;
			}

			if ( $b > $a ) {
				return 1;
			}

			return 0;
		}
	);

	foreach ( $path_tokens as $token => $token_path ) {
		if ( 0 !== strpos( $path, $token_path ) ) {
			continue;
		}

		$path = str_replace( $token_path, '{{' . $token . '}}', $path );
	}

	return $path;
}

/**
 * Fetches an array containing all of the configurable path constants to be used in tokenization.
 * Copied from WooCommerce
 *
 * @return array The key is the define and the path is the constant.
 */
function giveasap_get_path_define_tokens() {
	$defines = array(
		'ABSPATH',
		'WP_CONTENT_DIR',
		'WP_PLUGIN_DIR',
		'WPMU_PLUGIN_DIR',
		'PLUGINDIR',
		'WP_THEME_DIR',
	);

	$path_tokens = array();
	foreach ( $defines as $define ) {
		if ( defined( $define ) ) {
			$path_tokens[ $define ] = constant( $define );
		}
	}

	return apply_filters( 'giveasap_get_path_define_tokens', $path_tokens );
}

/**
 * Add a template to the template cache.
 * Copied from WooCommerce
 *
 * @param string $cache_key Object cache key.
 * @param string $template Located template.
 */
function giveasap_set_template_cache( $cache_key, $template ) {
	wp_cache_set( $cache_key, $template, 'simple-giveaways' );

	$cached_templates = wp_cache_get( 'cached_templates', 'simple-giveaways' );
	if ( is_array( $cached_templates ) ) {
		$cached_templates[] = $cache_key;
	} else {
		$cached_templates = array( $cache_key );
	}

	wp_cache_set( 'cached_templates', $cached_templates, 'simple-giveaways' );
}

/**
 * Given a tokenized path, this will expand the tokens to their full path.
 *
 * Was sg_untokenize_path
 *
 * Copied from WooCommerce
 *
 * @param string $path The absolute path to expand.
 * @param array  $path_tokens An array keyed with the token, containing paths that should be expanded.
 * @return string The absolute path.
 */
function giveasap_untokenize_path( $path, $path_tokens ) {
	foreach ( $path_tokens as $token => $token_path ) {
		$path = str_replace( '{{' . $token . '}}', $token_path, $path );
	}

	return $path;
}

/**
 * Check to display the Powered by message.
 *
 * @return bool
 */
function giveasap_is_powered_by_enabled() {
	$settings = giveasap_get_settings();

	return isset( $settings['display_powered_by'] ) ? (bool) $settings['display_powered_by'] : false;
}

add_action( 'sg_footer', 'wp_print_footer_scripts' );
add_action( 'sg_header', 'wp_print_styles' );
add_action( 'sg_header', 'wp_print_head_scripts' );

add_action( 'giveasap_form_after', 'giveasap_maybe_show_buttons_for_logged_in_giveaway' );

/**
 * Maybe show the login and registration buttons for logged_in Giveaway type
 *
 * @param \GiveASAP_Front $front front object.
 */
function giveasap_maybe_show_buttons_for_logged_in_giveaway( $front ) {

	$giveaway = $front->get_giveaway();

	if ( 'logged_in' !== $giveaway->get_type() ) {
		return;
	}

	if ( $giveaway->is_open_to_subscribe() ) {
		return;
	}

	$general_settings = $giveaway->get_general_settings();
	$login_url        = ! empty( $general_settings['sg_login_url'] ) ? $general_settings['sg_login_url'] : '';
	$registration_url = ! empty( $general_settings['sg_registration_url'] ) ? $general_settings['sg_registration_url'] : '';

	if ( ! $login_url && ! $registration_url ) {
		return;
	}

	?>
	<div class="giveasap-giveaway-buttons">
	<?php

	if ( $login_url ) {
		$login_url = add_query_arg( 'redirect', giveasap_get_current_page_url(), $login_url );
		?>
		<a class="sg-button-link" href="<?php echo esc_url( $login_url ); ?>" target="_blank"><?php esc_html_e( 'Login', 'giveasap' ); ?></a>
		<?php
	}

	if ( $registration_url ) {
		$registration_url = add_query_arg( 'redirect', giveasap_get_current_page_url(), $registration_url );
		?>
		<a class="sg-button-link" href="<?php echo esc_url( $registration_url ); ?>" target="_blank"><?php esc_html_e( 'Register', 'giveasap' ); ?></a>
		<?php
	}

	?>
	</div>
	<?php
}


add_action( 'giveasap_form_before_default', 'giveasap_set_http_referrer_field' );

function giveasap_set_http_referrer_field() {
    if ( ! giveasap_get_raw_referer() ) {
        return;
    }

    ?>
    <input type="hidden" name="sg_giveaway_http_referrer" value="<?php echo esc_attr( giveasap_get_raw_referer() ); ?>" />
    <?php
}

add_action( 'giveasap_the_form_end', 'giveasap_display_prizes_with_threshold', 9 );

/**
 * Display Prizes that have threshold to win
 * @param GiveASAP_Front $front object.
 *
 * @return void
 */
function giveasap_display_prizes_with_threshold( $front ) {
    $giveaway = $front->get_giveaway();

	if ( ! $giveaway->supports( 'prize_threshold' ) ) {
        return;
	}

    $prizes     = $giveaway->get_prize_settings();
    $subscriber = $front->get_subscriber();
    ?>
    <div class="giveasap-prize-thresholds" data-giveaway="<?php echo esc_attr( $giveaway->get_id() ) ?>" data-subscriber="<?php echo esc_attr( $subscriber->get_id() ) ?>">
        <h3 class="giveasap-entries-title"><?php esc_html_e( 'Prizes won', 'giveasap' ); ?></h3>
        <p class="giveasap-sharing-description"><?php esc_html_e( 'Collect entries to win these prizes.', 'giveasap' ); ?></p>
        <ul>
        <?php
            foreach ( $prizes['prizes'] as $prize ) {
                $percentage = $subscriber->get_entries() >= absint( $prize['threshold'] ) ? 100 : absint( $subscriber->get_entries() * 100 / absint( $prize['threshold'] ) );
                ?>
                <li>
                    <div class="prize-info"><?php echo $prize['title']; ?> <span><span class="prize-entries-label"><?php esc_html_e( 'Entries:', 'giveasap' ); ?></span><?php echo $subscriber->get_entries() . '/' . $prize['threshold'] ?></span></div>
                    <span class="progress" style="width:<?php echo esc_attr( $percentage ); ?>%;"></span>
                </li>
                <?php
            }
        ?>
        </ul>
        <style>
            .giveasap-prize-thresholds ul {
                padding: 0 1.5em;
                margin: 1em 0 0 0;
            }
            .giveasap-prize-thresholds ul li .prize-entries-label {
                font-size: small;
                margin-right: 5px;
            }
            .giveasap-prize-thresholds ul li {
                list-style: none;
                min-height: 40px;
                padding: 10px;
                line-height: 20px;
                background: #333;
                color: white;
                position: relative;
                border-radius: 3px;
                overflow: hidden;
                margin-bottom: 1em;
            }
            .giveasap-prize-thresholds ul li .prize-info {
                position: relative;
                z-index: 10;
                display: flex;
                justify-content: space-between;
                align-items: center;
            }
            .giveasap-prize-thresholds ul li .progress {
                position: absolute;
                top: 0;
                left: 0;
                height: 100%;
                background: #a46497;
                z-index: 5;
                opacity: 1;
            }
        </style>
        <?php
            do_action( 'giveasap_after_prize_threshold_inner', $front );
        ?>
    </div>
    <?php

}

add_action( 'giveasap_after_prize_threshold_inner', 'giveasap_show_prize_instructions' );
add_action( 'wp_ajax_giveasap_get_prize_threshold', 'giveasap_get_prize_threshold_ajax' );
add_action( 'wp_ajax_nopriv_giveasap_get_prize_threshold', 'giveasap_get_prize_threshold_ajax' );

function giveasap_get_prize_threshold_ajax() {
    check_ajax_referer( 'giveaway', 'nonce' );

    $giveaway   = isset( $_REQUEST['giveaway'] ) ? absint( $_REQUEST['giveaway'] ) : 0;
	$subscriber = isset( $_REQUEST['subscriber'] ) ? absint( $_REQUEST['subscriber'] ) : 0;

    if ( ! $giveaway || ! $subscriber ) {
        wp_send_json_error();
        wp_die();
    }

    $front = new GiveASAP_Front( $giveaway, false );
    $front->user = giveasap_get_user_by_id( $subscriber, $giveaway );

    ob_start();
	giveasap_display_prizes_with_threshold( $front );
    $html = ob_get_clean();

    wp_send_json_success([ 'html' => $html ]);
    wp_die();

}