??PK     £[u]ēˇ*Ö  Ö    save-post-handler.phpnu ÕIw¶´        <?php

namespace Yoast\WP\Duplicate_Post\Handlers;

use Yoast\WP\Duplicate_Post\Permissions_Helper;

/**
 * Duplicate Post handler class for save_post action.
 *
 * @since 4.0
 */
class Save_Post_Handler {

	/**
	 * Holds the permissions helper.
	 *
	 * @var Permissions_Helper
	 */
	protected $permissions_helper;

	/**
	 * Initializes the class.
	 *
	 * @param Permissions_Helper $permissions_helper The Permissions Helper object.
	 */
	public function __construct( Permissions_Helper $permissions_helper ) {
		$this->permissions_helper = $permissions_helper;
	}

	/**
	 * Adds hooks to integrate with WordPress.
	 *
	 * @return void
	 */
	public function register_hooks() {
		if ( \intval( \get_option( 'duplicate_post_show_original_meta_box' ) ) === 1
			|| \intval( \get_option( 'duplicate_post_show_original_column' ) ) === 1 ) {
			\add_action( 'save_post', [ $this, 'delete_on_save_post' ] );
		}
	}

	/**
	 * Deletes the custom field with the ID of the original post.
	 *
	 * @param int $post_id The current post ID.
	 *
	 * @return void
	 */
	public function delete_on_save_post( $post_id ) {
		if ( ( \defined( 'DOING_AUTOSAVE' ) && \DOING_AUTOSAVE )
			|| empty( $_POST['duplicate_post_remove_original'] )
			|| ! \current_user_can( 'edit_post', $post_id ) ) {
			return;
		}

		$post = \get_post( $post_id );
		if ( ! $post ) {
			return;
		}
		if ( ! $this->permissions_helper->is_rewrite_and_republish_copy( $post ) ) {
			\delete_post_meta( $post_id, '_dp_original' );
		}
	}
}
PK     £[u]ŁP!+      handler.phpnu ÕIw¶´        <?php

namespace Yoast\WP\Duplicate_Post\Handlers;

use Yoast\WP\Duplicate_Post\Permissions_Helper;
use Yoast\WP\Duplicate_Post\Post_Duplicator;

/**
 * Duplicate Post handler class for duplication actions.
 *
 * @since 4.0
 */
class Handler {

	/**
	 * Post_Duplicator object.
	 *
	 * @var Post_Duplicator
	 */
	protected $post_duplicator;

	/**
	 * Holds the permissions helper.
	 *
	 * @var Permissions_Helper
	 */
	protected $permissions_helper;

	/**
	 * The bulk actions handler.
	 *
	 * @var Bulk_Handler
	 */
	protected $bulk_handler;

	/**
	 * The link actions handler.
	 *
	 * @var Link_Handler
	 */
	protected $link_handler;

	/**
	 * The save_post action handler.
	 *
	 * @var Save_Post_Handler
	 */
	protected $save_post_handler;

	/**
	 * The link actions handler.
	 *
	 * @var Check_Changes_Handler
	 */
	protected $check_handler;

	/**
	 * Initializes the class.
	 *
	 * @param Post_Duplicator    $post_duplicator    The Post_Duplicator object.
	 * @param Permissions_Helper $permissions_helper The Permissions Helper object.
	 */
	public function __construct( Post_Duplicator $post_duplicator, Permissions_Helper $permissions_helper ) {
		$this->post_duplicator    = $post_duplicator;
		$this->permissions_helper = $permissions_helper;

		$this->bulk_handler      = new Bulk_Handler( $this->post_duplicator, $this->permissions_helper );
		$this->link_handler      = new Link_Handler( $this->post_duplicator, $this->permissions_helper );
		$this->check_handler     = new Check_Changes_Handler( $this->permissions_helper );
		$this->save_post_handler = new Save_Post_Handler( $this->permissions_helper );

		$this->bulk_handler->register_hooks();
		$this->link_handler->register_hooks();
		$this->check_handler->register_hooks();
		$this->save_post_handler->register_hooks();
	}
}
PK     £[u] ¨¦1  1    link-handler.phpnu ÕIw¶´        <?php

namespace Yoast\WP\Duplicate_Post\Handlers;

use Yoast\WP\Duplicate_Post\Permissions_Helper;
use Yoast\WP\Duplicate_Post\Post_Duplicator;

/**
 * Duplicate Post handler class for duplication actions from links.
 *
 * @since 4.0
 */
class Link_Handler {

	/**
	 * Post_Duplicator object.
	 *
	 * @var Post_Duplicator
	 */
	protected $post_duplicator;

