diff --git a/.phpcs.xml.dist b/.phpcs.xml.dist
index db20da0de..a2aed5cd5 100644
--- a/.phpcs.xml.dist
+++ b/.phpcs.xml.dist
@@ -144,6 +144,7 @@
at a future date. -->
/(admin|common)-functions\.php$
+ /src/abilities\.php$
/src/(permissions-helper|post-(duplicator|republisher))\.php$
/src/ui/link-builder\.php$
diff --git a/src/abilities.php b/src/abilities.php
new file mode 100644
index 000000000..5e1e04da9
--- /dev/null
+++ b/src/abilities.php
@@ -0,0 +1,615 @@
+post_duplicator = $post_duplicator;
+ $this->post_republisher = $post_republisher;
+ $this->permissions_helper = $permissions_helper;
+ }
+
+ /**
+ * Adds hooks to integrate with WordPress.
+ *
+ * The hooks only exist when the Abilities API is available, so no version check is needed.
+ *
+ * @return void
+ */
+ public function register_hooks() {
+ \add_action( 'wp_abilities_api_categories_init', [ $this, 'register_category' ] );
+ \add_action( 'wp_abilities_api_init', [ $this, 'register_abilities' ] );
+ }
+
+ /**
+ * Registers the ability category for the plugin.
+ *
+ * @return void
+ */
+ public function register_category() {
+ \wp_register_ability_category(
+ self::ABILITY_NAMESPACE,
+ [
+ 'label' => \__( 'Duplicate Post', 'duplicate-post' ),
+ 'description' => \__( 'Abilities to copy posts and to rewrite and republish published posts.', 'duplicate-post' ),
+ ],
+ );
+ }
+
+ /**
+ * Registers the abilities for the plugin.
+ *
+ * @return void
+ */
+ public function register_abilities() {
+ \wp_register_ability(
+ self::ABILITY_NAMESPACE . '/clone',
+ [
+ 'label' => \__( 'Clone', 'duplicate-post' ),
+ 'description' => \__( 'Creates a copy of a post, page or custom post type item, using the copy settings of Duplicate Post. The copy is not opened for editing.', 'duplicate-post' ),
+ 'category' => self::ABILITY_NAMESPACE,
+ 'input_schema' => $this->get_post_id_input_schema(
+ \__( 'The ID of the post to copy.', 'duplicate-post' ),
+ ),
+ 'output_schema' => $this->get_post_output_schema(
+ \__( 'The ID of the created copy.', 'duplicate-post' ),
+ ),
+ 'execute_callback' => [ $this, 'clone_post' ],
+ 'permission_callback' => [ $this, 'can_clone' ],
+ 'meta' => [
+ 'annotations' => [
+ 'readonly' => false,
+ 'destructive' => false,
+ 'idempotent' => false,
+ ],
+ 'public' => true,
+ 'show_in_rest' => true,
+ ],
+ ],
+ );
+
+ \wp_register_ability(
+ self::ABILITY_NAMESPACE . '/copy-to-new-draft',
+ [
+ 'label' => \__( 'Copy to a new draft', 'duplicate-post' ),
+ 'description' => \__( 'Creates a draft copy of a post, page or custom post type item, using the copy settings of Duplicate Post. Use this when the copy is meant to be edited, and use Clone when it is not.', 'duplicate-post' ),
+ 'category' => self::ABILITY_NAMESPACE,
+ 'input_schema' => $this->get_post_id_input_schema(
+ \__( 'The ID of the post to copy.', 'duplicate-post' ),
+ ),
+ 'output_schema' => $this->get_post_output_schema(
+ \__( 'The ID of the created draft copy.', 'duplicate-post' ),
+ ),
+ 'execute_callback' => [ $this, 'copy_to_new_draft' ],
+ 'permission_callback' => [ $this, 'can_copy_to_new_draft' ],
+ 'meta' => [
+ 'annotations' => [
+ 'readonly' => false,
+ 'destructive' => false,
+ 'idempotent' => false,
+ ],
+ 'public' => true,
+ 'show_in_rest' => true,
+ ],
+ ],
+ );
+
+ \wp_register_ability(
+ self::ABILITY_NAMESPACE . '/rewrite',
+ [
+ 'label' => \__( 'Rewrite', 'duplicate-post' ),
+ 'description' => \__( 'Creates a copy of a published post to rewrite it without touching the published version. The rewritten copy replaces the original when it is republished. Only one copy per post can exist at a time.', 'duplicate-post' ),
+ 'category' => self::ABILITY_NAMESPACE,
+ 'input_schema' => $this->get_post_id_input_schema(
+ \__( 'The ID of the published post to rewrite.', 'duplicate-post' ),
+ ),
+ 'output_schema' => $this->get_post_output_schema(
+ \__( 'The ID of the created Rewrite & Republish copy.', 'duplicate-post' ),
+ ),
+ 'execute_callback' => [ $this, 'rewrite' ],
+ 'permission_callback' => [ $this, 'can_rewrite' ],
+ 'meta' => [
+ 'annotations' => [
+ 'readonly' => false,
+ 'destructive' => false,
+ 'idempotent' => false,
+ ],
+ 'public' => true,
+ 'show_in_rest' => true,
+ ],
+ ],
+ );
+
+ \wp_register_ability(
+ self::ABILITY_NAMESPACE . '/republish',
+ [
+ 'label' => \__( 'Republish', 'duplicate-post' ),
+ 'description' => \__( 'Replaces the original post with its Rewrite & Republish copy and deletes the copy. The ID of either the copy or the original post can be passed.', 'duplicate-post' ),
+ 'category' => self::ABILITY_NAMESPACE,
+ 'input_schema' => $this->get_post_id_input_schema(
+ \__( 'The ID of the Rewrite & Republish copy to republish, or of the post it was made from.', 'duplicate-post' ),
+ ),
+ 'output_schema' => $this->get_post_output_schema(
+ \__( 'The ID of the republished post.', 'duplicate-post' ),
+ ),
+ 'execute_callback' => [ $this, 'republish' ],
+ 'permission_callback' => [ $this, 'can_republish' ],
+ 'meta' => [
+ 'annotations' => [
+ 'readonly' => false,
+ 'destructive' => true,
+ 'idempotent' => false,
+ ],
+ 'public' => true,
+ 'show_in_rest' => true,
+ ],
+ ],
+ );
+ }
+
+ /**
+ * Determines whether the current user can clone the passed post.
+ *
+ * @param array $input The ability input.
+ *
+ * @return bool|WP_Error True if the post can be cloned, a WP_Error object otherwise.
+ */
+ public function can_clone( $input ) {
+ return $this->can_copy( $input, '' );
+ }
+
+ /**
+ * Determines whether the current user can copy the passed post to a new draft.
+ *
+ * @param array $input The ability input.
+ *
+ * @return bool|WP_Error True if the post can be copied, a WP_Error object otherwise.
+ */
+ public function can_copy_to_new_draft( $input ) {
+ return $this->can_copy( $input, 'draft' );
+ }
+
+ /**
+ * Determines whether the current user can create a Rewrite & Republish copy of the passed post.
+ *
+ * @param array $input The ability input.
+ *
+ * @return bool|WP_Error True if the copy can be created, a WP_Error object otherwise.
+ */
+ public function can_rewrite( $input ) {
+ $post = $this->get_post( $input );
+
+ if ( \is_wp_error( $post ) ) {
+ return $post;
+ }
+
+ $allowed = $this->can_copy_post_type( $post );
+
+ if ( \is_wp_error( $allowed ) ) {
+ return $allowed;
+ }
+
+ if ( ! $this->permissions_helper->should_rewrite_and_republish_be_allowed( $post ) ) {
+ return new WP_Error(
+ 'duplicate_post_rewrite_not_allowed',
+ \__( 'You cannot create a copy for Rewrite & Republish if the original is not published or if it already has a copy.', 'duplicate-post' ),
+ [ 'status' => 403 ],
+ );
+ }
+
+ return true;
+ }
+
+ /**
+ * Determines whether the current user can republish the Rewrite & Republish copy of the passed post.
+ *
+ * @param array $input The ability input.
+ *
+ * @return bool|WP_Error True if the copy can be republished, a WP_Error object otherwise.
+ */
+ public function can_republish( $input ) {
+ $copy = $this->get_rewrite_and_republish_copy( $input );
+
+ if ( \is_wp_error( $copy ) ) {
+ return $copy;
+ }
+
+ $original = Utils::get_original( $copy->ID );
+
+ if ( ! $original instanceof WP_Post ) {
+ return new WP_Error(
+ 'duplicate_post_original_not_found',
+ \__( 'The post this copy was made from no longer exists.', 'duplicate-post' ),
+ [ 'status' => 404 ],
+ );
+ }
+
+ if ( ! \current_user_can( 'edit_post', $original->ID ) ) {
+ return new WP_Error(
+ 'duplicate_post_cannot_republish',
+ \__( 'You are not allowed to republish this post.', 'duplicate-post' ),
+ [ 'status' => 403 ],
+ );
+ }
+
+ return true;
+ }
+
+ /**
+ * Creates a copy of the passed post, keeping the status from the copy settings.
+ *
+ * @param array $input The ability input.
+ *
+ * @return array|WP_Error The copy data, or a WP_Error object on failure.
+ */
+ public function clone_post( $input ) {
+ return $this->create_copy( $input, false );
+ }
+
+ /**
+ * Creates a draft copy of the passed post.
+ *
+ * @param array $input The ability input.
+ *
+ * @return array|WP_Error The copy data, or a WP_Error object on failure.
+ */
+ public function copy_to_new_draft( $input ) {
+ return $this->create_copy( $input, true );
+ }
+
+ /**
+ * Creates a Rewrite & Republish copy of the passed post.
+ *
+ * @param array $input The ability input.
+ *
+ * @return array|WP_Error The copy data, or a WP_Error object on failure.
+ */
+ public function rewrite( $input ) {
+ $post = $this->get_post( $input );
+
+ if ( \is_wp_error( $post ) ) {
+ return $post;
+ }
+
+ $new_post_id = $this->post_duplicator->create_duplicate_for_rewrite_and_republish( $post );
+
+ if ( \is_wp_error( $new_post_id ) ) {
+ return $this->copy_creation_failed( $new_post_id );
+ }
+
+ return $this->prepare_post_response( $new_post_id );
+ }
+
+ /**
+ * Republishes the Rewrite & Republish copy of the passed post onto the original.
+ *
+ * @param array $input The ability input.
+ *
+ * @return array|WP_Error The republished post data, or a WP_Error object on failure.
+ */
+ public function republish( $input ) {
+ $copy = $this->get_rewrite_and_republish_copy( $input );
+
+ if ( \is_wp_error( $copy ) ) {
+ return $copy;
+ }
+
+ $original = Utils::get_original( $copy->ID );
+
+ if ( ! $original instanceof WP_Post ) {
+ return new WP_Error(
+ 'duplicate_post_original_not_found',
+ \__( 'The post this copy was made from no longer exists.', 'duplicate-post' ),
+ [ 'status' => 404 ],
+ );
+ }
+
+ $this->post_republisher->republish( $copy, $original );
+ $this->post_republisher->delete_copy( $copy->ID, $original->ID );
+
+ return $this->prepare_post_response( $original->ID );
+ }
+
+ /**
+ * Determines whether the current user can copy the passed post.
+ *
+ * @param array $input The ability input.
+ * @param string $status The intended destination status.
+ *
+ * @return bool|WP_Error True if the post can be copied, a WP_Error object otherwise.
+ */
+ protected function can_copy( $input, $status ) {
+ $post = $this->get_post( $input );
+
+ if ( \is_wp_error( $post ) ) {
+ return $post;
+ }
+
+ $allowed = $this->can_copy_post_type( $post );
+
+ if ( \is_wp_error( $allowed ) ) {
+ return $allowed;
+ }
+
+ if ( $this->permissions_helper->is_rewrite_and_republish_copy( $post ) ) {
+ return new WP_Error(
+ 'duplicate_post_cannot_copy',
+ \__( 'You cannot create a copy of a post which is intended for Rewrite & Republish.', 'duplicate-post' ),
+ [ 'status' => 403 ],
+ );
+ }
+
+ /** This filter is documented in admin-functions.php. */
+ if ( ! \apply_filters( 'duplicate_post_allow', true, $post, $status, '' ) ) {
+ return new WP_Error(
+ 'duplicate_post_cannot_copy',
+ \__( 'You aren\'t allowed to duplicate this post', 'duplicate-post' ),
+ [ 'status' => 403 ],
+ );
+ }
+
+ return true;
+ }
+
+ /**
+ * Determines whether the current user can copy posts of the passed post's type.
+ *
+ * @param WP_Post $post The post object.
+ *
+ * @return bool|WP_Error True if posts of this type can be copied, a WP_Error object otherwise.
+ */
+ protected function can_copy_post_type( WP_Post $post ) {
+ if ( ! $this->permissions_helper->is_current_user_allowed_to_copy() ) {
+ return new WP_Error(
+ 'duplicate_post_cannot_copy',
+ \__( 'Current user is not allowed to copy posts.', 'duplicate-post' ),
+ [ 'status' => 403 ],
+ );
+ }
+
+ if ( ! $this->permissions_helper->is_post_type_enabled( $post->post_type ) ) {
+ return new WP_Error(
+ 'duplicate_post_post_type_not_enabled',
+ \__( 'Copy features for this post type are not enabled in options page', 'duplicate-post' ),
+ [ 'status' => 403 ],
+ );
+ }
+
+ return true;
+ }
+
+ /**
+ * Creates a copy of the post passed in the ability input.
+ *
+ * @param array $input The ability input.
+ * @param bool $force_draft Whether the copy should be a draft, regardless of the copy settings.
+ *
+ * @return array|WP_Error The copy data, or a WP_Error object on failure.
+ */
+ protected function create_copy( $input, $force_draft ) {
+ $post = $this->get_post( $input );
+
+ if ( \is_wp_error( $post ) ) {
+ return $post;
+ }
+
+ $options = $this->post_duplicator->get_configured_options();
+
+ if ( $force_draft ) {
+ $options['copy_status'] = false;
+ }
+
+ $new_post_id = $this->post_duplicator->create_duplicate( $post, $options );
+
+ if ( \is_wp_error( $new_post_id ) ) {
+ return $this->copy_creation_failed( $new_post_id );
+ }
+
+ $this->post_duplicator->copy_post_taxonomies( $new_post_id, $post, $options );
+ $this->post_duplicator->copy_post_meta_info( $new_post_id, $post, $options );
+
+ return $this->prepare_post_response( $new_post_id );
+ }
+
+ /**
+ * Returns the post passed in the ability input.
+ *
+ * @param array $input The ability input.
+ *
+ * @return WP_Post|WP_Error The post object, or a WP_Error object if it doesn't exist.
+ */
+ protected function get_post( $input ) {
+ $post = \get_post( $input['post_id'] );
+
+ if ( ! $post instanceof WP_Post ) {
+ return new WP_Error(
+ 'duplicate_post_post_not_found',
+ \__( 'The post could not be found.', 'duplicate-post' ),
+ [ 'status' => 404 ],
+ );
+ }
+
+ return $post;
+ }
+
+ /**
+ * Returns the Rewrite & Republish copy for the post passed in the ability input.
+ *
+ * The input can hold either the ID of the copy itself or the ID of the post it was made from.
+ *
+ * @param array $input The ability input.
+ *
+ * @return WP_Post|WP_Error The copy's post object, or a WP_Error object if there is none.
+ */
+ protected function get_rewrite_and_republish_copy( $input ) {
+ $post = $this->get_post( $input );
+
+ if ( \is_wp_error( $post ) ) {
+ return $post;
+ }
+
+ if ( $this->permissions_helper->is_rewrite_and_republish_copy( $post ) ) {
+ return $post;
+ }
+
+ $copy = $this->permissions_helper->get_rewrite_and_republish_copy( $post );
+
+ if ( ! $copy instanceof WP_Post ) {
+ return new WP_Error(
+ 'duplicate_post_copy_not_found',
+ \__( 'This post does not have a copy for Rewrite & Republish.', 'duplicate-post' ),
+ [ 'status' => 404 ],
+ );
+ }
+
+ return $copy;
+ }
+
+ /**
+ * Returns the ability output for the passed post ID.
+ *
+ * @param int $post_id The post ID.
+ *
+ * @return array|WP_Error The post data, or a WP_Error object if the post doesn't exist.
+ */
+ protected function prepare_post_response( $post_id ) {
+ $post = \get_post( $post_id );
+
+ if ( ! $post instanceof WP_Post ) {
+ return new WP_Error(
+ 'duplicate_post_post_not_found',
+ \__( 'The post could not be found.', 'duplicate-post' ),
+ [ 'status' => 404 ],
+ );
+ }
+
+ return [
+ 'post_id' => (int) $post->ID,
+ 'title' => $post->post_title,
+ 'status' => $post->post_status,
+ 'edit_link' => (string) \get_edit_post_link( $post->ID, 'raw' ),
+ ];
+ }
+
+ /**
+ * Wraps the error returned when a copy could not be created.
+ *
+ * @param WP_Error $error The error returned by the duplicator.
+ *
+ * @return WP_Error The error to return from the ability.
+ */
+ protected function copy_creation_failed( WP_Error $error ) {
+ return new WP_Error(
+ 'duplicate_post_copy_creation_failed',
+ \__( 'Copy creation failed, could not create a copy.', 'duplicate-post' ),
+ [
+ 'status' => 500,
+ 'error' => $error->get_error_message(),
+ ],
+ );
+ }
+
+ /**
+ * Returns the input schema for an ability that takes a single post ID.
+ *
+ * @param string $description The description of the post ID.
+ *
+ * @return array The input schema.
+ */
+ protected function get_post_id_input_schema( $description ) {
+ return [
+ 'type' => 'object',
+ 'properties' => [
+ 'post_id' => [
+ 'type' => 'integer',
+ 'description' => $description,
+ 'minimum' => 1,
+ ],
+ ],
+ 'required' => [ 'post_id' ],
+ 'additionalProperties' => false,
+ ];
+ }
+
+ /**
+ * Returns the output schema for an ability that returns a post.
+ *
+ * @param string $description The description of the post ID.
+ *
+ * @return array The output schema.
+ */
+ protected function get_post_output_schema( $description ) {
+ return [
+ 'type' => 'object',
+ 'properties' => [
+ 'post_id' => [
+ 'type' => 'integer',
+ 'description' => $description,
+ ],
+ 'title' => [
+ 'type' => 'string',
+ 'description' => \__( 'The title of the post.', 'duplicate-post' ),
+ ],
+ 'status' => [
+ 'type' => 'string',
+ 'description' => \__( 'The status of the post.', 'duplicate-post' ),
+ ],
+ 'edit_link' => [
+ 'type' => 'string',
+ 'description' => \__( 'The URL to edit the post, empty if the current user cannot edit it.', 'duplicate-post' ),
+ ],
+ ],
+ ];
+ }
+}
diff --git a/src/duplicate-post.php b/src/duplicate-post.php
index 8d7d15601..c1702c039 100644
--- a/src/duplicate-post.php
+++ b/src/duplicate-post.php
@@ -62,6 +62,13 @@ class Duplicate_Post {
*/
protected $watchers;
+ /**
+ * Abilities object.
+ *
+ * @var Abilities
+ */
+ protected $abilities;
+
/**
* Initializes the main class.
*/
@@ -73,8 +80,10 @@ public function __construct() {
$this->post_republisher = new Post_Republisher( $this->post_duplicator, $this->permissions_helper );
$this->revisions_migrator = new Revisions_Migrator();
$this->watchers = new Watchers( $this->permissions_helper );
+ $this->abilities = new Abilities( $this->post_duplicator, $this->post_republisher, $this->permissions_helper );
$this->post_republisher->register_hooks();
$this->revisions_migrator->register_hooks();
+ $this->abilities->register_hooks();
}
}
diff --git a/src/post-duplicator.php b/src/post-duplicator.php
index e23a61e1e..c1fc7401d 100644
--- a/src/post-duplicator.php
+++ b/src/post-duplicator.php
@@ -44,6 +44,86 @@ public function get_default_options() {
];
}
+ /**
+ * Returns the options array built from the copy settings of the plugin.
+ *
+ * The admin copy handlers build the same options from these settings, so copies created
+ * outside an admin request, for example through an ability, respect the same settings.
+ * Copying children, attachments and comments is handled by the admin-only handlers on the
+ * `duplicate_post_after_duplicated` action and is therefore not part of these options.
+ *
+ * @return array The options based on the saved settings.
+ */
+ public function get_configured_options() {
+ $options = [
+ 'copy_title' => $this->is_copy_enabled( 'title' ),
+ 'copy_date' => $this->is_copy_enabled( 'date' ),
+ 'copy_status' => $this->is_copy_enabled( 'status' ),
+ 'copy_name' => $this->is_copy_enabled( 'slug' ),
+ 'copy_excerpt' => $this->is_copy_enabled( 'excerpt' ),
+ 'copy_content' => $this->is_copy_enabled( 'content' ),
+ 'copy_thumbnail' => $this->is_copy_enabled( 'thumbnail' ),
+ 'copy_template' => $this->is_copy_enabled( 'template' ),
+ 'copy_format' => $this->is_copy_enabled( 'format' ),
+ 'copy_author' => $this->is_copy_enabled( 'author' ),
+ 'copy_password' => $this->is_copy_enabled( 'password' ),
+ 'copy_menu_order' => $this->is_copy_enabled( 'menuorder' ),
+ 'title_prefix' => (string) \get_option( 'duplicate_post_title_prefix' ),
+ 'title_suffix' => (string) \get_option( 'duplicate_post_title_suffix' ),
+ 'increase_menu_order_by' => \get_option( 'duplicate_post_increase_menu_order_by' ),
+ 'meta_excludelist' => $this->get_meta_excludelist(),
+ 'taxonomies_excludelist' => $this->get_taxonomies_excludelist(),
+ ];
+
+ return \wp_parse_args( $options, $this->get_default_options() );
+ }
+
+ /**
+ * Determines whether the setting to copy the passed post element is enabled.
+ *
+ * @param string $element The post element to copy, as used in the option name
+ * after the `duplicate_post_copy` prefix. For example `title`.
+ *
+ * @return bool Whether the element is set to be copied.
+ */
+ protected function is_copy_enabled( $element ) {
+ return ( (int) \get_option( 'duplicate_post_copy' . $element ) === 1 );
+ }
+
+ /**
+ * Returns the meta fields excludelist from the settings.
+ *
+ * @return array The names of the meta fields not to copy.
+ */
+ protected function get_meta_excludelist() {
+ $meta_excludelist = \get_option( 'duplicate_post_blacklist' );
+
+ if ( ! \is_string( $meta_excludelist ) || $meta_excludelist === '' ) {
+ return [];
+ }
+
+ return \array_map( 'trim', \array_filter( \explode( ',', $meta_excludelist ) ) );
+ }
+
+ /**
+ * Returns the taxonomies excludelist from the settings.
+ *
+ * @return array The names of the taxonomies not to copy.
+ */
+ protected function get_taxonomies_excludelist() {
+ $taxonomies_excludelist = \get_option( 'duplicate_post_taxonomies_blacklist', [] );
+
+ if ( empty( $taxonomies_excludelist ) ) {
+ return [];
+ }
+
+ if ( ! \is_array( $taxonomies_excludelist ) ) {
+ return [ $taxonomies_excludelist ];
+ }
+
+ return $taxonomies_excludelist;
+ }
+
/**
* Creates a copy of a post object, accordingly to an options array.
*
diff --git a/tests/Unit/Abilities_Test.php b/tests/Unit/Abilities_Test.php
new file mode 100644
index 000000000..632c7fa4b
--- /dev/null
+++ b/tests/Unit/Abilities_Test.php
@@ -0,0 +1,196 @@
+stubTranslationFunctions();
+
+ $this->post_duplicator = Mockery::mock( Post_Duplicator::class );
+ $this->post_republisher = Mockery::mock( Post_Republisher::class );
+ $this->permissions_helper = Mockery::mock( Permissions_Helper::class );
+
+ $this->instance = new Abilities(
+ $this->post_duplicator,
+ $this->post_republisher,
+ $this->permissions_helper,
+ );
+ }
+
+ /**
+ * Tests the constructor.
+ *
+ * @covers \Yoast\WP\Duplicate_Post\Abilities::__construct
+ *
+ * @return void
+ */
+ public function test_constructor() {
+ $this->assertInstanceOf(
+ Post_Duplicator::class,
+ $this->getPropertyValue( $this->instance, 'post_duplicator' ),
+ );
+
+ $this->assertInstanceOf(
+ Post_Republisher::class,
+ $this->getPropertyValue( $this->instance, 'post_republisher' ),
+ );
+
+ $this->assertInstanceOf(
+ Permissions_Helper::class,
+ $this->getPropertyValue( $this->instance, 'permissions_helper' ),
+ );
+ }
+
+ /**
+ * Tests the registration of the hooks.
+ *
+ * @covers \Yoast\WP\Duplicate_Post\Abilities::register_hooks
+ *
+ * @return void
+ */
+ public function test_register_hooks() {
+ Monkey\Actions\expectAdded( 'wp_abilities_api_categories_init' )
+ ->with( [ $this->instance, 'register_category' ] );
+
+ Monkey\Actions\expectAdded( 'wp_abilities_api_init' )
+ ->with( [ $this->instance, 'register_abilities' ] );
+
+ $this->instance->register_hooks();
+ }
+
+ /**
+ * Tests the registration of the ability category.
+ *
+ * @covers \Yoast\WP\Duplicate_Post\Abilities::register_category
+ *
+ * @return void
+ */
+ public function test_register_category() {
+ Monkey\Functions\expect( 'wp_register_ability_category' )
+ ->once()
+ ->with(
+ 'duplicate-post',
+ Mockery::on(
+ static function ( $args ) {
+ return isset( $args['label'], $args['description'] );
+ },
+ ),
+ );
+
+ $this->instance->register_category();
+ }
+
+ /**
+ * Tests that all abilities are registered in the plugin's category.
+ *
+ * @covers \Yoast\WP\Duplicate_Post\Abilities::register_abilities
+ * @covers \Yoast\WP\Duplicate_Post\Abilities::get_post_id_input_schema
+ * @covers \Yoast\WP\Duplicate_Post\Abilities::get_post_output_schema
+ *
+ * @return void
+ */
+ public function test_register_abilities() {
+ $registered = [];
+
+ Monkey\Functions\expect( 'wp_register_ability' )
+ ->times( 4 )
+ ->andReturnUsing(
+ static function ( $name, $args ) use ( &$registered ) {
+ $registered[ $name ] = $args;
+ },
+ );
+
+ $this->instance->register_abilities();
+
+ $this->assertSame(
+ [
+ 'duplicate-post/clone',
+ 'duplicate-post/copy-to-new-draft',
+ 'duplicate-post/rewrite',
+ 'duplicate-post/republish',
+ ],
+ \array_keys( $registered ),
+ );
+
+ foreach ( $registered as $name => $args ) {
+ $this->assertSame( 'duplicate-post', $args['category'], "{$name} is not in the plugin's category." );
+ $this->assertIsCallable( $args['execute_callback'], "{$name} has no execute callback." );
+ $this->assertIsCallable( $args['permission_callback'], "{$name} has no permission callback." );
+ $this->assertSame( [ 'post_id' ], $args['input_schema']['required'], "{$name} does not require a post ID." );
+ $this->assertArrayHasKey( 'post_id', $args['output_schema']['properties'], "{$name} does not return a post ID." );
+ $this->assertTrue( $args['meta']['show_in_rest'], "{$name} is not exposed in the REST API." );
+ }
+ }
+
+ /**
+ * Tests that the republish ability is annotated as destructive and the copy abilities are not.
+ *
+ * @covers \Yoast\WP\Duplicate_Post\Abilities::register_abilities
+ *
+ * @return void
+ */
+ public function test_register_abilities_annotations() {
+ $registered = [];
+
+ Monkey\Functions\expect( 'wp_register_ability' )
+ ->times( 4 )
+ ->andReturnUsing(
+ static function ( $name, $args ) use ( &$registered ) {
+ $registered[ $name ] = $args;
+ },
+ );
+
+ $this->instance->register_abilities();
+
+ $this->assertFalse( $registered['duplicate-post/clone']['meta']['annotations']['destructive'] );
+ $this->assertFalse( $registered['duplicate-post/copy-to-new-draft']['meta']['annotations']['destructive'] );
+ $this->assertFalse( $registered['duplicate-post/rewrite']['meta']['annotations']['destructive'] );
+ $this->assertTrue( $registered['duplicate-post/republish']['meta']['annotations']['destructive'] );
+ }
+}
diff --git a/tests/Unit/Post_Duplicator_Test.php b/tests/Unit/Post_Duplicator_Test.php
index d273756e0..1d4678234 100644
--- a/tests/Unit/Post_Duplicator_Test.php
+++ b/tests/Unit/Post_Duplicator_Test.php
@@ -350,4 +350,152 @@ public static function generate_copy_author_provider() {
return $data;
}
+
+ /**
+ * Tests the get_configured_options function with all copy settings enabled.
+ *
+ * @covers \Yoast\WP\Duplicate_Post\Post_Duplicator::get_configured_options
+ * @covers \Yoast\WP\Duplicate_Post\Post_Duplicator::is_copy_enabled
+ * @covers \Yoast\WP\Duplicate_Post\Post_Duplicator::get_meta_excludelist
+ * @covers \Yoast\WP\Duplicate_Post\Post_Duplicator::get_taxonomies_excludelist
+ *
+ * @return void
+ */
+ public function test_get_configured_options_with_settings_enabled() {
+ $this->stub_options(
+ [
+ 'duplicate_post_copytitle' => '1',
+ 'duplicate_post_copydate' => '1',
+ 'duplicate_post_copystatus' => '1',
+ 'duplicate_post_copyslug' => '1',
+ 'duplicate_post_copyexcerpt' => '1',
+ 'duplicate_post_copycontent' => '1',
+ 'duplicate_post_copythumbnail' => '1',
+ 'duplicate_post_copytemplate' => '1',
+ 'duplicate_post_copyformat' => '1',
+ 'duplicate_post_copyauthor' => '1',
+ 'duplicate_post_copypassword' => '1',
+ 'duplicate_post_copymenuorder' => '1',
+ 'duplicate_post_title_prefix' => 'Copy of',
+ 'duplicate_post_title_suffix' => '(copy)',
+ 'duplicate_post_increase_menu_order_by' => '2',
+ 'duplicate_post_blacklist' => '_my_meta, _other_meta',
+ 'duplicate_post_taxonomies_blacklist' => [ 'post_tag' ],
+ ],
+ );
+
+ $options = $this->instance->get_configured_options();
+
+ $this->assertSame(
+ [
+ 'copy_title' => true,
+ 'copy_date' => true,
+ 'copy_status' => true,
+ 'copy_name' => true,
+ 'copy_excerpt' => true,
+ 'copy_content' => true,
+ 'copy_thumbnail' => true,
+ 'copy_template' => true,
+ 'copy_format' => true,
+ 'copy_author' => true,
+ 'copy_password' => true,
+ 'copy_attachments' => false,
+ 'copy_children' => false,
+ 'copy_comments' => false,
+ 'copy_menu_order' => true,
+ 'title_prefix' => 'Copy of',
+ 'title_suffix' => '(copy)',
+ 'increase_menu_order_by' => '2',
+ 'parent_id' => null,
+ 'meta_excludelist' => [ '_my_meta', '_other_meta' ],
+ 'taxonomies_excludelist' => [ 'post_tag' ],
+ 'use_filters' => true,
+ ],
+ $options,
+ );
+ }
+
+ /**
+ * Tests the get_configured_options function with all copy settings disabled.
+ *
+ * @covers \Yoast\WP\Duplicate_Post\Post_Duplicator::get_configured_options
+ * @covers \Yoast\WP\Duplicate_Post\Post_Duplicator::is_copy_enabled
+ * @covers \Yoast\WP\Duplicate_Post\Post_Duplicator::get_meta_excludelist
+ * @covers \Yoast\WP\Duplicate_Post\Post_Duplicator::get_taxonomies_excludelist
+ *
+ * @return void
+ */
+ public function test_get_configured_options_with_settings_disabled() {
+ $this->stub_options( [] );
+
+ $options = $this->instance->get_configured_options();
+
+ $this->assertSame(
+ [
+ 'copy_title' => false,
+ 'copy_date' => false,
+ 'copy_status' => false,
+ 'copy_name' => false,
+ 'copy_excerpt' => false,
+ 'copy_content' => false,
+ 'copy_thumbnail' => false,
+ 'copy_template' => false,
+ 'copy_format' => false,
+ 'copy_author' => false,
+ 'copy_password' => false,
+ 'copy_attachments' => false,
+ 'copy_children' => false,
+ 'copy_comments' => false,
+ 'copy_menu_order' => false,
+ 'title_prefix' => '',
+ 'title_suffix' => '',
+ 'increase_menu_order_by' => false,
+ 'parent_id' => null,
+ 'meta_excludelist' => [],
+ 'taxonomies_excludelist' => [],
+ 'use_filters' => true,
+ ],
+ $options,
+ );
+ }
+
+ /**
+ * Tests that a taxonomies excludelist which is not an array is cast to one.
+ *
+ * @covers \Yoast\WP\Duplicate_Post\Post_Duplicator::get_taxonomies_excludelist
+ *
+ * @return void
+ */
+ public function test_get_configured_options_with_single_taxonomy_excluded() {
+ $this->stub_options( [ 'duplicate_post_taxonomies_blacklist' => 'post_tag' ] );
+
+ $options = $this->instance->get_configured_options();
+
+ $this->assertSame( [ 'post_tag' ], $options['taxonomies_excludelist'] );
+ }
+
+ /**
+ * Stubs the options used to build the copy options.
+ *
+ * @param array $options The values of the stubbed options.
+ *
+ * @return void
+ */
+ protected function stub_options( array $options ) {
+ Monkey\Functions\when( 'get_option' )->alias(
+ static function ( $option, $default_value = false ) use ( $options ) {
+ if ( \array_key_exists( $option, $options ) ) {
+ return $options[ $option ];
+ }
+
+ return $default_value;
+ },
+ );
+
+ Monkey\Functions\when( 'wp_parse_args' )->alias(
+ static function ( $args, $defaults ) {
+ return \array_merge( $defaults, $args );
+ },
+ );
+ }
}
diff --git a/tests/WP/Abilities_Test.php b/tests/WP/Abilities_Test.php
new file mode 100644
index 000000000..ff60aa072
--- /dev/null
+++ b/tests/WP/Abilities_Test.php
@@ -0,0 +1,429 @@
+instance = new Abilities(
+ $post_duplicator,
+ new Post_Republisher( $post_duplicator, $permissions_helper ),
+ $permissions_helper,
+ );
+
+ $this->admin_user_id = $this->factory->user->create( [ 'role' => 'administrator' ] );
+ $this->subscriber_user_id = $this->factory->user->create( [ 'role' => 'subscriber' ] );
+
+ // The capability is normally added to the roles when the plugin is installed or upgraded.
+ \get_role( 'administrator' )->add_cap( 'copy_posts' );
+
+ \update_option( 'duplicate_post_types_enabled', [ 'post', 'page' ] );
+ \update_option( 'duplicate_post_copytitle', '1' );
+ \update_option( 'duplicate_post_copycontent', '1' );
+ \update_option( 'duplicate_post_copyexcerpt', '1' );
+ \update_option( 'duplicate_post_copystatus', '1' );
+ \update_option( 'duplicate_post_title_prefix', '' );
+ \update_option( 'duplicate_post_title_suffix', '' );
+ \update_option( 'duplicate_post_blacklist', '' );
+ \update_option( 'duplicate_post_taxonomies_blacklist', [] );
+ }
+
+ /**
+ * Cleaning up after each test.
+ *
+ * @return void
+ */
+ public function tear_down() {
+ \get_role( 'administrator' )->remove_cap( 'copy_posts' );
+ \wp_set_current_user( 0 );
+
+ parent::tear_down();
+ }
+
+ /**
+ * Tests that cloning a post creates a copy with the configured copy settings.
+ *
+ * @covers ::clone_post
+ * @covers ::can_clone
+ * @covers ::can_copy
+ * @covers ::can_copy_post_type
+ * @covers ::create_copy
+ * @covers ::get_post
+ * @covers ::prepare_post_response
+ *
+ * @return void
+ */
+ public function test_clone_creates_a_copy() {
+ \wp_set_current_user( $this->admin_user_id );
+ \update_option( 'duplicate_post_title_prefix', 'Copy of' );
+
+ $post_id = $this->factory->post->create(
+ [
+ 'post_title' => 'Original post',
+ 'post_content' => 'Original content',
+ 'post_status' => 'publish',
+ ],
+ );
+ \wp_set_post_terms( $post_id, [ 'duplicated' ], 'post_tag' );
+ \add_post_meta( $post_id, '_my_meta', 'meta value' );
+
+ $input = [ 'post_id' => $post_id ];
+
+ $this->assertTrue( $this->instance->can_clone( $input ) );
+
+ $result = $this->instance->clone_post( $input );
+
+ $this->assertIsArray( $result );
+ $this->assertNotSame( $post_id, $result['post_id'] );
+ $this->assertSame( 'Copy of Original post', $result['title'] );
+ $this->assertSame( 'publish', $result['status'] );
+
+ $copy = \get_post( $result['post_id'] );
+
+ $this->assertSame( 'Original content', $copy->post_content );
+ $this->assertSame( $post_id, (int) \get_post_meta( $copy->ID, '_dp_original', true ) );
+ $this->assertSame( 'meta value', \get_post_meta( $copy->ID, '_my_meta', true ) );
+ $this->assertSame( [ 'duplicated' ], \wp_get_post_terms( $copy->ID, 'post_tag', [ 'fields' => 'names' ] ) );
+ }
+
+ /**
+ * Tests that copying a post to a new draft creates a draft, regardless of the copy settings.
+ *
+ * @covers ::copy_to_new_draft
+ * @covers ::can_copy_to_new_draft
+ * @covers ::create_copy
+ *
+ * @return void
+ */
+ public function test_copy_to_new_draft_creates_a_draft() {
+ \wp_set_current_user( $this->admin_user_id );
+
+ $post_id = $this->factory->post->create(
+ [
+ 'post_title' => 'Original post',
+ 'post_status' => 'publish',
+ ],
+ );
+
+ $input = [ 'post_id' => $post_id ];
+
+ $this->assertTrue( $this->instance->can_copy_to_new_draft( $input ) );
+
+ $result = $this->instance->copy_to_new_draft( $input );
+
+ $this->assertIsArray( $result );
+ $this->assertSame( 'draft', $result['status'] );
+ $this->assertSame( 'Original post', $result['title'] );
+ }
+
+ /**
+ * Tests that a post which doesn't exist can't be copied.
+ *
+ * @covers ::can_clone
+ * @covers ::get_post
+ *
+ * @return void
+ */
+ public function test_can_clone_returns_error_for_unknown_post() {
+ \wp_set_current_user( $this->admin_user_id );
+
+ $result = $this->instance->can_clone( [ 'post_id' => 123_456 ] );
+
+ $this->assertWPError( $result );
+ $this->assertSame( 'duplicate_post_post_not_found', $result->get_error_code() );
+ }
+
+ /**
+ * Tests that a user without the copy_posts capability can't copy a post.
+ *
+ * @covers ::can_clone
+ * @covers ::can_copy_post_type
+ *
+ * @return void
+ */
+ public function test_can_clone_returns_error_for_user_without_capability() {
+ \wp_set_current_user( $this->subscriber_user_id );
+
+ $post_id = $this->factory->post->create( [ 'post_status' => 'publish' ] );
+
+ $result = $this->instance->can_clone( [ 'post_id' => $post_id ] );
+
+ $this->assertWPError( $result );
+ $this->assertSame( 'duplicate_post_cannot_copy', $result->get_error_code() );
+ }
+
+ /**
+ * Tests that a post of a post type which is not enabled can't be copied.
+ *
+ * @covers ::can_clone
+ * @covers ::can_copy_post_type
+ *
+ * @return void
+ */
+ public function test_can_clone_returns_error_for_disabled_post_type() {
+ \wp_set_current_user( $this->admin_user_id );
+ \update_option( 'duplicate_post_types_enabled', [ 'page' ] );
+
+ $post_id = $this->factory->post->create( [ 'post_status' => 'publish' ] );
+
+ $result = $this->instance->can_clone( [ 'post_id' => $post_id ] );
+
+ $this->assertWPError( $result );
+ $this->assertSame( 'duplicate_post_post_type_not_enabled', $result->get_error_code() );
+ }
+
+ /**
+ * Tests that a copy intended for Rewrite & Republish can't be copied.
+ *
+ * @covers ::can_clone
+ * @covers ::can_copy
+ *
+ * @return void
+ */
+ public function test_can_clone_returns_error_for_rewrite_and_republish_copy() {
+ \wp_set_current_user( $this->admin_user_id );
+
+ $post_id = $this->factory->post->create( [ 'post_status' => 'publish' ] );
+ \add_post_meta( $post_id, '_dp_is_rewrite_republish_copy', 1 );
+
+ $result = $this->instance->can_clone( [ 'post_id' => $post_id ] );
+
+ $this->assertWPError( $result );
+ $this->assertSame( 'duplicate_post_cannot_copy', $result->get_error_code() );
+ }
+
+ /**
+ * Tests that the duplicate_post_allow filter blocks copying a post.
+ *
+ * @covers ::can_copy
+ *
+ * @return void
+ */
+ public function test_can_clone_respects_the_duplicate_post_allow_filter() {
+ \wp_set_current_user( $this->admin_user_id );
+
+ $post_id = $this->factory->post->create( [ 'post_status' => 'publish' ] );
+
+ \add_filter( 'duplicate_post_allow', '__return_false' );
+ $result = $this->instance->can_clone( [ 'post_id' => $post_id ] );
+ \remove_filter( 'duplicate_post_allow', '__return_false' );
+
+ $this->assertWPError( $result );
+ $this->assertSame( 'duplicate_post_cannot_copy', $result->get_error_code() );
+ }
+
+ /**
+ * Tests that rewriting a published post creates a Rewrite & Republish copy.
+ *
+ * @covers ::rewrite
+ * @covers ::can_rewrite
+ *
+ * @return void
+ */
+ public function test_rewrite_creates_a_rewrite_and_republish_copy() {
+ \wp_set_current_user( $this->admin_user_id );
+
+ $post_id = $this->factory->post->create(
+ [
+ 'post_title' => 'Original post',
+ 'post_status' => 'publish',
+ ],
+ );
+
+ $input = [ 'post_id' => $post_id ];
+
+ $this->assertTrue( $this->instance->can_rewrite( $input ) );
+
+ $result = $this->instance->rewrite( $input );
+
+ $this->assertIsArray( $result );
+ $this->assertSame( 1, (int) \get_post_meta( $result['post_id'], '_dp_is_rewrite_republish_copy', true ) );
+ $this->assertSame( $post_id, (int) \get_post_meta( $result['post_id'], '_dp_original', true ) );
+ $this->assertSame( $result['post_id'], (int) \get_post_meta( $post_id, '_dp_has_rewrite_republish_copy', true ) );
+
+ // A post can only have one Rewrite & Republish copy at a time.
+ $this->assertWPError( $this->instance->can_rewrite( $input ) );
+ }
+
+ /**
+ * Tests that a post which is not published can't be rewritten.
+ *
+ * @covers ::can_rewrite
+ *
+ * @return void
+ */
+ public function test_can_rewrite_returns_error_for_draft() {
+ \wp_set_current_user( $this->admin_user_id );
+
+ $post_id = $this->factory->post->create( [ 'post_status' => 'draft' ] );
+
+ $result = $this->instance->can_rewrite( [ 'post_id' => $post_id ] );
+
+ $this->assertWPError( $result );
+ $this->assertSame( 'duplicate_post_rewrite_not_allowed', $result->get_error_code() );
+ }
+
+ /**
+ * Tests that republishing overwrites the original post and removes the copy.
+ *
+ * @covers ::republish
+ * @covers ::can_republish
+ * @covers ::get_rewrite_and_republish_copy
+ *
+ * @return void
+ */
+ public function test_republish_overwrites_the_original() {
+ \wp_set_current_user( $this->admin_user_id );
+
+ $post_id = $this->factory->post->create(
+ [
+ 'post_title' => 'Original post',
+ 'post_content' => 'Original content',
+ 'post_status' => 'publish',
+ ],
+ );
+
+ $copy_id = $this->instance->rewrite( [ 'post_id' => $post_id ] )['post_id'];
+
+ \wp_update_post(
+ [
+ 'ID' => $copy_id,
+ 'post_title' => 'Rewritten post',
+ 'post_content' => 'Rewritten content',
+ ],
+ );
+
+ $input = [ 'post_id' => $copy_id ];
+
+ $this->assertTrue( $this->instance->can_republish( $input ) );
+
+ $result = $this->instance->republish( $input );
+
+ $this->assertIsArray( $result );
+ $this->assertSame( $post_id, $result['post_id'] );
+ $this->assertSame( 'Rewritten post', $result['title'] );
+ $this->assertSame( 'publish', $result['status'] );
+
+ $original = \get_post( $post_id );
+
+ $this->assertSame( 'Rewritten content', $original->post_content );
+ $this->assertNull( \get_post( $copy_id ) );
+ $this->assertSame( '', \get_post_meta( $post_id, '_dp_has_rewrite_republish_copy', true ) );
+ }
+
+ /**
+ * Tests that republishing can be requested with the ID of the original post.
+ *
+ * @covers ::republish
+ * @covers ::get_rewrite_and_republish_copy
+ *
+ * @return void
+ */
+ public function test_republish_accepts_the_id_of_the_original_post() {
+ \wp_set_current_user( $this->admin_user_id );
+
+ $post_id = $this->factory->post->create(
+ [
+ 'post_title' => 'Original post',
+ 'post_status' => 'publish',
+ ],
+ );
+
+ $copy_id = $this->instance->rewrite( [ 'post_id' => $post_id ] )['post_id'];
+
+ \wp_update_post(
+ [
+ 'ID' => $copy_id,
+ 'post_title' => 'Rewritten post',
+ ],
+ );
+
+ $result = $this->instance->republish( [ 'post_id' => $post_id ] );
+
+ $this->assertIsArray( $result );
+ $this->assertSame( $post_id, $result['post_id'] );
+ $this->assertSame( 'Rewritten post', $result['title'] );
+ }
+
+ /**
+ * Tests that a post without a Rewrite & Republish copy can't be republished.
+ *
+ * @covers ::can_republish
+ * @covers ::get_rewrite_and_republish_copy
+ *
+ * @return void
+ */
+ public function test_can_republish_returns_error_without_copy() {
+ \wp_set_current_user( $this->admin_user_id );
+
+ $post_id = $this->factory->post->create( [ 'post_status' => 'publish' ] );
+
+ $result = $this->instance->can_republish( [ 'post_id' => $post_id ] );
+
+ $this->assertWPError( $result );
+ $this->assertSame( 'duplicate_post_copy_not_found', $result->get_error_code() );
+ }
+
+ /**
+ * Tests that a user who can't edit the original post can't republish its copy.
+ *
+ * @covers ::can_republish
+ *
+ * @return void
+ */
+ public function test_can_republish_returns_error_for_user_without_capability() {
+ \wp_set_current_user( $this->admin_user_id );
+
+ $post_id = $this->factory->post->create( [ 'post_status' => 'publish' ] );
+ $copy_id = $this->instance->rewrite( [ 'post_id' => $post_id ] )['post_id'];
+
+ \wp_set_current_user( $this->subscriber_user_id );
+
+ $result = $this->instance->can_republish( [ 'post_id' => $copy_id ] );
+
+ $this->assertWPError( $result );
+ $this->assertSame( 'duplicate_post_cannot_republish', $result->get_error_code() );
+ }
+}