Skip to:
Content

BuddyPress.org

Changeset 13681


Ignore:
Timestamp:
12/27/2023 04:13:15 AM (9 months ago)
Author:
imath
Message:

Improve the tool to reset slugs to BP default ones

  • Instead of deleting all component directory entries, only remove the ones that are not active.
  • Remove the post meta used to store custom slugs for each active components
  • Update the directory post names for active components using default BP slugs.

Fixes #9049 (branch 12.0)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/12.0/src/bp-core/admin/bp-core-admin-tools.php

    r13648 r13681  
    192192function bp_admin_reset_slugs() {
    193193    /* translators: %s: the result of the action performed by the repair tool */
    194     $statement = __( 'Removing all custom slugs and resetting default ones… %s', 'buddypress' );
    195 
    196     bp_core_add_page_mappings( buddypress()->active_components, 'delete' );
     194    $statement    = __( 'Removing all custom slugs and resetting default ones… %s', 'buddypress' );
     195    $components   = buddypress()->active_components;
     196    $bp_pages     = bp_get_option( 'bp-pages', array() );
     197    $keep         = array_intersect_key( $bp_pages, $components );
     198    $delete       = array_diff_key( $bp_pages, $keep );
     199    $needs_switch = is_multisite() && ! bp_is_root_blog();
     200
     201    if ( bp_allow_access_to_registration_pages() && isset( $delete['register'], $delete['activate'] ) ) {
     202        $keep = array_merge(
     203            $keep,
     204            array(
     205                'register' => $delete['register'],
     206                'activate' => $delete['activate'],
     207            )
     208        );
     209    }
     210
     211    if ( $needs_switch ) {
     212        switch_to_blog( bp_get_root_blog_id() );
     213    }
     214
     215    // Remove all inactive components BP Pages and reset active ones slugs.
     216    if ( $keep ) {
     217        $deleted_pages = get_posts(
     218            array(
     219                'numberposts' => -1,
     220                'post_type'   => bp_core_get_directory_post_type(),
     221                'exclude'     => array_values( $keep ),
     222                'fields'      => 'ids',
     223            )
     224        );
     225
     226        if ( $deleted_pages ) {
     227            foreach ( $deleted_pages as $deleted_id ) {
     228                wp_delete_post( $deleted_id, true );
     229            }
     230        }
     231
     232        foreach ( $keep as $component_id => $directory_page_id ) {
     233            if ( ! isset( $components[ $component_id ] ) && 'register' !== $component_id && 'activate' !== $component_id ) {
     234                continue;
     235            }
     236
     237            wp_update_post(
     238                array(
     239                    'ID'        => $directory_page_id,
     240                    'post_name' => $component_id,
     241                )
     242            );
     243        }
     244    }
     245
     246    // Remove all custom slugs.
     247    if ( $bp_pages ) {
     248        foreach ( $bp_pages as $page_id ) {
     249            delete_post_meta( $page_id, '_bp_component_slugs' );
     250        }
     251    }
     252
     253    if ( $needs_switch ) {
     254        restore_current_blog();
     255    }
     256
     257    // Reset page mapping.
     258    bp_core_add_page_mappings( $components );
    197259
    198260    // Delete BP Pages cache and rewrite rules.
Note: See TracChangeset for help on using the changeset viewer.