<?php
/**
 * Contact Form 7 Square Hosted Checkout integration.
 *
 * @package SHC4WC
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class SHC4WC_CF7 {
	private static $instance = null;

	/** @var string|null Redirect URL for CF7 AJAX response. */
	private static $pending_redirect = null;

	public static function instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	private function __construct() {
		add_action( 'wpcf7_init', array( $this, 'register_form_tag' ) );
		add_action( 'wpcf7_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_payment_success_styles' ), 20 );
		add_action( 'wpcf7_before_send_mail', array( $this, 'maybe_start_payment' ), 5, 3 );
		add_filter( 'wpcf7_feedback_response', array( $this, 'add_redirect_to_response' ), 10, 2 );
		add_filter( 'wpcf7_validate', array( $this, 'validate_amount' ), 20, 2 );
		add_filter( 'wpcf7_form_class_attr', array( $this, 'payment_success_form_class' ) );
		add_filter( 'wpcf7_form_elements', array( $this, 'prepend_payment_success_notice' ), 1 );
		add_filter( 'wpcf7_form_response_output', array( $this, 'payment_success_message' ), 10, 4 );
	}

	public function enqueue_scripts() {
		if ( ! SHC4WC_Plugin::is_connected() ) {
			return;
		}

		wp_enqueue_script(
			'shc4wc-cf7-redirect',
			SHC4WC_PLUGIN_URL . 'assets/js/cf7-redirect.js',
			array( 'contact-form-7' ),
			SHC4WC_VERSION,
			true
		);
	}

	/**
	 * Styles for the payment return banner (independent of Square connect state).
	 */
	public function enqueue_payment_success_styles() {
		if ( ! $this->is_payment_return_request() ) {
			return;
		}

		$css = '.shc4wc-cf7-payment-success{display:block !important;margin:0 0 1.5em;padding:1em 1.25em;border:2px solid #46b450;background:#ecf7ed;color:#1e4620;border-radius:4px;}';
		if ( wp_style_is( 'contact-form-7', 'enqueued' ) || wp_style_is( 'contact-form-7', 'registered' ) ) {
			wp_add_inline_style( 'contact-form-7', $css );
			return;
		}

		wp_register_style( 'shc4wc-cf7-payment-success', false, array(), SHC4WC_VERSION );
		wp_enqueue_style( 'shc4wc-cf7-payment-success' );
		wp_add_inline_style( 'shc4wc-cf7-payment-success', $css );
	}

	public function register_form_tag() {
		if ( ! function_exists( 'wpcf7_add_form_tag' ) ) {
			return;
		}
		wpcf7_add_form_tag(
			array( 'square_pay', 'square_pay*' ),
			array( $this, 'render_form_tag' ),
			array( 'name-attr' => false )
		);
		wpcf7_add_form_tag(
			array( 'square_subscribe', 'square_subscribe*' ),
			array( $this, 'render_subscribe_form_tag' ),
			array( 'name-attr' => false )
		);
	}

	/**
	 * [square_pay amount:25.00] or [square_pay amount-field your-amount] or [square_pay 25.00]
	 *
	 * @param WPCF7_FormTag $tag Form tag.
	 */
	public function render_form_tag( $tag ) {
		if ( ! SHC4WC_Plugin::is_connected() ) {
			return '<p class="shc4wc-cf7-notice">' . esc_html__( 'Square payments are not configured yet.', 'payments-connect-square' ) . '</p>';
		}

		$amount = $this->parse_tag_amount( $tag );
		$label  = __( 'Pay with Square', 'payments-connect-square' );
		if ( $amount > 0 ) {
			if ( function_exists( 'wc_price' ) ) {
				$label .= ' — ' . wp_strip_all_tags( wc_price( $amount ) );
			} else {
				$label .= ' — ' . number_format_i18n( $amount, 2 );
			}
		}

		return sprintf(
			'<p class="shc4wc-cf7-pay"><input type="hidden" name="shc4wc_cf7_pay" value="1" class="wpcf7-form-control" /><span>%s</span></p>',
			esc_html( wp_strip_all_tags( $label ) )
		);
	}

	/**
	 * [square_subscribe variation:PLAN_VARIATION_ID name:"Silver" amount:15.00]
	 *
	 * @param WPCF7_FormTag $tag Form tag.
	 */
	public function render_subscribe_form_tag( $tag ) {
		if ( ! SHC4WC_Plugin::is_connected() ) {
			return '<p class="shc4wc-cf7-notice">' . esc_html__( 'Square payments are not configured yet.', 'payments-connect-square' ) . '</p>';
		}

		$config = $this->parse_subscription_tag( $tag );
		$label  = $config['name'] ? $config['name'] : __( 'Subscribe with Square', 'payments-connect-square' );
		if ( $config['amount'] > 0 ) {
			if ( function_exists( 'wc_price' ) ) {
				$label .= ' — ' . wp_strip_all_tags( wc_price( $config['amount'] ) );
			} else {
				$label .= ' — ' . number_format_i18n( $config['amount'], 2 );
			}
		}

		return sprintf(
			'<p class="shc4wc-cf7-subscribe"><input type="hidden" name="shc4wc_cf7_subscribe" value="1" class="wpcf7-form-control" /><span>%s</span></p>',
			esc_html( wp_strip_all_tags( $label ) )
		);
	}

	public function validate_amount( $result, $tags ) {
		if ( ! $this->form_has_square_checkout( $tags ) ) {
			return $result;
		}
		if ( ! SHC4WC_Plugin::is_connected() ) {
			$result->invalidate(
				array( 'type' => 'square_pay', 'basetype' => 'square_pay' ),
				__( 'Square is not connected. Ask the site administrator to connect under Payments Square Connect.', 'payments-connect-square' )
			);
			return $result;
		}

		$submission = WPCF7_Submission::get_instance();
		if ( ! $submission ) {
			return $result;
		}

		$form = $submission->get_contact_form();
		if ( $form && $this->contact_form_has_square_subscribe( $form ) ) {
			$config = $this->resolve_subscription_from_form( $form );
			if ( '' === $config['variation_id'] ) {
				$result->invalidate(
					array( 'type' => 'square_subscribe', 'basetype' => 'square_subscribe' ),
					__( 'Square subscription plan variation is not configured on this form.', 'payments-connect-square' )
				);
			} elseif ( $config['amount'] <= 0 ) {
				$result->invalidate(
					array( 'type' => 'square_subscribe', 'basetype' => 'square_subscribe' ),
					__( 'Enter a valid subscription amount.', 'payments-connect-square' )
				);
			}
			return $result;
		}

		$amount = $this->resolve_amount_from_submission( $submission );
		if ( $amount <= 0 ) {
			$result->invalidate(
				array( 'type' => 'square_pay', 'basetype' => 'square_pay' ),
				__( 'Enter a valid payment amount.', 'payments-connect-square' )
			);
		}

		return $result;
	}

	/**
	 * @param WPCF7_ContactForm $contact_form Form.
	 * @param bool              $abort        Abort mail flag.
	 * @param WPCF7_Submission  $submission   Submission.
	 */
	public function maybe_start_payment( $contact_form, &$abort, $submission ) {
		if ( ! $submission instanceof WPCF7_Submission ) {
			return;
		}

		if ( ! $this->contact_form_has_square_checkout( $contact_form ) ) {
			return;
		}

		// Completed payment return — allow normal mail send.
		if ( $this->submission_is_paid_retry( $submission ) ) {
			return;
		}

		$settings    = SHC4WC_Plugin::get_settings();
		$location_id = isset( $settings['location_id'] ) ? trim( (string) $settings['location_id'] ) : '';
		if ( '' === $location_id ) {
			$submission->set_status( 'validation_failed' );
			$submission->set_response(
				$contact_form->filter_message(
					__( 'Square location is not configured.', 'payments-connect-square' )
				)
			);
			$abort = true;
			return;
		}

		$posted   = $submission->get_posted_data();
		$ref      = 'cf7_' . wp_generate_password( 16, false, false );
		$key      = wp_hash( $ref . wp_salt( 'auth' ) );
		$currency = $this->get_currency();
		$api      = new SHC4WC_API( $settings );
		$is_sub   = $this->contact_form_has_square_subscribe( $contact_form );
		$amount   = 0.0;

		if ( $is_sub ) {
			$config = $this->resolve_subscription_from_form( $contact_form );
			if ( '' === $config['variation_id'] || $config['amount'] <= 0 ) {
				return;
			}
			$amount = (float) $config['amount'];

			$response = $api->create_subscription_payment_link(
				$config['amount'],
				$currency,
				$ref,
				$config['variation_id'],
				array(
					'description' => sprintf(
						/* translators: 1: form title, 2: reference id. */
						__( 'Contact Form 7 subscription: %1$s (%2$s)', 'payments-connect-square' ),
						$contact_form->title(),
						$ref
					),
					'return_args' => array(
						'type' => 'cf7',
						'ref'  => $ref,
						'key'  => $key,
					),
					'line_name'   => $config['name'] ? $config['name'] : $contact_form->title(),
				)
			);
		} else {
			$amount = $this->resolve_amount_from_submission( $submission );
			if ( $amount <= 0 || ! SHC4WC_Plugin::is_connected() ) {
				return;
			}

			$response = $api->create_payment_link(
				$amount,
				$currency,
				$ref,
				array(
					'description' => sprintf(
						/* translators: 1: form title, 2: reference id. */
						__( 'Contact Form 7: %1$s (%2$s)', 'payments-connect-square' ),
						$contact_form->title(),
						$ref
					),
					'return_args' => array(
						'type' => 'cf7',
						'ref'  => $ref,
						'key'  => $key,
					),
					'line_name'   => $contact_form->title(),
				)
			);
		}

		if ( is_wp_error( $response ) ) {
			$api->log( 'CF7 create payment link failed', array( 'error' => $response->get_error_data() ), 'error' );
			$submission->set_status( 'validation_failed' );
			$submission->set_response(
				$contact_form->filter_message(
					__( 'Could not start Square payment. Please try again.', 'payments-connect-square' )
				)
			);
			$abort = true;
			return;
		}

		$link = isset( $response['payment_link']['url'] ) ? esc_url_raw( $response['payment_link']['url'] ) : '';
		if ( '' === $link ) {
			$submission->set_status( 'validation_failed' );
			$submission->set_response(
				$contact_form->filter_message(
					__( 'Invalid response from Square.', 'payments-connect-square' )
				)
			);
			$abort = true;
			return;
		}

		set_transient(
			'shc4wc_cf7_' . $ref,
			array(
				'form_id'         => (int) $contact_form->id(),
				'posted_data'     => $posted,
				'mail'            => $contact_form->prop( 'mail' ),
				'mail_2'          => $contact_form->prop( 'mail_2' ),
				'amount'          => $amount,
				'currency'        => $currency,
				'is_subscription' => $is_sub,
				'link_id'         => isset( $response['payment_link']['id'] ) ? (string) $response['payment_link']['id'] : '',
				'square_order_id' => $this->extract_square_order_id( $response ),
				'return_url'      => $this->get_submission_return_url( $submission ),
			),
			DAY_IN_SECONDS
		);

		self::$pending_redirect = $link;
		$submission->set_status( 'shc4wc_redirect' );
		$submission->add_result_props(
			array(
				'redirect' => $link,
			)
		);
		$abort = true;
		$submission->set_response(
			$contact_form->filter_message(
				__( 'Redirecting to Square secure checkout…', 'payments-connect-square' )
			)
		);
	}

	public function add_redirect_to_response( $response, $result ) {
		if ( self::$pending_redirect ) {
			$response['redirect'] = self::$pending_redirect;
			self::$pending_redirect = null;
		}
		return $response;
	}

	/**
	 * Whether the current request is a CF7 return after Square payment.
	 *
	 * @return bool
	 */
	private function is_payment_return_request() {
		return ! empty( $_GET['shc4wc_cf7_paid'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
	}

	/**
	 * CF7 hides .wpcf7-response-output while the form is in init state; show sent styling instead.
	 *
	 * @param string $class Form class attribute.
	 * @return string
	 */
	public function payment_success_form_class( $class ) {
		if ( ! $this->is_payment_return_request() ) {
			return $class;
		}

		return str_replace( ' init', ' sent', $class );
	}

	/**
	 * Prominent thank-you banner above the form after Square redirects back.
	 *
	 * @param string $elements Rendered form fields HTML.
	 * @return string
	 */
	public function prepend_payment_success_notice( $elements ) {
		if ( ! $this->is_payment_return_request() ) {
			return $elements;
		}

		$form = function_exists( 'wpcf7_get_current_contact_form' ) ? wpcf7_get_current_contact_form() : null;
		if ( ! $form || ! $this->contact_form_has_square_checkout( $form ) ) {
			return $elements;
		}

		$message = ! empty( $_GET['shc4wc_cf7_subscribed'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
			? esc_html__( 'Thank you — your Square subscription was started and your message was sent.', 'payments-connect-square' )
			: esc_html__( 'Thank you — your Square payment was received and your message was sent.', 'payments-connect-square' );
		$notice  = sprintf(
			'<div class="shc4wc-cf7-payment-success" role="status" style="display:block;">%s</div>',
			$message
		);

		return $notice . $elements;
	}

	/**
	 * Suppress the default hidden response box; the banner above the fields carries the message.
	 *
	 * @param string $output   Default output.
	 * @param string $class    CSS class.
	 * @param string $content  Message content.
	 * @param object $form     Contact form.
	 */
	public function payment_success_message( $output, $class, $content, $form ) {
		unset( $class, $content, $form );
		if ( ! $this->is_payment_return_request() ) {
			return $output;
		}

		return '<div class="wpcf7-response-output" aria-hidden="true"></div>';
	}

	/**
	 * Handle buyer return for CF7 payments.
	 *
	 * @param string $ref Reference id.
	 * @param string $key Verification key.
	 */
	public static function handle_return( $ref, $key ) {
		$ref = sanitize_text_field( (string) $ref );
		$key = sanitize_text_field( (string) $key );

		if ( '' === $ref || '' === $key || ! hash_equals( wp_hash( $ref . wp_salt( 'auth' ) ), $key ) ) {
			wp_safe_redirect( home_url( '/' ) );
			exit;
		}

		$stored = get_transient( 'shc4wc_cf7_' . $ref );
		if ( ! is_array( $stored ) ) {
			wp_safe_redirect( home_url( '/' ) );
			exit;
		}

		self::send_deferred_mail( $stored );
		delete_transient( 'shc4wc_cf7_' . $ref );

		$url = ! empty( $stored['return_url'] ) ? (string) $stored['return_url'] : home_url( '/' );
		if ( ! empty( $stored['is_subscription'] ) ) {
			$url = add_query_arg( 'shc4wc_cf7_subscribed', '1', $url );
		}
		$url = add_query_arg( 'shc4wc_cf7_paid', '1', $url );

		wp_safe_redirect( $url );
		exit;
	}

	private static function send_deferred_mail( array $stored ) {
		$form = function_exists( 'wpcf7_contact_form' ) ? wpcf7_contact_form( (int) $stored['form_id'] ) : null;
		if ( ! $form ) {
			return;
		}

		$posted = isset( $stored['posted_data'] ) && is_array( $stored['posted_data'] ) ? $stored['posted_data'] : array();
		$posted['shc4wc_cf7_paid_ref'] = 'paid';

		foreach ( array( 'mail', 'mail_2' ) as $slot ) {
			if ( empty( $stored[ $slot ] ) || ! is_array( $stored[ $slot ] ) || empty( $stored[ $slot ]['active'] ) ) {
				continue;
			}
			$mail = $stored[ $slot ];
			self::dispatch_mail( $mail, $posted, $form );
		}
	}

	private static function dispatch_mail( array $mail, array $posted, $form ) {
		$recipient = self::replace_mail_tags( (string) $mail['recipient'], $posted, $form );
		$subject   = self::replace_mail_tags( (string) $mail['subject'], $posted, $form );
		$body      = self::replace_mail_tags( (string) $mail['body'], $posted, $form );
		$sender    = self::replace_mail_tags( (string) $mail['sender'], $posted, $form );
		$headers   = array();

		if ( ! empty( $mail['use_html'] ) ) {
			$headers[] = 'Content-Type: text/html; charset=UTF-8';
		}
		if ( preg_match( '/^(.+?)<(.+?)>$/', $sender, $m ) ) {
			$headers[] = 'From: ' . trim( $m[1] ) . ' <' . trim( $m[2] ) . '>';
		} elseif ( is_email( $sender ) ) {
			$headers[] = 'From: ' . $sender;
		}

		if ( '' !== trim( $recipient ) ) {
			wp_mail( $recipient, $subject, $body, $headers );
		}
	}

	private static function replace_mail_tags( $content, array $posted, $form ) {
		foreach ( $posted as $name => $value ) {
			if ( is_array( $value ) ) {
				$value = implode( ', ', $value );
			}
			$content = str_replace( '[' . $name . ']', (string) $value, $content );
		}

		$special = array(
			'[_site_title]' => get_bloginfo( 'name' ),
			'[_site_url]'   => home_url( '/' ),
			'[_site_admin_email]' => get_option( 'admin_email' ),
			'[_date]'       => wp_date( get_option( 'date_format' ) ),
			'[_time]'       => wp_date( get_option( 'time_format' ) ),
			'[_post_title]' => $form ? get_the_title( $form->id() ) : '',
		);
		foreach ( $special as $tag => $replacement ) {
			$content = str_replace( $tag, (string) $replacement, $content );
		}

		return $content;
	}

	private function form_has_square_checkout( $tags ) {
		foreach ( (array) $tags as $tag ) {
			if ( ! is_object( $tag ) || empty( $tag->type ) ) {
				continue;
			}
			if ( in_array( $tag->type, array( 'square_pay', 'square_subscribe' ), true ) ) {
				return true;
			}
		}
		return false;
	}

	private function form_has_square_pay( $tags ) {
		return $this->form_has_square_checkout( $tags );
	}

	/**
	 * Whether the contact form includes Square checkout (pay or subscribe).
	 *
	 * @param WPCF7_ContactForm $contact_form Form.
	 * @return bool
	 */
	private function contact_form_has_square_checkout( $contact_form ) {
		return $this->contact_form_has_square_pay( $contact_form ) || $this->contact_form_has_square_subscribe( $contact_form );
	}

	/**
	 * Whether the contact form includes a Square pay tag.
	 *
	 * @param WPCF7_ContactForm $contact_form Form.
	 * @return bool
	 */
	private function contact_form_has_square_pay( $contact_form ) {
		if ( ! is_object( $contact_form ) || ! method_exists( $contact_form, 'scan_form_tags' ) ) {
			return false;
		}
		$tags = $contact_form->scan_form_tags( array( 'type' => array( 'square_pay', 'square_pay*' ) ) );
		return ! empty( $tags );
	}

	/**
	 * Whether the contact form includes a Square subscribe tag.
	 *
	 * @param WPCF7_ContactForm $contact_form Form.
	 * @return bool
	 */
	private function contact_form_has_square_subscribe( $contact_form ) {
		if ( ! is_object( $contact_form ) || ! method_exists( $contact_form, 'scan_form_tags' ) ) {
			return false;
		}
		$tags = $contact_form->scan_form_tags( array( 'type' => array( 'square_subscribe', 'square_subscribe*' ) ) );
		return ! empty( $tags );
	}

	/**
	 * CF7 strips POST keys starting with "_" from posted_data; read raw superglobal when needed.
	 *
	 * @param WPCF7_Submission $submission Submission.
	 * @return bool
	 */
	private function submission_is_paid_retry( $submission ) {
		$paid = '';
		if ( function_exists( 'wpcf7_superglobal_post' ) ) {
			$paid = (string) wpcf7_superglobal_post( 'shc4wc_cf7_paid_ref' );
		} elseif ( isset( $_POST['shc4wc_cf7_paid_ref'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
			$paid = (string) wp_unslash( $_POST['shc4wc_cf7_paid_ref'] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
		}
		return 'paid' === $paid;
	}

	private function parse_tag_amount( $tag ) {
		if ( ! is_object( $tag ) ) {
			return 0.0;
		}
		$options = isset( $tag->options ) ? (array) $tag->options : array();
		foreach ( $options as $opt ) {
			if ( 0 === strpos( $opt, 'amount:' ) ) {
				return (float) substr( $opt, 7 );
			}
			if ( 0 === strpos( $opt, 'amount-field:' ) ) {
				return 0.0;
			}
		}
		if ( ! empty( $tag->values[0] ) ) {
			return (float) $tag->values[0];
		}
		return 0.0;
	}

	private function resolve_amount_from_submission( WPCF7_Submission $submission ) {
		$form = $submission->get_contact_form();
		if ( ! $form ) {
			return 0.0;
		}
		$tags = $form->scan_form_tags( array( 'type' => array( 'square_pay', 'square_pay*' ) ) );
		if ( empty( $tags ) ) {
			return 0.0;
		}
		$tag    = $tags[0];
		$posted = $submission->get_posted_data();

		foreach ( (array) $tag->options as $opt ) {
			if ( 0 === strpos( $opt, 'amount-field:' ) ) {
				$field = substr( $opt, 13 );
				if ( isset( $posted[ $field ] ) ) {
					return (float) $posted[ $field ];
				}
				return 0.0;
			}
		}

		$fixed = $this->parse_tag_amount( $tag );
		if ( $fixed > 0 ) {
			return $fixed;
		}

		if ( ! empty( $posted['amount'] ) ) {
			return (float) $posted['amount'];
		}

		return 0.0;
	}

	private function get_currency() {
		if ( function_exists( 'get_woocommerce_currency' ) ) {
			return get_woocommerce_currency();
		}
		$locations = SHC4WC_Plugin::get_locations();
		if ( ! empty( $locations[0]['currency'] ) ) {
			return strtoupper( (string) $locations[0]['currency'] );
		}
		return 'USD';
	}

	private function get_submission_return_url( WPCF7_Submission $submission ) {
		$page_id = (int) $submission->get_meta( 'container_post_id' );
		if ( $page_id > 0 ) {
			$permalink = get_permalink( $page_id );
			if ( is_string( $permalink ) && '' !== $permalink ) {
				return $permalink;
			}
		}
		$referer = wp_get_referer();
		if ( is_string( $referer ) && '' !== $referer ) {
			return $referer;
		}
		return home_url( '/' );
	}

	private function extract_square_order_id( array $response ) {
		if ( ! empty( $response['related_resources']['orders'][0]['id'] ) ) {
			return sanitize_text_field( (string) $response['related_resources']['orders'][0]['id'] );
		}
		if ( ! empty( $response['payment_link']['order_id'] ) ) {
			return sanitize_text_field( (string) $response['payment_link']['order_id'] );
		}
		return '';
	}

	/**
	 * @param WPCF7_FormTag|object $tag Form tag.
	 * @return array{variation_id:string,amount:float,name:string}
	 */
	private function parse_subscription_tag( $tag ) {
		$config = array(
			'variation_id' => '',
			'amount'       => 0.0,
			'name'         => '',
		);
		if ( ! is_object( $tag ) ) {
			return $config;
		}

		foreach ( (array) $tag->options as $opt ) {
			if ( 0 === strpos( $opt, 'variation:' ) ) {
				$config['variation_id'] = sanitize_text_field( substr( $opt, 10 ) );
			} elseif ( 0 === strpos( $opt, 'amount:' ) ) {
				$config['amount'] = (float) substr( $opt, 7 );
			} elseif ( 0 === strpos( $opt, 'name:' ) ) {
				$config['name'] = sanitize_text_field( substr( $opt, 5 ) );
			}
		}

		if ( $config['amount'] <= 0 && '' !== $config['variation_id'] ) {
			$catalog = SHC4WC_Subscriptions::find_variation_by_id( $config['variation_id'] );
			if ( $catalog ) {
				$config['amount'] = (float) $catalog['amount'];
				if ( '' === $config['name'] ) {
					$config['name'] = (string) $catalog['name'];
				}
			}
		}

		return $config;
	}

	/**
	 * @param WPCF7_ContactForm $form Form.
	 * @return array{variation_id:string,amount:float,name:string}
	 */
	private function resolve_subscription_from_form( $form ) {
		$tags = $form->scan_form_tags( array( 'type' => array( 'square_subscribe', 'square_subscribe*' ) ) );
		if ( empty( $tags ) ) {
			return array(
				'variation_id' => '',
				'amount'       => 0.0,
				'name'         => '',
			);
		}
		return $this->parse_subscription_tag( $tags[0] );
	}
}
