Skip to content

Commit bc4e017

Browse files
Merge pull request #449 from Yoast/fix/short-prefix
Replace `dp_` prefix with `duplicate_post_`
2 parents 87c2ad9 + d9d9d62 commit bc4e017

File tree

5 files changed

+138
-41
lines changed

5 files changed

+138
-41
lines changed

.phpcs.xml.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
<property name="prefixes" type="array">
5050
<element value="Yoast\WP\Duplicate_Post"/>
5151
<element value="duplicate_post"/>
52-
<element value="dp_"/>
5352
</property>
5453

5554
<property name="psr4_paths" type="array">

admin-functions.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,26 +56,21 @@ function duplicate_post_admin_init() {
5656
add_action( 'wp_ajax_duplicate_post_dismiss_notice', 'duplicate_post_dismiss_notice' );
5757
}
5858

59-
add_action( 'dp_duplicate_post', 'duplicate_post_copy_post_meta_info', 10, 2 );
60-
add_action( 'dp_duplicate_page', 'duplicate_post_copy_post_meta_info', 10, 2 );
59+
add_action( 'duplicate_post_after_duplicated', 'duplicate_post_copy_post_meta_info', 10, 2 );
6160

6261
if ( intval( get_option( 'duplicate_post_copychildren' ) ) === 1 ) {
63-
add_action( 'dp_duplicate_post', 'duplicate_post_copy_children', 20, 3 );
64-
add_action( 'dp_duplicate_page', 'duplicate_post_copy_children', 20, 3 );
62+
add_action( 'duplicate_post_after_duplicated', 'duplicate_post_copy_children', 20, 3 );
6563
}
6664

6765
if ( intval( get_option( 'duplicate_post_copyattachments' ) ) === 1 ) {
68-
add_action( 'dp_duplicate_post', 'duplicate_post_copy_attachments', 30, 2 );
69-
add_action( 'dp_duplicate_page', 'duplicate_post_copy_attachments', 30, 2 );
66+
add_action( 'duplicate_post_after_duplicated', 'duplicate_post_copy_attachments', 30, 2 );
7067
}
7168

7269
if ( intval( get_option( 'duplicate_post_copycomments' ) ) === 1 ) {
73-
add_action( 'dp_duplicate_post', 'duplicate_post_copy_comments', 40, 2 );
74-
add_action( 'dp_duplicate_page', 'duplicate_post_copy_comments', 40, 2 );
70+
add_action( 'duplicate_post_after_duplicated', 'duplicate_post_copy_comments', 40, 2 );
7571
}
7672

77-
add_action( 'dp_duplicate_post', 'duplicate_post_copy_post_taxonomies', 50, 2 );
78-
add_action( 'dp_duplicate_page', 'duplicate_post_copy_post_taxonomies', 50, 2 );
73+
add_action( 'duplicate_post_after_duplicated', 'duplicate_post_copy_post_taxonomies', 50, 2 );
7974

