<?php
/**
 * Request Link for subscribers who lost it.
 */

class SG_Email_Request_Link extends SG_Email {

	/**
	 * Get the content for the registered email.
	 *
	 * @param $content
	 *
	 * @return string
	 */
	public function get_content( $content ) {

		$content .= $this->get_entry_details();

		return parent::get_content( $content );
	}

	/**
	 * Return if we should show the email links or not.
	 *
	 * @return bool
	 */
	public function hide_email_links() {
		$settings = giveasap_get_settings();

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

	/**
	 * Return all the details about the entry in a table.
	 *
	 * @return string
	 */
	public function get_entry_details() {
		$entry_details = apply_filters( 'sg_email_subscriber_details', array(
			'entry_link' => array(
				'title' => __( 'Entry Link', 'giveasap' ),
				'value' => $this->data['entry_link'],
				'type'  => 'link',
			),
			'activate_link' => array(
				'title' => __( 'Activate Link', 'giveasap' ),
				'value' => $this->data['activate_link'],
				'type'  => 'link',
			),
			'share_link' => array(
				'title' => __( 'Share Link', 'giveasap' ),
				'value' => $this->data['share_link'],
				'type'  => 'link',
			),
		));


		if ( ! giveasap_is_activation_required() && isset( $entry_details['activate_link'] ) ) {
			unset( $entry_details['activate_link'] );
		}

		if ( $this->hide_email_links() ) {
			if ( isset( $entry_details['entry_link'] ) ) {
				unset( $entry_details['entry_link'] );
			}

			if ( isset( $entry_details['activate_link'] ) ) {
				unset( $entry_details['activate_link'] );
			}

			if ( isset( $entry_details['share_link'] ) ) {
				unset( $entry_details['share_link'] );
			}
		} else {
			$email = $this->data['subscriber_email'];
			if ( giveasap_is_user_already_active( $email ) ) {
				if ( isset( $entry_details['activate_link'] ) ) {
					unset( $entry_details['activate_link'] );
				}
			}
		}

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

		ob_start();

		if ( 'plain' !== $this->get_content_type() ) {

			if ( isset( $entry_details['activate_link'] ) ) {
				echo '<a class="sg-button button" href="' . $entry_details['activate_link']['value'] . '">' . __( 'Activate', 'giveasap' ) . '</a>';
			}
			?>
            <table class="sg-email-table">
				<?php
				foreach ( $entry_details as $detail ) {
					if ( ! $detail['value'] ) {
						continue;
					}
					?>
                    <tr>
                        <th>
							<?php echo esc_html( $detail['title'] ); ?>
                        </th>
                        <td>
	                        <?php if ( isset( $detail['type'] ) && 'link' === $detail['type'] ) { ?>
                                <a href="<?php echo esc_url( $detail['value'] ); ?>">
                            <?php } ?>
                            
                            <?php echo wp_kses_post( $detail['value'] ); ?>

                            <?php if ( isset( $detail['type'] ) && 'link' === $detail['type'] ) { ?>
                                </a>
                            <?php } ?>
                        </td>
                    </tr>
					<?php
				}
				?>
            </table>
			<?php
		} else {

		    echo "\n\n";

			foreach ( $entry_details as $detail ) {
				if ( ! $detail['value'] ) {
					continue;
				}

				echo esc_html( $detail['title'] ) . ': ' . $detail['value'] . "\n\n";
			}

		}
		return ob_get_clean();
	}

	/**
	 * Process the Email.
	 * This is method is used for custom email types to send their custom emails.
	 * This method should utilize the send() method.
	 *
	 * @since 2.18.0
	 *
	 * @return mixed
	 */
	public function process() {
		$email = $this->data['subscriber_email'];

		if ( ! $email ) {
			return new WP_Error( 'no-email', __( 'No email provided.', 'giveasap' ) );
		}

		$giveaway_id = $this->data['giveaway_id'];

		if ( ! $giveaway_id ) {
			return new WP_Error( 'no-giveaway', __( 'No Giveaway provided.', 'giveasap' ) );
		}

		$settings = giveasap_get_settings();

		$request_email_body = isset( $settings['request_email'] ) ? $settings['request_email']  : '';

		if( $request_email_body != '' ) {
			$subscriber = giveasap_get_user_by_email( $email, $giveaway_id );

			if( ! $subscriber ) {
				return false;
			}

			$subscriber = sg_get_subscriber( $subscriber );
			$title      = html_entity_decode( get_the_title( $giveaway_id ) );
			$subscriber_email_subject = sprintf( __( 'Subscribed links requested for the giveaway %s', 'giveasap' ), $title );
			$permalink = $subscriber->get_subscribed_from_page();
			if ( ! $permalink ) {
				$permalink = get_permalink( $subscriber->get_giveaway_id() );
			}
			$share_link    = add_query_arg( 'share', $subscriber->get_id(), $permalink );
			$entry_link    = add_query_arg( 'ref', $subscriber->get_ref_id(), $permalink );
			$activate_link = add_query_arg( 'activate', '1', $entry_link );

			$request_email_body = str_replace( '{{TITLE}}', $title, $request_email_body );
			$request_email_body = str_replace( '{{ENTRY_LINK}}', $entry_link, $request_email_body );
			$request_email_body = str_replace( '{{SHARE_LINK}}', $share_link, $request_email_body );

			$this->data['entry_link']    = $entry_link;
			$this->data['share_link']    = $share_link;
			if ( $subscriber->get_status() === 'active' ) {
				$this->data['activate_link'] = '';
			} else {
				$this->data['activate_link'] = $activate_link;
			}

			return $this->send( $email, $subscriber_email_subject, $request_email_body );
		}

		return false;
	}
}