Skip to content

Commit 9b35605

Browse files
Merge pull request #451 from Yoast/enrico/adds-republish-hooks
Add action hooks before and after republishing.
2 parents bc4e017 + 2fae17f commit 9b35605

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/post-republisher.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,21 @@ public function is_rest_request() {
257257
* @return void
258258
*/
259259
public function republish( WP_Post $post, WP_Post $original_post ) {
260+
261+
/**
262+
* Fires before the Rewrite & Republish copy is republished to the original post.
263+
*
264+
* This action runs before any content, taxonomies, or meta are copied from the
265+
* Rewrite & Republish copy to the original post. Use this hook to perform actions
266+
* or modifications before the republishing process begins.
267+
*
268+
* @since 4.6
269+
*
270+
* @param WP_Post $post The Rewrite & Republish copy.
271+
* @param WP_Post $original_post The original post that will be overwritten.
272+
*/
273+
\do_action( 'duplicate_post_before_republish', $post, $original_post );
274+
260275
// Remove WordPress default filter so a new revision is not created on republish.
261276
\remove_action( 'post_updated', 'wp_save_post_revision', 10 );
262277

@@ -272,6 +287,21 @@ public function republish( WP_Post $post, WP_Post $original_post ) {
272287

273288
// Re-enable the creation of a new revision.
274289
\add_action( 'post_updated', 'wp_save_post_revision', 10, 1 );
290+
291+
/**
292+
* Fires after the Rewrite & Republish copy has been republished to the original post.
293+
*
294+
* This action runs after all content, taxonomies, and meta have been copied from
295+
* the Rewrite & Republish copy to the original post. The copy is marked as republished
296+
* but has not yet been deleted. Use this hook to perform cleanup or additional
297+
* processing after the republishing is complete.
298+
*
299+
* @since 4.6
300+
*
301+
* @param WP_Post $post The Rewrite & Republish copy.
302+
* @param WP_Post $original_post The original post that has been updated.
303+
*/
304+
\do_action( 'duplicate_post_after_republish', $post, $original_post );
275305
}
276306

277307
/**

tests/WP/Post_Republisher_Test.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,4 +1428,34 @@ public function test_republish_does_not_remove_meta_deleted_from_copy() {
14281428
// This is the expected behavior based on how the duplicator works.
14291429
$this->assertEquals( 'original_value', \get_post_meta( $original->ID, 'custom_meta_to_remove', true ) );
14301430
}
1431+
1432+
/**
1433+
* Tests that duplicate_post_before_republish is fired before duplicate_post_after_republish.
1434+
*
1435+
* @covers ::republish
1436+
*
1437+
* @return void
1438+
*/
1439+
public function test_republish_fires_hooks_in_correct_order() {
1440+
$action_order = [];
1441+
$before_callback = static function () use ( &$action_order ) {
1442+
$action_order[] = 'before';
1443+
};
1444+
$after_callback = static function () use ( &$action_order ) {
1445+
$action_order[] = 'after';
1446+
};
1447+
1448+
\add_action( 'duplicate_post_before_republish', $before_callback );
1449+
\add_action( 'duplicate_post_after_republish', $after_callback );
1450+
1451+
$original = $this->create_original_post();
1452+
$copy = $this->create_rewrite_and_republish_copy( $original );
1453+
1454+
$this->instance->republish( $copy, $original );
1455+
1456+
\remove_action( 'duplicate_post_before_republish', $before_callback );
1457+
\remove_action( 'duplicate_post_after_republish', $after_callback );
1458+
1459+
$this->assertSame( [ 'before', 'after' ], $action_order );
1460+
}
14311461
}

0 commit comments

Comments
 (0)