	/**
	 * Holds the permissions helper.
	 *
	 * @var Permissions_Helper
	 */
	protected $permissions_helper;

	/**
	 * Initializes the class.
	 *
	 * @param Post_Duplicator    $post_duplicator    The Post_Duplicator object.
	 * @param Permissions_Helper $permissions_helper The Permissions Helper object.
	 */
	public function __construct( Post_Duplicator $post_duplicator, Permissions_Helper $permissions_helper ) {
		$this->post_duplicator    = $post_duplicator;
		$this->permissions_helper = $permissions_helper;
	}

	/**
	 * Adds hooks to integrate with WordPress.
	 *
	 * @return void
	 */
	public function register_hooks() {
		\add_action( 'admin_action_duplicate_post_rewrite', [ $this, 'rewrite_link_action_handler' ] );
		\add_action( 'admin_action_duplicate_post_clone', [ $this, 'clone_link_action_handler' ] );
		\add_action( 'admin_action_duplicate_post_new_draft', [ $this, 'new_draft_link_action_handler' ] );
	}

	/**
	 * Handles the action for copying a post to a new draft.
	 *
	 * @return void
	 */
	public function new_draft_link_action_handler() {
		if ( ! $this->permissions_helper->is_current_user_allowed_to_copy() ) {
			\wp_die( \esc_html__( 'Current user is not allowed to copy posts.', 'duplicate-post' ) );
		}

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

		$id = ( isset( $_GET['post'] ) ? \intval( \wp_unslash( $_GET['post'] ) ) : \intval( \wp_unslash( $_POST['post'] ) ) );

		\check_admin_referer( 'duplicate_post_new_draft_' . $id );

		$post = \get_post( $id );

		if ( ! $post ) {
			\wp_die(
				\esc_html(
					\__( 'Copy creation failed, could not find original:', 'duplicate-post' ) . ' '
					. $id
				)
			);
		}

		if ( $this->permissions_helper->is_rewrite_and_republish_copy( $post ) ) {
			\wp_die(
				\esc_html__( 'You cannot create a copy of a post which is intended for Rewrite & Republish.', 'duplicate-post' )
			);
		}

		$new_id = \duplicate_post_create_duplicate( $post, 'draft' );

		if ( \is_wp_error( $new_id ) ) {
			\wp_die(
				\esc_html__( 'Copy creation failed, could not create a copy.', 'duplicate-post' )
			);
		}

		\wp_safe_redirect(
			\add_query_arg(
				[
					'cloned' => 1,
					'ids'    => $post->ID,
				],
				\admin_url( 'post.php?action=edit&post=' . $new_id . ( isset( $_GET['classic-editor'] ) ? '&classic-editor' : '' ) )
			)
		);
		exit();
	}

	/**
	 * Handles the action for copying a post and redirecting to the post list.
	 *
	 * @return void
	 */
	public function clone_link_action_handler() {
		if ( ! $this->permissions_helper->is_current_user_allowed_to_copy() ) {
			\wp_die( \esc_html__( 'Current user is not allowed to copy posts.', 'duplicate-post' ) );
		}

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

		$id = ( isset( $_GET['post'] ) ? \intval( \wp_unslash( $_GET['post'] ) ) : \intval( \wp_unslash( $_POST['post'] ) ) );

		\check_admin_referer( 'duplicate_post_clone_' . $id );

		$post = \get_post( $id );

		if ( ! $post ) {
			\wp_die(
				\esc_html(
					\__( 'Copy creation failed, could not find original:', 'duplicate-post' ) . ' '
					. $id
				)
			);
		}

		if ( $this->permissions_helper->is_rewrite_and_republish_copy( $post ) ) {
			\wp_die(
				\esc_html__( 'You cannot create a copy of a post which is intended for Rewrite & Republish.', 'duplicate-post' )
			);
		}

		$new_id = \duplicate_post_create_duplicate( $post );

		if ( \is_wp_error( $new_id ) ) {
			\wp_die(
				\esc_html__( 'Copy creation failed, could not create a copy.', 'duplicate-post' )
			);
		}

		$post_type = $post->post_type;
		$sendback  = \wp_get_referer();
		if ( ! $sendback || \strpos( $sendback, 'post.php' ) !== false || \strpos( $sendback, 'post-new.php' ) !== false ) {
			if ( $post_type === 'attachment' ) {
				$sendback = \admin_url( 'upload.php' );
			}
			else {
				$sendback = \admin_url( 'edit.php' );
				if ( ! empty( $post_type ) ) {
					$sendback = \add_query_arg( 'post_type', $post_type, $sendback );
				}
			}
		}
		else {
			$sendback = \remove_query_arg( [ 'trashed', 'untrashed', 'deleted', 'cloned', 'ids' ], $sendback );
		}

		// Redirect to the post list screen.
		\wp_safe_redirect(
			\add_query_arg(
				[
					'cloned' => 1,
					'ids'    => $post->ID,
				],
				$sendback
			)
		);
		exit();
	}

