[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-templates/bp-nouveau/includes/activity/ -> widgets.php (source)

   1  <?php
   2  /**
   3   * BP Nouveau Activity widgets
   4   *
   5   * @since 3.0.0
   6   * @version 8.0.0
   7   */
   8  
   9  // Exit if accessed directly.
  10  defined( 'ABSPATH' ) || exit;
  11  
  12  /**
  13   * A widget to display the latest activities of your community!
  14   *
  15   * @since 3.0.0
  16   */
  17  class BP_Latest_Activities extends WP_Widget {
  18      /**
  19       * Construct the widget.
  20       *
  21       * @since 3.0.0
  22       * @since 9.0.0 Adds the `show_instance_in_rest` property to Widget options.
  23       */
  24  	public function __construct() {
  25  
  26          /**
  27           * Filters the widget options for the BP_Latest_Activities widget.
  28           *
  29           * @since 3.0.0
  30           *
  31           * @param array $value Array of widget options.
  32           */
  33          $widget_ops = apply_filters(
  34              'bp_latest_activities', array(
  35                  'classname'                   => 'bp-latest-activities buddypress',
  36                  'description'                 => __( 'Display the latest updates of your community having the types of your choice.', 'buddypress' ),
  37                  'customize_selective_refresh' => true,
  38                  'show_instance_in_rest'       => true,
  39              )
  40          );
  41  
  42          parent::__construct( false, __( '(BuddyPress) Latest Activities', 'buddypress' ), $widget_ops );
  43      }
  44  
  45      /**
  46       * Register the widget.
  47       *
  48       * @since 3.0.0
  49       */
  50  	public static function register_widget() {
  51          register_widget( 'BP_Latest_Activities' );
  52      }
  53  
  54      /**
  55       * Display the widget content.
  56       *
  57       * @since 3.0.0
  58       *
  59       * @param array $args     Widget arguments.
  60       * @param array $instance Widget settings, as saved by the user.
  61       */
  62  	public function widget( $args, $instance ) {
  63          // Default values
  64          $title      = __( 'Latest updates', 'buddypress' );
  65          $type       = array( 'activity_update' );
  66          $max        = 5;
  67          $bp_nouveau = bp_nouveau();
  68  
  69          // Check instance for a custom title
  70          if ( ! empty( $instance['title'] ) ) {
  71              $title = $instance['title'];
  72          }
  73  
  74          /**
  75           * Filters the BP_Latest_Activities widget title.
  76           *
  77           * @since 3.0.0
  78           *
  79           * @param string $title    The widget title.
  80           * @param array  $instance The settings for the particular instance of the widget.
  81           * @param string $id_base  Root ID for all widgets of this type.
  82           */
  83          $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
  84  
  85          // Check instance for custom max number of activities to display.
  86          if ( ! empty( $instance['max'] ) ) {
  87              $max = (int) $instance['max'];
  88          }
  89  
  90          // Check instance for custom activity types.
  91          if ( ! empty( $instance['type'] ) ) {
  92              $type = maybe_unserialize( $instance['type'] );
  93              if ( ! is_array( $type ) ) {
  94                  $type = (array) maybe_unserialize( $type );
  95              }
  96  
  97              $classes = array_map( 'sanitize_html_class', array_merge( $type, array( 'bp-latest-activities' ) ) );
  98  
  99              // Add classes to the container
 100              $args['before_widget'] = str_replace( 'bp-latest-activities', join( ' ', $classes ), $args['before_widget'] );
 101          }
 102  
 103          echo $args['before_widget'];
 104  
 105          if ( $title ) {
 106              echo $args['before_title'] . $title . $args['after_title'];
 107          }
 108  
 109          $reset_activities_template = null;
 110          if ( ! empty( $GLOBALS['activities_template'] ) ) {
 111              $reset_activities_template = $GLOBALS['activities_template'];
 112          }
 113  
 114          /**
 115           * Globalize the activity widget arguments.
 116           * @see bp_nouveau_activity_widget_query() to override
 117           */
 118          $bp_nouveau->activity->widget_args = array(
 119              'max'          => $max,
 120              'scope'        => 'all',
 121              'user_id'      => 0,
 122              'object'       => false,
 123              'action'       => join( ',', $type ),
 124              'primary_id'   => 0,
 125              'secondary_id' => 0,
 126          );
 127  
 128          bp_get_template_part( 'activity/widget' );
 129  
 130          // Reset the globals
 131          $GLOBALS['activities_template']    = $reset_activities_template;
 132          $bp_nouveau->activity->widget_args = array();
 133  
 134          echo $args['after_widget'];
 135      }
 136  
 137      /**
 138       * Update the widget settings.
 139       *
 140       * @since 3.0.0
 141       *
 142       * @param array $new_instance The new instance settings.
 143       * @param array $old_instance The old instance settings.
 144       *
 145       * @return array The widget settings.
 146       */
 147  	public function update( $new_instance, $old_instance ) {
 148          $instance = $old_instance;
 149  
 150          $instance['title'] = strip_tags( $new_instance['title'] );
 151          $instance['max']   = 5;
 152          if ( ! empty( $new_instance['max'] ) ) {
 153              $instance['max'] = $new_instance['max'];
 154          }
 155  
 156          $instance['type'] = maybe_serialize( array( 'activity_update' ) );
 157          if ( ! empty( $new_instance['type'] ) ) {
 158              $instance['type'] = maybe_serialize( $new_instance['type'] );
 159          }
 160  
 161          return $instance;
 162      }
 163  
 164      /**
 165       * Display the form to set the widget settings.
 166       *
 167       * @since 3.0.0
 168       *
 169       * @param array $instance Settings for this widget.
 170       *
 171       * @return string HTML output.
 172       */
 173  	public function form( $instance ) {
 174          $instance = bp_parse_args(
 175              (array) $instance,
 176              array(
 177                  'title' => __( 'Latest updates', 'buddypress' ),
 178                  'max'   => 5,
 179                  'type'  => '',
 180              ),
 181              'widget_latest_activities'
 182          );
 183  
 184          $title = esc_attr( $instance['title'] );
 185          $max   = (int) $instance['max'];
 186  
 187          $type = array( 'activity_update' );
 188          if ( ! empty( $instance['type'] ) ) {
 189              $type = maybe_unserialize( $instance['type'] );
 190              if ( ! is_array( $type ) ) {
 191                  $type = (array) maybe_unserialize( $type );
 192              }
 193          }
 194          ?>
 195          <p>
 196              <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'buddypress' ); ?></label>
 197              <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( $title ); ?>" />
 198          </p>
 199  
 200          <p>
 201              <label for="<?php echo $this->get_field_id( 'max' ); ?>"><?php _e( 'Maximum amount to display:', 'buddypress' ); ?></label>
 202              <input type="number" class="widefat" id="<?php echo $this->get_field_id( 'max' ); ?>" name="<?php echo $this->get_field_name( 'max' ); ?>" value="<?php echo intval( $max ); ?>" step="1" min="1" max="20" />
 203          </p>
 204          <p>
 205              <label for="<?php echo $this->get_field_id( 'type' ); ?>"><?php esc_html_e( 'Type:', 'buddypress' ); ?></label>
 206              <select class="widefat" multiple="multiple" id="<?php echo $this->get_field_id( 'type' ); ?>" name="<?php echo $this->get_field_name( 'type' ); ?>[]">
 207                  <?php foreach ( bp_nouveau_get_activity_filters() as $key => $name ) : ?>
 208                      <option value="<?php echo esc_attr( $key ); ?>" <?php selected( in_array( $key, $type, true ) ); ?>><?php echo esc_html( $name ); ?></option>
 209                  <?php endforeach; ?>
 210              </select>
 211          </p>
 212          <?php
 213      }
 214  }


Generated: Thu Dec 26 01:01:04 2024 Cross-referenced by PHPXref 0.7.1