<?php
/**
 * Duplicate Post/Page Extension for Elementor Conditions
 *
 * This extension adds a 'Duplicate' link to post and page row actions in the admin,
 * and handles the duplication process for posts and pages.
 * This extension is designed to work with Elementor and follows its extension structure.
 *
 * @since 1.0.0
 *
 * @package    captain-widgets-kit
 * @subpackage captain-widgets-kit/includes
 */

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

/**
 * Class Captwiki_Duplicate_Posttype
 */
if ( ! class_exists( 'Captwiki_Duplicate_Posttype' ) ) {

	/**
	 * Class Captwiki_Duplicate_Posttype
	 */
	class Captwiki_Duplicate_Posttype {

		/**
		 * Constructor
		 *
		 * @return void
		 */
		public function __construct() {
			add_filter( 'post_row_actions', array( $this, 'captwiki_add_duplicate_link' ), 10, 2 );
			add_filter( 'page_row_actions', array( $this, 'captwiki_add_duplicate_link' ), 10, 2 );
			add_action( 'admin_action_captwiki_duplicate_post', array( $this, 'captwiki_duplicate_post_action' ) );
			add_action( 'admin_notices', array( $this, 'captwiki_show_notice' ) );
		}

		/**
		 * Add duplicate link to post and page row actions
		 *
		 * @param array  $actions The array of actions for the post.
		 * @param object $post The post object.
		 * @return array The modified array of actions.
		 */
		public function captwiki_add_duplicate_link( $actions, $post ) {

			if ( current_user_can( 'edit_posts' ) && in_array( $post->post_type, array( 'post', 'page' ), true ) ) {
				$url = wp_nonce_url(
					admin_url( 'admin.php?action=captwiki_duplicate_post&post=' . $post->ID ),
					'captwiki_duplicate_post_' . $post->ID
				);

				$duplicate_text  = esc_html__( 'Captwiki Duplicate', 'captain-widgets-kit' );
				$duplicate_title = esc_html__( 'Duplicate this item', 'captain-widgets-kit' );

				$actions['duplicate'] = '<a href="' . esc_url( $url ) . '" title="' . $duplicate_title . '">' . $duplicate_text . '</a>';
			}

			return $actions;
		}

		/**
		 * Duplicate post action
		 *
		 * @return void
		 */
		public function captwiki_duplicate_post_action() {

			$_wpnonce = isset( $_GET['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) : '';
			$post     = isset( $_GET['post'] ) ? sanitize_text_field( wp_unslash( $_GET['post'] ) ) : '';

			if ( empty( $post ) || empty( $_wpnonce ) ) {
				wp_die( esc_html__( 'Invalid request.', 'captain-widgets-kit' ) );
			}

			$post_id = absint( $post );
			if ( ! current_user_can( 'edit_posts' ) || ! wp_verify_nonce( $_wpnonce, 'captwiki_duplicate_post_' . $post_id ) ) {
				wp_die( esc_html__( 'Permission denied.', 'captain-widgets-kit' ) );
			}

			$post = get_post( $post_id );
			if ( ! $post ) {
				wp_die( esc_html__( 'Post not found.', 'captain-widgets-kit' ) );
			}

			$new_post = array(
				'post_title'   => $post->post_title . ' (Copy)',
				'post_content' => $post->post_content,
				'post_excerpt' => $post->post_excerpt,
				'post_status'  => 'draft',
				'post_type'    => $post->post_type,
				'post_author'  => get_current_user_id(),
			);

			$new_post_id = wp_insert_post( $new_post );

			$taxonomies = get_object_taxonomies( $post->post_type );
			foreach ( $taxonomies as $taxonomy ) {
				$terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'ids' ) );
				wp_set_object_terms( $new_post_id, $terms, $taxonomy );
			}

			$post_meta = get_post_meta( $post_id );
			foreach ( $post_meta as $meta_key => $values ) {
				foreach ( $values as $value ) {
					add_post_meta( $new_post_id, $meta_key, maybe_unserialize( $value ) );
				}
			}

			$thumbnail_id = get_post_thumbnail_id( $post_id );
			if ( $thumbnail_id ) {
				set_post_thumbnail( $new_post_id, $thumbnail_id );
			}

			wp_safe_redirect( admin_url( 'edit.php?post_type=' . $post->post_type . '&duplicated=1' ) );
			exit;
		}

		/**
		 * Show notice
		 *
		 * @return void
		 */
		public function captwiki_show_notice() {

			$duplicated = isset( $_GET['duplicated'] ) ? intval( $_GET['duplicated'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended

			if ( 1 === $duplicated ) {
				// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
				echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'Post duplicated successfully!', 'captain-widgets-kit' ) . '</p></div>';
			}
		}
	}

	new Captwiki_Duplicate_Posttype();
}