	/**
	 * Handles the action for copying a post for the Rewrite & Republish feature.
	 *
	 * @return void
	 */
	public function rewrite_link_action_handler() {
		if ( ! $this->permissions_helper->is_current_user_allowed_to_copy() ) {
			\wp_die( \esc_html__( 'Current user is not allowed to copy posts.', 'duplicate-post' ) );
		}

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

		$id = ( isset( $_GET['post'] ) ? \intval( \wp_unslash( $_GET['post'] ) ) : \intval( \wp_unslash( $_POST['post'] ) ) );

		\check_admin_referer( 'duplicate_post_rewrite_' . $id );

		$post = \get_post( $id );

		if ( ! $post ) {
			\wp_die(
				\esc_html(
					\__( 'Copy creation failed, could not find original:', 'duplicate-post' ) . ' '
					. $id
				)
			);
		}

		if ( ! $this->permissions_helper->should_rewrite_and_republish_be_allowed( $post ) ) {
			\wp_die(
				\esc_html__( 'You cannot create a copy for Rewrite & Republish if the original is not published or if it already has a copy.', 'duplicate-post' )
			);
		}

		$new_id = $this->post_duplicator->create_duplicate_for_rewrite_and_republish( $post );

		if ( \is_wp_error( $new_id ) ) {
			\wp_die(
				\esc_html__( 'Copy creation failed, could not create a copy.', 'duplicate-post' )
			);
		}

		\wp_safe_redirect(
			\add_query_arg(
				[
					'rewriting' => 1,
					'ids'       => $post->ID,
				],
				\admin_url( 'post.php?action=edit&post=' . $new_id . ( isset( $_GET['classic-editor'] ) ? '&classic-editor' : '' ) )
			)
		);
		exit();
	}
}
PK     £[u]÷A#@      check-changes-handler.phpnu ÕIw¶´        <?php

namespace Yoast\WP\Duplicate_Post\Handlers;

use WP_Post;
use Yoast\WP\Duplicate_Post\Permissions_Helper;
use Yoast\WP\Duplicate_Post\Utils;

/**
 * Duplicate Post handler class for changes overview.
 *
 * Represents the handler for checking the changes between a copy and the original post.
 *
 * @since 4.0
 */
class Check_Changes_Handler {

	/**
	 * Holds the permissions helper.
	 *
	 * @var Permissions_Helper
	 */
	protected $permissions_helper;

	/**
	 * Holds the current post object.
	 *
	 * @var WP_Post
	 */
	private $post;

	/**
	 * Holds the original post object.
	 *
	 * @var WP_Post
	 */
	private $original;

	/**
	 * Initializes the class.
	 *
	 * @param Permissions_Helper $permissions_helper The Permissions Helper object.
	 */
	public function __construct( Permissions_Helper $permissions_helper ) {
		$this->permissions_helper = $permissions_helper;
	}

	/**
	 * Adds hooks to integrate with WordPress.
	 *
	 * @return void
	 */
	public function register_hooks() {
		\add_action( 'admin_action_duplicate_post_check_changes', [ $this, 'check_changes_action_handler' ] );
	}

