Make WordPress Core

Changeset 59020


Ignore:
Timestamp:
09/13/2024 10:10:51 PM (2 years ago)
Author:
SergeyBiryukov
Message:

Networks and Sites: Set WP_Network properties via setters upon creation.

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-network.php

    r58138 r59020  
    132132        public function __construct( $network ) {
    133133                foreach ( get_object_vars( $network ) as $key => $value ) {
    134                         $this->$key = $value;
     134                        $this->__set( $key, $value );
    135135                }
    136136
  • trunk/tests/phpunit/tests/multisite/network.php

    r56549 r59020  
    128128                /**
    129129                 * @ticket 37050
     130                 *
     131                 * @covers WP_Network::__get
    130132                 */
    131133                public function test_wp_network_object_id_property_is_int() {
     
    135137
    136138                        $this->assertSame( (int) $id, $network->id );
     139                }
     140
     141                /**
     142                 * Tests that the `WP_Network::$id` property is stored as int.
     143                 *
     144                 * Uses reflection to access the private property.
     145                 * Differs from using the public getter method, which casts to int.
     146                 *
     147                 * @ticket 62035
     148                 *
     149                 * @covers WP_Network::__construct
     150                 */
     151                public function test_wp_network_object_id_property_stored_as_int() {
     152                        $id = self::factory()->network->create();
     153
     154                        $network = WP_Network::get_instance( $id );
     155
     156                        $reflection = new ReflectionObject( $network );
     157                        $property   = $reflection->getProperty( 'id' );
     158                        $property->setAccessible( true );
     159
     160                        $this->assertSame( (int) $id, $property->getValue( $network ) );
    137161                }
    138162
Note: See TracChangeset for help on using the changeset viewer.