Skip to:
Content

BuddyPress.org

Changeset 13377


Ignore:
Timestamp:
12/01/2022 05:02:24 AM (23 months ago)
Author:
imath
Message:

Introduce a function to get allowed post stati for BP directory pages

The bp_core_get_directory_pages_stati() function has been introduced to be able to get BP Directory pages having various registered post stati. So far only the publish post status was allowed. Filtering 'bp_core_get_directory_pages_stati', it's now possible to include other registered post stati. This new possibility gives a way to use WorPress Post Status to set custom & more granular user visibilities for components having a directory page.

Closes https://github.com/buddypress/buddypress/pull/40
Fixes #8772

File:
1 edited

Legend:

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

    r13363 r13377  
    665665
    666666/**
     667 * Get the BP Directory pages allowed stati.
     668 *
     669 * @since 11.0.0
     670 *
     671 * @return array The BP Directory pages allowed stati.
     672 */
     673function bp_core_get_directory_pages_stati() {
     674    $default_page_status = array( 'publish' );
     675
     676    /**
     677     * Filter here to edit the allowed BP Directory pages stati.
     678     *
     679     * @since 11.0.0
     680     *
     681     * @param array $default_page_status The default allowed BP Directory pages stati.
     682     */
     683    $page_stati = (array) apply_filters( 'bp_core_get_directory_pages_stati', $default_page_status );
     684
     685    // Validate the post stati, making sure each status is registered.
     686    foreach ( $page_stati as $page_status_key => $page_status ) {
     687        if ( ! get_post_status_object( $page_status ) ) {
     688            unset( $page_stati[ $page_status_key ] );
     689        }
     690    }
     691
     692    if ( ! $page_stati ) {
     693        $page_stati = $default_page_status;
     694    }
     695
     696    return $page_stati;
     697}
     698
     699/**
    667700 * Get names and slugs for BuddyPress component directory pages.
    668701 *
     
    684717        // Get pages and IDs.
    685718        $page_ids = bp_core_get_directory_page_ids();
    686         if ( !empty( $page_ids ) ) {
     719        if ( ! empty( $page_ids ) ) {
    687720
    688721            // Always get page data from the root blog, except on multiblog mode, when it comes
     
    690723            $posts_table_name = bp_is_multiblog_mode() ? $wpdb->posts : $wpdb->get_blog_prefix( bp_get_root_blog_id() ) . 'posts';
    691724            $page_ids_sql     = implode( ',', wp_parse_id_list( $page_ids ) );
    692             $page_names       = $wpdb->get_results( "SELECT ID, post_name, post_parent, post_title FROM {$posts_table_name} WHERE ID IN ({$page_ids_sql}) AND post_status = 'publish' " );
     725            $page_stati_sql   = '\'' . implode( '\', \'', array_map( 'sanitize_key', bp_core_get_directory_pages_stati() ) ) . '\'';
     726            $page_names       = $wpdb->get_results( "SELECT ID, post_name, post_parent, post_title FROM {$posts_table_name} WHERE ID IN ({$page_ids_sql}) AND post_status IN ({$page_stati_sql}) " );
    693727
    694728            foreach ( (array) $page_ids as $component_id => $page_id ) {
Note: See TracChangeset for help on using the changeset viewer.