	/**
	 * Handles the action for displaying the changes between a copy and the original.
	 *
	 * @return void
	 */
	public function check_changes_action_handler() {
		global $wp_version;

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

		$id = ( isset( $_GET['post'] ) ? \intval( \wp_unslash( $_GET['post'] ) ) : \intval( \wp_unslash( $_POST['post'] ) ) );

		\check_admin_referer( 'duplicate_post_check_changes_' . $id );

		$this->post = \get_post( $id );

		if ( ! $this->post ) {
			\wp_die(
				\esc_html(
					\sprintf(
						/* translators: %s: post ID. */
						\__( 'Changes overview failed, could not find post with ID %s.', 'duplicate-post' ),
						$id
					)
				)
			);
			return;
		}

		$this->original = Utils::get_original( $this->post );

		if ( ! $this->original ) {
			\wp_die(
				\esc_html(
					\__( 'Changes overview failed, could not find original post.', 'duplicate-post' )
				)
			);
			return;
		}
		$post_edit_link = \get_edit_post_link( $this->post->ID );

		$this->require_wordpress_header();
		?>
		<div class="wrap">
			<h1 class="long-header">
			<?php
				echo \sprintf(
					/* translators: %s: original item link (to view or edit) or title. */
					\esc_html__( 'Compare changes of duplicated post with the original (&#8220;%s&#8221;)', 'duplicate-post' ),
					Utils::get_edit_or_view_link( $this->original ) // phpcs:ignore WordPress.Security.EscapeOutput
				);
			?>
				</h1>
			<a href="<?php echo \esc_url( $post_edit_link ); ?>"><?php \esc_html_e( '&larr; Return to editor', 'duplicate-post' ); ?></a>
			<div class="revisions">
				<div class="revisions-control-frame">
					<div class="revisions-controls"></div>
				</div>
				<div class="revisions-diff-frame">
					<div class="revisions-diff">
						<div class="diff">
						<?php
						$fields = [
							'post_title'   => \__( 'Title', 'duplicate-post' ),
							'post_content' => \__( 'Content', 'duplicate-post' ),
							'post_excerpt' => \__( 'Excerpt', 'duplicate-post' ),
						];

						$args = [
							'show_split_view' => true,
							'title_left'      => \__( 'Removed', 'duplicate-post' ),
							'title_right'     => \__( 'Added', 'duplicate-post' ),
						];

						if ( \version_compare( $wp_version, '5.7' ) < 0 ) {
							unset( $args['title_left'] );
							unset( $args['title_right'] );
						}

						$post_array = \get_post( $this->post, \ARRAY_A );

						/** This filter is documented in wp-admin/includes/revision.php */
						// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reason: using WP core hook.
						$fields = \apply_filters( '_wp_post_revision_fields', $fields, $post_array );

						foreach ( $fields as $field => $name ) {
							/** This filter is documented in wp-admin/includes/revision.php */
							// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reason: using WP core hook.
							$content_from = \apply_filters( "_wp_post_revision_field_{$field}", $this->original->$field, $field, $this->original, 'from' );

							/** This filter is documented in wp-admin/includes/revision.php */
							// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reason: using WP core hook.
							$content_to = \apply_filters( "_wp_post_revision_field_{$field}", $this->post->$field, $field, $this->post, 'to' );

							$diff = \wp_text_diff( $content_from, $content_to, $args );

							if ( ! $diff && $field === 'post_title' ) {
								// It's a better user experience to still show the Title, even if it didn't change.
								$diff  = '<table class="diff"><colgroup><col class="content diffsplit left"><col class="content diffsplit middle"><col class="content diffsplit right"></colgroup><tbody><tr>';
								$diff .= '<td>' . \esc_html( $this->original->post_title ) . '</td><td></td><td>' . \esc_html( $this->post->post_title ) . '</td>';
								$diff .= '</tr></tbody>';
								$diff .= '</table>';
							}

							if ( $diff ) {
								?>
								<h3><?php echo \esc_html( $name ); ?></h3>
								<?php
									echo $diff; // phpcs:ignore WordPress.Security.EscapeOutput
							}
						}
						?>

						</div>
					</div>
				</div>
			</div>
		</div>
		<?php
		$this->require_wordpress_footer();
	}

	/**
	 * Requires the WP admin header.
	 *
	 * @codeCoverageIgnore
	 *
	 * @return void
	 */
	public function require_wordpress_header() {
		global $post;
		\set_current_screen( 'revision' );
		// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- The revision screen expects $post to be set.
		$post = $this->post;
		require_once \ABSPATH . 'wp-admin/admin-header.php';
	}

	/**
	 * Requires the WP admin footer.
	 *
	 * @codeCoverageIgnore
	 *
	 * @return void
	 */
	public function require_wordpress_footer() {
		require_once \ABSPATH . 'wp-admin/admin-footer.php';
	}
}
PK     £[u]‘-mB  B    bulk-handler.phpnu ÕIw¶´        <?php

namespace Yoast\WP\Duplicate_Post\Handlers;

use Yoast\WP\Duplicate_Post\Permissions_Helper;
use Yoast\WP\Duplicate_Post\Post_Duplicator;
use Yoast\WP\Duplicate_Post\Utils;

/**
 * Duplicate Post handler class for duplication bulk actions.
 *
 * @since 4.0
 */
class Bulk_Handler {

	/**
	 * Post_Duplicator object.
	 *
	 * @var Post_Duplicator
	 */
	protected $post_duplicator;

	/**
	 * Holds the permissions helper.
	 *
	 * @var Permissions_Helper
	 */
	protected $permissions_helper;

