[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Common Classes
   4   *
   5   * @since 3.0.0
   6   * @version 9.0.0
   7   */
   8  
   9  // Exit if accessed directly.
  10  defined( 'ABSPATH' ) || exit;
  11  
  12  /**
  13   * Builds a group of BP_Button
  14   *
  15   * @since 3.0.0
  16   */
  17  class BP_Buttons_Group {
  18  
  19      /**
  20       * The parameters of the Group of buttons
  21       *
  22       * @var array
  23       */
  24      protected $group = array();
  25  
  26      /**
  27       * Constructor
  28       *
  29       * @since 3.0.0
  30       *
  31       * @param array $args Optional array having the following parameters {
  32       *     @type string $id                A string to use as the unique ID for the button. Required.
  33       *     @type int    $position          Where to insert the Button. Defaults to 99.
  34       *     @type string $component         The Component's the button is build for (eg: Activity, Groups..). Required.
  35       *     @type bool   $must_be_logged_in Whether the button should only be displayed to logged in users. Defaults to True.
  36       *     @type bool   $block_self        Optional. True if the button should be hidden when a user is viewing his own profile.
  37       *                                     Defaults to False.
  38       *     @type string $parent_element    Whether to use a wrapper. Defaults to false.
  39       *     @type string $parent_attr       set an array of attributes for the parent element.
  40       *     @type string $button_element    Set this to 'button', 'img', or 'a', defaults to anchor.
  41       *     @type string $button_attr       Any attributes required for the button_element
  42       *     @type string $link_text         The text of the link. Required.
  43       * }
  44       */
  45  	public function __construct( $args = array() ) {
  46          foreach ( $args as $arg ) {
  47              $this->add( $arg );
  48          }
  49      }
  50  
  51  
  52      /**
  53       * Sort the Buttons of the group according to their position attribute
  54       *
  55       * @since 3.0.0
  56       *
  57       * @param array the list of buttons to sort.
  58       *
  59       * @return array the list of buttons sorted.
  60       */
  61  	public function sort( $buttons ) {
  62          $sorted = array();
  63  
  64          foreach ( $buttons as $button ) {
  65              $position = 99;
  66  
  67              if ( isset( $button['position'] ) ) {
  68                  $position = (int) $button['position'];
  69              }
  70  
  71              // If position is already taken, move to the first next available
  72              if ( isset( $sorted[ $position ] ) ) {
  73                  $sorted_keys = array_keys( $sorted );
  74  
  75                  do {
  76                      $position += 1;
  77                  } while ( in_array( $position, $sorted_keys, true ) );
  78              }
  79  
  80              $sorted[ $position ] = $button;
  81          }
  82  
  83          ksort( $sorted );
  84          return $sorted;
  85      }
  86  
  87      /**
  88       * Get the BuddyPress buttons for the group
  89       *
  90       * @since 3.0.0
  91       *
  92       * @param bool $sort whether to sort the buttons or not.
  93       *
  94       * @return array An array of HTML links.
  95       */
  96  	public function get( $sort = true ) {
  97          $buttons = array();
  98  
  99          if ( empty( $this->group ) ) {
 100              return $buttons;
 101          }
 102  
 103          if ( true === $sort ) {
 104              $this->group = $this->sort( $this->group );
 105          }
 106  
 107          foreach ( $this->group as $key_button => $button ) {
 108              // Reindex with ids.
 109              if ( true === $sort ) {
 110                  $this->group[ $button['id'] ] = $button;
 111                  unset( $this->group[ $key_button ] );
 112              }
 113  
 114              $buttons[ $button['id'] ] = bp_get_button( $button );
 115          }
 116  
 117          return $buttons;
 118      }
 119  
 120      /**
 121       * Update the group of buttons
 122       *
 123       * @since 3.0.0
 124       *
 125       * @param array $args Optional. See the __constructor for a description of this argument.
 126       */
 127  	public function update( $args = array() ) {
 128          foreach ( $args as $id => $params ) {
 129              if ( isset( $this->group[ $id ] ) ) {
 130                  $this->group[ $id ] = bp_parse_args(
 131                      $params,
 132                      $this->group[ $id ],
 133                      'buttons_group_update'
 134                  );
 135              } else {
 136                  $this->add( $params );
 137              }
 138          }
 139      }
 140  
 141      /**
 142       * Adds a button.
 143       *
 144       * @since 9.0.0
 145       *
 146       * @param array $args Required. See the __constructor for a description of this argument.
 147       * @return bool true on success, false on failure to add.
 148       */
 149  	private function add( $args ) {
 150          $r = bp_parse_args(
 151              (array) $args,
 152              array(
 153                  'id'                => '',
 154                  'position'          => 99,
 155                  'component'         => '',
 156                  'must_be_logged_in' => true,
 157                  'block_self'        => false,
 158                  'parent_element'    => false,
 159                  'parent_attr'       => array(),
 160                  'button_element'    => 'a',
 161                  'button_attr'       => array(),
 162                  'link_text'         => '',
 163              ),
 164              'buttons_group_constructor'
 165          );
 166  
 167          // Just don't set the button if a param is missing
 168          if ( empty( $r['id'] ) || empty( $r['component'] ) || empty( $r['link_text'] ) ) {
 169              return false;
 170          }
 171  
 172          $r['id'] = sanitize_key( $r['id'] );
 173  
 174          // If the button already exist don't add it
 175          if ( isset( $this->group[ $r['id'] ] ) ) {
 176              return false;
 177          }
 178  
 179          /*
 180           * If, in bp_nouveau_get_*_buttons(), we pass through a false value for 'parent_element'
 181           * but we have attributtes for it in the array, let's default to setting a div.
 182           *
 183           * Otherwise, the original false value will be passed through to BP buttons.
 184           * @todo: this needs review, probably trying to be too clever
 185           */
 186          if ( ( ! empty( $r['parent_attr'] ) ) && false === $r['parent_element'] ) {
 187              $r['parent_element'] = 'div';
 188          }
 189  
 190          $this->group[ $r['id'] ] = $r;
 191          return true;
 192      }
 193  }
 194  
 195  /**
 196   * BP Sidebar Item Nav_Widget
 197   *
 198   * Adds a widget to move avatar/item nav into the sidebar
 199   *
 200   * @since 3.0.0
 201   *
 202   * @uses WP_Widget
 203   */
 204  class BP_Nouveau_Object_Nav_Widget extends WP_Widget {
 205      /**
 206       * Constructor
 207       *
 208       * @since 3.0.0
 209       * @since 9.0.0 Adds the `show_instance_in_rest` property to Widget options.
 210       */
 211  	public function __construct() {
 212          $widget_ops = array(
 213              'description'           => __( 'Displays BuddyPress primary nav in the sidebar of your site. Make sure to use it as the first widget of the sidebar and only once.', 'buddypress' ),
 214              'classname'             => 'widget_nav_menu buddypress_object_nav',
 215              'show_instance_in_rest' => true,
 216          );
 217  
 218          parent::__construct(
 219              'bp_nouveau_sidebar_object_nav_widget',
 220              __( '(BuddyPress) Primary navigation', 'buddypress' ),
 221              $widget_ops
 222          );
 223      }
 224  
 225      /**
 226       * Register the widget
 227       *
 228       * @since 3.0.0
 229       */
 230  	public static function register_widget() {
 231          register_widget( 'BP_Nouveau_Object_Nav_Widget' );
 232      }
 233  
 234      /**
 235       * Displays the output, the button to post new support topics
 236       *
 237       * @since 3.0.0
 238       *
 239       * @param mixed   $args     Arguments
 240       * @param unknown $instance
 241       */
 242  	public function widget( $args, $instance ) {
 243          if ( ! is_buddypress() || bp_is_group_create() ) {
 244              return;
 245          }
 246  
 247          /**
 248           * Filters the nav widget args for the BP_Nouveau_Object_Nav_Widget widget.
 249           *
 250           * @since 3.0.0
 251           *
 252           * @param array $value Array of arguments {
 253           *     @param bool $bp_nouveau_widget_title Whether or not to assign a title for the widget.
 254           * }
 255           */
 256          $item_nav_args = bp_parse_args(
 257              $instance,
 258              apply_filters(
 259                  'bp_nouveau_object_nav_widget_args',
 260                  array( 'bp_nouveau_widget_title' => true )
 261              ),
 262              'widget_object_nav'
 263          );
 264  
 265          $title = '';
 266  
 267          if ( ! empty( $item_nav_args['bp_nouveau_widget_title'] ) ) {
 268              if ( bp_is_group() ) {
 269                  $title = bp_get_current_group_name();
 270              } elseif ( bp_is_user() ) {
 271                  $title = bp_get_displayed_user_fullname();
 272              } elseif ( bp_get_directory_title( bp_current_component() ) ) {
 273                  $title = bp_get_directory_title( bp_current_component() );
 274              }
 275          }
 276  
 277          /**
 278           * Filters the BP_Nouveau_Object_Nav_Widget widget title.
 279           *
 280           * @since 3.0.0
 281           *
 282           * @param string $title    The widget title.
 283           * @param array  $instance The settings for the particular instance of the widget.
 284           * @param string $id_base  Root ID for all widgets of this type.
 285           */
 286          $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
 287  
 288          echo $args['before_widget'];
 289  
 290          if ( ! empty( $title ) ) {
 291              echo $args['before_title'] . $title . $args['after_title'];
 292          }
 293  
 294          if ( bp_is_user() ) {
 295              bp_get_template_part( 'members/single/parts/item-nav' );
 296          } elseif ( bp_is_group() ) {
 297              bp_get_template_part( 'groups/single/parts/item-nav' );
 298          } elseif ( bp_is_directory() ) {
 299              bp_get_template_part( 'common/nav/directory-nav' );
 300          }
 301  
 302          echo $args['after_widget'];
 303      }
 304  
 305      /**
 306       * Update the new support topic widget options (title)
 307       *
 308       * @since 3.0.0
 309       *
 310       * @param array $new_instance The new instance options
 311       * @param array $old_instance The old instance options
 312       *
 313       * @return array the instance
 314       */
 315  	public function update( $new_instance, $old_instance ) {
 316          $instance                            = $old_instance;
 317          $instance['bp_nouveau_widget_title'] = (bool) $new_instance['bp_nouveau_widget_title'];
 318  
 319          return $instance;
 320      }
 321  
 322      /**
 323       * Output the new support topic widget options form
 324       *
 325       * @since 3.0.0
 326       *
 327       * @param $instance Instance
 328       *
 329       * @return string HTML Output
 330       */
 331  	public function form( $instance ) {
 332          $defaults = array(
 333              'bp_nouveau_widget_title' => true,
 334          );
 335  
 336          $instance = bp_parse_args(
 337              (array) $instance,
 338              $defaults,
 339              'widget_object_nav_form'
 340          );
 341  
 342          $bp_nouveau_widget_title = (bool) $instance['bp_nouveau_widget_title'];
 343          ?>
 344  
 345          <p>
 346              <input class="checkbox" type="checkbox" <?php checked( $bp_nouveau_widget_title, true ); ?> id="<?php echo $this->get_field_id( 'bp_nouveau_widget_title' ); ?>" name="<?php echo $this->get_field_name( 'bp_nouveau_widget_title' ); ?>" />
 347              <label for="<?php echo $this->get_field_id( 'bp_nouveau_widget_title' ); ?>"><?php esc_html_e( 'Include navigation title', 'buddypress' ); ?></label>
 348          </p>
 349  
 350          <?php
 351      }
 352  }


Generated: Sun Oct 24 01:00:54 2021 Cross-referenced by PHPXref 0.7.1