[ Index ]

PHP Cross Reference of BBPress

title

Body

[close]

/src/includes/common/ -> widgets.php (source)

   1  <?php
   2  
   3  /**
   4   * bbPress Widgets
   5   *
   6   * Contains the forum list, topic list, reply list and login form widgets.
   7   *
   8   * @package bbPress
   9   * @subpackage Widgets
  10   */
  11  
  12  // Exit if accessed directly
  13  defined( 'ABSPATH' ) || exit;
  14  
  15  /**
  16   * bbPress Login Widget
  17   *
  18   * Adds a widget which displays the login form
  19   *
  20   * @since 2.0.0 bbPress (r2827)
  21   */
  22  class BBP_Login_Widget extends WP_Widget {
  23  
  24      /**
  25       * bbPress Login Widget
  26       *
  27       * Registers the login widget
  28       *
  29       * @since 2.0.0 bbPress (r2827)
  30       */
  31  	public function __construct() {
  32          $widget_ops = apply_filters( 'bbp_login_widget_options', array(
  33              'classname'                   => 'bbp_widget_login',
  34              'description'                 => esc_html__( 'A simple login form with optional links to sign-up and lost password pages.', 'bbpress' ),
  35              'customize_selective_refresh' => true
  36          ) );
  37  
  38          parent::__construct( false, esc_html__( '(bbPress) Login Widget', 'bbpress' ), $widget_ops );
  39      }
  40  
  41      /**
  42       * Register the widget
  43       *
  44       * @since 2.0.0 bbPress (r3389)
  45       */
  46  	public static function register_widget() {
  47          register_widget( 'BBP_Login_Widget' );
  48      }
  49  
  50      /**
  51       * Displays the output, the login form
  52       *
  53       * @since 2.0.0 bbPress (r2827)
  54       *
  55       * @param array $args Arguments
  56       * @param array $instance Instance
  57       */
  58  	public function widget( $args = array(), $instance = array() ) {
  59  
  60          // Get widget settings
  61          $settings = $this->parse_settings( $instance );
  62  
  63          // Typical WordPress filter
  64          $settings['title'] = apply_filters( 'widget_title', $settings['title'], $instance, $this->id_base );
  65  
  66          // bbPress filters
  67          $settings['title']    = apply_filters( 'bbp_login_widget_title',    $settings['title'],    $instance, $this->id_base );
  68          $settings['register'] = apply_filters( 'bbp_login_widget_register', $settings['register'], $instance, $this->id_base );
  69          $settings['lostpass'] = apply_filters( 'bbp_login_widget_lostpass', $settings['lostpass'], $instance, $this->id_base );
  70  
  71          echo $args['before_widget'];
  72  
  73          if ( ! empty( $settings['title'] ) ) {
  74              echo $args['before_title'] . $settings['title'] . $args['after_title'];
  75          }
  76  
  77          if ( ! is_user_logged_in() ) : ?>
  78  
  79              <form method="post" action="<?php bbp_wp_login_action( array( 'context' => 'login_post' ) ); ?>" class="bbp-login-form">
  80                  <fieldset class="bbp-form">
  81                      <legend><?php esc_html_e( 'Log In', 'bbpress' ); ?></legend>
  82  
  83                      <div class="bbp-username">
  84                          <label for="user_login"><?php esc_html_e( 'Username', 'bbpress' ); ?>: </label>
  85                          <input type="text" name="log" value="<?php bbp_sanitize_val( 'user_login', 'text' ); ?>" size="20" maxlength="100" id="user_login" autocomplete="off" />
  86                      </div>
  87  
  88                      <div class="bbp-password">
  89                          <label for="user_pass"><?php esc_html_e( 'Password', 'bbpress' ); ?>: </label>
  90                          <input type="password" name="pwd" value="<?php bbp_sanitize_val( 'user_pass', 'password' ); ?>" size="20" id="user_pass" autocomplete="off" />
  91                      </div>
  92  
  93                      <div class="bbp-remember-me">
  94                          <input type="checkbox" name="rememberme" value="forever" <?php checked( bbp_get_sanitize_val( 'rememberme', 'checkbox' ) ); ?> id="rememberme" />
  95                          <label for="rememberme"><?php esc_html_e( 'Keep me signed in', 'bbpress' ); ?></label>
  96                      </div>
  97  
  98                      <?php do_action( 'login_form' ); ?>
  99  
 100                      <div class="bbp-submit-wrapper">
 101  
 102                          <button type="submit" name="user-submit" id="user-submit" class="button submit user-submit"><?php esc_html_e( 'Log In', 'bbpress' ); ?></button>
 103  
 104                          <?php bbp_user_login_fields(); ?>
 105  
 106                      </div>
 107  
 108                      <?php if ( ! empty( $settings['register'] ) || ! empty( $settings['lostpass'] ) ) : ?>
 109  
 110                          <div class="bbp-login-links">
 111  
 112                              <?php if ( ! empty( $settings['register'] ) ) : ?>
 113  
 114                                  <a href="<?php echo esc_url( $settings['register'] ); ?>" title="<?php esc_attr_e( 'Register', 'bbpress' ); ?>" class="bbp-register-link"><?php esc_html_e( 'Register', 'bbpress' ); ?></a>
 115  
 116                              <?php endif; ?>
 117  
 118                              <?php if ( ! empty( $settings['lostpass'] ) ) : ?>
 119  
 120                                  <a href="<?php echo esc_url( $settings['lostpass'] ); ?>" title="<?php esc_attr_e( 'Lost Password', 'bbpress' ); ?>" class="bbp-lostpass-link"><?php esc_html_e( 'Lost Password', 'bbpress' ); ?></a>
 121  
 122                              <?php endif; ?>
 123  
 124                          </div>
 125  
 126                      <?php endif; ?>
 127  
 128                  </fieldset>
 129              </form>
 130  
 131          <?php else : ?>
 132  
 133              <div class="bbp-logged-in">
 134                  <a href="<?php bbp_user_profile_url( bbp_get_current_user_id() ); ?>" class="submit user-submit"><?php echo get_avatar( bbp_get_current_user_id(), '40' ); ?></a>
 135                  <h4><?php bbp_user_profile_link( bbp_get_current_user_id() ); ?></h4>
 136  
 137                  <?php bbp_logout_link(); ?>
 138              </div>
 139  
 140          <?php endif;
 141  
 142          echo $args['after_widget'];
 143      }
 144  
 145      /**
 146       * Update the login widget options
 147       *
 148       * @since 2.0.0 bbPress (r2827)
 149       *
 150       * @param array $new_instance The new instance options
 151       * @param array $old_instance The old instance options
 152       */
 153  	public function update( $new_instance, $old_instance ) {
 154          $instance             = $old_instance;
 155          $instance['title']    = strip_tags( $new_instance['title'] );
 156          $instance['register'] = esc_url_raw( $new_instance['register'] );
 157          $instance['lostpass'] = esc_url_raw( $new_instance['lostpass'] );
 158  
 159          return $instance;
 160      }
 161  
 162      /**
 163       * Output the login widget options form
 164       *
 165       * @since 2.0.0 bbPress (r2827)
 166       *
 167       * @param $instance Instance
 168       */
 169  	public function form( $instance = array() ) {
 170  
 171          // Get widget settings
 172          $settings = $this->parse_settings( $instance ); ?>
 173  
 174          <p>
 175              <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'bbpress' ); ?>
 176              <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $settings['title'] ); ?>" /></label>
 177          </p>
 178  
 179          <p>
 180              <label for="<?php echo $this->get_field_id( 'register' ); ?>"><?php esc_html_e( 'Register URI:', 'bbpress' ); ?>
 181              <input class="widefat" id="<?php echo $this->get_field_id( 'register' ); ?>" name="<?php echo $this->get_field_name( 'register' ); ?>" type="text" value="<?php echo esc_url( $settings['register'] ); ?>" /></label>
 182          </p>
 183  
 184          <p>
 185              <label for="<?php echo $this->get_field_id( 'lostpass' ); ?>"><?php esc_html_e( 'Lost Password URI:', 'bbpress' ); ?>
 186              <input class="widefat" id="<?php echo $this->get_field_id( 'lostpass' ); ?>" name="<?php echo $this->get_field_name( 'lostpass' ); ?>" type="text" value="<?php echo esc_url( $settings['lostpass'] ); ?>" /></label>
 187          </p>
 188  
 189          <?php
 190      }
 191  
 192      /**
 193       * Merge the widget settings into defaults array.
 194       *
 195       * @since 2.3.0 bbPress (r4802)
 196       *
 197       * @param $instance Instance
 198       */
 199  	public function parse_settings( $instance = array() ) {
 200          return bbp_parse_args( $instance, array(
 201              'title'    => '',
 202              'register' => '',
 203              'lostpass' => ''
 204          ), 'login_widget_settings' );
 205      }
 206  }
 207  
 208  /**
 209   * bbPress Views Widget
 210   *
 211   * Adds a widget which displays the view list
 212   *
 213   * @since 2.0.0 bbPress (r3020)
 214   */
 215  class BBP_Views_Widget extends WP_Widget {
 216  
 217      /**
 218       * bbPress View Widget
 219       *
 220       * Registers the view widget
 221       *
 222       * @since 2.0.0 bbPress (r3020)
 223       */
 224  	public function __construct() {
 225          $widget_ops = apply_filters( 'bbp_views_widget_options', array(
 226              'classname'                   => 'widget_display_views',
 227              'description'                 => esc_html__( 'A list of registered optional topic views.', 'bbpress' ),
 228              'customize_selective_refresh' => true
 229          ) );
 230  
 231          parent::__construct( false, esc_html__( '(bbPress) Topic Views List', 'bbpress' ), $widget_ops );
 232      }
 233  
 234      /**
 235       * Register the widget
 236       *
 237       * @since 2.0.0 bbPress (r3389)
 238       */
 239  	public static function register_widget() {
 240          register_widget( 'BBP_Views_Widget' );
 241      }
 242  
 243      /**
 244       * Displays the output, the view list
 245       *
 246       * @since 2.0.0 bbPress (r3020)
 247       *
 248       * @param array $args Arguments
 249       * @param array $instance Instance
 250       */
 251  	public function widget( $args = array(), $instance = array() ) {
 252  
 253          // Only output widget contents if views exist
 254          if ( ! bbp_get_views() ) {
 255              return;
 256          }
 257  
 258          // Get widget settings
 259          $settings = $this->parse_settings( $instance );
 260  
 261          // Typical WordPress filter
 262          $settings['title'] = apply_filters( 'widget_title',          $settings['title'], $instance, $this->id_base );
 263  
 264          // bbPress filter
 265          $settings['title'] = apply_filters( 'bbp_view_widget_title', $settings['title'], $instance, $this->id_base );
 266  
 267          // Start an output buffer
 268          ob_start();
 269  
 270          echo $args['before_widget'];
 271  
 272          if ( ! empty( $settings['title'] ) ) {
 273              echo $args['before_title'] . $settings['title'] . $args['after_title'];
 274          } ?>
 275  
 276          <ul class="bbp-views-widget">
 277  
 278              <?php foreach ( array_keys( bbp_get_views() ) as $view ) : ?>
 279  
 280                  <li><a class="bbp-view-title" href="<?php bbp_view_url( $view ); ?>"><?php bbp_view_title( $view ); ?></a></li>
 281  
 282              <?php endforeach; ?>
 283  
 284          </ul>
 285  
 286          <?php echo $args['after_widget'];
 287  
 288          // Output the current buffer
 289          echo ob_get_clean();
 290      }
 291  
 292      /**
 293       * Update the view widget options
 294       *
 295       * @since 2.0.0 bbPress (r3020)
 296       *
 297       * @param array $new_instance The new instance options
 298       * @param array $old_instance The old instance options
 299       */
 300  	public function update( $new_instance = array(), $old_instance = array() ) {
 301          $instance          = $old_instance;
 302          $instance['title'] = strip_tags( $new_instance['title'] );
 303  
 304          return $instance;
 305      }
 306  
 307      /**
 308       * Output the view widget options form
 309       *
 310       * @since 2.0.0 bbPress (r3020)
 311       *
 312       * @param $instance Instance
 313       */
 314  	public function form( $instance = array() ) {
 315  
 316          // Get widget settings
 317          $settings = $this->parse_settings( $instance ); ?>
 318  
 319          <p>
 320              <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'bbpress' ); ?>
 321                  <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $settings['title'] ); ?>" />
 322              </label>
 323          </p>
 324  
 325          <?php
 326      }
 327  
 328      /**
 329       * Merge the widget settings into defaults array.
 330       *
 331       * @since 2.3.0 bbPress (r4802)
 332       *
 333       * @param $instance Instance
 334       */
 335  	public function parse_settings( $instance = array() ) {
 336          return bbp_parse_args( $instance, array(
 337              'title' => ''
 338          ), 'view_widget_settings' );
 339      }
 340  }
 341  
 342  /**
 343   * bbPress Search Widget
 344   *
 345   * Adds a widget which displays the forum search form
 346   *
 347   * @since 2.3.0 bbPress (r4579)
 348   */
 349  class BBP_Search_Widget extends WP_Widget {
 350  
 351      /**
 352       * bbPress Search Widget
 353       *
 354       * Registers the search widget
 355       *
 356       * @since 2.3.0 bbPress (r4579)
 357       */
 358  	public function __construct() {
 359          $widget_ops = apply_filters( 'bbp_search_widget_options', array(
 360              'classname'                   => 'widget_display_search',
 361              'description'                 => esc_html__( 'The bbPress forum search form.', 'bbpress' ),
 362              'customize_selective_refresh' => true
 363          ) );
 364  
 365          parent::__construct( false, esc_html__( '(bbPress) Forum Search Form', 'bbpress' ), $widget_ops );
 366      }
 367  
 368      /**
 369       * Register the widget
 370       *
 371       * @since 2.3.0 bbPress (r4579)
 372       */
 373  	public static function register_widget() {
 374          register_widget( 'BBP_Search_Widget' );
 375      }
 376  
 377      /**
 378       * Displays the output, the search form
 379       *
 380       * @since 2.3.0 bbPress (r4579)
 381       */
 382  	public function widget( $args, $instance ) {
 383  
 384          // Bail if search is disabled
 385          if ( ! bbp_allow_search() ) {
 386              return;
 387          }
 388  
 389          // Get widget settings
 390          $settings = $this->parse_settings( $instance );
 391  
 392          // Typical WordPress filter
 393          $settings['title'] = apply_filters( 'widget_title',            $settings['title'], $instance, $this->id_base );
 394  
 395          // bbPress filter
 396          $settings['title'] = apply_filters( 'bbp_search_widget_title', $settings['title'], $instance, $this->id_base );
 397  
 398          echo $args['before_widget'];
 399  
 400          if ( ! empty( $settings['title'] ) ) {
 401              echo $args['before_title'] . $settings['title'] . $args['after_title'];
 402          }
 403  
 404          bbp_get_template_part( 'form', 'search' );
 405  
 406          echo $args['after_widget'];
 407      }
 408  
 409      /**
 410       * Update the widget options
 411       *
 412       * @since 2.3.0 bbPress (r4579)
 413       *
 414       * @param array $new_instance The new instance options
 415       * @param array $old_instance The old instance options
 416       */
 417  	public function update( $new_instance, $old_instance ) {
 418          $instance          = $old_instance;
 419          $instance['title'] = strip_tags( $new_instance['title'] );
 420  
 421          return $instance;
 422      }
 423  
 424      /**
 425       * Output the search widget options form
 426       *
 427       * @since 2.3.0 bbPress (r4579)
 428       *
 429       * @param $instance Instance
 430       */
 431  	public function form( $instance ) {
 432  
 433          // Get widget settings
 434          $settings = $this->parse_settings( $instance ); ?>
 435  
 436          <p>
 437              <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'bbpress' ); ?>
 438                  <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $settings['title'] ); ?>" />
 439              </label>
 440          </p>
 441  
 442          <?php
 443      }
 444  
 445      /**
 446       * Merge the widget settings into defaults array.
 447       *
 448       * @since 2.3.0 bbPress (r4802)
 449       *
 450       * @param $instance Instance
 451       */
 452  	public function parse_settings( $instance = array() ) {
 453          return bbp_parse_args( $instance, array(
 454              'title' => esc_html__( 'Search Forums', 'bbpress' )
 455          ), 'search_widget_settings' );
 456      }
 457  }
 458  
 459  /**
 460   * bbPress Forum Widget
 461   *
 462   * Adds a widget which displays the forum list
 463   *
 464   * @since 2.0.0 bbPress (r2653)
 465   */
 466  class BBP_Forums_Widget extends WP_Widget {
 467  
 468      /**
 469       * bbPress Forum Widget
 470       *
 471       * Registers the forum widget
 472       *
 473       * @since 2.0.0 bbPress (r2653)
 474       */
 475  	public function __construct() {
 476          $widget_ops = apply_filters( 'bbp_forums_widget_options', array(
 477              'classname'                   => 'widget_display_forums',
 478              'description'                 => esc_html__( 'A list of forums with an option to set the parent.', 'bbpress' ),
 479              'customize_selective_refresh' => true
 480          ) );
 481  
 482          parent::__construct( false, esc_html__( '(bbPress) Forums List', 'bbpress' ), $widget_ops );
 483      }
 484  
 485      /**
 486       * Register the widget
 487       *
 488       * @since 2.0.0 bbPress (r3389)
 489       */
 490  	public static function register_widget() {
 491          register_widget( 'BBP_Forums_Widget' );
 492      }
 493  
 494      /**
 495       * Displays the output, the forum list
 496       *
 497       * @since 2.0.0 bbPress (r2653)
 498       *
 499       * @param array $args Arguments
 500       * @param array $instance Instance
 501       */
 502  	public function widget( $args, $instance ) {
 503  
 504          // Get widget settings
 505          $settings = $this->parse_settings( $instance );
 506  
 507          // Typical WordPress filter
 508          $settings['title'] = apply_filters( 'widget_title',           $settings['title'], $instance, $this->id_base );
 509  
 510          // bbPress filter
 511          $settings['title'] = apply_filters( 'bbp_forum_widget_title', $settings['title'], $instance, $this->id_base );
 512  
 513          // Note: private and hidden forums will be excluded via the
 514          // bbp_pre_get_posts_normalize_forum_visibility action and function.
 515          $widget_query = new WP_Query( array(
 516  
 517              // What and how
 518              'post_type'      => bbp_get_forum_post_type(),
 519              'post_status'    => bbp_get_public_status_id(),
 520              'post_parent'    => $settings['parent_forum'],
 521              'posts_per_page' => (int) get_option( '_bbp_forums_per_page', 50 ),
 522  
 523              // Order
 524              'orderby' => 'menu_order title',
 525              'order'   => 'ASC',
 526  
 527              // Performance
 528              'ignore_sticky_posts'    => true,
 529              'no_found_rows'          => true,
 530              'update_post_term_cache' => false,
 531              'update_post_meta_cache' => false
 532          ) );
 533  
 534          // Bail if no posts
 535          if ( ! $widget_query->have_posts() ) {
 536              return;
 537          }
 538  
 539          echo $args['before_widget'];
 540  
 541          if ( ! empty( $settings['title'] ) ) {
 542              echo $args['before_title'] . $settings['title'] . $args['after_title'];
 543          } ?>
 544  
 545          <ul class="bbp-forums-widget">
 546  
 547              <?php while ( $widget_query->have_posts() ) : $widget_query->the_post(); ?>
 548  
 549                  <li <?php echo ( bbp_get_forum_id() === $widget_query->post->ID ? ' class="bbp-forum-widget-current-forum"' : '' ); ?>>
 550                      <a class="bbp-forum-title" href="<?php bbp_forum_permalink( $widget_query->post->ID ); ?>">
 551                          <?php bbp_forum_title( $widget_query->post->ID ); ?>
 552                      </a>
 553                  </li>
 554  
 555              <?php endwhile; ?>
 556  
 557          </ul>
 558  
 559          <?php echo $args['after_widget'];
 560  
 561          // Reset the $post global
 562          wp_reset_postdata();
 563      }
 564  
 565      /**
 566       * Update the forum widget options
 567       *
 568       * @since 2.0.0 bbPress (r2653)
 569       *
 570       * @param array $new_instance The new instance options
 571       * @param array $old_instance The old instance options
 572       */
 573  	public function update( $new_instance, $old_instance ) {
 574          $instance                 = $old_instance;
 575          $instance['title']        = strip_tags( $new_instance['title'] );
 576          $instance['parent_forum'] = sanitize_text_field( $new_instance['parent_forum'] );
 577  
 578          // Force to any
 579          if ( ! empty( $instance['parent_forum'] ) && ! is_numeric( $instance['parent_forum'] ) ) {
 580              $instance['parent_forum'] = 'any';
 581          }
 582  
 583          return $instance;
 584      }
 585  
 586      /**
 587       * Output the forum widget options form
 588       *
 589       * @since 2.0.0 bbPress (r2653)
 590       *
 591       * @param $instance Instance
 592       */
 593  	public function form( $instance ) {
 594  
 595          // Get widget settings
 596          $settings = $this->parse_settings( $instance ); ?>
 597  
 598          <p>
 599              <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'bbpress' ); ?>
 600                  <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $settings['title'] ); ?>" />
 601              </label>
 602          </p>
 603  
 604          <p>
 605              <label for="<?php echo $this->get_field_id( 'parent_forum' ); ?>"><?php esc_html_e( 'Parent Forum ID:', 'bbpress' ); ?>
 606                  <input class="widefat" id="<?php echo $this->get_field_id( 'parent_forum' ); ?>" name="<?php echo $this->get_field_name( 'parent_forum' ); ?>" type="text" value="<?php echo esc_attr( $settings['parent_forum'] ); ?>" />
 607              </label>
 608  
 609              <br />
 610  
 611              <small><?php esc_html_e( '"0" to show only root - "any" to show all', 'bbpress' ); ?></small>
 612          </p>
 613  
 614          <?php
 615      }
 616  
 617      /**
 618       * Merge the widget settings into defaults array.
 619       *
 620       * @since 2.3.0 bbPress (r4802)
 621       *
 622       * @param $instance Instance
 623       */
 624  	public function parse_settings( $instance = array() ) {
 625          return bbp_parse_args( $instance, array(
 626              'title'        => esc_html__( 'Forums', 'bbpress' ),
 627              'parent_forum' => 0
 628          ), 'forum_widget_settings' );
 629      }
 630  }
 631  
 632  /**
 633   * bbPress Topic Widget
 634   *
 635   * Adds a widget which displays the topic list
 636   *
 637   * @since 2.0.0 bbPress (r2653)
 638   */
 639  class BBP_Topics_Widget extends WP_Widget {
 640  
 641      /**
 642       * bbPress Topic Widget
 643       *
 644       * Registers the topic widget
 645       *
 646       * @since 2.0.0 bbPress (r2653)
 647       */
 648  	public function __construct() {
 649          $widget_ops = apply_filters( 'bbp_topics_widget_options', array(
 650              'classname'                   => 'widget_display_topics',
 651              'description'                 => esc_html__( 'A list of recent topics, sorted by: newness, popularity, or recent replies.', 'bbpress' ),
 652              'customize_selective_refresh' => true
 653          ) );
 654  
 655          parent::__construct( false, esc_html__( '(bbPress) Recent Topics', 'bbpress' ), $widget_ops );
 656      }
 657  
 658      /**
 659       * Register the widget
 660       *
 661       * @since 2.0.0 bbPress (r3389)
 662       */
 663  	public static function register_widget() {
 664          register_widget( 'BBP_Topics_Widget' );
 665      }
 666  
 667      /**
 668       * Displays the output, the topic list
 669       *
 670       * @since 2.0.0 bbPress (r2653)
 671       *
 672       * @param array $args
 673       * @param array $instance
 674       */
 675  	public function widget( $args = array(), $instance = array() ) {
 676  
 677          // Get widget settings
 678          $settings = $this->parse_settings( $instance );
 679  
 680          // Typical WordPress filter
 681          $settings['title'] = apply_filters( 'widget_title',           $settings['title'], $instance, $this->id_base );
 682  
 683          // bbPress filter
 684          $settings['title'] = apply_filters( 'bbp_topic_widget_title', $settings['title'], $instance, $this->id_base );
 685  
 686          // How do we want to order our results?
 687          switch ( $settings['order_by'] ) {
 688  
 689              // Order by most recent replies
 690              case 'freshness' :
 691                  $topics_query = array(
 692  
 693                      // What and how
 694                      'post_type'      => bbp_get_topic_post_type(),
 695                      'post_status'    => bbp_get_public_topic_statuses(),
 696                      'post_parent'    => $settings['parent_forum'],
 697                      'posts_per_page' => (int) $settings['max_shown'],
 698                      'meta_query'     => array( array(
 699                          'key'  => '_bbp_last_active_time',
 700                          'type' => 'DATETIME'
 701                      ) ),
 702  
 703                      // Ordering
 704                      'orderby' => 'meta_value',
 705                      'order'   => 'DESC',
 706  
 707                      // Performance
 708                      'ignore_sticky_posts'    => true,
 709                      'no_found_rows'          => true,
 710                      'update_post_term_cache' => false,
 711                      'update_post_meta_cache' => false
 712                  );
 713                  break;
 714  
 715              // Order by total number of replies
 716              case 'popular' :
 717                  $topics_query = array(
 718  
 719                      // What and how
 720                      'post_type'      => bbp_get_topic_post_type(),
 721                      'post_status'    => bbp_get_public_topic_statuses(),
 722                      'post_parent'    => $settings['parent_forum'],
 723                      'posts_per_page' => (int) $settings['max_shown'],
 724                      'meta_query'     => array( array(
 725                          'key'  => '_bbp_reply_count',
 726                          'type' => 'NUMERIC'
 727                      ) ),
 728  
 729                      // Ordering
 730                      'orderby' => 'meta_value_num',
 731                      'order'   => 'DESC',
 732  
 733                      // Performance
 734                      'ignore_sticky_posts'    => true,
 735                      'no_found_rows'          => true,
 736                      'update_post_term_cache' => false,
 737                      'update_post_meta_cache' => false
 738                  );
 739                  break;
 740  
 741              // Order by which topic was created most recently
 742              case 'newness' :
 743              default :
 744                  $topics_query = array(
 745  
 746                      // What and how
 747                      'post_type'      => bbp_get_topic_post_type(),
 748                      'post_status'    => bbp_get_public_topic_statuses(),
 749                      'post_parent'    => $settings['parent_forum'],
 750                      'posts_per_page' => (int) $settings['max_shown'],
 751  
 752                      // Ordering
 753                      'orderby' => 'date',
 754                      'order'   => 'DESC',
 755  
 756                      // Performance
 757                      'ignore_sticky_posts'    => true,
 758                      'no_found_rows'          => true,
 759                      'update_post_term_cache' => false,
 760                      'update_post_meta_cache' => false
 761                  );
 762                  break;
 763          }
 764  
 765          // Note: private and hidden forums will be excluded via the
 766          // bbp_pre_get_posts_normalize_forum_visibility action and function.
 767          $widget_query = new WP_Query( $topics_query );
 768  
 769          // Bail if no topics are found
 770          if ( ! $widget_query->have_posts() ) {
 771              return;
 772          }
 773  
 774          // Start an output buffer
 775          ob_start();
 776  
 777          echo $args['before_widget'];
 778  
 779          if ( ! empty( $settings['title'] ) ) {
 780              echo $args['before_title'] . $settings['title'] . $args['after_title'];
 781          } ?>
 782  
 783          <ul class="bbp-topics-widget <?php echo esc_attr( $settings['order_by'] ); ?>">
 784  
 785              <?php while ( $widget_query->have_posts() ) :
 786  
 787                  $widget_query->the_post();
 788                  $topic_id    = bbp_get_topic_id( $widget_query->post->ID );
 789                  $author_link = '';
 790  
 791                  // Maybe get the topic author
 792                  if ( ! empty( $settings['show_user'] ) ) :
 793                      $author_link = bbp_get_topic_author_link( array( 'post_id' => $topic_id, 'type' => 'both', 'size' => 14 ) );
 794                  endif; ?>
 795  
 796                  <li>
 797                      <a class="bbp-forum-title" href="<?php bbp_topic_permalink( $topic_id ); ?>"><?php bbp_topic_title( $topic_id ); ?></a>
 798  
 799                      <?php if ( ! empty( $author_link ) ) : ?>
 800  
 801                          <?php printf( esc_html_x( 'by %1$s', 'widgets', 'bbpress' ), '<span class="topic-author">' . $author_link . '</span>' ); ?>
 802  
 803                      <?php endif; ?>
 804  
 805                      <?php if ( ! empty( $settings['show_date'] ) ) : ?>
 806  
 807                          <div><?php bbp_topic_last_active_time( $topic_id ); ?></div>
 808  
 809                      <?php endif; ?>
 810  
 811                  </li>
 812  
 813              <?php endwhile; ?>
 814  
 815          </ul>
 816  
 817          <?php echo $args['after_widget'];
 818  
 819          // Reset the $post global
 820          wp_reset_postdata();
 821  
 822          // Output the current buffer
 823          echo ob_get_clean();
 824      }
 825  
 826      /**
 827       * Update the topic widget options
 828       *
 829       * @since 2.0.0 bbPress (r2653)
 830       *
 831       * @param array $new_instance The new instance options
 832       * @param array $old_instance The old instance options
 833       */
 834  	public function update( $new_instance = array(), $old_instance = array() ) {
 835          $instance                 = $old_instance;
 836          $instance['title']        = strip_tags( $new_instance['title'] );
 837          $instance['order_by']     = strip_tags( $new_instance['order_by'] );
 838          $instance['parent_forum'] = sanitize_text_field( $new_instance['parent_forum'] );
 839          $instance['max_shown']    = (int) $new_instance['max_shown'];
 840  
 841          // Date
 842          $instance['show_date'] = isset( $new_instance['show_date'] )
 843              ? (bool) $new_instance['show_date']
 844              : false;
 845  
 846          // Author
 847          $instance['show_user'] = isset( $new_instance['show_user'] )
 848              ? (bool) $new_instance['show_user']
 849              : false;
 850  
 851          // Force to any
 852          if ( ! empty( $instance['parent_forum'] ) && ! is_numeric( $instance['parent_forum'] ) ) {
 853              $instance['parent_forum'] = 'any';
 854          }
 855  
 856          return $instance;
 857      }
 858  
 859      /**
 860       * Output the topic widget options form
 861       *
 862       * @since 2.0.0 bbPress (r2653)
 863       *
 864       * @param $instance Instance
 865       */
 866  	public function form( $instance = array() ) {
 867  
 868          // Get widget settings
 869          $settings = $this->parse_settings( $instance ); ?>
 870  
 871          <p><label for="<?php echo $this->get_field_id( 'title'     ); ?>"><?php esc_html_e( 'Title:',                  'bbpress' ); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'title'     ); ?>" name="<?php echo $this->get_field_name( 'title'     ); ?>" type="text" value="<?php echo esc_attr( $settings['title']     ); ?>" /></label></p>
 872          <p><label for="<?php echo $this->get_field_id( 'max_shown' ); ?>"><?php esc_html_e( 'Maximum topics to show:', 'bbpress' ); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'max_shown' ); ?>" name="<?php echo $this->get_field_name( 'max_shown' ); ?>" type="text" value="<?php echo esc_attr( $settings['max_shown'] ); ?>" /></label></p>
 873  
 874          <p>
 875              <label for="<?php echo $this->get_field_id( 'parent_forum' ); ?>"><?php esc_html_e( 'Parent Forum ID:', 'bbpress' ); ?>
 876                  <input class="widefat" id="<?php echo $this->get_field_id( 'parent_forum' ); ?>" name="<?php echo $this->get_field_name( 'parent_forum' ); ?>" type="text" value="<?php echo esc_attr( $settings['parent_forum'] ); ?>" />
 877              </label>
 878  
 879              <br />
 880  
 881              <small><?php esc_html_e( '"0" to show only root - "any" to show all', 'bbpress' ); ?></small>
 882          </p>
 883  
 884          <p><label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php esc_html_e( 'Show post date:',    'bbpress' ); ?> <input type="checkbox" id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" <?php checked( true, $settings['show_date'] ); ?> value="1" /></label></p>
 885          <p><label for="<?php echo $this->get_field_id( 'show_user' ); ?>"><?php esc_html_e( 'Show topic author:', 'bbpress' ); ?> <input type="checkbox" id="<?php echo $this->get_field_id( 'show_user' ); ?>" name="<?php echo $this->get_field_name( 'show_user' ); ?>" <?php checked( true, $settings['show_user'] ); ?> value="1" /></label></p>
 886  
 887          <p>
 888              <label for="<?php echo $this->get_field_id( 'order_by' ); ?>"><?php esc_html_e( 'Order By:', 'bbpress' ); ?></label>
 889              <select name="<?php echo $this->get_field_name( 'order_by' ); ?>" id="<?php echo $this->get_field_id( 'order_by' ); ?>">
 890                  <option <?php selected( $settings['order_by'], 'newness'   ); ?> value="newness"><?php esc_html_e( 'Newest Topics',                'bbpress' ); ?></option>
 891                  <option <?php selected( $settings['order_by'], 'popular'   ); ?> value="popular"><?php esc_html_e( 'Popular Topics',               'bbpress' ); ?></option>
 892                  <option <?php selected( $settings['order_by'], 'freshness' ); ?> value="freshness"><?php esc_html_e( 'Topics With Recent Replies', 'bbpress' ); ?></option>
 893              </select>
 894          </p>
 895  
 896          <?php
 897      }
 898  
 899      /**
 900       * Merge the widget settings into defaults array.
 901       *
 902       * @since 2.3.0 bbPress (r4802)
 903       *
 904       * @param $instance Instance
 905       */
 906  	public function parse_settings( $instance = array() ) {
 907          return bbp_parse_args( $instance, array(
 908              'title'        => esc_html__( 'Recent Topics', 'bbpress' ),
 909              'max_shown'    => 5,
 910              'show_date'    => false,
 911              'show_user'    => false,
 912              'parent_forum' => 'any',
 913              'order_by'     => false
 914          ), 'topic_widget_settings' );
 915      }
 916  }
 917  
 918  /**
 919   * bbPress Statistics Widget
 920   *
 921   * Adds a widget which displays the forum statistics
 922   *
 923   * @since 2.3.0 bbPress (r4509)
 924   */
 925  class BBP_Stats_Widget extends WP_Widget {
 926  
 927      /**
 928       * bbPress Statistics Widget
 929       *
 930       * Registers the statistics widget
 931       *
 932       * @since 2.3.0 bbPress (r4509)
 933       */
 934  	public function __construct() {
 935          $widget_ops = apply_filters( 'bbp_stats_widget_options', array(
 936              'classname'                   => 'widget_display_stats',
 937              'description'                 => esc_html__( 'Some statistics from your forum.', 'bbpress' ),
 938              'customize_selective_refresh' => true
 939          ) );
 940  
 941          parent::__construct( false, esc_html__( '(bbPress) Statistics', 'bbpress' ), $widget_ops );
 942      }
 943  
 944      /**
 945       * Register the widget
 946       *
 947       * @since 2.3.0 bbPress (r4509)
 948       */
 949  	public static function register_widget() {
 950          register_widget( 'BBP_Stats_Widget' );
 951      }
 952  
 953      /**
 954       * Displays the output, the statistics
 955       *
 956       * @since 2.3.0 bbPress (r4509)
 957       *
 958       * @param array $args     Arguments
 959       * @param array $instance Instance
 960       */
 961  	public function widget( $args = array(), $instance = array() ) {
 962  
 963          // Get widget settings
 964          $settings = $this->parse_settings( $instance );
 965  
 966          // Typical WordPress filter
 967          $settings['title'] = apply_filters( 'widget_title',           $settings['title'], $instance, $this->id_base );
 968  
 969          // bbPress widget title filter
 970          $settings['title'] = apply_filters( 'bbp_stats_widget_title', $settings['title'], $instance, $this->id_base );
 971  
 972          echo $args['before_widget'];
 973  
 974          if ( ! empty( $settings['title'] ) ) {
 975              echo $args['before_title'] . $settings['title'] . $args['after_title'];
 976          }
 977  
 978          bbp_get_template_part( 'content', 'statistics' );
 979  
 980          echo $args['after_widget'];
 981      }
 982  
 983      /**
 984       * Update the statistics widget options
 985       *
 986       * @since 2.3.0 bbPress (r4509)
 987       *
 988       * @param array $new_instance The new instance options
 989       * @param array $old_instance The old instance options
 990       *
 991       * @return array
 992       */
 993  	public function update( $new_instance, $old_instance ) {
 994          $instance          = $old_instance;
 995          $instance['title'] = strip_tags( $new_instance['title'] );
 996  
 997          return $instance;
 998      }
 999  
1000      /**
1001       * Output the statistics widget options form
1002       *
1003       * @since 2.3.0 bbPress (r4509)
1004       *
1005       * @param $instance
1006       *
1007       * @return string|void
1008       */
1009  	public function form( $instance ) {
1010  
1011          // Get widget settings
1012          $settings = $this->parse_settings( $instance ); ?>
1013  
1014          <p>
1015              <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'bbpress' ); ?>
1016                  <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $settings['title'] ); ?>"/>
1017              </label>
1018          </p>
1019  
1020      <?php
1021      }
1022  
1023      /**
1024       * Merge the widget settings into defaults array.
1025       *
1026       * @since 2.3.0 bbPress (r4802)
1027       *
1028       * @param $instance Instance
1029       */
1030  	public function parse_settings( $instance = array() ) {
1031          return bbp_parse_args( $instance, array(
1032              'title' => esc_html__( 'Forum Statistics', 'bbpress' )
1033          ), 'stats_widget_settings' );
1034      }
1035  }
1036  
1037  /**
1038   * bbPress Replies Widget
1039   *
1040   * Adds a widget which displays the replies list
1041   *
1042   * @since 2.0.0 bbPress (r2653)
1043   */
1044  class BBP_Replies_Widget extends WP_Widget {
1045  
1046      /**
1047       * bbPress Replies Widget
1048       *
1049       * Registers the replies widget
1050       *
1051       * @since 2.0.0 bbPress (r2653)
1052       */
1053  	public function __construct() {
1054          $widget_ops = apply_filters( 'bbp_replies_widget_options', array(
1055              'classname'                   => 'widget_display_replies',
1056              'description'                 => esc_html__( 'A list of the most recent replies.', 'bbpress' ),
1057              'customize_selective_refresh' => true
1058          ) );
1059  
1060          parent::__construct( false, esc_html__( '(bbPress) Recent Replies', 'bbpress' ), $widget_ops );
1061      }
1062  
1063      /**
1064       * Register the widget
1065       *
1066       * @since 2.0.0 bbPress (r3389)
1067       */
1068  	public static function register_widget() {
1069          register_widget( 'BBP_Replies_Widget' );
1070      }
1071  
1072      /**
1073       * Displays the output, the replies list
1074       *
1075       * @since 2.0.0 bbPress (r2653)
1076       *
1077       * @param array $args
1078       * @param array $instance
1079       */
1080  	public function widget( $args, $instance ) {
1081  
1082          // Get widget settings
1083          $settings = $this->parse_settings( $instance );
1084  
1085          // Typical WordPress filter
1086          $settings['title'] = apply_filters( 'widget_title',             $settings['title'], $instance, $this->id_base );
1087  
1088          // bbPress filter
1089          $settings['title'] = apply_filters( 'bbp_replies_widget_title', $settings['title'], $instance, $this->id_base );
1090  
1091          // Note: private and hidden forums will be excluded via the
1092          // bbp_pre_get_posts_normalize_forum_visibility action and function.
1093          $widget_query = new WP_Query( array(
1094  
1095              // What and when
1096              'post_type'      => bbp_get_reply_post_type(),
1097              'post_status'    => bbp_get_public_reply_statuses(),
1098              'posts_per_page' => (int) $settings['max_shown'],
1099  
1100              // Performance
1101              'ignore_sticky_posts'    => true,
1102              'no_found_rows'          => true,
1103              'update_post_term_cache' => false,
1104              'update_post_meta_cache' => false
1105          ) );
1106  
1107          // Bail if no replies
1108          if ( ! $widget_query->have_posts() ) {
1109              return;
1110          }
1111  
1112          // Start an output buffer
1113          ob_start();
1114  
1115          echo $args['before_widget'];
1116  
1117          if ( ! empty( $settings['title'] ) ) {
1118              echo $args['before_title'] . $settings['title'] . $args['after_title'];
1119          } ?>
1120  
1121          <ul class="bbp-replies-widget">
1122  
1123              <?php while ( $widget_query->have_posts() ) : $widget_query->the_post(); ?>
1124  
1125                  <li>
1126  
1127                      <?php
1128  
1129                      // Verify the reply ID
1130                      $reply_id   = bbp_get_reply_id( $widget_query->post->ID );
1131                      $reply_link = '<a class="bbp-reply-topic-title" href="' . esc_url( bbp_get_reply_url( $reply_id ) ) . '" title="' . esc_attr( bbp_get_reply_excerpt( $reply_id, 50 ) ) . '">' . esc_html( bbp_get_reply_topic_title( $reply_id ) ) . '</a>';
1132                      $time       = get_the_time( 'U', $reply_id );
1133                      $show_date  = '<time datetime="' . gmdate( 'Y-m-d H:i:s', $time ) . '">' . esc_html( bbp_get_time_since( $time ) ) . '</time>';
1134  
1135                      // Only query user if showing them
1136                      if ( ! empty( $settings['show_user'] ) ) :
1137                          $author_link = bbp_get_reply_author_link( array( 'post_id' => $reply_id, 'type' => 'both', 'size' => 14 ) );
1138                      else :
1139                          $author_link = false;
1140                      endif;
1141  
1142                      // Reply author, link, and timestamp
1143                      if ( ! empty( $settings['show_date'] ) && ! empty( $author_link ) ) :
1144  
1145                          // translators: 1: reply author, 2: reply link, 3: reply timestamp
1146                          printf( esc_html_x( '%1$s on %2$s %3$s', 'widgets', 'bbpress' ), $author_link, $reply_link, $show_date );
1147  
1148                      // Reply link and timestamp
1149                      elseif ( ! empty( $settings['show_date'] ) ) :
1150                          echo $reply_link . ' ' . $show_date;
1151  
1152                      // Reply author and title
1153                      elseif ( ! empty( $author_link ) ) :
1154  
1155                          // translators: 1: reply author, 2: reply link
1156                          printf( esc_html_x( '%1$s on %2$s', 'widgets', 'bbpress' ), $author_link, $reply_link );
1157  
1158                      // Only the reply title
1159                      else :
1160                          echo $reply_link;
1161                      endif;
1162  
1163                      ?>
1164  
1165                  </li>
1166  
1167              <?php endwhile; ?>
1168  
1169          </ul>
1170  
1171          <?php echo $args['after_widget'];
1172  
1173          // Reset the $post global
1174          wp_reset_postdata();
1175  
1176          // Output the current buffer
1177          echo ob_get_clean();
1178      }
1179  
1180      /**
1181       * Update the reply widget options
1182       *
1183       * @since 2.0.0 bbPress (r2653)
1184       *
1185       * @param array $new_instance The new instance options
1186       * @param array $old_instance The old instance options
1187       */
1188  	public function update( $new_instance = array(), $old_instance = array() ) {
1189          $instance              = $old_instance;
1190          $instance['title']     = strip_tags( $new_instance['title'] );
1191          $instance['max_shown'] = (int) $new_instance['max_shown'];
1192  
1193          // Date
1194          $instance['show_date'] = isset( $new_instance['show_date'] )
1195              ? (bool) $new_instance['show_date']
1196              : false;
1197  
1198          // Author
1199          $instance['show_user'] = isset( $new_instance['show_user'] )
1200              ? (bool) $new_instance['show_user']
1201              : false;
1202  
1203          return $instance;
1204      }
1205  
1206      /**
1207       * Output the reply widget options form
1208       *
1209       * @since 2.0.0 bbPress (r2653)
1210       *
1211       * @param $instance Instance
1212       */
1213  	public function form( $instance = array() ) {
1214  
1215          // Get widget settings
1216          $settings = $this->parse_settings( $instance ); ?>
1217  
1218          <p><label for="<?php echo $this->get_field_id( 'title'     ); ?>"><?php esc_html_e( 'Title:',                   'bbpress' ); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'title'     ); ?>" name="<?php echo $this->get_field_name( 'title'     ); ?>" type="text" value="<?php echo esc_attr( $settings['title']     ); ?>" /></label></p>
1219          <p><label for="<?php echo $this->get_field_id( 'max_shown' ); ?>"><?php esc_html_e( 'Maximum replies to show:', 'bbpress' ); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'max_shown' ); ?>" name="<?php echo $this->get_field_name( 'max_shown' ); ?>" type="text" value="<?php echo esc_attr( $settings['max_shown'] ); ?>" /></label></p>
1220          <p><label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php esc_html_e( 'Show post date:',          'bbpress' ); ?> <input type="checkbox" id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" <?php checked( true, $settings['show_date'] ); ?> value="1" /></label></p>
1221          <p><label for="<?php echo $this->get_field_id( 'show_user' ); ?>"><?php esc_html_e( 'Show reply author:',       'bbpress' ); ?> <input type="checkbox" id="<?php echo $this->get_field_id( 'show_user' ); ?>" name="<?php echo $this->get_field_name( 'show_user' ); ?>" <?php checked( true, $settings['show_user'] ); ?> value="1" /></label></p>
1222  
1223          <?php
1224      }
1225  
1226      /**
1227       * Merge the widget settings into defaults array.
1228       *
1229       * @since 2.3.0 bbPress (r4802)
1230       *
1231       * @param $instance Instance
1232       */
1233  	public function parse_settings( $instance = array() ) {
1234          return bbp_parse_args( $instance, array(
1235              'title'     => esc_html__( 'Recent Replies', 'bbpress' ),
1236              'max_shown' => 5,
1237              'show_date' => false,
1238              'show_user' => false
1239          ), 'replies_widget_settings' );
1240      }
1241  }


Generated: Thu Apr 25 01:01:05 2024 Cross-referenced by PHPXref 0.7.1