@@ -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