[ 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 3.1.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              $r = bp_parse_args(
  48                  (array) $arg,
  49                  array(
  50                      'id'                => '',
  51                      'position'          => 99,
  52                      'component'         => '',
  53                      'must_be_logged_in' => true,
  54                      'block_self'        => false,
  55                      'parent_element'    => false,
  56                      'parent_attr'       => array(),
  57                      'button_element'    => 'a',
  58                      'button_attr'       => array(),
  59                      'link_text'         => '',
  60                  ),
  61                  'buttons_group_constructor'
  62              );
  63  
  64              // Just don't set the button if a param is missing
  65              if ( empty( $r['id'] ) || empty( $r['component'] ) || empty( $r['link_text'] ) ) {
  66                  continue;
  67              }
  68  
  69              $r['id'] = sanitize_key( $r['id'] );
  70  
  71              // If the button already exist don't add it
  72              if ( isset( $this->group[ $r['id'] ] ) ) {
  73                  continue;
  74              }
  75  
  76              /*
  77               * If, in bp_nouveau_get_*_buttons(), we pass through a false value for 'parent_element'
  78               * but we have attributtes for it in the array, let's default to setting a div.
  79               *
  80               * Otherwise, the original false value will be passed through to BP buttons.
  81               * @todo: this needs review, probably trying to be too clever
  82               */
  83              if ( ( ! empty( $r['parent_attr'] ) ) && false === $r['parent_element'] ) {
  84                  $r['parent_element'] = 'div';
  85              }
  86  
  87              $this->group[ $r['id'] ] = $r;
  88          }
  89      }
  90  
  91  
  92      /**
  93       * Sort the Buttons of the group according to their position attribute
  94       *
  95       * @since 3.0.0
  96       *
  97       * @param array the list of buttons to sort.
  98       *
  99       * @return array the list of buttons sorted.
 100       */
 101  	public function sort( $buttons ) {
 102          $sorted = array();
 103  
 104          foreach ( $buttons as $button ) {
 105              $position = 99;
 106  
 107              if ( isset( $button['position'] ) ) {
 108                  $position = (int) $button['position'];
 109              }
 110  
 111              // If position is already taken, move to the first next available
 112              if ( isset( $sorted[ $position ] ) ) {
 113                  $sorted_keys = array_keys( $sorted );
 114  
 115                  do {
 116                      $position += 1;
 117                  } while ( in_array( $position, $sorted_keys, true ) );
 118              }
 119  
 120              $sorted[ $position ] = $button;
 121          }
 122  
 123          ksort( $sorted );
 124          return $sorted;
 125      }
 126  
 127      /**
 128       * Get the BuddyPress buttons for the group
 129       *
 130       * @since 3.0.0
 131       *
 132       * @param bool $sort whether to sort the buttons or not.
 133       *
 134       * @return array An array of HTML links.
 135       */
 136  	public function get( $sort = true ) {
 137          $buttons = array();
 138  
 139          if ( empty( $this->group ) ) {
 140              return $buttons;
 141          }
 142  
 143          if ( true === $sort ) {
 144              $this->group = $this->sort( $this->group );
 145          }
 146  
 147          foreach ( $this->group as $key_button => $button ) {
 148              // Reindex with ids.
 149              if ( true === $sort ) {
 150                  $this->group[ $button['id'] ] = $button;
 151                  unset( $this->group[ $key_button ] );
 152              }
 153  
 154              $buttons[ $button['id'] ] = bp_get_button( $button );
 155          }
 156  
 157          return $buttons;
 158      }
 159  
 160      /**
 161       * Update the group of buttons
 162       *
 163       * @since 3.0.0
 164       *
 165       * @param array $args Optional. See the __constructor for a description of this argument.
 166       */
 167  	public function update( $args = array() ) {
 168          foreach ( $args as $id => $params ) {
 169              if ( isset( $this->group[ $id ] ) ) {
 170                  $this->group[ $id ] = bp_parse_args(
 171                      $params,
 172                      $this->group[ $id ],
 173                      'buttons_group_update'
 174                  );
 175              }
 176          }
 177      }
 178  }
 179  
 180  /**
 181   * BP Sidebar Item Nav_Widget
 182   *
 183   * Adds a widget to move avatar/item nav into the sidebar
 184   *
 185   * @since 3.0.0
 186   *
 187   * @uses WP_Widget
 188   */
 189  class BP_Nouveau_Object_Nav_Widget extends WP_Widget {
 190      /**
 191       * Constructor
 192       *
 193       * @since 3.0.0
 194       */
 195  	public function __construct() {
 196          $widget_ops = array(
 197              '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' ),
 198              'classname'   => 'widget_nav_menu buddypress_object_nav',
 199          );
 200  
 201          parent::__construct(
 202              'bp_nouveau_sidebar_object_nav_widget',
 203              __( '(BuddyPress) Primary navigation', 'buddypress' ),
 204              $widget_ops
 205          );
 206      }
 207  
 208      /**
 209       * Register the widget
 210       *
 211       * @since 3.0.0
 212       */
 213  	public static function register_widget() {
 214          register_widget( 'BP_Nouveau_Object_Nav_Widget' );
 215      }
 216  
 217      /**
 218       * Displays the output, the button to post new support topics
 219       *
 220       * @since 3.0.0
 221       *
 222       * @param mixed   $args     Arguments
 223       * @param unknown $instance
 224       */
 225  	public function widget( $args, $instance ) {
 226          if ( ! is_buddypress() || bp_is_group_create() ) {
 227              return;
 228          }
 229  
 230          /**
 231           * Filters the nav widget args for the BP_Nouveau_Object_Nav_Widget widget.
 232           *
 233           * @since 3.0.0
 234           *
 235           * @param array $value Array of arguments {
 236           *     @param bool $bp_nouveau_widget_title Whether or not to assign a title for the widget.
 237           * }
 238           */
 239          $item_nav_args = bp_parse_args(
 240              $instance,
 241              apply_filters(
 242                  'bp_nouveau_object_nav_widget_args',
 243                  array( 'bp_nouveau_widget_title' => true )
 244              ),
 245              'widget_object_nav'
 246          );
 247  
 248          $title = '';
 249  
 250          if ( ! empty( $item_nav_args['bp_nouveau_widget_title'] ) ) {
 251              if ( bp_is_group() ) {
 252                  $title = bp_get_current_group_name();
 253              } elseif ( bp_is_user() ) {
 254                  $title = bp_get_displayed_user_fullname();
 255              } elseif ( bp_get_directory_title( bp_current_component() ) ) {
 256                  $title = bp_get_directory_title( bp_current_component() );
 257              }
 258          }
 259  
 260          /**
 261           * Filters the BP_Nouveau_Object_Nav_Widget widget title.
 262           *
 263           * @since 3.0.0
 264           *
 265           * @param string $title    The widget title.
 266           * @param array  $instance The settings for the particular instance of the widget.
 267           * @param string $id_base  Root ID for all widgets of this type.
 268           */
 269          $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
 270  
 271          echo $args['before_widget'];
 272  
 273          if ( ! empty( $title ) ) {
 274              echo $args['before_title'] . $title . $args['after_title'];
 275          }
 276  
 277          if ( bp_is_user() ) {
 278              bp_get_template_part( 'members/single/parts/item-nav' );
 279          } elseif ( bp_is_group() ) {
 280              bp_get_template_part( 'groups/single/parts/item-nav' );
 281          } elseif ( bp_is_directory() ) {
 282              bp_get_template_part( 'common/nav/directory-nav' );
 283          }
 284  
 285          echo $args['after_widget'];
 286      }
 287  
 288      /**
 289       * Update the new support topic widget options (title)
 290       *
 291       * @since 3.0.0
 292       *
 293       * @param array $new_instance The new instance options
 294       * @param array $old_instance The old instance options
 295       *
 296       * @return array the instance
 297       */
 298  	public function update( $new_instance, $old_instance ) {
 299          $instance                            = $old_instance;
 300          $instance['bp_nouveau_widget_title'] = (bool) $new_instance['bp_nouveau_widget_title'];
 301  
 302          return $instance;
 303      }
 304  
 305      /**
 306       * Output the new support topic widget options form
 307       *
 308       * @since 3.0.0
 309       *
 310       * @param $instance Instance
 311       *
 312       * @return string HTML Output
 313       */
 314  	public function form( $instance ) {
 315          $defaults = array(
 316              'bp_nouveau_widget_title' => true,
 317          );
 318  
 319          $instance = bp_parse_args(
 320              (array) $instance,
 321              $defaults,
 322              'widget_object_nav_form'
 323          );
 324  
 325          $bp_nouveau_widget_title = (bool) $instance['bp_nouveau_widget_title'];
 326          ?>
 327  
 328          <p>
 329              <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' ); ?>" />
 330              <label for="<?php echo $this->get_field_id( 'bp_nouveau_widget_title' ); ?>"><?php esc_html_e( 'Include navigation title', 'buddypress' ); ?></label>
 331          </p>
 332  
 333          <?php
 334      }
 335  }


Generated: Fri Nov 15 01:01:37 2019 Cross-referenced by PHPXref 0.7.1