Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
04/20/2023 02:30:11 AM (12 months ago)
Author:
imath
Message:

Start implementing parse_query() methods for directory components

First concerned commponents are: Activity, Blogs & Members.

  • Remove the notice used to force pretty permalinks
  • Use plain links to test parse_query() methods are rightly setting BuddyPress URI globals
  • Introduce some helper functions used during the BP Rewrites API parsing process :
    • bp_is_directory_homepage() is checking if a BP Directory is used as the site's homepage
    • bp_rewrites_get_custom_slug_rewrite_id() finds a Rewrite ID out of a custom slug.
    • bp_rewrites_get_member_data() returns the field to use to get the user by.
    • bp_reset_query() makes it possible to parse again a request in specific cases (eg: root profiles)

Props r-a-y, johnjamesjacoby, boonebgorges

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-members/classes/class-bp-members-component.php

    r13450 r13455  
    759759
    760760    /**
     761     * Parse the WP_Query and eventually display the component's directory or single item.
     762     *
     763     * @since 12.0.0
     764     *
     765     * @param WP_Query $query Required. See BP_Component::parse_query() for
     766     *                        description.
     767     */
     768    public function parse_query( $query ) {
     769        // Init the current member and member type.
     770        $member      = false;
     771        $member_type = false;
     772        $member_data = bp_rewrites_get_member_data();
     773
     774        if ( isset( $member_data['object'] ) && $member_data['object'] ) {
     775            bp_reset_query( trailingslashit( $this->root_slug ) . $GLOBALS['wp']->request, $query );
     776            $member = $member_data['object'];
     777
     778            // Make sure the Member's screen is fired.
     779            add_action( 'bp_screens', 'bp_members_screen_display_profile', 3 );
     780        }
     781
     782        if ( bp_is_directory_homepage( $this->id ) ) {
     783            $query->set( $this->rewrite_ids['directory'], 1 );
     784        }
     785
     786        // Which component are we displaying?
     787        $is_members_component  = 1 === (int) $query->get( $this->rewrite_ids['directory'] );
     788        $is_register_component = 1 === (int) $query->get( $this->rewrite_ids['member_register'] );
     789        $is_activate_component = 1 === (int) $query->get( $this->rewrite_ids['member_activate'] );
     790
     791        // Get BuddyPress main instance.
     792        $bp = buddypress();
     793
     794        if ( $is_members_component ) {
     795            $bp->current_component = 'members';
     796            $member_slug           = $query->get( $this->rewrite_ids['single_item'] );
     797            $member_type_slug      = $query->get( $this->rewrite_ids['directory_type'] );
     798
     799            if ( $member_slug ) {
     800                /**
     801                 * Filter the portion of the URI that is the displayed user's slug.
     802                 *
     803                 * Eg. example.com/ADMIN (when root profiles is enabled)
     804                 *     example.com/members/ADMIN (when root profiles isn't enabled)
     805                 *
     806                 * ADMIN would be the displayed user's slug.
     807                 *
     808                 * @since 2.6.0
     809                 *
     810                 * @param string $member_slug
     811                 */
     812                $member_slug           = apply_filters( 'bp_core_set_uri_globals_member_slug', $member_slug );
     813                $bp->current_component = '';
     814
     815                // Unless root profiles are on, the member shouldn't be set yet.
     816                if ( ! $member ) {
     817                    $member = get_user_by( $member_data['field'], $member_slug );
     818
     819                    if ( ! $member ) {
     820                        bp_do_404();
     821                        return;
     822                    }
     823                }
     824
     825                // If the member is marked as a spammer, 404 (unless logged-in user is a super admin).
     826                if ( bp_is_user_spammer( $member->ID ) ) {
     827                    if ( bp_current_user_can( 'bp_moderate' ) ) {
     828                        bp_core_add_message( __( 'This user has been marked as a spammer. Only site admins can view this profile.', 'buddypress' ), 'warning' );
     829                    } else {
     830                        bp_do_404();
     831                        return;
     832                    }
     833                }
     834
     835                // Set the displayed user and the current item.
     836                $bp->displayed_user->id = $member->ID;
     837                $bp->current_item       = $member_slug;
     838
     839                // The core userdata of the user who is currently being displayed.
     840                if ( ! isset( $bp->displayed_user->userdata ) || ! $bp->displayed_user->userdata ) {
     841                    $bp->displayed_user->userdata = bp_core_get_core_userdata( bp_displayed_user_id() );
     842                }
     843
     844                // Fetch the full name displayed user.
     845                if ( ! isset( $bp->displayed_user->fullname ) || ! $bp->displayed_user->fullname ) {
     846                    $bp->displayed_user->fullname = '';
     847                    if ( isset( $bp->displayed_user->userdata->display_name ) ) {
     848                        $bp->displayed_user->fullname = $bp->displayed_user->userdata->display_name;
     849                    }
     850                }
     851
     852                // The domain for the user currently being displayed.
     853                if ( ! isset( $bp->displayed_user->domain ) || ! $bp->displayed_user->domain ) {
     854                    $bp->displayed_user->domain = bp_members_get_user_url( bp_displayed_user_id() );
     855                }
     856
     857                // If A user is displayed, check if there is a front template.
     858                if ( bp_get_displayed_user() ) {
     859                    $bp->displayed_user->front_template = bp_displayed_user_get_front_template();
     860                }
     861
     862                $member_component = $query->get( $this->rewrite_ids['single_item_component'] );
     863                if ( $member_component ) {
     864                    // Check if the member's component slug has been customized.
     865                    $item_component_rewrite_id = bp_rewrites_get_custom_slug_rewrite_id( 'members', $member_component );
     866                    if ( $item_component_rewrite_id ) {
     867                        $member_component = str_replace( 'bp_member_', '', $item_component_rewrite_id );
     868                    }
     869
     870                    $bp->current_component = $member_component;
     871                }
     872
     873                $current_action = $query->get( $this->rewrite_ids['single_item_action'] );
     874                if ( $current_action ) {
     875                    $context = sprintf( 'bp_member_%s_', $bp->current_component );
     876
     877                    // Check if the member's component action slug has been customized.
     878                    $item_component_action_rewrite_id = bp_rewrites_get_custom_slug_rewrite_id( 'members', $current_action, $context );
     879                    if ( $item_component_action_rewrite_id ) {
     880                        $custom_action_slug = str_replace( $context, '', $item_component_action_rewrite_id );
     881
     882                        // Make sure the action is stored as a slug: underscores need to be replaced by dashes.
     883                        $current_action = str_replace( '_', '-', $custom_action_slug );
     884                    }
     885
     886                    $bp->current_action = $current_action;
     887                }
     888
     889                $action_variables = $query->get( $this->rewrite_ids['single_item_action_variables'] );
     890                if ( $action_variables ) {
     891                    if ( ! is_array( $action_variables ) ) {
     892                        $bp->action_variables = explode( '/', ltrim( $action_variables, '/' ) );
     893                    } else {
     894                        $bp->action_variables = $action_variables;
     895                    }
     896                }
     897
     898                // Is this a member type query?
     899            } elseif ( $member_type_slug ) {
     900                $member_type = bp_get_member_types(
     901                    array(
     902                        'has_directory'  => true,
     903                        'directory_slug' => $member_type_slug,
     904                    )
     905                );
     906
     907                if ( $member_type ) {
     908                    $member_type             = reset( $member_type );
     909                    $bp->current_member_type = $member_type;
     910                } else {
     911                    $bp->current_component = '';
     912                    bp_do_404();
     913                    return;
     914                }
     915            }
     916
     917            // Set the BuddyPress queried object.
     918            if ( isset( $bp->pages->members->id ) ) {
     919                $query->queried_object    = get_post( $bp->pages->members->id );
     920                $query->queried_object_id = $query->queried_object->ID;
     921
     922                if ( $member ) {
     923                    $query->queried_object->single_item_name = $member->display_name;
     924                } elseif ( $member_type ) {
     925                    $query->queried_object->directory_type_name = $member_type;
     926                }
     927            }
     928
     929            // Handle the custom registration page.
     930        } elseif ( $is_register_component ) {
     931            $bp->current_component = 'register';
     932
     933            // Handle the custom activation page.
     934        } elseif ( $is_activate_component ) {
     935            $bp->current_component = 'activate';
     936
     937            $current_action = $query->get( $this->rewrite_ids['member_activate_key'] );
     938            if ( $current_action ) {
     939                $bp->current_action = $current_action;
     940            }
     941        }
     942
     943        parent::parse_query( $query );
     944    }
     945
     946    /**
    761947     * Init the BP REST API.
    762948     *
Note: See TracChangeset for help on using the changeset viewer.