buddydev

Untitled

Aug 20th, 2025
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.41 KB | None | 0 0
  1.  
  2. /**
  3.  * Extends topic shortcode for BuddyBoss to allow filtering by author.
  4.  */
  5. class BBP_Extended_Shortcode {
  6.  
  7.     /**
  8.      * Cached attributes for the current shortcode instance.
  9.      *
  10.      * @var array Attributes
  11.      */
  12.     private array $atts = array();
  13.  
  14.     /**
  15.      * Sets up shortcode.
  16.      */
  17.     public function setup() {
  18.         add_shortcode( 'bbp-topic-index-extended', array( $this, 'shortcode' ) );
  19.  
  20.     }
  21.  
  22.     /**
  23.      * Extended bbPress topic index shortcode.
  24.      *
  25.      * @param array $atts Attributes.
  26.      * @param string $content Content.
  27.      *
  28.      * @return string
  29.      */
  30.     public function shortcode( $atts = array(), $content = null ) {
  31.  
  32.  
  33.         $atts = shortcode_atts( array(
  34.             'author' => bp_displayed_user_id(),
  35.         ), $atts );
  36.  
  37.         $this->atts = $atts;
  38.         if ( ! function_exists( 'bbpress' ) ) {
  39.             return '';
  40.         }
  41.  
  42.         $bbp_shortcodes = bbpress()->shortcodes;
  43.  
  44.         // Bail if shortcodes are unset somehow
  45.         if ( ! is_a( $bbp_shortcodes, 'BBP_Shortcodes' ) ) {
  46.             return '';
  47.         }
  48.  
  49.         $this->enable_pagination_filter();
  50.         // hook our author filter.
  51.         if ( ! bbp_is_topic_archive() ) {
  52.             add_filter( 'bbp_before_has_topics_parse_args', array( $this, 'update_query' ), 200 );
  53.         }
  54.  
  55.         if ( ! is_callable( array( $bbp_shortcodes, 'display_topic_index' ) ) ) {
  56.             return '';
  57.         }
  58.  
  59.         //$this->disable_pagination_filter();
  60.  
  61.         return $bbp_shortcodes->display_topic_index();
  62.     }
  63.  
  64.  
  65.     /**
  66.      * Updates bbPress topic query
  67.      *
  68.      * @param array $args args.
  69.      *
  70.      * @return array
  71.      */
  72.     public function update_query( $args = array() ) {
  73.  
  74.         if ( ! empty( $this->atts['author'] ) ) {
  75.             $args['author'] = $this->atts['author'];
  76.             unset( $this->atts['author'] );
  77.         }
  78.  
  79.         $args['posts_per_page'] = 1;
  80.         return $args;
  81.     }
  82.  
  83.     /**
  84.      * Adds pagination base url filter for the topic index.
  85.      */
  86.     public function enable_pagination_filter() {
  87.         add_filter( 'bbp_get_topics_pagination_base', array( $this, 'filter_pagination_bae_url' ), 200 );
  88.     }
  89.  
  90.     /**
  91.      * Disables the pagination base url filter for the topic list.
  92.      */
  93.     public function disable_pagination_filter() {
  94.         remove_filter( 'bbp_get_topics_pagination_base', array( $this, 'filter_pagination_bae_url' ), 200 );
  95.     }
  96.  
  97.     public function filter_pagination_bae_url( $base_url ) {
  98.         $base_url = trailingslashit( bp_displayed_user_domain() ) . "forums/mes-discussions/";
  99.  
  100.         return $base_url . user_trailingslashit( bbp_get_paged_slug() . '/%#%/' );
  101.     }
  102. }
  103.  
  104. ( new BBP_Extended_Shortcode() )->setup();
  105.  
Advertisement
Add Comment
Please, Sign In to add comment