@@ -251,6 +251,20 @@ protected function create_post( array $args = array() ): int {
251251 );
252252 }
253253
254+ /**
255+ * Creates a published post and returns the post object.
256+ *
257+ * @param array<string, mixed> $args Optional. Post arguments.
258+ * @return WP_Post Post object.
259+ */
260+ protected function create_post_object ( array $ args = array () ): WP_Post {
261+ $ post = get_post ( $ this ->create_post ( $ args ) );
262+
263+ $ this ->assertInstanceOf ( WP_Post::class, $ post );
264+
265+ return $ post ;
266+ }
267+
254268 /**
255269 * Asserts that the request redirected to one of the given posts.
256270 *
@@ -1406,6 +1420,198 @@ public function test_saving_unrelated_pages_does_not_flush_rewrite_rules(): void
14061420 $ this ->assertSame ( 0 , $ flushes ->get_call_count (), 'Only pages and posts at the random base should flush the rewrite rules. ' );
14071421 }
14081422
1423+ /**
1424+ * @dataProvider data_invalid_arguments
1425+ *
1426+ * @param array<int, mixed> $args The arguments to call the function with.
1427+ */
1428+ public function test_invalid_arguments_do_not_flush_rewrite_rules ( array $ args ): void {
1429+ // The data provider cannot query the database, so PHP_INT_MAX stands in for a nonexistent post ID.
1430+ $ args = array_map (
1431+ function ( $ arg ) {
1432+ return PHP_INT_MAX === $ arg ? $ this ->get_nonexistent_post_id () : $ arg ;
1433+ },
1434+ $ args
1435+ );
1436+
1437+ $ this ->setExpectedIncorrectUsage ( 'wp_random_content_flush_rewrite_rules_for_post ' );
1438+
1439+ $ flushes = new MockAction ();
1440+ add_filter ( 'pre_update_option_rewrite_rules ' , array ( $ flushes , 'filter ' ) );
1441+
1442+ wp_random_content_flush_rewrite_rules_for_post ( ...$ args ); // @phpstan-ignore argument.type (Intentionally passing invalid values.)
1443+
1444+ $ this ->assertSame ( 0 , $ flushes ->get_call_count (), 'Invalid arguments should not flush the rewrite rules. ' );
1445+ }
1446+
1447+ /**
1448+ * Data provider.
1449+ *
1450+ * Core never fires the `wp_after_insert_post` and `after_delete_post` actions with invalid
1451+ * arguments, but third-party code can. Doing so is incorrect usage and must not cause a
1452+ * fatal error.
1453+ *
1454+ * `PHP_INT_MAX` is replaced with a post ID that is verified not to exist when the test runs.
1455+ *
1456+ * @return array<string, array{array<int, mixed>}>
1457+ */
1458+ public function data_invalid_arguments (): array {
1459+ return array (
1460+ 'nulls ' => array ( array ( null , null , false , null ) ),
1461+ 'strings ' => array ( array ( 'random ' , 'random ' , 'yes ' , 'random ' ) ),
1462+ 'arrays ' => array ( array ( array (), array ( 'post_name ' => 'random ' ), 1 , array () ) ),
1463+ 'nulls without optional values ' => array ( array ( null , null ) ),
1464+ 'strings without optional values ' => array ( array ( 'random ' , 'random ' ) ),
1465+ 'post ID of zero without a post ' => array ( array ( 0 ) ),
1466+ 'nonexistent post ID without a post ' => array ( array ( PHP_INT_MAX ) ),
1467+ );
1468+ }
1469+
1470+ /**
1471+ * Returns a post ID that does not exist.
1472+ *
1473+ * Starts at `PHP_INT_MAX` and decrements until no post is found.
1474+ *
1475+ * @return int Post ID.
1476+ */
1477+ protected function get_nonexistent_post_id (): int {
1478+ $ post_id = PHP_INT_MAX ;
1479+
1480+ while ( get_post ( $ post_id ) instanceof WP_Post ) {
1481+ --$ post_id ;
1482+ }
1483+
1484+ return $ post_id ;
1485+ }
1486+
1487+ /**
1488+ * Tests that an omitted post object does not fall back to the global post.
1489+ *
1490+ * `get_post()` returns the global post for an empty value, which would flush the
1491+ * rewrite rules for whatever post is being displayed rather than the one passed in.
1492+ */
1493+ public function test_omitted_post_does_not_fall_back_to_the_global_post (): void {
1494+ $ this ->setExpectedIncorrectUsage ( 'wp_random_content_flush_rewrite_rules_for_post ' );
1495+
1496+ $ GLOBALS ['post ' ] = $ this ->create_post_object (
1497+ array (
1498+ 'post_type ' => 'page ' ,
1499+ 'post_name ' => 'random ' ,
1500+ )
1501+ );
1502+
1503+ $ flushes = new MockAction ();
1504+ add_filter ( 'pre_update_option_rewrite_rules ' , array ( $ flushes , 'filter ' ) );
1505+
1506+ wp_random_content_flush_rewrite_rules_for_post ( null ); // @phpstan-ignore argument.type (Intentionally omitting the post ID.)
1507+
1508+ $ this ->assertSame ( 0 , $ flushes ->get_call_count (), 'An omitted post should not be looked up from the global post. ' );
1509+ }
1510+
1511+ /**
1512+ * Tests that an omitted post object is looked up from the post ID.
1513+ *
1514+ * The `$post` parameter was added to `after_delete_post` in 5.5.0, but third-party
1515+ * code written against the original signature may still fire the action with only
1516+ * the post ID.
1517+ */
1518+ public function test_omitted_post_is_looked_up_from_the_post_id (): void {
1519+ $ page_id = $ this ->create_post (
1520+ array (
1521+ 'post_type ' => 'page ' ,
1522+ 'post_name ' => 'random ' ,
1523+ )
1524+ );
1525+
1526+ $ flushes = new MockAction ();
1527+ add_filter ( 'pre_update_option_rewrite_rules ' , array ( $ flushes , 'filter ' ) );
1528+
1529+ wp_random_content_flush_rewrite_rules_for_post ( $ page_id );
1530+
1531+ $ this ->assertSame ( 1 , $ flushes ->get_call_count (), 'A page at the random base should flush the rewrite rules when only its ID is passed. ' );
1532+ }
1533+
1534+ /**
1535+ * Tests that firing `after_delete_post` with only a post ID does not cause a fatal error.
1536+ *
1537+ * As the post is already deleted, it cannot be looked up from the ID, so the call is
1538+ * incorrect usage and the rewrite rules are left alone.
1539+ *
1540+ * @covers ::_update_posts_count_on_delete
1541+ */
1542+ public function test_after_delete_post_fired_with_only_a_post_id_does_not_flush_rewrite_rules (): void {
1543+ $ post_id = $ this ->create_post ();
1544+ wp_delete_post ( $ post_id , true );
1545+
1546+ $ this ->setExpectedIncorrectUsage ( 'wp_random_content_flush_rewrite_rules_for_post ' );
1547+
1548+ $ flushes = new MockAction ();
1549+ add_filter ( 'pre_update_option_rewrite_rules ' , array ( $ flushes , 'filter ' ) );
1550+
1551+ do_action ( 'after_delete_post ' , $ post_id ); // @phpstan-ignore WordPress.hookParamCountMismatch (Intentionally omitting the post object.)
1552+
1553+ $ this ->assertSame ( 0 , $ flushes ->get_call_count (), 'Firing after_delete_post for a deleted post without the post object should not flush the rewrite rules. ' );
1554+ }
1555+
1556+ public function test_page_named_random_still_flushes_rewrite_rules_when_the_other_arguments_are_invalid (): void {
1557+ $ page = $ this ->create_post_object (
1558+ array (
1559+ 'post_type ' => 'page ' ,
1560+ 'post_name ' => 'random ' ,
1561+ )
1562+ );
1563+
1564+ $ this ->setExpectedIncorrectUsage ( 'wp_random_content_flush_rewrite_rules_for_post ' );
1565+
1566+ $ flushes = new MockAction ();
1567+ add_filter ( 'pre_update_option_rewrite_rules ' , array ( $ flushes , 'filter ' ) );
1568+
1569+ do_action ( 'wp_after_insert_post ' , (string ) $ page ->ID , $ page , 'yes ' , false );
1570+
1571+ $ this ->assertSame ( 1 , $ flushes ->get_call_count (), 'A page at the random base should flush the rewrite rules even when the other arguments are invalid. ' );
1572+ }
1573+
1574+ /**
1575+ * Tests that the previous post object is only considered on `wp_after_insert_post`.
1576+ *
1577+ * `after_delete_post` has no previous post, so a value passed in its place is ignored
1578+ * rather than reported as incorrect usage.
1579+ */
1580+ public function test_post_before_is_ignored_outside_wp_after_insert_post (): void {
1581+ $ page_before = $ this ->create_post_object (
1582+ array (
1583+ 'post_type ' => 'page ' ,
1584+ 'post_name ' => 'random ' ,
1585+ )
1586+ );
1587+ $ post = $ this ->create_post_object ();
1588+
1589+ $ flushes = new MockAction ();
1590+ add_filter ( 'pre_update_option_rewrite_rules ' , array ( $ flushes , 'filter ' ) );
1591+
1592+ wp_random_content_flush_rewrite_rules_for_post ( $ post ->ID , $ post , false , 'not a post ' ); // @phpstan-ignore argument.type (Intentionally passing an invalid value.)
1593+ wp_random_content_flush_rewrite_rules_for_post ( $ post ->ID , $ post , false , $ page_before );
1594+
1595+ $ this ->assertSame ( 0 , $ flushes ->get_call_count (), 'The previous post should not be considered outside wp_after_insert_post. ' );
1596+ }
1597+
1598+ public function test_renamed_page_at_random_base_flushes_rewrite_rules_via_post_before (): void {
1599+ $ page_before = $ this ->create_post_object (
1600+ array (
1601+ 'post_type ' => 'page ' ,
1602+ 'post_name ' => 'random ' ,
1603+ )
1604+ );
1605+ $ post = $ this ->create_post_object ();
1606+
1607+ $ flushes = new MockAction ();
1608+ add_filter ( 'pre_update_option_rewrite_rules ' , array ( $ flushes , 'filter ' ) );
1609+
1610+ do_action ( 'wp_after_insert_post ' , $ post ->ID , $ post , true , $ page_before );
1611+
1612+ $ this ->assertSame ( 1 , $ flushes ->get_call_count (), 'A page previously at the random base should flush the rewrite rules on wp_after_insert_post. ' );
1613+ }
1614+
14091615 /*
14101616 * Opting out.
14111617 */
0 commit comments