Skip to content

Commit

Permalink
Networks and Sites: Set WP_Network properties via setters upon crea…
Browse files Browse the repository at this point in the history
…tion.

This ensures that `WP_Network::$id` is stored internally as `int`, to match the documented type.

Follow-up to [37870].

Props ironprogrammer, scottculverhouse, spacedmonkey, SergeyBiryukov.
See #62035.

git-svn-id: https://develop.svn.wordpress.org/trunk@59020 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed Sep 13, 2024
1 parent a78540b commit be75ea8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/wp-includes/class-wp-network.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public static function get_instance( $network_id ) {
*/
public function __construct( $network ) {
foreach ( get_object_vars( $network ) as $key => $value ) {
$this->$key = $value;
$this->__set( $key, $value );
}

$this->_set_site_name();
Expand Down
24 changes: 24 additions & 0 deletions tests/phpunit/tests/multisite/network.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ public function get_main_network_id() {

/**
* @ticket 37050
*
* @covers WP_Network::__get
*/
public function test_wp_network_object_id_property_is_int() {
$id = self::factory()->network->create();
Expand All @@ -136,6 +138,28 @@ public function test_wp_network_object_id_property_is_int() {
$this->assertSame( (int) $id, $network->id );
}

/**
* Tests that the `WP_Network::$id` property is stored as int.
*
* Uses reflection to access the private property.
* Differs from using the public getter method, which casts to int.
*
* @ticket 62035
*
* @covers WP_Network::__construct
*/
public function test_wp_network_object_id_property_stored_as_int() {
$id = self::factory()->network->create();

$network = WP_Network::get_instance( $id );

$reflection = new ReflectionObject( $network );
$property = $reflection->getProperty( 'id' );
$property->setAccessible( true );

$this->assertSame( (int) $id, $property->getValue( $network ) );
}

/**
* @ticket 22917
*/
Expand Down

0 comments on commit be75ea8

Please sign in to comment.