Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hooks for network population process and related tests #8532

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/wp-admin/includes/schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,20 @@ function populate_network( $network_id = 1, $domain = '', $email = '', $site_nam

$network_id = (int) $network_id;

/**
* Fires before a network is populated.
*
* @since 6.8.0
*
* @param int $network_id ID of network to populate.
* @param string $domain The domain name for the network.
* @param string $email Email address for the network administrator.
* @param string $site_name The name of the network.
* @param string $path The path to append to the network's domain name.
* @param bool $subdomain_install Whether the network is a subdomain installation or a subdirectory installation.
*/
do_action( 'before_populate_network', $network_id, $domain, $email, $site_name, $path, $subdomain_install );

$errors = new WP_Error();
if ( '' === $domain ) {
$errors->add( 'empty_domain', __( 'You must provide a domain name.' ) );
Expand Down Expand Up @@ -1107,6 +1121,20 @@ function populate_network( $network_id = 1, $domain = '', $email = '', $site_nam

flush_rewrite_rules();

/**
* Fires after a network is created when converting a single site to multisite.
*
* @since 6.8.0
*
* @param int $network_id ID of network created.
* @param string $domain The domain name for the network.
* @param string $email Email address for the network administrator.
* @param string $site_name The name of the network.
* @param string $path The path to append to the network's domain name.
* @param bool $subdomain_install Whether the network is a subdomain installation or a subdirectory installation.
*/
do_action( 'after_upgrade_to_multisite', $network_id, $domain, $email, $site_name, $path, $subdomain_install );

if ( ! $subdomain_install ) {
return true;
}
Expand Down Expand Up @@ -1153,6 +1181,20 @@ function populate_network( $network_id = 1, $domain = '', $email = '', $site_nam
}
}

/**
* Fires after a network is fully populated.
*
* @since 6.8.0
*
* @param int $network_id ID of network created.
* @param string $domain The domain name for the network.
* @param string $email Email address for the network administrator.
* @param string $site_name The name of the network.
* @param string $path The path to append to the network's domain name.
* @param bool $subdomain_install Whether the network is a subdomain installation or a subdirectory installation.
*/
do_action( 'after_populate_network', $network_id, $domain, $email, $site_name, $path, $subdomain_install );

return true;
}

Expand Down
110 changes: 110 additions & 0 deletions tests/phpunit/tests/multisite/populateNetworkHooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

if ( is_multisite() ) :

/**
* Tests for the populate_network hooks.
*
* @group ms-network
* @group ms-populate-network
* @group multisite
*/
class Tests_Multisite_PopulateNetworkHooks extends WP_UnitTestCase {
protected $action_counts = array(
'before_populate_network' => 0,
'after_upgrade_to_multisite' => 0,
'after_populate_network' => 0,
);

protected $action_args = array();

/**
* Flag to track if hook was called.
*/
public $hook_called = false;

public function hook_action_counter( $network_id, $domain, $email, $site_name, $path, $subdomain_install ) {
$action = current_filter();
++$this->action_counts[ $action ];
$this->action_args[ $action ] = array(
'network_id' => $network_id,
'domain' => $domain,
'email' => $email,
'site_name' => $site_name,
'path' => $path,
'subdomain_install' => $subdomain_install,
);
}

/**
* Test that the before_populate_network hook fires.
*
* @ticket 27289
*/
public function test_before_populate_network_hook() {
$this->action_counts = array_fill_keys( array_keys( $this->action_counts ), 0 );
$this->action_args = array();

add_action( 'before_populate_network', array( $this, 'hook_action_counter' ), 10, 6 );
add_action( 'after_populate_network', array( $this, 'hook_action_counter' ), 10, 6 );

$domain = 'example' . time() . '.org';
$network_id = self::factory()->network->create(
array(
'domain' => $domain,
'path' => '/',
)
);

$this->assertSame( 1, $this->action_counts['before_populate_network'], 'before_populate_network action should fire once' );
$this->assertSame( 1, $this->action_counts['after_populate_network'], 'after_populate_network action should fire once' );

$this->assertEquals( $network_id, $this->action_args['before_populate_network']['network_id'], 'Network ID should match in before_populate_network hook' );
$this->assertEquals( $domain, $this->action_args['before_populate_network']['domain'], 'Domain should match in before_populate_network hook' );
$this->assertEquals( $network_id, $this->action_args['after_populate_network']['network_id'], 'Network ID should match in after_populate_network hook' );
$this->assertEquals( $domain, $this->action_args['after_populate_network']['domain'], 'Domain should match in after_populate_network hook' );

remove_action( 'before_populate_network', array( $this, 'hook_action_counter' ), 10 );
remove_action( 'after_populate_network', array( $this, 'hook_action_counter' ), 10 );

global $wpdb;
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $network_id ) );
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id = %d", $network_id ) );
}

/**
* Test that the hooks can modify parameters.
*
* @ticket 27289
*/
public function test_populate_network_hook_filter() {
$this->hook_called = false;

add_action( 'before_populate_network', array( $this, 'modify_domain_hook' ), 10, 6 );

$domain = 'example' . time() . '.org';
$network_id = self::factory()->network->create(
array(
'domain' => $domain,
'path' => '/',
)
);

$this->assertTrue( $this->hook_called, 'The modify_domain_hook should have been called' );

remove_action( 'before_populate_network', array( $this, 'modify_domain_hook' ), 10 );

global $wpdb;
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $network_id ) );
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id = %d", $network_id ) );
}

/**
* Action hook to track if hooks are being called.
*/
public function modify_domain_hook( $network_id, $domain, $email, $site_name, $path, $subdomain_install ) {
$this->hook_called = true;
}
}

endif;
Loading