8075
add_filter( 'plugin_row_meta', 'duplicate_post_add_plugin_links', 10, 2 );
8176
}
@@ -737,11 +732,22 @@ function duplicate_post_create_duplicate( $post, $status = '', $parent_id = '' )
737732
// information about a post you can hook this action to dupe that data.
738733
if ( $new_post_id !== 0 && ! is_wp_error( $new_post_id ) ) {
739734

735+
/**
736+
* Fires after a post has been duplicated.
737+
*
738+
* @param int $new_post_id The ID of the new post.
739+
* @param WP_Post $post The original post object.
740+
* @param string $status The status of the new post.
741+
* @param string $post_type The post type of the duplicated post.
742+
*/
743+
do_action( 'duplicate_post_after_duplicated', $new_post_id, $post, $status, $post->post_type );
744+
745+
// Deprecated hooks for backward compatibility.
740746
if ( $post->post_type === 'page' || is_post_type_hierarchical( $post->post_type ) ) {
741-
do_action( 'dp_duplicate_page', $new_post_id, $post, $status );
747+
do_action_deprecated( 'dp_duplicate_page', [ $new_post_id, $post, $status ], 'Yoast Duplicate Post 4.6', 'duplicate_post_after_duplicated' );
742748
}
743749
else {
744-
do_action( 'dp_duplicate_post', $new_post_id, $post, $status );
750+
do_action_deprecated( 'dp_duplicate_post', [ $new_post_id, $post, $status ], 'Yoast Duplicate Post 4.6', 'duplicate_post_after_duplicated' );
745751
}
746752

747753
delete_post_meta( $new_post_id, '_dp_original' );

compat/wpml-functions.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
*/
1818
function duplicate_post_wpml_init() {
1919
if ( defined( 'ICL_SITEPRESS_VERSION' ) ) {
20-
add_action( 'dp_duplicate_page', 'duplicate_post_wpml_copy_translations', 10, 3 );
21-
add_action( 'dp_duplicate_post', 'duplicate_post_wpml_copy_translations', 10, 3 );
20+
add_action( 'duplicate_post_after_duplicated', 'duplicate_post_wpml_copy_translations', 10, 3 );
2221
add_action( 'shutdown', 'duplicate_wpml_string_packages', 11 );
2322
}
2423
}
@@ -44,8 +43,7 @@ function duplicate_post_wpml_copy_translations( $post_id, $post, $status = '' )
4443
global $sitepress;
4544
global $duplicated_posts;
4645

47-
remove_action( 'dp_duplicate_page', 'duplicate_post_wpml_copy_translations', 10 );
48-
remove_action( 'dp_duplicate_post', 'duplicate_post_wpml_copy_translations', 10 );
46+
remove_action( 'duplicate_post_after_duplicated', 'duplicate_post_wpml_copy_translations', 10 );
4947

5048
$current_language = $sitepress->get_current_language();
5149
$trid = $sitepress->get_element_trid( $post->ID );

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"Yoast\\WP\\Duplicate_Post\\Config\\Composer\\Actions::check_coding_standards"
6161
],
6262
"check-cs-thresholds": [
63-
"@putenv YOASTCS_THRESHOLD_ERRORS=67",
63+
"@putenv YOASTCS_THRESHOLD_ERRORS=57",
6464
"@putenv YOASTCS_THRESHOLD_WARNINGS=0",
6565
"Yoast\\WP\\Duplicate_Post\\Config\\Composer\\Actions::check_cs_thresholds"
6666
],

tests/WP/Admin_Functions_Test.php

