<!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][13456] trunk/src/bp-groups/classes/class-bp-groups-component.php: Implement a `parse_query()` method for the Groups component</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/13456">13456</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/13456","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 10:35:25 +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'>Implement a `parse_query()` method for the Groups component
Props r-a-y, johnjamesjacoby, boonebgorges
Closes https://github.com/buddypress/buddypress/pull/88
See <a href="http://buddypress.trac.wordpress.org/ticket/4954">#4954</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpgroupsclassesclassbpgroupscomponentphp">trunk/src/bp-groups/classes/class-bp-groups-component.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpgroupsclassesclassbpgroupscomponentphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-groups/classes/class-bp-groups-component.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-groups/classes/class-bp-groups-component.php 2023-04-20 02:30:11 UTC (rev 13455)
+++ trunk/src/bp-groups/classes/class-bp-groups-component.php 2023-04-20 10:35:25 UTC (rev 13456)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -231,86 +231,114 @@
</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">- * Set up additional globals for the component.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Sets up the current (displayed) group it it exists.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @since 10.0.0
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 12.0.0
+ *
+ * @param string $group_slug The current action which is possibly a group slug.
+ * @return BP_Groups_Group|Object|integer A group's object or 0 if no groups were found.
</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 setup_additional_globals() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function setup_current_group( $group_slug = '' ) {
+ if ( ! bp_is_groups_component() || ! $group_slug ) {
+ return 0;
+ }
+
+ // Get the BuddyPress main instance.
</ins><span class="cx" style="display: block; padding: 0 10px"> $bp = buddypress();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- /* Single Group Globals **********************************************/
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Try to find a group ID matching the requested slug.
+ $group_id = BP_Groups_Group::group_exists( $group_slug );
+ if ( ! $group_id ) {
+ $group_id = BP_Groups_Group::get_id_by_previous_slug( $group_slug );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Are we viewing a single group?
- if ( bp_is_groups_component()
- && ( ( $group_id = BP_Groups_Group::group_exists( bp_current_action() ) )
- || ( $group_id = BP_Groups_Group::get_id_by_previous_slug( bp_current_action() ) ) )
- ) {
- $bp->is_single_item = true;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // The Group was not found.
+ if ( ! $group_id ) {
+ return 0;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Set BP single item's global.
+ $bp->is_single_item = true;
+
+ /**
+ * Filters the current PHP Class being used.
+ *
+ * @since 1.5.0
+ *
+ * @param string $value Name of the class being used.
+ */
+ $current_group_class = apply_filters( 'bp_groups_current_group_class', 'BP_Groups_Group' );
+
+ if ( $current_group_class == 'BP_Groups_Group' ) {
+ $current_group = groups_get_group( $group_id );
+
+ } else {
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Filters the current PHP Class being used.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Filters the current group object being instantiated from previous filter.
</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><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param string $value Name of the class being used.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param object $value Newly instantiated object for the group.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $current_group_class = apply_filters( 'bp_groups_current_group_class', 'BP_Groups_Group' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $current_group = apply_filters( 'bp_groups_current_group_object', new $current_group_class( $group_id ) );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( $current_group_class == 'BP_Groups_Group' ) {
- $this->current_group = groups_get_group( $group_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! isset( $current_group->id ) || ! $current_group->id ) {
+ return 0;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- } else {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Make sure the Group ID is an integer.
+ $current_group->id = (int) $current_group->id;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- /**
- * Filters the current group object being instantiated from previous filter.
- *
- * @since 1.5.0
- *
- * @param object $value Newly instantiated object for the group.
- */
- $this->current_group = apply_filters( 'bp_groups_current_group_object', new $current_group_class( $group_id ) );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * When in a single group, the first action is bumped down one because of the
+ * group name, so we need to adjust this and set the group name to current_item.
+ */
+ $bp->current_item = bp_current_action();
+ $bp->current_action = bp_action_variable( 0 );
+ array_shift( $bp->action_variables );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Make sure the Group ID is an integer.
- $this->current_group->id = (int) $this->current_group->id;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Using "item" not "group" for generic support in other components.
+ if ( bp_current_user_can( 'bp_moderate' ) ) {
+ bp_update_is_item_admin( true, 'groups' );
+ } else {
+ bp_update_is_item_admin( groups_is_user_admin( bp_loggedin_user_id(), $current_group->id ), 'groups' );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // When in a single group, the first action is bumped down one because of the
- // group name, so we need to adjust this and set the group name to current_item.
- $bp->current_item = bp_current_action();
- $bp->current_action = bp_action_variable( 0 );
- array_shift( $bp->action_variables );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // If the user is not an admin, check if they are a moderator.
+ if ( ! bp_is_item_admin() ) {
+ bp_update_is_item_mod( groups_is_user_mod( bp_loggedin_user_id(), $current_group->id ), 'groups' );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Using "item" not "group" for generic support in other components.
- if ( bp_current_user_can( 'bp_moderate' ) ) {
- bp_update_is_item_admin( true, 'groups' );
- } else {
- bp_update_is_item_admin( groups_is_user_admin( bp_loggedin_user_id(), $this->current_group->id ), 'groups' );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Check once if the current group has a custom front template.
+ $current_group->front_template = bp_groups_get_front_template( $current_group );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // If the user is not an admin, check if they are a moderator.
- if ( ! bp_is_item_admin() ) {
- bp_update_is_item_mod( groups_is_user_mod( bp_loggedin_user_id(), $this->current_group->id ), 'groups' );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Fires once the `current_group` global is fully set.
+ *
+ * @since 10.0.0
+ *
+ * @param BP_Groups_Group|object $current_group The current group object.
+ */
+ do_action_ref_array( 'bp_groups_set_current_group', array( $current_group ) );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Check once if the current group has a custom front template.
- $this->current_group->front_template = bp_groups_get_front_template( $this->current_group );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Initialize the nav for the groups component.
+ $this->nav = new BP_Core_Nav( $current_group->id, $this->id );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- /**
- * Fires once the `current_group` global is fully set.
- *
- * @since 10.0.0
- *
- * @param BP_Groups_Group|object $current_group The current group object.
- */
- do_action_ref_array( 'bp_groups_set_current_group', array( $this->current_group ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Finally return the current group.
+ return $current_group;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Initialize the nav for the groups component.
- $this->nav = new BP_Core_Nav( $this->current_group->id, $this->id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Set up additional globals for the component.
+ *
+ * @since 10.0.0
+ */
+ public function setup_additional_globals() {
+ $bp = buddypress();
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Set current_group to 0 to prevent debug errors.
- } else {
- $this->current_group = 0;
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Are we viewing a single group?
+ $this->current_group = $this->setup_current_group( bp_current_action() );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Set up variables specific to the group creation process.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( bp_is_groups_component() && bp_is_current_action( 'create' ) && bp_user_can_create_groups() && isset( $_COOKIE['bp_new_group_id'] ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1008,6 +1036,153 @@
</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();
+ $group_type = false;
+ $bp->current_component = 'groups';
+ $group_slug = $query->get( $this->rewrite_ids['single_item'] );
+ $group_type_slug = $query->get( $this->rewrite_ids['directory_type'] );
+ $is_group_create = 1 === (int) $query->get( $this->rewrite_ids['create_single_item'] );
+
+ if ( $group_slug ) {
+ $this->current_group = $this->setup_current_group( $group_slug );
+
+ if ( ! $this->current_group ) {
+ $bp->current_component = false;
+ bp_do_404();
+ return;
+ }
+
+ // Set the current item using the group slug.
+ $bp->current_item = $group_slug;
+
+ $current_action = $query->get( $this->rewrite_ids['single_item_action'] );
+ if ( $current_action ) {
+ $context = 'bp_group_read_';
+
+ // Get the rewrite ID corresponfing to the custom slug.
+ $current_action_rewrite_id = bp_rewrites_get_custom_slug_rewrite_id( 'groups', $current_action, $context );
+
+ if ( $current_action_rewrite_id ) {
+ $current_action = str_replace( $context, '', $current_action_rewrite_id );
+
+ // Make sure the action is stored as a slug: underscores need to be replaced by dashes.
+ $current_action = str_replace( '_', '-', $current_action );
+ }
+
+ // Set the BuddyPress global.
+ $bp->current_action = $current_action;
+ }
+
+ $action_variables = $query->get( $this->rewrite_ids['single_item_action_variables'] );
+ if ( $action_variables ) {
+ if ( ! is_array( $action_variables ) ) {
+ $action_variables = explode( '/', ltrim( $action_variables, '/' ) );
+ }
+
+ // In the Manage context, we need to translate custom slugs to BP Expected variables.
+ if ( 'admin' === $bp->current_action ) {
+ $context = 'bp_group_manage_';
+
+ // Get the rewrite ID corresponfing to the custom slug.
+ $first_action_variable_rewrite_id = bp_rewrites_get_custom_slug_rewrite_id( 'groups', $action_variables[0], $context );
+
+ if ( $first_action_variable_rewrite_id ) {
+ $first_action_variable = str_replace( $context, '', $first_action_variable_rewrite_id );
+
+ // Make sure the action is stored as a slug: underscores need to be replaced by dashes.
+ $action_variables[0] = str_replace( '_', '-', $first_action_variable );
+ }
+ }
+
+ // Set the BuddyPress global.
+ $bp->action_variables = $action_variables;
+ }
+ } elseif ( $group_type_slug ) {
+ $group_type = bp_groups_get_group_types(
+ array(
+ 'has_directory' => true,
+ 'directory_slug' => $group_type_slug,
+ )
+ );
+
+ if ( $group_type ) {
+ $group_type = reset( $group_type );
+ $this->current_directory_type = $group_type;
+ $bp->current_action = bp_get_groups_group_type_base();
+ $bp->action_variables = array( $group_type_slug );
+ } else {
+ $bp->current_component = false;
+ bp_do_404();
+ return;
+ }
+ } elseif ( $is_group_create ) {
+ $bp->current_action = 'create';
+
+ if ( bp_user_can_create_groups() && isset( $_COOKIE['bp_new_group_id'] ) ) {
+ $bp->groups->new_group_id = (int) $_COOKIE['bp_new_group_id'];
+ }
+
+ $create_variables = $query->get( $this->rewrite_ids['create_single_item_variables'] );
+ if ( $create_variables ) {
+ $context = 'bp_group_create_';
+ $action_variables = array();
+
+ if ( ! is_array( $create_variables ) ) {
+ $action_variables = explode( '/', ltrim( $create_variables, '/' ) );
+ } else {
+ $action_variables = $create_variables;
+ }
+
+ // The slug of the step is the second action variable.
+ if ( isset( $action_variables[1] ) && $action_variables[1] ) {
+ // Get the rewrite ID corresponfing to the custom slug.
+ $second_action_variable_rewrite_id = bp_rewrites_get_custom_slug_rewrite_id( 'groups', $action_variables[1], $context );
+
+ // Reset the action variable with BP Default create step slug.
+ if ( $second_action_variable_rewrite_id ) {
+ $second_action_variable = str_replace( $context, '', $second_action_variable_rewrite_id );
+
+ // Make sure the action is stored as a slug: underscores need to be replaced by dashes.
+ $action_variables[1] = str_replace( '_', '-', $second_action_variable );
+ }
+ }
+
+ $bp->action_variables = $action_variables;
+ }
+ }
+
+ /**
+ * Set the BuddyPress queried object.
+ */
+ if ( isset( $bp->pages->groups->id ) ) {
+ $query->queried_object = get_post( $bp->pages->groups->id );
+ $query->queried_object_id = $query->queried_object->ID;
+
+ if ( $this->current_group ) {
+ $query->queried_object->single_item_name = $this->current_group->name;
+ } elseif ( $group_type ) {
+ $query->queried_object->directory_type_name = $group_type;
+ }
+ }
+ }
+
+ 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>