Multisite: Ensure `wpmu_new_blog` hook receives expected data in `$meta`.

Restores `public`, `archived`, `mature`, `spam`, `deleted`, `lang_id`, and `WPLANG` to the `$meta` data passed to `wpmu_new_blog`. This hook was deprecated in 5.1.0, but code using it still relies on this data.

Props david.binda, pbiron.
Fixes #46351.


git-svn-id: https://develop.svn.wordpress.org/trunk@44805 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jeremy Felt 2019-03-07 03:33:25 +00:00
parent 0cc536e173
commit 0d707e5da2
2 changed files with 99 additions and 10 deletions

View File

@ -52,18 +52,12 @@ function wp_insert_site( array $data ) {
'lang_id' => 0,
);
// Extract the passed arguments that may be relevant for site initialization.
$args = array_diff_key( $data, $defaults );
if ( isset( $args['site_id'] ) ) {
unset( $args['site_id'] );
$prepared_data = wp_prepare_site_data( $data, $defaults );
if ( is_wp_error( $prepared_data ) ) {
return $prepared_data;
}
$data = wp_prepare_site_data( $data, $defaults );
if ( is_wp_error( $data ) ) {
return $data;
}
if ( false === $wpdb->insert( $wpdb->blogs, $data ) ) {
if ( false === $wpdb->insert( $wpdb->blogs, $prepared_data ) ) {
return new WP_Error( 'db_insert_error', __( 'Could not insert site into the database.' ), $wpdb->last_error );
}
@ -84,6 +78,12 @@ function wp_insert_site( array $data ) {
*/
do_action( 'wp_insert_site', $new_site );
// Extract the passed arguments that may be relevant for site initialization.
$args = array_diff_key( $data, $defaults );
if ( isset( $args['site_id'] ) ) {
unset( $args['site_id'] );
}
/**
* Fires when a site's initialization routine should be executed.
*
@ -99,6 +99,16 @@ function wp_insert_site( array $data ) {
$user_id = ! empty( $args['user_id'] ) ? $args['user_id'] : 0;
$meta = ! empty( $args['options'] ) ? $args['options'] : array();
// WPLANG was passed with `$meta` to the `wpmu_new_blog` hook prior to 5.1.0.
if ( ! array_key_exists( 'WPLANG', $meta ) ) {
$meta['WPLANG'] = get_network_option( $new_site->network_id, 'WPLANG' );
}
// Rebuild the data expected by the `wpmu_new_blog` hook prior to 5.1.0 using whitelisted keys.
// The `$site_data_whitelist` matches the one used in `wpmu_create_blog()`.
$site_data_whitelist = array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
$meta = array_merge( array_intersect_key( $data, array_flip( $site_data_whitelist ) ), $meta );
/**
* Fires immediately after a new site is created.
*

View File

@ -12,6 +12,7 @@ if ( is_multisite() ) :
protected $suppress = false;
protected $site_status_hooks = array();
protected $wp_initialize_site_args = array();
protected $wp_initialize_site_meta = array();
protected static $network_ids;
protected static $site_ids;
protected static $uninitialized_site_id;
@ -2348,6 +2349,84 @@ if ( is_multisite() ) :
// Set siteurl
update_option( 'siteurl', 'http://testsite1.example.org/test' );
}
/**
* Tests whether all expected meta are provided in deprecated `wpmu_new_blog` action.
*
* @dataProvider data_wpmu_new_blog_action_backward_commpatible
*
* @ticket 46351
*/
public function test_wpmu_new_blog_action_backward_compatible( $meta, $expected_meta ) {
// We are testing deprecated hook. Register it to expected deprecated notices.
$this->setExpectedDeprecated( 'wpmu_new_blog' );
add_action( 'wpmu_new_blog', array( $this, 'wpmu_new_blog_callback' ), 10, 6 );
wpmu_create_blog( 'testsite1.example.org', '/new-blog/', 'New Blog', get_current_user_id(), $meta, 1 );
$this->assertEquals( $expected_meta, $this->wp_initialize_site_meta );
$this->wp_initialize_site_meta = array();
}
/**
* Capture the $meta value passed to the wpmu_new_blog action and compare it.
*/
public function wpmu_new_blog_callback( $blog_id, $user_id, $domain, $path, $network_id, $meta ) {
$this->wp_initialize_site_meta = $meta;
}
public function data_wpmu_new_blog_action_backward_commpatible() {
return array(
'default values' => array(
array(
),
array(
'public' => 0, // `public` is one of the defaults metas in `wpmu_create_blog' function prior WordPress 5.1.0
'WPLANG' => 'en_US', // WPLANG is another default meta in `wpmu_create_blog` function prior WordPress 5.1.0.
),
),
'public site' => array(
array(
'public' => 1,
),
array(
'public' => 1,
'WPLANG' => 'en_US'
),
),
'all whitelisted' => array(
array(
'public' => -1,
'archived' => 0,
'mature' => 0,
'spam' => 0,
'deleted' => 0,
'lang_id' => 11,
),
array(
'public' => -1,
'WPLANG' => 'en_US',
'archived' => 0,
'mature' => 0,
'spam' => 0,
'deleted' => 0,
'lang_id' => 11,
),
),
'extra meta key' => array(
array(
'foo' => 'bar',
),
array(
'public' => 0,
'WPLANG' => 'en_US',
'foo' => 'bar',
),
),
);
}
}
endif;