<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[BuddyPress][13455] trunk/src: Start implementing `parse_query()` methods for directory components</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://buddypress.trac.wordpress.org/changeset/13455">13455</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://buddypress.trac.wordpress.org/changeset/13455","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>imath</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-04-20 02:30:11 +0000 (Thu, 20 Apr 2023)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="http://buddypress.trac.wordpress.org/ticket/4954">#4954</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpactivityclassesclassbpactivitycomponentphp">trunk/src/bp-activity/classes/class-bp-activity-component.php</a></li>
<li><a href="#trunksrcbpblogsclassesclassbpblogscomponentphp">trunk/src/bp-blogs/classes/class-bp-blogs-component.php</a></li>
<li><a href="#trunksrcbpcoreadminbpcoreadminfunctionsphp">trunk/src/bp-core/admin/bp-core-admin-functions.php</a></li>
<li><a href="#trunksrcbpcorebpcorefunctionsphp">trunk/src/bp-core/bp-core-functions.php</a></li>
<li><a href="#trunksrcbpcorebpcorerewritesphp">trunk/src/bp-core/bp-core-rewrites.php</a></li>
<li><a href="#trunksrcbpcorebpcoretemplateloaderphp">trunk/src/bp-core/bp-core-template-loader.php</a></li>
<li><a href="#trunksrcbpcoreclassesclassbpcomponentphp">trunk/src/bp-core/classes/class-bp-component.php</a></li>
<li><a href="#trunksrcbpmembersclassesclassbpmemberscomponentphp">trunk/src/bp-members/classes/class-bp-members-component.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpactivityclassesclassbpactivitycomponentphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-activity/classes/class-bp-activity-component.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-activity/classes/class-bp-activity-component.php 2023-04-16 15:21:30 UTC (rev 13454)
+++ trunk/src/bp-activity/classes/class-bp-activity-component.php 2023-04-20 02:30:11 UTC (rev 13455)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -497,6 +497,49 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Parse the WP_Query and eventually display the component's directory or single item.
+ *
+ * @since 12.0.0
+ *
+ * @param WP_Query $query Required. See BP_Component::parse_query() for
+ * description.
+ */
+ public function parse_query( $query ) {
+ if ( bp_is_directory_homepage( $this->id ) ) {
+ $query->set( $this->rewrite_ids['directory'], 1 );
+ }
+
+ if ( 1 === (int) $query->get( $this->rewrite_ids['directory'] ) ) {
+ $bp = buddypress();
+
+ // Set the Activity component as current.
+ $bp->current_component = 'activity';
+
+ $current_action = $query->get( $this->rewrite_ids['single_item_action'] );
+ if ( $current_action ) {
+ $bp->current_action = $current_action;
+ }
+
+ $action_variables = $query->get( $this->rewrite_ids['single_item_action_variables'] );
+ if ( $action_variables ) {
+ if ( ! is_array( $action_variables ) ) {
+ $bp->action_variables = explode( '/', ltrim( $action_variables, '/' ) );
+ } else {
+ $bp->action_variables = $action_variables;
+ }
+ }
+
+ // Set the BuddyPress queried object.
+ if ( isset( $bp->pages->activity->id ) ) {
+ $query->queried_object = get_post( $bp->pages->activity->id );
+ $query->queried_object_id = $query->queried_object->ID;
+ }
+ }
+
+ parent::parse_query( $query );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Init the BP REST API.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.0.0
</span></span></pre></div>
<a id="trunksrcbpblogsclassesclassbpblogscomponentphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-blogs/classes/class-bp-blogs-component.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-blogs/classes/class-bp-blogs-component.php 2023-04-16 15:21:30 UTC (rev 13454)
+++ trunk/src/bp-blogs/classes/class-bp-blogs-component.php 2023-04-20 02:30:11 UTC (rev 13455)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -437,6 +437,53 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Parse the WP_Query and eventually display the component's directory or single item.
+ *
+ * @since 12.0.0
+ *
+ * @param WP_Query $query Required. See BP_Component::parse_query() for
+ * description.
+ */
+ public function parse_query( $query ) {
+ if ( ! is_multisite() ) {
+ return parent::parse_query( $query );
+ }
+
+ // Get the BuddyPress main instance.
+ $bp = buddypress();
+
+ if ( bp_is_directory_homepage( $this->id ) ) {
+ $query->set( $this->rewrite_ids['directory'], 1 );
+ }
+
+ if ( 1 === (int) $query->get( $this->rewrite_ids['directory'] ) ) {
+ $bp->current_component = 'blogs';
+
+ $current_action = $query->get( $this->rewrite_ids['single_item_action'] );
+ if ( $current_action ) {
+ $bp->current_action = $current_action;
+ }
+
+ $action_variables = $query->get( $this->rewrite_ids['single_item_action_variables'] );
+ if ( $action_variables ) {
+ if ( ! is_array( $action_variables ) ) {
+ $bp->action_variables = explode( '/', ltrim( $action_variables, '/' ) );
+ } else {
+ $bp->action_variables = $action_variables;
+ }
+ }
+
+ // Set the BuddyPress queried object.
+ if ( isset( $bp->pages->blogs->id ) ) {
+ $query->queried_object = get_post( $bp->pages->blogs->id );
+ $query->queried_object_id = $query->queried_object->ID;
+ }
+ }
+
+ parent::parse_query( $query );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Init the BP REST API.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 6.0.0
</span></span></pre></div>
<a id="trunksrcbpcoreadminbpcoreadminfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-core/admin/bp-core-admin-functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-core/admin/bp-core-admin-functions.php 2023-04-16 15:21:30 UTC (rev 13454)
+++ trunk/src/bp-core/admin/bp-core-admin-functions.php 2023-04-20 02:30:11 UTC (rev 13455)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -204,7 +204,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Verify that some BP prerequisites are set up properly, and notify the admin if not.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * On every Dashboard page, this function checks the following:
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * - that pretty permalinks are enabled.
</del><span class="cx" style="display: block; padding: 0 10px"> * - that every BP component that needs a WP page for a directory has one.
</span><span class="cx" style="display: block; padding: 0 10px"> * - that no WP page has multiple BP components associated with it.
</span><span class="cx" style="display: block; padding: 0 10px"> * The administrator will be shown a notice for each check that fails.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -251,18 +250,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Add notice if no rewrite rules are enabled.
- if ( empty( $wp_rewrite->permalink_structure ) ) {
- bp_core_add_admin_notice(
- sprintf(
- // Translators: %s is the url to the permalink settings.
- __( '<strong>BuddyPress is almost ready</strong>. You must <a href="%s">update your permalink structure</a> to something other than the default for it to work.', 'buddypress' ),
- admin_url( 'options-permalink.php' )
- ),
- 'error'
- );
- }
-
</del><span class="cx" style="display: block; padding: 0 10px"> // Get BuddyPress instance.
</span><span class="cx" style="display: block; padding: 0 10px"> $bp = buddypress();
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunksrcbpcorebpcorefunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-core/bp-core-functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-core/bp-core-functions.php 2023-04-16 15:21:30 UTC (rev 13454)
+++ trunk/src/bp-core/bp-core-functions.php 2023-04-20 02:30:11 UTC (rev 13455)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -957,6 +957,28 @@
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'wp_unique_post_slug', 'bp_core_set_unique_directory_page_slug', 10, 6 );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Checks if a component's directory is set as the site's homepage.
+ *
+ * @since 12.0.0
+ *
+ * @param string $component The component ID.
+ * @return boolean True if a component's directory is set as the site's homepage.
+ * False otherwise.
+ */
+function bp_is_directory_homepage( $component = '' ) {
+ $is_directory_homepage = false;
+ $is_page_on_front = 'page' === get_option( 'show_on_front', 'posts' );
+ $page_id_on_front = get_option( 'page_on_front', 0 );
+ $directory_pages = bp_core_get_directory_pages();
+
+ if ( $is_page_on_front && isset( $directory_pages->{$component} ) && (int) $page_id_on_front === (int) $directory_pages->{$component}->id ) {
+ $is_directory_homepage = true;
+ }
+
+ return $is_directory_homepage;
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Remove the entry from bp_pages when the corresponding WP page is deleted.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * Bails early on multisite installations when not viewing the root site.
</span></span></pre></div>
<a id="trunksrcbpcorebpcorerewritesphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-core/bp-core-rewrites.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-core/bp-core-rewrites.php 2023-04-16 15:21:30 UTC (rev 13454)
+++ trunk/src/bp-core/bp-core-rewrites.php 2023-04-20 02:30:11 UTC (rev 13455)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -118,6 +118,41 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Returns the rewrite ID of a customized slug.
+ *
+ * @since 12.0.0
+ *
+ * @param string $component_id The component ID (eg: `activity` for the BP Activity component).
+ * @param string $slug The customized slug.
+ * @param string $context The context for the customized slug, useful when the same slug is used
+ * for more than one rewrite ID of the same component.
+ * @return string The rewrite ID matching the customized slug.
+ */
+function bp_rewrites_get_custom_slug_rewrite_id( $component_id = '', $slug = '', $context = '' ) {
+ $directory_pages = bp_core_get_directory_pages();
+
+ if ( ! isset( $directory_pages->{$component_id}->custom_slugs ) || ! $slug ) {
+ return null;
+ }
+
+ $custom_slugs = (array) $directory_pages->{$component_id}->custom_slugs;
+ $rewrite_ids = array_keys( $custom_slugs, $slug, true );
+
+ if ( 1 < count( $rewrite_ids ) && isset( $context ) && $context ) {
+ foreach ( $rewrite_ids as $rewrite_id_key => $rewrite_id ) {
+ if ( false !== strpos( $rewrite_id, $context ) ) {
+ continue;
+ }
+
+ unset( $rewrite_ids[ $rewrite_id_key ] );
+ }
+ }
+
+ // Always return the first match.
+ return reset( $rewrite_ids );
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Builds a BuddyPress link using the WP Rewrite API.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 12.0.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -283,3 +318,33 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> return apply_filters( 'bp_rewrites_get_root_url', $url );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Get needed data to find a member single item from the requested URL.
+ *
+ * @since 12.0.0
+ *
+ * @param string $request The request used during parsing.
+ * @return array Data to use to find a member single item from the request.
+ */
+function bp_rewrites_get_member_data( $request = '' ) {
+ $member_data = array( 'field' => 'slug' );
+
+ if ( bp_is_username_compatibility_mode() ) {
+ $member_data = array( 'field' => 'login' );
+ }
+
+ if ( bp_core_enable_root_profiles() ) {
+ if ( ! $request ) {
+ $request = $GLOBALS['wp']->request;
+ }
+
+ $request_chunks = explode( '/', ltrim( $request, '/' ) );
+ $member_chunk = reset( $request_chunks );
+
+ // Try to get an existing member to eventually reset the WP Query.
+ $member_data['object'] = get_user_by( $member_data['field'], $member_chunk );
+ }
+
+ return $member_data;
+}
</ins></span></pre></div>
<a id="trunksrcbpcorebpcoretemplateloaderphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-core/bp-core-template-loader.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-core/bp-core-template-loader.php 2023-04-16 15:21:30 UTC (rev 13454)
+++ trunk/src/bp-core/bp-core-template-loader.php 2023-04-20 02:30:11 UTC (rev 13455)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -578,6 +578,49 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Resets the query to fit our permalink structure if needed.
+ *
+ * This is used for specific cases such as Root Member's profile.
+ *
+ * @since 12.0.0
+ *
+ * @param string $bp_request A specific BuddyPress request.
+ * @param WP_Query $query The WordPress query object.
+ * @return true
+ */
+function bp_reset_query( $bp_request = '', WP_Query $query = null ) {
+ global $wp;
+
+ // Get BuddyPress main instance.
+ $bp = buddypress();
+
+ // Back up request uri.
+ $reset_server_request_uri = '';
+ if ( isset( $_SERVER['REQUEST_URI'] ) ) {
+ $reset_server_request_uri = esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) );
+ }
+
+ // Temporarly override the request uri.
+ if ( isset( $wp->request ) ) {
+ $_SERVER['REQUEST_URI'] = str_replace( $wp->request, $bp_request, $reset_server_request_uri );
+
+ // Reparse request.
+ $wp->parse_request();
+
+ // Reparse query.
+ bp_remove_all_filters( 'parse_query' );
+ $query->parse_query( $wp->query_vars );
+ bp_restore_all_filters( 'parse_query' );
+ }
+
+ // Restore request uri.
+ $_SERVER['REQUEST_URI'] = $reset_server_request_uri;
+
+ // The query is reset.
+ return true;
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Possibly intercept the template being loaded.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * Listens to the 'template_include' filter and waits for any BuddyPress specific
</span></span></pre></div>
<a id="trunksrcbpcoreclassesclassbpcomponentphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-core/classes/class-bp-component.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-core/classes/class-bp-component.php 2023-04-16 15:21:30 UTC (rev 13454)
+++ trunk/src/bp-core/classes/class-bp-component.php 2023-04-20 02:30:11 UTC (rev 13455)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -577,7 +577,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'bp_add_permastructs', array( $this, 'add_permastructs' ), 10 );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Allow components to parse the main query.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- add_action( 'bp_parse_query', array( $this, 'parse_query' ), 10 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! bp_has_pretty_urls() ) {
+ /**
+ * Only fire this hook when pretty links are disabled.
+ *
+ * @todo Remove once BP Rewrites merge process is ended.
+ */
+ add_action( 'bp_parse_query', array( $this, 'parse_query' ), 10 );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Generate rewrite rules.
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'bp_generate_rewrite_rules', array( $this, 'generate_rewrite_rules' ), 10 );
</span></span></pre></div>
<a id="trunksrcbpmembersclassesclassbpmemberscomponentphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-members/classes/class-bp-members-component.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-members/classes/class-bp-members-component.php 2023-04-16 15:21:30 UTC (rev 13454)
+++ trunk/src/bp-members/classes/class-bp-members-component.php 2023-04-20 02:30:11 UTC (rev 13455)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -758,6 +758,192 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Parse the WP_Query and eventually display the component's directory or single item.
+ *
+ * @since 12.0.0
+ *
+ * @param WP_Query $query Required. See BP_Component::parse_query() for
+ * description.
+ */
+ public function parse_query( $query ) {
+ // Init the current member and member type.
+ $member = false;
+ $member_type = false;
+ $member_data = bp_rewrites_get_member_data();
+
+ if ( isset( $member_data['object'] ) && $member_data['object'] ) {
+ bp_reset_query( trailingslashit( $this->root_slug ) . $GLOBALS['wp']->request, $query );
+ $member = $member_data['object'];
+
+ // Make sure the Member's screen is fired.
+ add_action( 'bp_screens', 'bp_members_screen_display_profile', 3 );
+ }
+
+ if ( bp_is_directory_homepage( $this->id ) ) {
+ $query->set( $this->rewrite_ids['directory'], 1 );
+ }
+
+ // Which component are we displaying?
+ $is_members_component = 1 === (int) $query->get( $this->rewrite_ids['directory'] );
+ $is_register_component = 1 === (int) $query->get( $this->rewrite_ids['member_register'] );
+ $is_activate_component = 1 === (int) $query->get( $this->rewrite_ids['member_activate'] );
+
+ // Get BuddyPress main instance.
+ $bp = buddypress();
+
+ if ( $is_members_component ) {
+ $bp->current_component = 'members';
+ $member_slug = $query->get( $this->rewrite_ids['single_item'] );
+ $member_type_slug = $query->get( $this->rewrite_ids['directory_type'] );
+
+ if ( $member_slug ) {
+ /**
+ * Filter the portion of the URI that is the displayed user's slug.
+ *
+ * Eg. example.com/ADMIN (when root profiles is enabled)
+ * example.com/members/ADMIN (when root profiles isn't enabled)
+ *
+ * ADMIN would be the displayed user's slug.
+ *
+ * @since 2.6.0
+ *
+ * @param string $member_slug
+ */
+ $member_slug = apply_filters( 'bp_core_set_uri_globals_member_slug', $member_slug );
+ $bp->current_component = '';
+
+ // Unless root profiles are on, the member shouldn't be set yet.
+ if ( ! $member ) {
+ $member = get_user_by( $member_data['field'], $member_slug );
+
+ if ( ! $member ) {
+ bp_do_404();
+ return;
+ }
+ }
+
+ // If the member is marked as a spammer, 404 (unless logged-in user is a super admin).
+ if ( bp_is_user_spammer( $member->ID ) ) {
+ if ( bp_current_user_can( 'bp_moderate' ) ) {
+ bp_core_add_message( __( 'This user has been marked as a spammer. Only site admins can view this profile.', 'buddypress' ), 'warning' );
+ } else {
+ bp_do_404();
+ return;
+ }
+ }
+
+ // Set the displayed user and the current item.
+ $bp->displayed_user->id = $member->ID;
+ $bp->current_item = $member_slug;
+
+ // The core userdata of the user who is currently being displayed.
+ if ( ! isset( $bp->displayed_user->userdata ) || ! $bp->displayed_user->userdata ) {
+ $bp->displayed_user->userdata = bp_core_get_core_userdata( bp_displayed_user_id() );
+ }
+
+ // Fetch the full name displayed user.
+ if ( ! isset( $bp->displayed_user->fullname ) || ! $bp->displayed_user->fullname ) {
+ $bp->displayed_user->fullname = '';
+ if ( isset( $bp->displayed_user->userdata->display_name ) ) {
+ $bp->displayed_user->fullname = $bp->displayed_user->userdata->display_name;
+ }
+ }
+
+ // The domain for the user currently being displayed.
+ if ( ! isset( $bp->displayed_user->domain ) || ! $bp->displayed_user->domain ) {
+ $bp->displayed_user->domain = bp_members_get_user_url( bp_displayed_user_id() );
+ }
+
+ // If A user is displayed, check if there is a front template.
+ if ( bp_get_displayed_user() ) {
+ $bp->displayed_user->front_template = bp_displayed_user_get_front_template();
+ }
+
+ $member_component = $query->get( $this->rewrite_ids['single_item_component'] );
+ if ( $member_component ) {
+ // Check if the member's component slug has been customized.
+ $item_component_rewrite_id = bp_rewrites_get_custom_slug_rewrite_id( 'members', $member_component );
+ if ( $item_component_rewrite_id ) {
+ $member_component = str_replace( 'bp_member_', '', $item_component_rewrite_id );
+ }
+
+ $bp->current_component = $member_component;
+ }
+
+ $current_action = $query->get( $this->rewrite_ids['single_item_action'] );
+ if ( $current_action ) {
+ $context = sprintf( 'bp_member_%s_', $bp->current_component );
+
+ // Check if the member's component action slug has been customized.
+ $item_component_action_rewrite_id = bp_rewrites_get_custom_slug_rewrite_id( 'members', $current_action, $context );
+ if ( $item_component_action_rewrite_id ) {
+ $custom_action_slug = str_replace( $context, '', $item_component_action_rewrite_id );
+
+ // Make sure the action is stored as a slug: underscores need to be replaced by dashes.
+ $current_action = str_replace( '_', '-', $custom_action_slug );
+ }
+
+ $bp->current_action = $current_action;
+ }
+
+ $action_variables = $query->get( $this->rewrite_ids['single_item_action_variables'] );
+ if ( $action_variables ) {
+ if ( ! is_array( $action_variables ) ) {
+ $bp->action_variables = explode( '/', ltrim( $action_variables, '/' ) );
+ } else {
+ $bp->action_variables = $action_variables;
+ }
+ }
+
+ // Is this a member type query?
+ } elseif ( $member_type_slug ) {
+ $member_type = bp_get_member_types(
+ array(
+ 'has_directory' => true,
+ 'directory_slug' => $member_type_slug,
+ )
+ );
+
+ if ( $member_type ) {
+ $member_type = reset( $member_type );
+ $bp->current_member_type = $member_type;
+ } else {
+ $bp->current_component = '';
+ bp_do_404();
+ return;
+ }
+ }
+
+ // Set the BuddyPress queried object.
+ if ( isset( $bp->pages->members->id ) ) {
+ $query->queried_object = get_post( $bp->pages->members->id );
+ $query->queried_object_id = $query->queried_object->ID;
+
+ if ( $member ) {
+ $query->queried_object->single_item_name = $member->display_name;
+ } elseif ( $member_type ) {
+ $query->queried_object->directory_type_name = $member_type;
+ }
+ }
+
+ // Handle the custom registration page.
+ } elseif ( $is_register_component ) {
+ $bp->current_component = 'register';
+
+ // Handle the custom activation page.
+ } elseif ( $is_activate_component ) {
+ $bp->current_component = 'activate';
+
+ $current_action = $query->get( $this->rewrite_ids['member_activate_key'] );
+ if ( $current_action ) {
+ $bp->current_action = $current_action;
+ }
+ }
+
+ parent::parse_query( $query );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Init the BP REST API.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.0.0
</span></span></pre>
</div>
</div>
</body>
</html>