<?php

namespace PaymentPage;

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

use PaymentPage\PaymentForm as PP_PaymentForm;

class ShortCodes {

	/**
	 * Register the shortcode
	 */
	public static function register() {
		add_shortcode( 'payment-page-success-details', '\PaymentPage\ShortCodes::success_details' );
    add_shortcode( 'payment-page-payment-form', '\PaymentPage\ShortCodes::payment_form' );
	}

	/**
	 * Render the content of the shortcode
	 */
	public static function success_details(): string {
		// phpcs:disable WordPress.Security.NonceVerification.Recommended -- Shortcode displays data from payment redirect URL parameters, no form submission
		return Template::get_template( 'shortcode-payment-success.php', [
			'title'          => ( isset( $_REQUEST['title'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['title'] ) ) : '' ),
			'message'        => ( isset( $_REQUEST['message'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['message'] ) ) : '' ),
			'item'           => ( isset( $_REQUEST['item'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['item'] ) ) : '' ),
			'purchased_from' => ( isset( $_REQUEST['purchased_from'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['purchased_from'] ) ) : '' ),
			'payment_date'   => ( isset( $_REQUEST['payment_date'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['payment_date'] ) ) : '' ),
			'currency'       => ( isset( $_REQUEST['currency'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['currency'] ) ) : '' ),
			'payment_amount' => ( isset( $_REQUEST['amount'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['amount'] ) ) : '' ),
			'customer_name'  => ( isset( $_REQUEST['customer_name'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['customer_name'] ) ) : '' ),
			'customer_email' => ( isset( $_REQUEST['email'] ) ? sanitize_email( wp_unslash( $_REQUEST['email'] ) ) : '' ),
		] );
		// phpcs:enable WordPress.Security.NonceVerification.Recommended
	}

  public static function payment_form( $args, $content) {
    if(empty($args['id'])) {
      return '';
    }

    $post_id = intval($args['id']);

    if(get_post_type($post_id) !== PAYMENT_PAGE_POST_TYPE_PAYMENT_FORM) {
      return '';
    }

    $response = '';
	
	/*WPSPINS CODE FIX BELOW FOR DRAFT FORM ERROR*/
	
	$post_status = get_post_status($post_id);
	
	if( 'draft' === $post_status ) {
		
		if( ! current_user_can( PAYMENT_PAGE_ADMIN_CAP ) ) {
			return $response;
		}
			
		$response .= '<p data-payment-page-notification="warning">' . esc_html__( "This form is currently in draft mode, and cannot be viewed by non-admin users", 'payment-page' ) . '</p>';
	}

    payment_page_register_universal_interface();

    $response .= PP_PaymentForm::instance()->get_from_payment_form_id( $post_id );

    return $response;
  }

}