<?php

/**
 * The Domain For Sale Duplicator.
 *
 * @since        2.1.11
 *
 * @package    domain-for-sale
 * @subpackage domain-for-sale/admin
 * @author     ThemeAtelier<themeatelierbd@gmail.com>
 */

namespace ThemeAtelier\DomainForSale\Admin;

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

/**
 * The Slider Duplicator
 */
class Duplicator
{

	/**
	 * Construct
	 *
	 * @return void
	 */
	public function __construct()
	{
		add_action('admin_action_domain_for_sale_duplicate_shortcode', array($this, 'domain_for_sale_duplicate_shortcode'));
		add_filter('post_row_actions', array($this, 'domain_for_sale_duplicate_shortcode_link'), 10, 2);
	}

	/**
	 * Function creates product slider duplicate as a draft.
	 */
	public function domain_for_sale_duplicate_shortcode()
	{
		global $wpdb;
		if (! (isset($_GET['post']) || isset($_POST['post']) || (isset($_REQUEST['action']) && 'domain_for_sale_duplicate_shortcode' === $_REQUEST['action']))) {
			wp_die(esc_html__('No shortcode to duplicate has been supplied!', 'domain-for-sale'));
		}

		/*
		 * Nonce verification
		 */
		if (! isset($_GET['domain_for_sale_duplicate_nonce']) || ! wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['domain_for_sale_duplicate_nonce'])), basename(__FILE__))) {
			return;
		}

		/*
		 * Get the original shortcode id
		 */
		$dfs_id = isset($_GET['post']) ? absint($_GET['post']) : absint($_POST['post']);

		$capability = apply_filters('domain_for_sale_ui_permission', 'manage_options');
		$show_ui    = current_user_can($capability) ? true : false;

		if (! $show_ui && get_post_type($dfs_id) !== 'dfs_template' && get_post_type($dfs_id) !== 'domain_listing') {
			wp_die(esc_html__('No shortcode to duplicate has been supplied!', 'domain-for-sale'));
		}
		// And all the original shortcode data then.
		$domain = get_post($dfs_id);

		$current_user    = wp_get_current_user();
		$new_post_author = $current_user->ID;

		/*
		 * if shortcode data exists, create the shortcode duplicate
		 */
		if (isset($domain) && null !== $domain) {

			// new shortcode data array.
			$args = array(
				'comment_status' => $domain->comment_status,
				'ping_status'    => $domain->ping_status,
				'post_author'    => $new_post_author,
				'post_content'   => $domain->post_content,
				'post_excerpt'   => $domain->post_excerpt,
				'post_name'      => $domain->post_name,
				'post_parent'    => $domain->post_parent,
				'post_password'  => $domain->post_password,
				'post_status'    => 'draft',
				'post_title'     => $domain->post_title,
				'post_type'      => $domain->post_type,
				'to_ping'        => $domain->to_ping,
				'menu_order'     => $domain->menu_order,
			);

			/*
			 * insert the shortcode by wp_insert_post() function
			 */
			$new_post_id = wp_insert_post($args);

			/*
			 * get all current post terms ad set them to the new post draft
			 */
			$taxonomies = get_object_taxonomies($domain->post_type); // Returns array of taxonomy names for post type, ex array("category", "post_tag").
			foreach ($taxonomies as $taxonomy) {
				$domain_terms = wp_get_object_terms($dfs_id, $taxonomy, array('fields' => 'slugs'));
				wp_set_object_terms($new_post_id, $domain_terms, $taxonomy, false);
			}

			$domain_meta_infos = get_post_custom($dfs_id);
			// Duplicate all post meta.
			foreach ($domain_meta_infos as $key => $values) {
				foreach ($values as $value) {
					$value = wp_slash(maybe_unserialize($value)); // Unserialize data to avoid conflicts.
					add_post_meta($new_post_id, $key, $value);
				}
			}
			// Finally, redirect to the edit post screen for the new draft.
			wp_safe_redirect(esc_url(admin_url('edit.php?post_type=' . $domain->post_type)));
			exit;
		} else {
			wp_die(esc_html__('Shortcode creation failed, could not find original post: ', 'domain-for-sale') . esc_attr($dfs_id));
		}
	}


	/**
	 * Add the duplicate link to action list for post_row_actions
	 *
	 * @param  array  $actions Actions.
	 * @param  object $domain post.
	 * @return array
	 */
	public function domain_for_sale_duplicate_shortcode_link($actions, $domain)
	{
		$capability = apply_filters('domain_for_sale_ui_permission', 'manage_options');
		$show_ui    = current_user_can($capability) ? true : false;
		if ($show_ui && 'dfs_template' === $domain->post_type || 'domain_listing' === $domain->post_type) {
			$actions['duplicate'] = '<a href="' . wp_nonce_url('admin.php?action=domain_for_sale_duplicate_shortcode&post=' . $domain->ID, basename(__FILE__), 'domain_for_sale_duplicate_nonce') . '" rel="permalink">' . __('Duplicate', 'domain-for-sale') . '</a>';
		}
		return $actions;
	}
}
