Skip to:
Content

bbPress.org

Changeset 7316


Ignore:
Timestamp:
06/27/2025 07:53:25 PM (6 months ago)
Author:
johnjamesjacoby
Message:

Extend - BuddyPress: Improve support for BuddyPress 12.0 and higher.

This change fixes fatal errors when BuddyPress Group/Member functions (deprecated in 12.0) are not included.

(Note that this commit differs from the proposed patch in that it does not modify the action-order of existing integrations, hence it does not rely on BuddyPress versions greater than 12.0 being in-use, and is safe for use with older BuddyPress versions.)

Props boonebgorges, imath, robin-w.

In trunk, for 2.7.

Fixes #3576.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/extend/buddypress/groups.php

    r7210 r7316  
    117117        add_filter( 'bbp_topic_pagination',      array( $this, 'topic_pagination'   ) );
    118118        add_filter( 'bbp_replies_pagination',    array( $this, 'replies_pagination' ) );
     119        add_filter( 'bbp_get_global_object',     array( $this, 'rewrite_pagination' ), 10, 2 );
    119120
    120121        // Tweak the redirect field
     
    614615        // Redirect after save when not in admin
    615616        if ( ! is_admin() ) {
    616             bp_core_redirect( trailingslashit( bp_get_group_permalink( buddypress()->groups->current_group ) . '/admin/' . $this->slug ) );
     617            bp_core_redirect( trailingslashit( $this->group_url( buddypress()->groups->current_group ) . '/admin/' . $this->slug ) );
    617618        }
    618619    }
     
    12701271            $topic        = bbp_get_topic( $topic_id );
    12711272            $topic_hash   = '#post-' . $topic_id;
    1272             $redirect_url = trailingslashit( bp_get_group_permalink( groups_get_current_group() ) ) . trailingslashit( $this->slug ) . trailingslashit( $this->topic_slug ) . trailingslashit( $topic->post_name ) . $topic_hash;
     1273            $redirect_url = trailingslashit( $this->group_url( groups_get_current_group() ) ) . trailingslashit( $this->slug ) . trailingslashit( $this->topic_slug ) . trailingslashit( $topic->post_name ) . $topic_hash;
    12731274        }
    12741275
     
    12891290            $reply_page     = ceil( (int) $reply_position / (int) bbp_get_replies_per_page() );
    12901291            $reply_hash     = '#post-' . $reply_id;
    1291             $topic_url      = trailingslashit( bp_get_group_permalink( groups_get_current_group() ) ) . trailingslashit( $this->slug ) . trailingslashit( $this->topic_slug ) . trailingslashit( $topic->post_name );
     1292            $topic_url      = trailingslashit( $this->group_url( groups_get_current_group() ) ) . trailingslashit( $this->slug ) . trailingslashit( $this->topic_slug ) . trailingslashit( $topic->post_name );
    12921293
    12931294            // Don't include pagination if on first page
     
    14451446
    14461447    /**
     1448     * Get the URL for a group.
     1449     *
     1450     * @since 2.6.14
     1451     *
     1452     * @param int $group_id
     1453     * @return string
     1454     */
     1455    private function group_url( $group_id = 0 ) {
     1456
     1457        // Default return value
     1458        $retval = '';
     1459
     1460        // BuddyPress < 12.0 (deprecated code is intentionally included)
     1461        if ( function_exists( 'bp_get_group_permalink' ) ) {
     1462            $retval = bp_get_group_permalink( $group_id );
     1463
     1464        // BuddyPress > 12.0 (rewrite rules)
     1465        } elseif ( function_exists( 'bp_get_group_url' ) ) {
     1466            $retval = bp_get_group_url( $group_id );
     1467        }
     1468
     1469        // Return
     1470        return $retval;
     1471    }
     1472
     1473    /**
     1474     * Get the management URL for a group.
     1475     *
     1476     * @since 2.6.14
     1477     *
     1478     * @param int $group_id
     1479     * @return string
     1480     */
     1481    private function group_manage_url( $group_id = 0 ) {
     1482
     1483        // Default return value
     1484        $retval = '';
     1485
     1486        // BuddyPress < 12.0 (deprecated code is intentionally included)
     1487        if ( function_exists( 'bp_get_group_admin_permalink' ) ) {
     1488            $retval = bp_get_group_admin_permalink( $group_id );
     1489
     1490        // BuddyPress > 12.0 (rewrite rules)
     1491        } elseif ( function_exists( 'bp_get_group_manage_url' ) ) {
     1492            $retval = bp_get_group_manage_url( $group_id );
     1493        }
     1494
     1495        // Return
     1496        return $retval;
     1497    }
     1498
     1499    /**
    14471500     * Maybe map a bbPress forum/topic/reply permalink to the corresponding group
    14481501     *
     
    14941547        $group    = groups_get_group( array( 'group_id' => $group_id ) );
    14951548
    1496         if ( bp_is_group_admin_screen( $this->slug ) ) {
    1497             $group_permalink = trailingslashit( bp_get_group_admin_permalink( $group ) );
    1498         } else {
    1499             $group_permalink = trailingslashit( bp_get_group_permalink( $group ) );
    1500         }
    1501 
    1502         return trailingslashit( trailingslashit( $group_permalink . $this->slug ) . $url_end );
     1549        // Admin
     1550        $group_permalink = bp_is_group_admin_screen( $this->slug )
     1551            ? $this->group_manage_url( $group )
     1552            : $this->group_url( $group );
     1553
     1554        return trailingslashit( trailingslashit( trailingslashit( $group_permalink ) . $this->slug ) . $url_end );
    15031555    }
    15041556
     
    16421694        return $args;
    16431695    }
     1696
     1697    /**
     1698     * Fixes rewrite pagination in BuddyPress Group Forums & Topics.
     1699     *
     1700     * Required for compatibility with BuddyPress > 12.0, where the /groups/
     1701     * rewrite rule will be caught before bbPress's /page/ rule.
     1702     *
     1703     * @since 2.6.14
     1704     *
     1705     * @param  object object  Verified object
     1706     * @param  string $type   Type of variable to check with `is_a()`
     1707     * @return mixed  $object Verified object if valid, Default or null if invalid
     1708     */
     1709    public function rewrite_pagination( $object, $type = '' ) {
     1710
     1711        // Bail if wrong global
     1712        if ( 'wp_query' !== $type ) {
     1713            return $object;
     1714        }
     1715
     1716        // Bail if not inside a BuddyPress Group
     1717        if ( ! bp_is_group() ) {
     1718            return $object;
     1719        }
     1720
     1721        // Bail if not inside a BuddyPress Group Forum
     1722        if ( ! bp_is_current_action( 'forum' ) ) {
     1723            return $object;
     1724        }
     1725
     1726        // Default "paged" value
     1727        $page_number = null;
     1728
     1729        // Can't use bbp_is_single_topic() because it triggers a loop.
     1730        $is_single_topic = bp_is_action_variable( 'topic', 0 );
     1731
     1732        // Single Topic
     1733        if ( true === $is_single_topic ) {
     1734
     1735            // Get the page number from 3rd position
     1736            if ( bp_is_action_variable( 'page', 2 ) ) {
     1737                $page_number = bp_action_variable( 3 );
     1738            }
     1739
     1740        // Single Forum
     1741        } else {
     1742
     1743            // Get the page number from 1st position
     1744            if ( bp_is_action_variable( 'page', 0 ) ) {
     1745                $page_number = bp_action_variable( 1 );
     1746            }
     1747        }
     1748
     1749        // Bail if no page number
     1750        if ( empty( $page_number ) ) {
     1751            return $object;
     1752        }
     1753
     1754        // Set the 'paged' WP_Query var to the new action-based value
     1755        $object->set( 'paged', $page_number );
     1756
     1757        // Return the filtered/modified object
     1758        return $object;
     1759    }
    16441760
    16451761    /**
     
    16811797        $group_id    = $group_ids[0];
    16821798        $group       = groups_get_group( array( 'group_id' => $group_id ) );
    1683         $group_link  = trailingslashit( bp_get_group_permalink( $group ) );
     1799        $group_link  = trailingslashit( $this->group_url( $group ) );
    16841800        $redirect_to = trailingslashit( $group_link . $this->slug );
    16851801
Note: See TracChangeset for help on using the changeset viewer.