<?php
/**
 * Form Shortcode
 *
 * @package Ugcify\Form
 */

namespace Ugcify\Form;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class FormShortcode
 */
class FormShortcode {

	/**
	 * Form renderer instance
	 *
	 * @var FormRenderer
	 */
	private $renderer;

	/**
	 * Constructor
	 *
	 * @param FormRenderer $renderer Form renderer instance.
	 */
	public function __construct( $renderer ) {
		$this->renderer = $renderer;
		$this->register_shortcodes();
	}

	/**
	 * Register shortcodes
	 *
	 * @return void
	 */
	private function register_shortcodes() {
		add_shortcode( 'ugcify_review_form', array( $this, 'review_form_shortcode' ) );
		add_shortcode( 'ugcify_review_button', array( $this, 'review_button_shortcode' ) );
	}

	/**
	 * Review form shortcode
	 *
	 * [ugcify_review_form product_id="123"]
	 *
	 * @param array $atts Shortcode attributes.
	 * @return string
	 */
	public function review_form_shortcode( $atts ) {
		// Signal that UGC assets are needed on this page.
		do_action( 'ugcify_assets_needed' );

		$atts = shortcode_atts(
			array(
				'product_id' => get_the_ID(),
			),
			$atts,
			'ugcify_review_form'
		);

		return $this->renderer->render(
			array(
				'product_id' => intval( $atts['product_id'] ),
				'type'       => 'inline',
			)
		);
	}

	/**
	 * Review button shortcode
	 *
	 * [ugcify_review_button product_id="123"]
	 *
	 * @param array $atts Shortcode attributes.
	 * @return string
	 */
	public function review_button_shortcode( $atts ) {
		// Signal that UGC assets are needed on this page.
		do_action( 'ugcify_assets_needed' );

		$atts = shortcode_atts(
			array(
				'product_id' => get_the_ID(),
			),
			$atts,
			'ugcify_review_button'
		);

		return $this->renderer->render(
			array(
				'product_id' => intval( $atts['product_id'] ),
				'type'       => 'button',
			)
		);
	}
}
