Skip to content

Commit aa5fdef

Browse files
committed
Networks and Sites: introduce 3 new actions inside the populate_network() function.
* `before_populate_network` * `after_populate_network` * `after_upgrade_to_multisite` These hooks are strategically located to allow the execution of custom code before & after a new multisite network is created, as well as after the first time a single-site installation is upgraded to multisite. This change also includes a set of unit tests via a new `Tests_Multisite_PopulateNetworkHooks` class, to confirm that these hooks are executing when and as intended. Props jeremyfelt, johnjamesjacoby, Mista-Flo, spacedmonkey, sukhendu2002, rmccue, rollybueno. Fixes #27289. git-svn-id: https://develop.svn.wordpress.org/trunk@60954 602fd350-edb4-49c9-b593-d223f7449a82
1 parent c309f28 commit aa5fdef

2 files changed

Lines changed: 152 additions & 0 deletions

File tree

src/wp-admin/includes/schema.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,20 @@ function populate_network( $network_id = 1, $domain = '', $email = '', $site_nam
10041004

10051005
$network_id = (int) $network_id;
10061006

1007+
/**
1008+
* Fires before a network is populated.
1009+
*
1010+
* @since 6.9.0
1011+
*
1012+
* @param int $network_id ID of network to populate.
1013+
* @param string $domain The domain name for the network.
1014+
* @param string $email Email address for the network administrator.
1015+
* @param string $site_name The name of the network.
1016+
* @param string $path The path to append to the network's domain name.
1017+
* @param bool $subdomain_install Whether the network is a subdomain installation or a subdirectory installation.
1018+
*/
1019+
do_action( 'before_populate_network', $network_id, $domain, $email, $site_name, $path, $subdomain_install );
1020+
10071021
$errors = new WP_Error();
10081022
if ( '' === $domain ) {
10091023
$errors->add( 'empty_domain', __( 'You must provide a domain name.' ) );
@@ -1123,6 +1137,20 @@ function populate_network( $network_id = 1, $domain = '', $email = '', $site_nam
11231137

11241138
flush_rewrite_rules();
11251139

1140+
/**
1141+
* Fires after a network is created when converting a single site to multisite.
1142+
*
1143+
* @since 6.9.0
1144+
*
1145+
* @param int $network_id ID of network created.
1146+
* @param string $domain The domain name for the network.
1147+
* @param string $email Email address for the network administrator.
1148+
* @param string $site_name The name of the network.
1149+
* @param string $path The path to append to the network's domain name.
1150+
* @param bool $subdomain_install Whether the network is a subdomain installation or a subdirectory installation.
1151+
*/
1152+
do_action( 'after_upgrade_to_multisite', $network_id, $domain, $email, $site_name, $path, $subdomain_install );
1153+
11261154
if ( ! $subdomain_install ) {
11271155
return true;
11281156
}
@@ -1169,6 +1197,20 @@ function populate_network( $network_id = 1, $domain = '', $email = '', $site_nam
11691197
}
11701198
}
11711199

1200+
/**
1201+
* Fires after a network is fully populated.
1202+
*
1203+
* @since 6.9.0
1204+
*
1205+
* @param int $network_id ID of network created.
1206+
* @param string $domain The domain name for the network.
1207+
* @param string $email Email address for the network administrator.
1208+
* @param string $site_name The name of the network.
1209+
* @param string $path The path to append to the network's domain name.
1210+
* @param bool $subdomain_install Whether the network is a subdomain installation or a subdirectory installation.
1211+
*/
1212+
do_action( 'after_populate_network', $network_id, $domain, $email, $site_name, $path, $subdomain_install );
1213+
11721214
return true;
11731215
}
11741216

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
if ( 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+
110+
endif;

0 commit comments

Comments
 (0)