Make WordPress Core

Changeset 44807


Ignore:
Timestamp:
03/07/2019 04:36:36 AM (7 years ago)
Author:
jeremyfelt
Message:

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 davidbinda, pbiron.
Merges [44805] and [44806] to the 5.1 branch.
Fixes #46351.

Location:
branches/5.1
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/5.1

  • branches/5.1/src/wp-includes/ms-site.php

    r44800 r44807  
    5353        );
    5454
     55        $prepared_data = wp_prepare_site_data( $data, $defaults );
     56        if ( is_wp_error( $prepared_data ) ) {
     57                return $prepared_data;
     58        }
     59
     60        if ( false === $wpdb->insert( $wpdb->blogs, $prepared_data ) ) {
     61                return new WP_Error( 'db_insert_error', __( 'Could not insert site into the database.' ), $wpdb->last_error );
     62        }
     63
     64        $new_site = get_site( $wpdb->insert_id );
     65
     66        if ( ! $new_site ) {
     67                return new WP_Error( 'get_site_error', __( 'Could not retrieve site data.' ) );
     68        }
     69
     70        clean_blog_cache( $new_site );
     71
     72        /**
     73         * Fires once a site has been inserted into the database.
     74         *
     75         * @since 5.1.0
     76         *
     77         * @param WP_Site $new_site New site object.
     78         */
     79        do_action( 'wp_insert_site', $new_site );
     80
    5581        // Extract the passed arguments that may be relevant for site initialization.
    5682        $args = array_diff_key( $data, $defaults );
     
    5985        }
    6086
    61         $data = wp_prepare_site_data( $data, $defaults );
    62         if ( is_wp_error( $data ) ) {
    63                 return $data;
    64         }
    65 
    66         if ( false === $wpdb->insert( $wpdb->blogs, $data ) ) {
    67                 return new WP_Error( 'db_insert_error', __( 'Could not insert site into the database.' ), $wpdb->last_error );
    68         }
    69 
    70         $new_site = get_site( $wpdb->insert_id );
    71 
    72         if ( ! $new_site ) {
    73                 return new WP_Error( 'get_site_error', __( 'Could not retrieve site data.' ) );
    74         }
    75 
    76         clean_blog_cache( $new_site );
    77 
    78         /**
    79          * Fires once a site has been inserted into the database.
    80          *
    81          * @since 5.1.0
    82          *
    83          * @param WP_Site $new_site New site object.
    84          */
    85         do_action( 'wp_insert_site', $new_site );
    86 
    8787        /**
    8888         * Fires when a site's initialization routine should be executed.
     
    9999                $user_id = ! empty( $args['user_id'] ) ? $args['user_id'] : 0;
    100100                $meta    = ! empty( $args['options'] ) ? $args['options'] : array();
     101
     102                // WPLANG was passed with `$meta` to the `wpmu_new_blog` hook prior to 5.1.0.
     103                if ( ! array_key_exists( 'WPLANG', $meta ) ) {
     104                        $meta['WPLANG'] = get_network_option( $new_site->network_id, 'WPLANG' );
     105                }
     106
     107                // Rebuild the data expected by the `wpmu_new_blog` hook prior to 5.1.0 using whitelisted keys.
     108                // The `$site_data_whitelist` matches the one used in `wpmu_create_blog()`.
     109                $site_data_whitelist = array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
     110                $meta                = array_merge( array_intersect_key( $data, array_flip( $site_data_whitelist ) ), $meta );
    101111
    102112                /**
  • branches/5.1/tests/phpunit/tests/multisite/site.php

    r44727 r44807  
    1313                protected $site_status_hooks       = array();
    1414                protected $wp_initialize_site_args = array();
     15                protected $wp_initialize_site_meta = array();
    1516                protected static $network_ids;
    1617                protected static $site_ids;
     
    23492350                        update_option( 'siteurl', 'http://testsite1.example.org/test' );
    23502351                }
     2352
     2353                /**
     2354                 * Tests whether all expected meta are provided in deprecated `wpmu_new_blog` action.
     2355                 *
     2356                 * @dataProvider data_wpmu_new_blog_action_backward_commpatible
     2357                 *
     2358                 * @ticket 46351
     2359                 */
     2360                public function test_wpmu_new_blog_action_backward_compatible( $meta, $expected_meta ) {
     2361                        // We are testing deprecated hook. Register it to expected deprecated notices.
     2362                        $this->setExpectedDeprecated( 'wpmu_new_blog' );
     2363                        add_action( 'wpmu_new_blog', array( $this, 'wpmu_new_blog_callback' ), 10, 6 );
     2364
     2365                        wpmu_create_blog( 'testsite1.example.org', '/new-blog/', 'New Blog', get_current_user_id(), $meta, 1 );
     2366
     2367                        $this->assertEquals( $expected_meta, $this->wp_initialize_site_meta );
     2368
     2369                        $this->wp_initialize_site_meta = array();
     2370                }
     2371
     2372                /**
     2373                 * Capture the $meta value passed to the wpmu_new_blog action and compare it.
     2374                 */
     2375                public function wpmu_new_blog_callback( $blog_id, $user_id, $domain, $path, $network_id, $meta ) {
     2376                        $this->wp_initialize_site_meta = $meta;
     2377                }
     2378
     2379                public function data_wpmu_new_blog_action_backward_commpatible() {
     2380                        return array(
     2381                                'default values'  => array(
     2382                                        array(),
     2383                                        array(
     2384                                                'public' => 0, // `public` is one of the defaults metas in `wpmu_create_blog' function prior WordPress 5.1.0
     2385                                                'WPLANG' => 'en_US', // WPLANG is another default meta in `wpmu_create_blog` function prior WordPress 5.1.0.
     2386                                        ),
     2387                                ),
     2388                                'public site'     => array(
     2389                                        array(
     2390                                                'public' => 1,
     2391                                        ),
     2392                                        array(
     2393                                                'public' => 1,
     2394                                                'WPLANG' => 'en_US',
     2395                                        ),
     2396                                ),
     2397                                'all whitelisted' => array(
     2398                                        array(
     2399                                                'public'   => -1,
     2400                                                'archived' => 0,
     2401                                                'mature'   => 0,
     2402                                                'spam'     => 0,
     2403                                                'deleted'  => 0,
     2404                                                'lang_id'  => 11,
     2405
     2406                                        ),
     2407                                        array(
     2408                                                'public'   => -1,
     2409                                                'WPLANG'   => 'en_US',
     2410                                                'archived' => 0,
     2411                                                'mature'   => 0,
     2412                                                'spam'     => 0,
     2413                                                'deleted'  => 0,
     2414                                                'lang_id'  => 11,
     2415                                        ),
     2416                                ),
     2417                                'extra meta key'  => array(
     2418                                        array(
     2419                                                'foo' => 'bar',
     2420                                        ),
     2421                                        array(
     2422                                                'public' => 0,
     2423                                                'WPLANG' => 'en_US',
     2424                                                'foo'    => 'bar',
     2425                                        ),
     2426                                ),
     2427                        );
     2428                }
    23512429        }
    23522430
Note: See TracChangeset for help on using the changeset viewer.