Make WordPress Core

Ticket #27289: 27289-2.patch

File 27289-2.patch, 6.6 KB (added by johnjamesjacoby, 5 months ago)

Refreshed for 6.9

  • src/wp-admin/includes/schema.php

     
    10031003
    10041004        $network_id = (int) $network_id;
    10051005
     1006        /**
     1007         * Fires before a network is populated.
     1008         *
     1009         * @since 6.9.0
     1010         *
     1011         * @param int    $network_id        ID of network to populate.
     1012         * @param string $domain            The domain name for the network.
     1013         * @param string $email             Email address for the network administrator.
     1014         * @param string $site_name         The name of the network.
     1015         * @param string $path              The path to append to the network's domain name.
     1016         * @param bool   $subdomain_install Whether the network is a subdomain installation or a subdirectory installation.
     1017         */
     1018        do_action( 'before_populate_network', $network_id, $domain, $email, $site_name, $path, $subdomain_install );
     1019
    10061020        $errors = new WP_Error();
    10071021        if ( '' === $domain ) {
    10081022                $errors->add( 'empty_domain', __( 'You must provide a domain name.' ) );
     
    11221136
    11231137                flush_rewrite_rules();
    11241138
     1139                /**
     1140                 * Fires after a network is created when converting a single site to multisite.
     1141                 *
     1142                 * @since 6.9.0
     1143                 *
     1144                 * @param int    $network_id        ID of network created.
     1145                 * @param string $domain            The domain name for the network.
     1146                 * @param string $email             Email address for the network administrator.
     1147                 * @param string $site_name         The name of the network.
     1148                 * @param string $path              The path to append to the network's domain name.
     1149                 * @param bool   $subdomain_install Whether the network is a subdomain installation or a subdirectory installation.
     1150                 */
     1151                do_action( 'after_upgrade_to_multisite', $network_id, $domain, $email, $site_name, $path, $subdomain_install );
     1152
    11251153                if ( ! $subdomain_install ) {
    11261154                        return true;
    11271155                }
     
    11681196                }
    11691197        }
    11701198
     1199        /**
     1200         * Fires after a network is fully populated.
     1201         *
     1202         * @since 6.9.0
     1203         *
     1204         * @param int    $network_id        ID of network created.
     1205         * @param string $domain            The domain name for the network.
     1206         * @param string $email             Email address for the network administrator.
     1207         * @param string $site_name         The name of the network.
     1208         * @param string $path              The path to append to the network's domain name.
     1209         * @param bool   $subdomain_install Whether the network is a subdomain installation or a subdirectory installation.
     1210         */
     1211        do_action( 'after_populate_network', $network_id, $domain, $email, $site_name, $path, $subdomain_install );
     1212
    11711213        return true;
    11721214}
    11731215
  • tests/phpunit/tests/multisite/populateNetworkHooks.php

     
     1<?php
     2
     3if ( is_multisite() ) :
     4
     5        /**
     6         * Tests for the populate_network hooks.
     7         *
     8         * @group ms-network
     9         * @group ms-populate-network
     10         * @group multisite
     11         */
     12        class Tests_Multisite_PopulateNetworkHooks extends WP_UnitTestCase {
     13                protected $action_counts = array(
     14                        'before_populate_network'    => 0,
     15                        'after_upgrade_to_multisite' => 0,
     16                        'after_populate_network'     => 0,
     17                );
     18
     19                protected $action_args = array();
     20
     21                /**
     22                 * Flag to track if hook was called.
     23                 */
     24                public $hook_called = false;
     25
     26                public function hook_action_counter( $network_id, $domain, $email, $site_name, $path, $subdomain_install ) {
     27                        $action = current_filter();
     28                        ++$this->action_counts[ $action ];
     29                        $this->action_args[ $action ] = array(
     30                                'network_id'        => $network_id,
     31                                'domain'            => $domain,
     32                                'email'             => $email,
     33                                'site_name'         => $site_name,
     34                                'path'              => $path,
     35                                'subdomain_install' => $subdomain_install,
     36                        );
     37                }
     38
     39                /**
     40                 * Test that the before_populate_network hook fires.
     41                 *
     42                 * @ticket 27289
     43                 */
     44                public function test_before_populate_network_hook() {
     45                        $this->action_counts = array_fill_keys( array_keys( $this->action_counts ), 0 );
     46                        $this->action_args   = array();
     47
     48                        add_action( 'before_populate_network', array( $this, 'hook_action_counter' ), 10, 6 );
     49                        add_action( 'after_populate_network', array( $this, 'hook_action_counter' ), 10, 6 );
     50
     51                        $domain     = 'example' . time() . '.org';
     52                        $network_id = self::factory()->network->create(
     53                                array(
     54                                        'domain' => $domain,
     55                                        'path'   => '/',
     56                                )
     57                        );
     58
     59                        $this->assertSame( 1, $this->action_counts['before_populate_network'], 'before_populate_network action should fire once' );
     60                        $this->assertSame( 1, $this->action_counts['after_populate_network'], 'after_populate_network action should fire once' );
     61
     62                        $this->assertEquals( $network_id, $this->action_args['before_populate_network']['network_id'], 'Network ID should match in before_populate_network hook' );
     63                        $this->assertEquals( $domain, $this->action_args['before_populate_network']['domain'], 'Domain should match in before_populate_network hook' );
     64                        $this->assertEquals( $network_id, $this->action_args['after_populate_network']['network_id'], 'Network ID should match in after_populate_network hook' );
     65                        $this->assertEquals( $domain, $this->action_args['after_populate_network']['domain'], 'Domain should match in after_populate_network hook' );
     66
     67                        remove_action( 'before_populate_network', array( $this, 'hook_action_counter' ), 10 );
     68                        remove_action( 'after_populate_network', array( $this, 'hook_action_counter' ), 10 );
     69
     70                        global $wpdb;
     71                        $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $network_id ) );
     72                        $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id = %d", $network_id ) );
     73                }
     74
     75                /**
     76                 * Test that the hooks can modify parameters.
     77                 *
     78                 * @ticket 27289
     79                 */
     80                public function test_populate_network_hook_filter() {
     81                        $this->hook_called = false;
     82
     83                        add_action( 'before_populate_network', array( $this, 'modify_domain_hook' ), 10, 6 );
     84
     85                        $domain     = 'example' . time() . '.org';
     86                        $network_id = self::factory()->network->create(
     87                                array(
     88                                        'domain' => $domain,
     89                                        'path'   => '/',
     90                                )
     91                        );
     92
     93                        $this->assertTrue( $this->hook_called, 'The modify_domain_hook action should have been called' );
     94
     95                        remove_action( 'before_populate_network', array( $this, 'modify_domain_hook' ), 10 );
     96
     97                        global $wpdb;
     98                        $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $network_id ) );
     99                        $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id = %d", $network_id ) );
     100                }
     101
     102                /**
     103                 * Action to track if hooks are being executed.
     104                 */
     105                public function modify_domain_hook( $network_id, $domain, $email, $site_name, $path, $subdomain_install ) {
     106                        $this->hook_called = true;
     107                }
     108        }
     109
     110endif;