Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Extends topic shortcode for BuddyBoss to allow filtering by author.
- */
- class BBP_Extended_Shortcode {
- /**
- * Cached attributes for the current shortcode instance.
- *
- * @var array Attributes
- */
- private array $atts = array();
- /**
- * Sets up shortcode.
- */
- public function setup() {
- add_shortcode( 'bbp-topic-index-extended', array( $this, 'shortcode' ) );
- }
- /**
- * Extended bbPress topic index shortcode.
- *
- * @param array $atts Attributes.
- * @param string $content Content.
- *
- * @return string
- */
- public function shortcode( $atts = array(), $content = null ) {
- $atts = shortcode_atts( array(
- 'author' => bp_displayed_user_id(),
- ), $atts );
- $this->atts = $atts;
- if ( ! function_exists( 'bbpress' ) ) {
- return '';
- }
- $bbp_shortcodes = bbpress()->shortcodes;
- // Bail if shortcodes are unset somehow
- if ( ! is_a( $bbp_shortcodes, 'BBP_Shortcodes' ) ) {
- return '';
- }
- $this->enable_pagination_filter();
- // hook our author filter.
- if ( ! bbp_is_topic_archive() ) {
- add_filter( 'bbp_before_has_topics_parse_args', array( $this, 'update_query' ), 200 );
- }
- if ( ! is_callable( array( $bbp_shortcodes, 'display_topic_index' ) ) ) {
- return '';
- }
- //$this->disable_pagination_filter();
- return $bbp_shortcodes->display_topic_index();
- }
- /**
- * Updates bbPress topic query
- *
- * @param array $args args.
- *
- * @return array
- */
- public function update_query( $args = array() ) {
- if ( ! empty( $this->atts['author'] ) ) {
- $args['author'] = $this->atts['author'];
- unset( $this->atts['author'] );
- }
- $args['posts_per_page'] = 1;
- return $args;
- }
- /**
- * Adds pagination base url filter for the topic index.
- */
- public function enable_pagination_filter() {
- add_filter( 'bbp_get_topics_pagination_base', array( $this, 'filter_pagination_bae_url' ), 200 );
- }
- /**
- * Disables the pagination base url filter for the topic list.
- */
- public function disable_pagination_filter() {
- remove_filter( 'bbp_get_topics_pagination_base', array( $this, 'filter_pagination_bae_url' ), 200 );
- }
- public function filter_pagination_bae_url( $base_url ) {
- $base_url = trailingslashit( bp_displayed_user_domain() ) . "forums/mes-discussions/";
- return $base_url . user_trailingslashit( bbp_get_paged_slug() . '/%#%/' );
- }
- }
- ( new BBP_Extended_Shortcode() )->setup();
Advertisement
Add Comment
Please, Sign In to add comment