Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/24/2023 09:37:44 AM (14 months ago)
Author:
imath
Message:

Use a custom post type instead of the page post type for directories

  • The buddypress post type is now the one used for the BP component

directory page.

  • Introduce the bp_core_set_unique_directory_page_slug() function to

make sure there is no conflict with an existing WP page when setting the
BP Component directory page post_name.

  • Introduce the bp_restricted custom status. We'll be able to use it for

future visibility features.

  • Deprecate the Admin Slugs screen and corresponding functions
  • Bump raw_db_version to 13422 (the first commit about merging BP

Rewrites).

  • Add an update function to update the post type of the existing BP

component directory pages and potential WP Nav Menus including these.

Props r-a-y, johnjamesjacoby, boonebgorges

Closes https://github.com/buddypress/buddypress/pull/67
See #4954

File:
1 edited

Legend:

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

    r13377 r13431  
    698698
    699699/**
     700 * Get the directory pages post type.
     701 *
     702 * @since 12.0.0
     703 *
     704 * @return string The post type to use for directory pages.
     705 */
     706function bp_core_get_directory_post_type() {
     707    $post_type = 'buddypress';
     708
     709    /**
     710     * Filter here to edit the post type to use for directory pages.
     711     *
     712     * @since 12.0.0
     713     *
     714     * @param string $post_type The post type to use for directory pages.
     715     */
     716    return apply_filters( 'bp_core_get_directory_post_type', $post_type );
     717}
     718
     719/**
    700720 * Get names and slugs for BuddyPress component directory pages.
    701721 *
     
    834854    // Create the pages.
    835855    foreach ( $pages_to_create as $component_name => $page_name ) {
    836         $exists = get_page_by_path( $component_name );
     856        $existing_id = bp_core_get_directory_page_id( $component_name );
    837857
    838858        // If page already exists, use it.
    839         if ( ! empty( $exists ) ) {
    840             $pages[ $component_name ] = $exists->ID;
     859        if ( ! empty( $existing_id ) ) {
     860            $pages[ $component_name ] = (int) $existing_id;
    841861        } else {
    842862            $pages[ $component_name ] = wp_insert_post( array(
     
    845865                'post_status'    => 'publish',
    846866                'post_title'     => $page_name,
    847                 'post_type'      => 'page',
     867                'post_type'      => bp_core_get_directory_post_type(),
    848868            ) );
    849869        }
     
    887907
    888908/**
     909 * Make sure Components directory page `post_name` are unique.
     910 *
     911 * Goal is to avoid a slug conflict between a Page and a Component's directory page `post_name`.
     912 *
     913 * @since 12.0.0
     914 *
     915 * @param string $slug          The post slug.
     916 * @param int    $post_ID       Post ID.
     917 * @param string $post_status   The post status.
     918 * @param string $post_type     Post type.
     919 * @param int    $post_parent   Post parent ID.
     920 * @param string $original_slug The original post slug.
     921 */
     922function bp_core_set_unique_directory_page_slug( $slug = '', $post_ID = 0, $post_status = '', $post_type = '', $post_parent = 0, $original_slug = '' ) {
     923    if ( ( 'buddypress' === $post_type || 'page' === $post_type ) && $slug === $original_slug ) {
     924        $pages = get_posts(
     925            array(
     926                'post__not_in' => array( $post_ID ),
     927                'post_status'  => array( 'publish', 'bp_restricted' ),
     928                'post_type'    => array( 'buddypress', 'page' ),
     929            )
     930        );
     931
     932        $illegal_names = wp_list_pluck( $pages, 'post_name' );
     933        if ( is_multisite() && ! is_subdomain_install() ) {
     934            $current_site = get_current_site();
     935            $site         = get_site_by_path( $current_site->domain, trailingslashit( $current_site->path ) . $slug );
     936
     937            if ( isset( $site->blog_id ) && 1 !== $site->blog_id ) {
     938                $illegal_names[] = $slug;
     939            }
     940        }
     941
     942        if ( in_array( $slug, $illegal_names, true ) ) {
     943            $suffix = 2;
     944            do {
     945                $alt_post_name   = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
     946                $post_name_check = in_array( $alt_post_name, $illegal_names, true );
     947                $suffix++;
     948            } while ( $post_name_check );
     949            $slug = $alt_post_name;
     950        }
     951    }
     952
     953    return $slug;
     954}
     955add_filter( 'wp_unique_post_slug', 'bp_core_set_unique_directory_page_slug', 10, 6 );
     956
     957/**
    889958 * Remove the entry from bp_pages when the corresponding WP page is deleted.
    890959 *
     
    914983}
    915984add_action( 'delete_post', 'bp_core_on_directory_page_delete' );
    916 
    917 /**
    918  * Create a default component slug from a WP page root_slug.
    919  *
    920  * Since 1.5, BP components get their root_slug (the slug used immediately
    921  * following the root domain) from the slug of a corresponding WP page.
    922  *
    923  * E.g. if your BP installation at example.com has its members page at
    924  * example.com/community/people, $bp->members->root_slug will be
    925  * 'community/people'.
    926  *
    927  * By default, this function creates a shorter version of the root_slug for
    928  * use elsewhere in the URL, by returning the content after the final '/'
    929  * in the root_slug ('people' in the example above).
    930  *
    931  * Filter on 'bp_core_component_slug_from_root_slug' to override this method
    932  * in general, or define a specific component slug constant (e.g.
    933  * BP_MEMBERS_SLUG) to override specific component slugs.
    934  *
    935  * @since 1.5.0
    936  *
    937  * @param string $root_slug The root slug, which comes from $bp->pages->[component]->slug.
    938  * @return string The short slug for use in the middle of URLs.
    939  */
    940 function bp_core_component_slug_from_root_slug( $root_slug ) {
    941     $slug_chunks = explode( '/', $root_slug );
    942     $slug        = array_pop( $slug_chunks );
    943 
    944     /**
    945      * Filters the default component slug from a WP page root_slug.
    946      *
    947      * @since 1.5.0
    948      *
    949      * @param string $slug      Short slug for use in the middle of URLs.
    950      * @param string $root_slug The root slug which comes from $bp->pages-[component]->slug.
    951      */
    952     return apply_filters( 'bp_core_component_slug_from_root_slug', $slug, $root_slug );
    953 }
    954 
    955 /**
    956  * Add support for a top-level ("root") component.
    957  *
    958  * This function originally (pre-1.5) let plugins add support for pages in the
    959  * root of the install. These root level pages are now handled by actual
    960  * WordPress pages and this function is now a convenience for compatibility
    961  * with the new method.
    962  *
    963  * @since 1.0.0
    964  *
    965  * @param string $slug The slug of the component being added to the root list.
    966  */
    967 function bp_core_add_root_component( $slug ) {
    968     $bp = buddypress();
    969 
    970     if ( empty( $bp->pages ) ) {
    971         $bp->pages = bp_core_get_directory_pages();
    972     }
    973 
    974     $match = false;
    975 
    976     // Check if the slug is registered in the $bp->pages global.
    977     foreach ( (array) $bp->pages as $key => $page ) {
    978         if ( $key == $slug || $page->slug == $slug ) {
    979             $match = true;
    980         }
    981     }
    982 
    983     // Maybe create the add_root array.
    984     if ( empty( $bp->add_root ) ) {
    985         $bp->add_root = array();
    986     }
    987 
    988     // If there was no match, add a page for this root component.
    989     if ( empty( $match ) ) {
    990         $add_root_items   = $bp->add_root;
    991         $add_root_items[] = $slug;
    992         $bp->add_root     = $add_root_items;
    993     }
    994 
    995     // Make sure that this component is registered as requiring a top-level directory.
    996     if ( isset( $bp->{$slug} ) ) {
    997         $bp->loaded_components[$bp->{$slug}->slug] = $bp->{$slug}->id;
    998         $bp->{$slug}->has_directory = true;
    999     }
    1000 }
    1001 
    1002 /**
    1003  * Create WordPress pages to be used as BP component directories.
    1004  *
    1005  * @since 1.5.0
    1006  */
    1007 function bp_core_create_root_component_page() {
    1008 
    1009     // Get BuddyPress.
    1010     $bp = buddypress();
    1011 
    1012     $new_page_ids = array();
    1013 
    1014     foreach ( (array) $bp->add_root as $slug ) {
    1015         $new_page_ids[ $slug ] = wp_insert_post( array(
    1016             'comment_status' => 'closed',
    1017             'ping_status'    => 'closed',
    1018             'post_title'     => ucwords( $slug ),
    1019             'post_status'    => 'publish',
    1020             'post_type'      => 'page'
    1021         ) );
    1022     }
    1023 
    1024     $page_ids = array_merge( $new_page_ids, bp_core_get_directory_page_ids( 'all' ) );
    1025     bp_core_update_directory_page_ids( $page_ids );
    1026 }
    1027985
    1028986/**
     
    48204778        '10.0',
    48214779        '11.0',
     4780        '12.0',
    48224781    );
    48234782
     
    48924851    return $latest_deprecated_functions_versions;
    48934852}
     4853
     4854/**
     4855 * Get the BuddyPress Post Type site ID.
     4856 *
     4857 * @since 12.0.0
     4858 *
     4859 * @return int The site ID the BuddyPress Post Type should be registered on.
     4860 */
     4861function bp_get_post_type_site_id() {
     4862    $site_id = bp_get_root_blog_id();
     4863
     4864    /**
     4865     * Filter here to edit the site ID.
     4866     *
     4867     * @todo This will need to be improved to take in account
     4868     * specific configurations like multiblog.
     4869     *
     4870     * @since 12.0.0
     4871     *
     4872     * @param integer $site_id The site ID to register the post type on.
     4873     */
     4874    return (int) apply_filters( 'bp_get_post_type_site_id', $site_id );
     4875}
Note: See TracChangeset for help on using the changeset viewer.