Skip to:
Content

BuddyPress.org

Changeset 12606


Ignore:
Timestamp:
03/31/2020 02:45:13 AM (4 years 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

Location:
trunk
Files:
16 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-blogs/bp-blogs-cache.php

    r12173 r12606  
    5454// List actions to clear object caches on.
    5555add_action( 'bp_blogs_remove_blog_for_user', 'bp_blogs_clear_blog_object_cache', 10, 2 );
    56 add_action( 'wpmu_new_blog',                 'bp_blogs_clear_blog_object_cache', 10, 2 );
     56add_action( 'bp_insert_site',                'bp_blogs_clear_blog_object_cache', 10, 2 );
    5757add_action( 'bp_blogs_remove_blog',          'bp_blogs_clear_blog_object_cache' );
    5858
  • trunk/src/bp-blogs/bp-blogs-functions.php

    r12605 r12606  
    412412    do_action_ref_array( 'bp_blogs_new_blog', array( &$recorded_blog, $is_private, $is_recorded, $no_activity ) );
    413413}
    414 add_action( 'wpmu_new_blog', 'bp_blogs_record_blog', 10, 2 );
     414add_action( 'bp_insert_site', 'bp_blogs_record_blog', 10, 2 );
    415415
    416416/**
     
    995995    do_action( 'bp_blogs_remove_blog', $blog_id );
    996996}
    997 add_action( 'delete_blog', 'bp_blogs_remove_blog' );
     997add_action( 'bp_delete_site', 'bp_blogs_remove_blog' );
    998998
    999999/**
     
    12191219    do_action( 'bp_blogs_remove_data_for_blog', $blog_id );
    12201220}
    1221 add_action( 'delete_blog', 'bp_blogs_remove_data_for_blog', 1 );
     1221add_action( 'bp_delete_site', 'bp_blogs_remove_data_for_blog', 1 );
    12221222
    12231223/**
  • trunk/src/bp-core/admin/bp-core-admin-actions.php

    r10825 r12606  
    4747add_action( 'custom_menu_order',                  'bp_admin_custom_menu_order'       );
    4848add_action( 'menu_order',                         'bp_admin_menu_order'              );
    49 add_action( 'wpmu_new_blog',                      'bp_new_site',               10, 6 );
     49add_action( 'bp_insert_site',                     'bp_new_site',               10, 6 );
    5050
    5151// Hook on to admin_init.
  • 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 );
  • trunk/tests/phpunit/testcases/activity/functions.php

    r12605 r12606  
    856856        }
    857857
    858         if ( function_exists( 'wp_initialize_site' ) ) {
    859             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    860         }
    861 
    862858        $b = self::factory()->blog->create();
    863859        $u = self::factory()->user->create();
     
    972968        }
    973969
    974         if ( function_exists( 'wp_initialize_site' ) ) {
    975             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    976         }
    977 
    978970        $b = self::factory()->blog->create();
    979971        $u = self::factory()->user->create();
     
    11071099    public function test_bp_activity_format_activity_action_custom_post_type_comment() {
    11081100        if ( is_multisite() ) {
    1109             if ( function_exists( 'wp_initialize_site' ) ) {
    1110                 $this->setExpectedDeprecated( 'wpmu_new_blog' );
    1111             }
    1112 
    11131101            $b = self::factory()->blog->create();
    11141102            switch_to_blog( $b );
  • trunk/tests/phpunit/testcases/blogs/activity.php

    r12395 r12606  
    3434        }
    3535
    36         if ( function_exists( 'wp_initialize_site' ) ) {
    37             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    38         }
    39 
    4036        $b = self::factory()->blog->create();
    4137        $u = self::factory()->user->create();
     
    131127        }
    132128
    133         if ( function_exists( 'wp_initialize_site' ) ) {
    134             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    135         }
    136 
    137129        $b = self::factory()->blog->create();
    138130        $u = self::factory()->user->create();
     
    176168        }
    177169
    178         if ( function_exists( 'wp_initialize_site' ) ) {
    179             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    180         }
    181 
    182170        $b = self::factory()->blog->create();
    183171        $u = self::factory()->user->create();
     
    223211        }
    224212
    225         if ( function_exists( 'wp_initialize_site' ) ) {
    226             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    227         }
    228 
    229213        add_filter( 'bp_blogs_activity_created_blog_action', array( $this, 'created_blog_passthrough' ), 10, 2 );
    230214
     
    255239        }
    256240
    257         if ( function_exists( 'wp_initialize_site' ) ) {
    258             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    259         }
    260 
    261241        add_filter( 'bp_blogs_activity_new_post_action', array( $this, 'new_post_passthrough' ), 10, 2 );
    262242
     
    287267        }
    288268
    289         if ( function_exists( 'wp_initialize_site' ) ) {
    290             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    291         }
    292 
    293269        add_filter( 'bp_blogs_activity_new_comment_action', array( $this, 'new_comment_passthrough' ), 10, 2 );
    294270
     
    322298        if ( ! is_multisite() ) {
    323299            $this->markTestSkipped();
    324         }
    325 
    326         if ( function_exists( 'wp_initialize_site' ) ) {
    327             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    328300        }
    329301
  • trunk/tests/phpunit/testcases/blogs/cache.php

    r12247 r12606  
    1414        }
    1515
    16         if ( function_exists( 'wp_initialize_site' ) ) {
    17             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    18         }
    19 
    2016        $b1 = self::factory()->blog->create();
    2117        $b2 = self::factory()->blog->create();
     
    9995        }
    10096
    101         if ( function_exists( 'wp_initialize_site' ) ) {
    102             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    103         }
    104 
    10597        $u = self::factory()->user->create();
    10698
     
    197189        }
    198190
    199         if ( function_exists( 'wp_initialize_site' ) ) {
    200             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    201         }
    202 
    203191        $u = self::factory()->user->create();
    204192
     
    274262        if ( ! is_multisite() ) {
    275263            $this->markTestSkipped();
    276         }
    277 
    278         if ( function_exists( 'wp_initialize_site' ) ) {
    279             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    280             $this->setExpectedDeprecated( 'delete_blog' );
    281264        }
    282265
     
    315298        }
    316299
    317         if ( function_exists( 'wp_initialize_site' ) ) {
    318             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    319         }
    320 
    321300        $u = self::factory()->user->create();
    322301
  • trunk/tests/phpunit/testcases/blogs/class-bp-blogs-blog.php

    r12246 r12606  
    99        if ( ! is_multisite() ) {
    1010            $this->markTestSkipped();
    11         }
    12 
    13         if ( function_exists( 'wp_initialize_site' ) ) {
    14             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    1511        }
    1612
     
    4238        }
    4339
    44         if ( function_exists( 'wp_initialize_site' ) ) {
    45             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    46         }
    47 
    4840        $old_user = get_current_user_id();
    4941
     
    7163        if ( ! is_multisite() ) {
    7264            $this->markTestSkipped();
    73         }
    74 
    75         if ( function_exists( 'wp_initialize_site' ) ) {
    76             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    7765        }
    7866
     
    10694        }
    10795
    108         if ( function_exists( 'wp_initialize_site' ) ) {
    109             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    110         }
    111 
    11296        $old_user = get_current_user_id();
    11397
     
    136120        if ( ! is_multisite() ) {
    137121            $this->markTestSkipped();
    138         }
    139 
    140         if ( function_exists( 'wp_initialize_site' ) ) {
    141             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    142122        }
    143123
  • trunk/tests/phpunit/testcases/blogs/functions.php

    r12605 r12606  
    286286        if ( ! is_multisite() ) {
    287287            $this->markTestSkipped();
    288         }
    289 
    290         if ( function_exists( 'wp_initialize_site' ) ) {
    291             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    292288        }
    293289
     
    891887    public function test_bp_blogs_comment_sync_activity_comment_for_custom_post_type() {
    892888        if ( is_multisite() ) {
    893             if ( function_exists( 'wp_initialize_site' ) ) {
    894                 $this->setExpectedDeprecated( 'wpmu_new_blog' );
    895             }
    896 
    897889            $b = self::factory()->blog->create();
    898890            switch_to_blog( $b );
     
    1001993        }
    1002994
    1003         if ( function_exists( 'wp_initialize_site' ) ) {
    1004             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    1005         }
    1006 
    1007995        $old_user = get_current_user_id();
    1008996
     
    10351023        if ( ! is_multisite() ) {
    10361024            $this->markTestSkipped();
    1037         }
    1038 
    1039         if ( function_exists( 'wp_initialize_site' ) ) {
    1040             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    1041             $this->setExpectedDeprecated( 'delete_blog' );
    10421025        }
    10431026
     
    10891072        }
    10901073
    1091         if ( function_exists( 'wp_initialize_site' ) ) {
    1092             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    1093         }
    1094 
    10951074        $reset_post = $_POST;
    10961075        $old_user = get_current_user_id();
     
    11511130        }
    11521131
    1153         if ( function_exists( 'wp_initialize_site' ) ) {
    1154             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    1155         }
    1156 
    11571132        $u1 = self::factory()->user->create();
    11581133        $b1 = get_current_blog_id();
     
    11811156        }
    11821157
    1183         if ( function_exists( 'wp_initialize_site' ) ) {
    1184             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    1185         }
    1186 
    11871158        $u1 = self::factory()->user->create();
    11881159        $b1 = get_current_blog_id();
  • trunk/tests/phpunit/testcases/blogs/template.php

    r12496 r12606  
    378378        }
    379379
    380         if ( function_exists( 'wp_initialize_site' ) ) {
    381             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    382         }
    383 
    384380        global $blogs_template;
    385381        $reset_blogs_template = $blogs_template;
     
    422418        }
    423419
    424         if ( function_exists( 'wp_initialize_site' ) ) {
    425             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    426         }
    427 
    428420        global $blogs_template;
    429421        $reset_blogs_template = $blogs_template;
  • trunk/tests/phpunit/testcases/core/avatars.php

    r12507 r12606  
    2424        if ( ! is_multisite() ) {
    2525            $this->markTestSkipped();
    26         }
    27 
    28         if ( function_exists( 'wp_initialize_site' ) ) {
    29             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    3026        }
    3127
  • trunk/tests/phpunit/testcases/core/caps.php

    r12246 r12606  
    99        if ( ! is_multisite() ) {
    1010            $this->markTestSkipped( __METHOD__ . ' requires multisite.' );
    11         }
    12 
    13         if ( function_exists( 'wp_initialize_site' ) ) {
    14             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    1511        }
    1612
     
    3531        if ( ! is_multisite() ) {
    3632            $this->markTestSkipped( __METHOD__ . ' requires multisite.' );
    37         }
    38 
    39         if ( function_exists( 'wp_initialize_site' ) ) {
    40             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    4133        }
    4234
  • trunk/tests/phpunit/testcases/core/functions.php

    r12569 r12606  
    701701
    702702        if ( is_multisite() ) {
    703             if ( function_exists( 'wp_initialize_site' ) ) {
    704                 $this->setExpectedDeprecated( 'wpmu_new_blog' );
    705             }
    706 
    707703            $b = self::factory()->blog->create();
    708704            switch_to_blog( $b );
     
    850846        }
    851847
    852         if ( function_exists( 'wp_initialize_site' ) ) {
    853             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    854         }
    855 
    856848        $bp = buddypress();
    857849        $reset_bp_pages = $bp->pages;
  • trunk/tests/phpunit/testcases/core/functions/bpCoreGetDirectoryPageIds.php

    r12246 r12606  
    260260        }
    261261
    262         if ( function_exists( 'wp_initialize_site' ) ) {
    263             $this->setExpectedDeprecated( 'wpmu_new_blog' );
    264         }
    265 
    266262        $dir_pages = bp_core_get_directory_pages();
    267263
  • trunk/tests/phpunit/testcases/members/functions.php

    r12605 r12606  
    578578
    579579        if ( is_multisite() ) {
    580             if ( function_exists( 'wp_initialize_site' ) ) {
    581                 $this->setExpectedDeprecated( 'wpmu_new_blog' );
    582             }
    583 
    584580            $signups['ms-blog'] = array( 'signup_id' => self::factory()->signup->create( array(
    585581                    'user_login'     => 'msblog',
  • trunk/tests/phpunit/testcases/routing/url.php

    r12246 r12606  
    4242        // (3) Multisite, root blog other than 1
    4343        if ( is_multisite() ) {
    44             if ( function_exists( 'wp_initialize_site' ) ) {
    45                 $this->setExpectedDeprecated( 'wpmu_new_blog' );
    46             }
    47 
    4844            $original_root_blog = bp_get_root_blog_id();
    4945            $blog_id = self::factory()->blog->create( array(
Note: See TracChangeset for help on using the changeset viewer.