<?php

namespace Cctor\Coupon\Admin\Duplicate;

use Pngx\Duplicate\Post_Types;

/**
 * Class Main
 *
 * @since   3.1
 *
 * @package Pngx\Duplicate
 */
class Coupons {

	/**
	 * The duplicater instance.
	 *
	 * @since 3.1
	 *
	 * @var Post_Types
	 */
	protected $duplicater;

	/**
	 * The post type to duplicate.
	 *
	 * @since 3.1
	 *
	 * @var string
	 */
	protected $post_type;

	/**
	 * Meetings constructor.
	 *
	 * @since 3.1
	 *
	 * @param Post_Types $duplicate An instance of the Post_Type duplicate class.
	 */
	public function __construct( Post_Types $duplicater ) {
		$this->duplicater = $duplicater;
		$this->post_type = \Cctor__Coupon__Main::POSTTYPE;
	}

	/**
	 * Setup hooks for class.
	 *
	 * @since 3.2.0
	 */
	public function hooks() {

		add_action( 'admin_action_pngx_duplicate_coupon', [ $this, 'duplicate_coupon' ] );
		add_filter( 'post_row_actions', [ $this, 'duplicate_coupon_link' ], 10, 2 );
	}

	/**
	 * Duplicate Coupon
	 */
	public function duplicate_coupon() {

		if (
			! (
				isset( $_GET['post'] ) ||
				isset( $_POST['post'] ) ||
				(
					isset( $_REQUEST['action'] ) &&
					'pngx_duplicate_coupon' === $_REQUEST['action']
				)
			)
		) {
			wp_die( esc_html__( 'No coupon to duplicate has been supplied!', 'coupon-creator' ) );
		}

		$post_id = isset( $_GET['post'] ) ? absint( $_GET['post'] ) : absint( $_POST['post'] );

		if (
			! isset( $_GET['pngx_duplicate_nonce'] ) ||
			! wp_verify_nonce( $_GET['pngx_duplicate_nonce'], 'cctor_duplicate_coupon_' . $post_id )
		) {
			return;
		}

		if ( ! current_user_can( 'edit_cctor_coupons' ) ) {
			return;
		}

		$this->duplicater->duplicate( $this->post_type );
	}

	/**
	 * Add the create link to action list for post_row_actions
	 *
	 * @param $actions
	 * @param $post
	 *
	 * @return mixed
	 */
	public function duplicate_coupon_link( $actions, $post ) {
		if ( ! current_user_can( 'edit_posts' ) ) {
			return $actions;
		}

		if ( $this->post_type !== $post->post_type ) {
			return $actions;
		}

		$actions['duplicate'] = '<a
				href="' . wp_nonce_url( 'admin.php?action=pngx_duplicate_coupon&post=' . $post->ID, 'cctor_duplicate_coupon_' . $post->ID, 'pngx_duplicate_nonce' ) . '"
				title="' . esc_html__( 'Duplicate Coupon', 'coupon-creator' ) . '"
			>' .
                esc_html__( 'Duplicate', 'coupon-creator' ) .
			'</a>';

		return $actions;
	}
}