Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
03/31/2020 02:45:13 AM (22 months ago)
Author:
imath
Message:

Adapt BuddyPress to WP 5.1.0 multisite changes & deprecations

Version 5.1.0 of WordPress deprecated two hooks (wpmu_new_blog & delete_blog) BuddyPress is using to run some of the BP Blogs component's features. In order to preserve these features for the versions of WordPress we support and that are older than 5.1.0 as well as start using the replacement hooks introduced in 5.1.0 (wp_initialize_site & wp_validate_site_deletion), we are introducing a compatibility mechanism to make sure BuddyPress is using the right hooks depending on the installed WordPress it's activated on.

Props boonebgorges & I ;)

Fixes #7984

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-wpabstraction.php

    r11447 r12606  
    308308    }
    309309}
     310
     311/**
     312 * Returns the name of the hook to use once a WordPress Site is inserted into the Database.
     313 *
     314 * WordPress 5.1.0 deprecated the `wpmu_new_blog` action. As BuddyPress is supporting WordPress back
     315 * to 4.8.0, this function makes sure we are using the new hook `wp_initialize_site` when the current
     316 * WordPress version is upper or equal to 5.1.0 and that we keep on using `wpmu_new_blog` for earlier
     317 * versions of WordPress.
     318 *
     319 * @since 6.0.0
     320 *
     321 * @return string The name of the hook to use.
     322 */
     323function bp_insert_site_hook() {
     324    $wp_hook = 'wpmu_new_blog';
     325
     326    if ( function_exists( 'wp_insert_site' ) ) {
     327        $wp_hook = 'wp_initialize_site';
     328    }
     329
     330    return $wp_hook;
     331}
     332
     333/**
     334 * Catch the new site data for a later use.
     335 *
     336 * @since 6.0.0
     337 */
     338function bp_catch_site_data( $errors = null, $data = array() ) {
     339    buddypress()->new_site_data = $data;
     340}
     341add_action( 'wp_validate_site_data', 'bp_catch_site_data', 10, 2 );
     342
     343/**
     344 * Fires a BuddyPress hook when a new WordPress site is inserted into the database.
     345 *
     346 * This hook makes sure BuddyPress is back compatible with WordPress versions < 5.1.0.
     347 *
     348 * @since 6.0.0
     349 *
     350 * @param int|WP_Site $site            The Site ID or the WP Site object.
     351 * @param int|array   $args_or_user_id An array of Site arguments or the User ID.
     352 * @param string      $domain          Site domain.
     353 * @param string      $path            Site path.
     354 * @param int         $network_id      Network ID. Only relevant on multi-network installations.
     355 * @param array       $meta            Meta data. Used to set initial site options.
     356 */
     357function bp_insert_site( $site, $args_or_user_id = null, $domain = '', $path = '', $network_id = 0, $meta = array() ) {
     358    if ( $site instanceof WP_Site ) {
     359        $bp         = buddypress();
     360        $site_id    = $site->id;
     361        $domain     = $site->domain;
     362        $path       = $site->path;
     363        $network_id = $site->network_id;
     364        $args       = (array) $args_or_user_id;
     365
     366        $user_id = 0;
     367        if ( isset( $args['user_id'] ) && $args['user_id'] ) {
     368            $user_id = (int) $args['user_id'];
     369        }
     370
     371        $meta = array();
     372        if ( isset( $args['options'] ) && $args['options'] ) {
     373            $meta = (array) $args['options'];
     374
     375            if ( ! array_key_exists( 'WPLANG', $meta ) ) {
     376                $meta['WPLANG'] = get_network_option( $site->network_id, 'WPLANG' );
     377            }
     378
     379            if ( isset( $bp->new_site_data ) ) {
     380                $meta = array_merge( $bp->new_site_data, $meta );
     381            }
     382        }
     383    } else {
     384        $site_id = $site;
     385        $user_id = (int) $args_or_user_id;
     386    }
     387
     388    /**
     389     * Fires when a new WordPress site has been inserted into the database.
     390     *
     391     * @since 6.0.0
     392     *
     393     * @param int    $site_id    Site ID.
     394     * @param int    $user_id    User ID.
     395     * @param string $domain     Site domain.
     396     * @param string $path       Site path.
     397     * @param int    $network_id Network ID. Only relevant on multi-network installations.
     398     * @param array  $meta       Meta data. Used to set initial site options.
     399     */
     400    do_action( 'bp_insert_site', $site_id, $user_id, $domain, $path, $network_id, $meta );
     401}
     402add_action( bp_insert_site_hook(), 'bp_insert_site' );
     403
     404/**
     405 * Returns the name of the hook to use once a WordPress Site is deleted.
     406 *
     407 * WordPress 5.1.0 deprecated the `delete_blog` action. As BuddyPress is supporting WordPress back
     408 * to 4.8.0, this function makes sure we are using the new hook `wp_validate_site_deletion` when the
     409 * current WordPress version is upper or equal to 5.1.0 and that we keep on using `delete_blog` for
     410 * earlier versions of WordPress.
     411 *
     412 * @since 6.0.0
     413 *
     414 * @return string The name of the hook to use.
     415 */
     416function bp_delete_site_hook() {
     417    $wp_hook = 'delete_blog';
     418
     419    if ( function_exists( 'wp_delete_site' ) ) {
     420        $wp_hook = 'wp_validate_site_deletion';
     421    }
     422
     423    return $wp_hook;
     424}
     425
     426/**
     427 * Makes sure the `bp_delete_site` hook is fired if site's deletion
     428 * was performed without dropping tables.
     429 *
     430 * @since 6.0.0
     431 *
     432 * @param WP_Site $site The site object.
     433 */
     434function bp_delete_site_no_tables_drop( $site ) {
     435    if ( isset( $site->deleted ) && 1 === (int) $site->deleted ) {
     436        return bp_delete_site( $site->id, false );
     437    }
     438}
     439add_action( 'wp_update_site', 'bp_delete_site_no_tables_drop', 10, 1 );
     440
     441/**
     442 * Fires a BuddyPress hook when a new WordPress site is deleted.
     443 *
     444 * This hook makes sure BuddyPress is back compatible with WordPress versions < 5.1.0.
     445 *
     446 * @since 6.0.0
     447 *
     448 * @param int|WP_Error $site_id_or_error A WP Error object or the site ID.
     449 * @param bool|WP_Site $drop_or_site     A WP Site object or a boolean to inform whether site's table should be dropped.
     450 */
     451function bp_delete_site( $site_id_or_error, $drop_or_site = false ) {
     452    if ( $drop_or_site instanceof WP_Site ) {
     453        if ( ! empty( $site_id_or_error->errors ) ) {
     454            return;
     455        }
     456
     457        $site_id = (int) $drop_or_site->id;
     458        $drop    = true;
     459    } else {
     460        $site_id = (int) $site_id_or_error;
     461        $drop    = (bool) $drop_or_site;
     462    }
     463
     464    /**
     465     * Fires when a WordPress site is deleted.
     466     *
     467     * @since 6.0.0
     468     *
     469     * @param int  $site_id The site ID.
     470     * @param bool $drop    True if site's table should be dropped. Default is false.
     471     */
     472    do_action( 'bp_delete_site', $site_id, $drop );
     473}
     474add_action( bp_delete_site_hook(), 'bp_delete_site', 10, 2 );
Note: See TracChangeset for help on using the changeset viewer.