Lines changed: 117 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,11 @@ public static function set_up_before_class() {
6666

6767
// Register the hooks that are normally registered in admin_init.
6868
// These are always needed for duplication to work properly.
69-
if ( ! \has_action( 'dp_duplicate_post', 'duplicate_post_copy_post_meta_info' ) ) {
70-
\add_action( 'dp_duplicate_post', 'duplicate_post_copy_post_meta_info', 10, 2 );
69+
if ( ! \has_action( 'duplicate_post_after_duplicated', 'duplicate_post_copy_post_meta_info' ) ) {
70+
\add_action( 'duplicate_post_after_duplicated', 'duplicate_post_copy_post_meta_info', 10, 2 );
7171
}
72-
if ( ! \has_action( 'dp_duplicate_page', 'duplicate_post_copy_post_meta_info' ) ) {
73-
\add_action( 'dp_duplicate_page', 'duplicate_post_copy_post_meta_info', 10, 2 );
74-
}
75-
if ( ! \has_action( 'dp_duplicate_post', 'duplicate_post_copy_post_taxonomies' ) ) {
76-
\add_action( 'dp_duplicate_post', 'duplicate_post_copy_post_taxonomies', 50, 2 );
77-
}
78-
if ( ! \has_action( 'dp_duplicate_page', 'duplicate_post_copy_post_taxonomies' ) ) {
79-
\add_action( 'dp_duplicate_page', 'duplicate_post_copy_post_taxonomies', 50, 2 );
72+
if ( ! \has_action( 'duplicate_post_after_duplicated', 'duplicate_post_copy_post_taxonomies' ) ) {
73+
\add_action( 'duplicate_post_after_duplicated', 'duplicate_post_copy_post_taxonomies', 50, 2 );
8074
}
8175
}
8276

@@ -88,6 +82,9 @@ public static function set_up_before_class() {
8882
public function set_up() {
8983
parent::set_up();
9084

85+
// Ensure we're in admin context.
86+
\set_current_screen( 'edit.php' );
87+
9188
// Store original options.
9289
foreach ( self::$options_to_reset as $option ) {
9390
$this->original_options[ $option ] = \get_option( $option );
@@ -788,7 +785,7 @@ public function test_create_duplicate_copies_children_when_enabled() {
788785

789786
// The hook is registered in admin_init based on the option value at that time.
790787
// Since we changed the option after admin_init, we need to add the hook manually.
791-
\add_action( 'dp_duplicate_page', 'duplicate_post_copy_children', 20, 3 );
788+
\add_action( 'duplicate_post_after_duplicated', 'duplicate_post_copy_children', 20, 3 );
792789

793790
$parent = $this->factory->post->create_and_get(
794791
[
@@ -810,7 +807,7 @@ public function test_create_duplicate_copies_children_when_enabled() {
810807
$new_parent_id = \duplicate_post_create_duplicate( $parent );
811808

812809
// Remove the hook to avoid affecting other tests.
813-
\remove_action( 'dp_duplicate_page', 'duplicate_post_copy_children', 20 );
810+
\remove_action( 'duplicate_post_after_duplicated', 'duplicate_post_copy_children', 20 );
814811

815812
// Get children of the new parent.
816813
$new_children = \get_children(
@@ -878,7 +875,7 @@ public function test_create_duplicate_copies_comments_when_enabled() {
878875

879876
// The hook is registered in admin_init based on the option value at that time.
880877
// Since we changed the option after admin_init, we need to add the hook manually.
881-
\add_action( 'dp_duplicate_post', 'duplicate_post_copy_comments', 40, 2 );
878+
\add_action( 'duplicate_post_after_duplicated', 'duplicate_post_copy_comments', 40, 2 );
882879

883880
$original = $this->create_original_post();
884881

@@ -892,7 +889,7 @@ public function test_create_duplicate_copies_comments_when_enabled() {
892889
$new_id = \duplicate_post_create_duplicate( $original );
893890

894891
// Remove the hook to avoid affecting other tests.
895-
\remove_action( 'dp_duplicate_post', 'duplicate_post_copy_comments', 40 );
892+
\remove_action( 'duplicate_post_after_duplicated', 'duplicate_post_copy_comments', 40 );
896893

897894
$new_comments = \get_comments( [ 'post_id' => $new_id ] );
898895

@@ -973,31 +970,128 @@ public function test_create_duplicate_fires_post_copy_action() {
973970
}
974971

975972
/**
976-
* Tests that duplicate_post_create_duplicate fires the dp_duplicate_post action.
973+
* Tests that duplicate_post_create_duplicate fires the after_duplicated action.
977974
*
978975
* @covers ::duplicate_post_create_duplicate
979976
*
980977
* @return void
981978
*/
982-
public function test_create_duplicate_fires_dp_duplicate_post_action() {
979+
public function test_create_duplicate_fires_after_duplicated_action() {
983980
$captured_data = [];
984-
$callback = static function ( $new_id, $post, $status ) use ( &$captured_data ) {
981+
$callback = static function ( $new_id, $post, $status, $post_type ) use ( &$captured_data ) {
985982
$captured_data = [
986-
'new_id' => $new_id,
987-
'post' => $post,
988-
'status' => $status,
983+
'new_id' => $new_id,
984+
'post' => $post,
985+
'status' => $status,
986+
'post_type' => $post_type,
989987
];
990988
};
991989

992-
\add_action( 'dp_duplicate_post', $callback, 10, 3 );
990+
\add_action( 'duplicate_post_after_duplicated', $callback, 10, 4 );
993991

994992
$original = $this->create_original_post();
995-
$new_id = \duplicate_post_create_duplicate( $original );
993+
$new_id = \duplicate_post_create_duplicate( $original, 'draft' );
994+
995+
\remove_action( 'duplicate_post_after_duplicated', $callback, 10 );
996+
997+
$this->assertEquals( $new_id, $captured_data['new_id'] );
998+
$this->assertEquals( $original->ID, $captured_data['post']->ID );
999+
$this->assertEquals( 'post', $captured_data['post_type'] );
1000+
$this->assertEquals( 'draft', $captured_data['status'] );
1001+
}
1002+
1003+
/**
1004+
* Tests that duplicate_post_create_duplicate fires the after_duplicated action with page post type.
1005+
*
1006+
* @covers ::duplicate_post_create_duplicate
1007+
*
1008+
* @return void
1009+
*/
1010+
public function test_create_duplicate_fires_after_duplicated_action_for_page() {
1011+
$captured_data = [];
1012+
$callback = static function ( $new_id, $post, $status, $post_type ) use ( &$captured_data ) {
1013+
$captured_data = [
1014+
'new_id' => $new_id,
1015+
'post' => $post,
1016+
'status' => $status,
1017+
'post_type' => $post_type,
1018+
];
1019+
};
1020+
1021+
\add_action( 'duplicate_post_after_duplicated', $callback, 10, 4 );
1022+
1023+
$original = $this->factory->post->create_and_get(
1024+
[
1025+
'post_type' => 'page',
1026+
'post_title' => 'Original Page for Hook Test',
1027+
'post_content' => 'Page content for testing hook.',
1028+
'post_status' => 'publish',
1029+
]
1030+
);
1031+
1032+
$new_id = \duplicate_post_create_duplicate( $original, 'draft' );
1033+
1034+
\remove_action( 'duplicate_post_after_duplicated', $callback, 10 );
1035+
1036+
$this->assertEquals( $new_id, $captured_data['new_id'] );
1037+
$this->assertEquals( $original->ID, $captured_data['post']->ID );
1038+
$this->assertEquals( 'page', $captured_data['post_type'] );
1039+
$this->assertEquals( 'draft', $captured_data['status'] );
1040+
}
1041+
1042+
/**
1043+
* Tests that duplicate_post_create_duplicate fires the after_duplicated action with custom post type.
1044+
*
1045+
* @covers ::duplicate_post_create_duplicate
1046+
*
1047+
* @return void
1048+
*/
1049+
public function test_create_duplicate_fires_after_duplicated_action_for_custom_post_type() {
1050+
// Register a custom post type for testing.
1051+
\register_post_type(
1052+
'dp_test_cpt',
1053+
[
1054+
'public' => true,
1055+
'label' => 'Test CPT',
1056+
'supports' => [ 'title', 'editor', 'excerpt' ],
1057+
]
1058+
);
1059+
1060+
// Enable the custom post type for duplication.
1061+
\update_option( 'duplicate_post_types_enabled', [ 'post', 'page', 'dp_test_cpt' ] );
1062+
1063+
$captured_data = [];
1064+
$callback = static function ( $new_id, $post, $status, $post_type ) use ( &$captured_data ) {
1065+
$captured_data = [
1066+
'new_id' => $new_id,
1067+
'post' => $post,
1068+
'status' => $status,
1069+
'post_type' => $post_type,
1070+
];
1071+
};
1072+
1073+
\add_action( 'duplicate_post_after_duplicated', $callback, 10, 4 );
1074+
1075+
$original = $this->factory->post->create_and_get(
1076+
[
1077+
'post_type' => 'dp_test_cpt',
1078+
'post_title' => 'Original Custom Post Type',
1079+
'post_content' => 'Custom post type content for testing hook.',
1080+
'post_status' => 'publish',
1081+
]
1082+
);
1083+
1084+
$new_id = \duplicate_post_create_duplicate( $original, 'pending' );
1085+
1086+
\remove_action( 'duplicate_post_after_duplicated', $callback, 10 );
9961087

997-
\remove_action( 'dp_duplicate_post', $callback, 10 );
1088+
// Unregister the custom post type.
1089+
\unregister_post_type( 'dp_test_cpt' );
9981090

9991091
$this->assertEquals( $new_id, $captured_data['new_id'] );
10001092
$this->assertEquals( $original->ID, $captured_data['post']->ID );
1093+
$this->assertEquals( 'dp_test_cpt', $captured_data['post_type'] );
1094+
$this->assertEquals( 'pending', $captured_data['status'] );
10011095
}
10021096

10031097
/**

0 commit comments

Comments
 (0)