	/**
	 * Initializes the class.
	 *
	 * @param Post_Duplicator    $post_duplicator    The Post_Duplicator object.
	 * @param Permissions_Helper $permissions_helper The Permissions Helper object.
	 */
	public function __construct( Post_Duplicator $post_duplicator, Permissions_Helper $permissions_helper ) {
		$this->post_duplicator    = $post_duplicator;
		$this->permissions_helper = $permissions_helper;
	}

	/**
	 * Adds hooks to integrate with WordPress.
	 *
	 * @return void
	 */
	public function register_hooks() {
		\add_action( 'admin_init', [ $this, 'add_bulk_handlers' ] );
	}

	/**
	 * Hooks the handler for the Rewrite & Republish action for all the selected post types.
	 *
	 * @return void
	 */
	public function add_bulk_handlers() {
		$duplicate_post_types_enabled = $this->permissions_helper->get_enabled_post_types();

		foreach ( $duplicate_post_types_enabled as $duplicate_post_type_enabled ) {
			\add_filter( "handle_bulk_actions-edit-{$duplicate_post_type_enabled}", [ $this, 'bulk_action_handler' ], 10, 3 );
		}
	}

	/**
	 * Handles the bulk actions.
	 *
	 * @param string $redirect_to The URL to redirect to.
	 * @param string $doaction    The action that has been called.
	 * @param array  $post_ids    The array of marked post IDs.
	 *
	 * @return string The URL to redirect to.
	 */
	public function bulk_action_handler( $redirect_to, $doaction, $post_ids ) {
		$redirect_to = $this->clone_bulk_action_handler( $redirect_to, $doaction, $post_ids );
		return $this->rewrite_bulk_action_handler( $redirect_to, $doaction, $post_ids );
	}

	/**
	 * Handles the bulk action for the Rewrite & Republish feature.
	 *
	 * @param string $redirect_to The URL to redirect to.
	 * @param string $doaction    The action that has been called.
	 * @param array  $post_ids    The array of marked post IDs.
	 *
	 * @return string The URL to redirect to.
	 */
	public function rewrite_bulk_action_handler( $redirect_to, $doaction, $post_ids ) {
		if ( $doaction !== 'duplicate_post_bulk_rewrite_republish' ) {
			return $redirect_to;
		}

		$counter = 0;
		if ( \is_array( $post_ids ) ) {
			foreach ( $post_ids as $post_id ) {
				$post = \get_post( $post_id );
				if ( ! empty( $post ) && $this->permissions_helper->should_rewrite_and_republish_be_allowed( $post ) ) {
					$new_post_id = $this->post_duplicator->create_duplicate_for_rewrite_and_republish( $post );
					if ( ! \is_wp_error( $new_post_id ) ) {
						++$counter;
					}
				}
			}
		}
		return \add_query_arg( 'bulk_rewriting', $counter, $redirect_to );
	}

	/**
	 * Handles the bulk action for the Clone feature.
	 *
	 * @param string $redirect_to The URL to redirect to.
	 * @param string $doaction    The action that has been called.
	 * @param array  $post_ids    The array of marked post IDs.
	 *
	 * @return string The URL to redirect to.
	 */
	public function clone_bulk_action_handler( $redirect_to, $doaction, $post_ids ) {
		if ( $doaction !== 'duplicate_post_bulk_clone' ) {
			return $redirect_to;
		}

		$counter = 0;
		if ( \is_array( $post_ids ) ) {
			foreach ( $post_ids as $post_id ) {
				$post = \get_post( $post_id );
				if ( ! empty( $post ) && ! $this->permissions_helper->is_rewrite_and_republish_copy( $post ) ) {
					if ( \intval( \get_option( 'duplicate_post_copychildren' ) !== 1 )
						|| ! \is_post_type_hierarchical( $post->post_type )
						|| ( \is_post_type_hierarchical( $post->post_type ) && ! Utils::has_ancestors_marked( $post, $post_ids ) )
					) {
						if ( ! \is_wp_error( \duplicate_post_create_duplicate( $post ) ) ) {
							++$counter;
						}
					}
				}
			}
		}
		return \add_query_arg( 'bulk_cloned', $counter, $redirect_to );
	}
}
PK       £[u]ēˇ*Ö  Ö                  save-post-handler.phpnu ÕIw¶´        PK       £[u]ŁP!+                  handler.phpnu ÕIw¶´        PK       £[u] ¨¦1  1              Z  link-handler.phpnu ÕIw¶´        PK       £[u]÷A#@                Ė'  check-changes-handler.phpnu ÕIw¶´        PK       £[u]‘-mB  B              -@  bulk-handler.phpnu ÕIw¶´        PK      ™  ÆP  