<!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][13422] trunk: Improve `BP_Component` methods relative to setting BP Rewrites</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/13422">13422</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/13422","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-02-15 10:23:05 +0000 (Wed, 15 Feb 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'>Improve `BP_Component` methods relative to setting BP Rewrites

So far `BP_Component::add_rewrite_tags()`, 
`BP_Component::add_rewrite_rules()` & `BP_Component::add_permastructs()` 
were just firing hooks. Thanks to the BP Rewrites plugin we were able to 
test how we can use them to actually enjoy the WP Rewrite API in 
BuddyPress. Compared to what was tested in this feature as plugin, some 
improvements has been brought so that it's easier to benefit from the BP 
Rewrites API for custom components (analyzing the included PHPUnit tests 
can give BP plugin authors a preview about how to process).

Introduce the `BP_Component::pre_query()` method to avoid superfluous Post 
queries when a BuddyPress page is displayed.

The `src/bp-core/bp-rewrites.php` file has also been inited, this is the 
place where all Core component rewrites function will be placed.

Props r-a-y, johnjamesjacoby, boonebgorges

See <a href="http://buddypress.trac.wordpress.org/ticket/4954">#4954</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpcoreclassesclassbpcomponentphp">trunk/src/bp-core/classes/class-bp-component.php</a></li>
<li><a href="#trunksrcclassbuddypressphp">trunk/src/class-buddypress.php</a></li>
<li><a href="#trunktestsphpunitassetsclassbptestcomponentphp">trunk/tests/phpunit/assets/class-bptest-component.php</a></li>
<li><a href="#trunktestsphpunittestcasescoreclassbpcomponentphp">trunk/tests/phpunit/testcases/core/class-bp-component.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunksrcbpcorebpcorerewritesphp">trunk/src/bp-core/bp-core-rewrites.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpcorebpcorerewritesphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: 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                            (rev 0)
+++ trunk/src/bp-core/bp-core-rewrites.php      2023-02-15 10:23:05 UTC (rev 13422)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,45 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Core Rewrite API functions.
+ *
+ * @package BuddyPress
+ * @subpackage Core
+ * @since 12.0.0
+ */
+
+// Exit if accessed directly.
+if ( ! defined( 'ABSPATH' ) ) {
+       exit;
+}
+
+/**
+ * Gets default URL chunks rewrite information.
+ *
+ * @since 12.0.0
+ *
+ * @return array Default URL chunks rewrite information.
+ */
+function bp_rewrites_get_default_url_chunks() {
+       return array(
+               'directory'                    => array(
+                       'regex' => '([1]{1,})',
+                       'order' => 100,
+               ),
+               'single_item'                  => array(
+                       'regex' => '([^/]+)',
+                       'order' => 90,
+               ),
+               'single_item_component'        => array(
+                       'regex' => '([^/]+)',
+                       'order' => 80,
+               ),
+               'single_item_action'           => array(
+                       'regex' => '([^/]+)',
+                       'order' => 70,
+               ),
+               'single_item_action_variables' => array(
+                       'regex' => '(.+?)',
+                       'order' => 60,
+               ),
+       );
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/src/bp-core/bp-core-rewrites.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><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-02-15 09:46:14 UTC (rev 13421)
+++ trunk/src/bp-core/classes/class-bp-component.php    2023-02-15 10:23:05 UTC (rev 13422)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -60,6 +60,22 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public $has_directory = false;
</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">+         * Directory's permalink structure for the component.
+        *
+        * @since 12.0.0
+        * @var string
+        */
+       public $directory_permastruct = '';
+
+       /**
+        * List of available rewrite IDs for the component.
+        *
+        * @since 12.0.0
+        * @var array
+        */
+       public $rewrite_ids = array();
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * The path to the component's files.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 1.5.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -257,6 +273,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                'slug'                  => $this->id,
</span><span class="cx" style="display: block; padding: 0 10px">                                'root_slug'             => '',
</span><span class="cx" style="display: block; padding: 0 10px">                                'has_directory'         => false,
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                'rewrite_ids'           => array(),
</ins><span class="cx" style="display: block; padding: 0 10px">                                 'directory_title'       => '',
</span><span class="cx" style="display: block; padding: 0 10px">                                'notification_callback' => '',
</span><span class="cx" style="display: block; padding: 0 10px">                                'search_string'         => '',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -306,6 +323,33 @@
</span><span class="cx" style="display: block; padding: 0 10px">                         */
</span><span class="cx" style="display: block; padding: 0 10px">                        $this->has_directory = apply_filters( 'bp_' . $this->id . '_has_directory', $r['has_directory'] );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        $rewrite_ids = bp_parse_args(
+                               /**
+                                * Filters the component's rewrite IDs if available.
+                                *
+                                * @since 12.0.0
+                                *
+                                * @param array $value The list of rewrite IDs for the component.
+                                */
+                               (array) apply_filters( 'bp_' . $this->id. '_rewrite_ids', $r['rewrite_ids'] ),
+                               array_fill_keys( array_keys( bp_rewrites_get_default_url_chunks() ), '' )
+                       );
+
+                       if ( array_filter( $rewrite_ids ) ) {
+                               foreach ( $rewrite_ids as $rewrite_id_key => $rewrite_id_value ) {
+                                       if ( ! $rewrite_id_value ) {
+                                               continue;
+                                       }
+
+                                       $this->rewrite_ids[ sanitize_key( $rewrite_id_key ) ] = 'bp_' . str_replace( 'bp_', '', sanitize_key( $rewrite_id_value ) );
+                               }
+                       }
+
+                       // Set the component's directory permastruct early so that it's available to build links.
+                       if ( true === $this->has_directory && isset( $this->rewrite_ids['directory'] ) ) {
+                               $this->directory_permastruct = $this->root_slug . '/%' . $this->rewrite_ids['directory'] . '%';
+                       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         /**
</span><span class="cx" style="display: block; padding: 0 10px">                         * Filters the component's directory title.
</span><span class="cx" style="display: block; padding: 0 10px">                         *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -502,10 +546,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'bp_register_taxonomies',    array( $this, 'register_taxonomies'    ), 10 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Add the rewrite tags.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                add_action( 'bp_add_rewrite_tags',       array( $this, 'add_rewrite_tags'       ), 10 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         add_action( 'bp_add_rewrite_tags',       array( $this, 'add_rewrite_tags'       ), 10, 0 );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Add the rewrite rules.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                add_action( 'bp_add_rewrite_rules',      array( $this, 'add_rewrite_rules'      ), 10 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         add_action( 'bp_add_rewrite_rules',      array( $this, 'add_rewrite_rules'      ), 10, 0 );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Add the permalink structure.
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'bp_add_permastructs',       array( $this, 'add_permastructs'       ), 10 );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -832,13 +876,36 @@
</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 any additional rewrite tags.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Add Component's additional rewrite tags.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 1.5.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @since 12.0.0 Adds the `$rewrite_tags` parameter.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @param array $rewrite_tags Array of arguments list used to add WordPress rewrite tags.
+        *                            Each argument key needs to match one of `$this->rewrite_ids` keys.
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function add_rewrite_tags() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function add_rewrite_tags( $rewrite_tags = array() ) {
+               if ( array_filter( $this->rewrite_ids ) ) {
+                       $chunks = bp_rewrites_get_default_url_chunks();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        foreach ( $this->rewrite_ids as $rewrite_id_key => $rewrite_id_value ) {
+                               $rewrite_tag   = '%' . $rewrite_id_value . '%';
+                               $rewrite_regex = '';
+
+                               if ( isset( $rewrite_tags[ $rewrite_id_key ] ) ) {
+                                       $rewrite_regex = $rewrite_tags[ $rewrite_id_key ];
+                               } elseif ( isset( $chunks[ $rewrite_id_key ]['regex'] ) ) {
+                                       $rewrite_regex = $chunks[ $rewrite_id_key ]['regex'];
+                               }
+
+                               if ( ! $rewrite_regex ) {
+                                       continue;
+                               }
+
+                               add_rewrite_tag( $rewrite_tag, $rewrite_regex );
+                       }
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 /**
</span><span class="cx" style="display: block; padding: 0 10px">                 * Fires in the add_rewrite_tags method inside BP_Component.
</span><span class="cx" style="display: block; padding: 0 10px">                 *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -850,13 +917,77 @@
</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 any additional rewrite rules.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Add Component's additional rewrite rules.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 1.9.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @since 12.0.0 Adds the `$rewrite_rules` parameter.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @param array $rewrite_rules {
+        *     Array of associative arrays of arguments list used to add WordPress rewrite rules.
+        *     Each associative array needs to include the following keys.
+        *
+        *     @type string $regex    Regular expression to match request against. Required.
+        *     @type string $query    The corresponding query vars for this rewrite rule. Required.
+        *     @type int    $order    The insertion order for the rewrite rule. Required.
+        *     @type string $priority The Priority of the new rule. Accepts 'top' or 'bottom'. Optional.
+        *                            Default 'top'.
+        * }
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function add_rewrite_rules() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function add_rewrite_rules( $rewrite_rules = array() ) {
+               if ( array_filter( $this->rewrite_ids ) ) {
+                       $priority = 'top';
+                       $chunks   = array_merge( bp_rewrites_get_default_url_chunks(), $rewrite_rules );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        $rules          = bp_sort_by_key( $chunks, 'order', 'num', true );
+                       $reversed_rules = array_reverse( $rules, true );
+
+                       $regex = '';
+                       $query = '';
+                       $match = 1;
+
+                       // Build rewrite rules for the component.
+                       foreach ( $reversed_rules as $rule_key => $rule_information ) {
+                               if ( ! isset( $this->rewrite_ids[ $rule_key ] ) ) {
+                                       unset( $rules[ $rule_key ] );
+                                       continue;
+                               }
+
+                               // The query is already set, use it.
+                               if ( isset( $rule_information['query'] ) ) {
+                                       $rules[ $rule_key ]['regex'] = $rule_information['regex'];
+                                       $rules[ $rule_key ]['query'] = $rule_information['query'];
+                               } elseif ( 'directory' === $rule_key ) {
+                                       $regex = $this->root_slug;
+                                       $query = 'index.php?' . $this->rewrite_ids['directory'] . '=1';
+
+                                       $rules[ $rule_key ]['regex'] = $regex;
+                                       $rules[ $rule_key ]['query'] = $query;
+                               } else {
+                                       $regex  = trailingslashit( $regex ) . $rule_information['regex'];
+                                       $query .= '&' . $this->rewrite_ids[ $rule_key ] . '=$matches['. $match .']';
+                                       $match += 1;
+
+                                       $rules[ $rule_key ]['regex'] = $regex . '/?$';
+                                       $rules[ $rule_key ]['query'] = $query;
+                               }
+                       }
+
+                       // Then register the rewrite rules.
+                       if ( $rules ) {
+                               foreach ( $rules as $rewrite_rule ) {
+                                       if ( ! isset( $rewrite_rule['regex'] ) || ! isset( $rewrite_rule['query'] ) ) {
+                                               continue;
+                                       }
+
+                                       if ( ! isset( $rewrite_rule['priority'] ) || ! $rewrite_rule['priority'] ) {
+                                               $rewrite_rule['priority'] = $priority;
+                                       }
+
+                                       add_rewrite_rule( $rewrite_rule['regex'], $rewrite_rule['query'], $rewrite_rule['priority'] );
+                               }
+                       }
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 /**
</span><span class="cx" style="display: block; padding: 0 10px">                 * Fires in the add_rewrite_rules method inside BP_Component.
</span><span class="cx" style="display: block; padding: 0 10px">                 *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -868,13 +999,61 @@
</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 any permalink structures.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Add Component's permalink structures.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 1.9.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @since 12.0.0 Adds the `$permastructs` parameter.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @param array $permastructs {
+        *      Array of associative arrays of arguments list used to register WordPress additional permalink structures.
+        *      Each array enty is keyed with the permalink structure.
+        *      Each associative array needs to include the following keys.
+        *
+        *      @type string $permastruct The permalink structure. Required.
+        *      @type array  $args        The permalink structure arguments. Optional.
+        * }
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function add_permastructs() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function add_permastructs( $permastructs = array() ) {
+               // Always include the directory permastruct when the component has a directory.
+               if ( isset( $this->rewrite_ids['directory'] ) ) {
+                       $directory_permastruct = array(
+                               $this->rewrite_ids['directory'] => array(
+                                       'permastruct' => $this->directory_permastruct,
+                                       'args'        => array(),
+                               ),
+                       );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        $permastructs = array_merge( $directory_permastruct, (array) $permastructs );
+               }
+
+               if ( $permastructs ) {
+                       foreach ( $permastructs as $name => $params ) {
+                               if ( ! $name || ! isset( $params['permastruct'] ) || ! $params['permastruct'] ) {
+                                       continue;
+                               }
+
+                               if ( ! $params['args'] ) {
+                                       $params['args'] = array();
+                               }
+
+                               $args = wp_parse_args(
+                                       $params['args'],
+                                       array(
+                                               'with_front'  => false,
+                                               'ep_mask'     => EP_NONE,
+                                               'paged'       => true,
+                                               'feed'        => false,
+                                               'forcomments' => false,
+                                               'walk_dirs'   => true,
+                                               'endpoints'   => false,
+                                       )
+                               );
+
+                               // Add the permastruct.
+                               add_permastruct( $name, $params['permastruct'], $args );
+                       }
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 /**
</span><span class="cx" style="display: block; padding: 0 10px">                 * Fires in the add_permastructs method inside BP_Component.
</span><span class="cx" style="display: block; padding: 0 10px">                 *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -890,10 +1069,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 1.9.0
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         *
</del><span class="cx" style="display: block; padding: 0 10px">          * @param object $query The main WP_Query.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function parse_query( $query ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( is_buddypress() ) {
+                       add_filter( 'posts_pre_query', array( $this, 'pre_query' ), 10, 2 );
+               }
</ins><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">                 * Fires in the parse_query method inside BP_Component.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -908,6 +1089,29 @@
</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">+         * Make sure to avoid querying for regular posts when displaying a BuddyPress page.
+        *
+        * @since 12.0.0
+        *
+        * @param  null     $retval A null value to use the regular WP Query.
+        * @param  WP_Query $query  The WP Query object.
+        * @return null|array Null if not displaying a BuddyPress page.
+        *                    An array containing the BuddyPress directory post otherwise.
+        */
+       public function pre_query( $retval = null, $query = null ) {
+               remove_filter( 'posts_pre_query', array( $this, 'pre_query' ), 10 );
+
+               $queried_object = $query->get_queried_object();
+
+               if ( $queried_object instanceof \WP_Post && 'buddypress' === get_post_type( $queried_object ) ) {
+                       // Only include the queried directory post into returned posts.
+                       $retval = array( $queried_object );
+               }
+
+               return $retval;
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Generate any additional rewrite rules.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 1.5.0
</span></span></pre></div>
<a id="trunksrcclassbuddypressphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/class-buddypress.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/class-buddypress.php    2023-02-15 09:46:14 UTC (rev 13421)
+++ trunk/src/class-buddypress.php      2023-02-15 10:23:05 UTC (rev 13422)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -622,6 +622,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                require $this->plugin_dir . 'bp-core/bp-core-template.php';
</span><span class="cx" style="display: block; padding: 0 10px">                require $this->plugin_dir . 'bp-core/bp-core-adminbar.php';
</span><span class="cx" style="display: block; padding: 0 10px">                require $this->plugin_dir . 'bp-core/bp-core-buddybar.php';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                require $this->plugin_dir . 'bp-core/bp-core-rewrites.php';
</ins><span class="cx" style="display: block; padding: 0 10px">                 require $this->plugin_dir . 'bp-core/bp-core-catchuri.php';
</span><span class="cx" style="display: block; padding: 0 10px">                require $this->plugin_dir . 'bp-core/bp-core-functions.php';
</span><span class="cx" style="display: block; padding: 0 10px">                require $this->plugin_dir . 'bp-core/bp-core-moderation.php';
</span></span></pre></div>
<a id="trunktestsphpunitassetsclassbptestcomponentphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/assets/class-bptest-component.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/assets/class-bptest-component.php     2023-02-15 09:46:14 UTC (rev 13421)
+++ trunk/tests/phpunit/assets/class-bptest-component.php       2023-02-15 10:23:05 UTC (rev 13422)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -34,4 +34,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public function setup_globals( $args = array() ) {
</span><span class="cx" style="display: block; padding: 0 10px">                parent::setup_globals( $this->globals );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       public function add_rewrite_tags( $rewrite_tags = array() ) {
+               parent::add_rewrite_tags( $rewrite_tags );
+       }
+
+       public function add_rewrite_rules( $rewrite_rules = array() ) {
+               parent::add_rewrite_rules( $rewrite_rules );
+       }
+
+       public function add_permastructs( $permastructs = array() ) {
+               parent::add_permastructs( $permastructs );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestcasescoreclassbpcomponentphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/testcases/core/class-bp-component.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/testcases/core/class-bp-component.php 2023-02-15 09:46:14 UTC (rev 13421)
+++ trunk/tests/phpunit/testcases/core/class-bp-component.php   2023-02-15 10:23:05 UTC (rev 13422)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -92,4 +92,142 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( $expected, $example->block_globals['bp/example-block']->props );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @group bp_rewrites
+        */
+       public function test_component_rewrite_globals() {
+               $expected = array(
+                       'directory'          => 'bp_examples',
+                       'single_item'        => 'bp_example',
+                       'single_item_action' => 'bp_example_action',
+               );
+
+               $example = new BPTest_Component(
+                       array(
+                               'globals' => array(
+                                       'rewrite_ids' => array(
+                                               'directory'          => 'examples ',
+                                               'single_item'        => 'Exam?ple',
+                                               'single_item_action' => 'bp_example_action',
+                                       )
+                               ),
+                       )
+               );
+
+               do_action( 'bp_setup_globals' );
+
+               $this->assertEquals( $expected, $example->rewrite_ids );
+       }
+
+       /**
+        * @group bp_rewrites
+        */
+       public function test_component_add_rewrite_tags() {
+               $example = new BPTest_Component(
+                       array(
+                               'globals' => array(
+                                       'rewrite_ids' => array(
+                                               'directory'      => 'examples',
+                                               'directory_type' => 'example_type',
+                                       )
+                               ),
+                       )
+               );
+
+               do_action( 'bp_setup_globals' );
+
+               $expected_directory_regex = '([1]{1,})';
+               $rewrite_tags             = array(
+                       'directory_type' => '([^/]+)',
+               );
+
+               $example->add_rewrite_tags( $rewrite_tags );
+
+               global $wp_rewrite;
+
+               $position = array_search( '%' . $example->rewrite_ids['directory'] . '%', $wp_rewrite->rewritecode, true );
+               $this->assertEquals( $wp_rewrite->rewritereplace[ $position ], $expected_directory_regex );
+
+               $position = array_search( '%' . $example->rewrite_ids['directory_type'] . '%', $wp_rewrite->rewritecode, true );
+               $this->assertEquals( $wp_rewrite->rewritereplace[ $position ], $rewrite_tags['directory_type'] );
+       }
+
+       /**
+        * @group bp_rewrites
+        */
+       public function test_component_add_rewrite_rules() {
+               $example = new BPTest_Component(
+                       array(
+                               'globals' => array(
+                                       'root_slug'   => 'examples',
+                                       'rewrite_ids' => array(
+                                               'directory'             => 'examples',
+                                               'directory_type'        => 'example_type',
+                                               'single_item'           => 'example',
+                                               'single_item_component' => 'example_component',
+                                       )
+                               ),
+                       )
+               );
+
+               do_action( 'bp_setup_globals' );
+
+               $rewrite_tags = array(
+                       'directory_type' => '([^/]+)',
+               );
+
+               $example->add_rewrite_tags( $rewrite_tags );
+
+               $rewrite_rules = array(
+                       'directory_type' => array(
+                               'order' => 95,
+                               'regex' => $example->root_slug . '/type/([^/]+)/?$',
+                               'query' => 'index.php?' . $example->rewrite_ids['directory'] . '=1&' . $example->rewrite_ids['directory_type'] . '=$matches[1]',
+                       ),
+               );
+
+               $example->add_rewrite_rules( $rewrite_rules );
+
+               global $wp_rewrite;
+               $this->assertEquals( $wp_rewrite->extra_rules_top[ $rewrite_rules['directory_type']['regex'] ], $rewrite_rules['directory_type']['query'] );
+       }
+
+       /**
+        * @group bp_rewrites
+        */
+       public function test_component_add_permastructs() {
+               $example = new BPTest_Component(
+                       array(
+                               'globals' => array(
+                                       'has_directory' => true,
+                                       'root_slug'     => 'examples',
+                                       'rewrite_ids'   => array(
+                                               'directory'      => 'examples',
+                                               'example_signup' => 'signup',
+                                       )
+                               ),
+                       )
+               );
+
+               do_action( 'bp_setup_globals' );
+
+               $expected     = 'example-signup/%' . $example->rewrite_ids['example_signup'] . '%';
+               $permastructs = array(
+                       $example->rewrite_ids['example_signup'] => array(
+                               'permastruct' => $expected,
+                               'args'        => array(),
+                       ),
+               );
+
+               $example->add_permastructs( $permastructs );
+
+               global $wp_rewrite;
+
+               // The directory permastruct should be created automatically.
+               $this->assertTrue( isset( $wp_rewrite->extra_permastructs['bp_examples'] ) );
+
+               // The custom permastruct should be created as requested.
+               $this->assertEquals( $wp_rewrite->extra_permastructs[ $example->rewrite_ids['example_signup'] ]['struct'], $expected );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>