[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-groups/ -> bp-groups-blocks.php (source)

   1  <?php
   2  /**
   3   * BP Groups Blocks Functions.
   4   *
   5   * @package BuddyPress
   6   * @subpackage GroupsBlocks
   7   * @since 6.0.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  if ( ! defined( 'ABSPATH' ) ) {
  12      exit;
  13  }
  14  
  15  /**
  16   * Add BP Groups blocks specific settings to the BP Blocks Editor ones.
  17   *
  18   * @since 6.0.0
  19   *
  20   * @param array $bp_editor_settings BP blocks editor settings.
  21   * @return array BP Groups blocks editor settings.
  22   */
  23  function bp_groups_editor_settings( $bp_editor_settings = array() ) {
  24      $bp = buddypress();
  25  
  26      return array_merge(
  27          $bp_editor_settings,
  28          array(
  29              'groups' => array(
  30                  'isAvatarEnabled'     => $bp->avatar && $bp->avatar->show_avatars && ! bp_disable_group_avatar_uploads(),
  31                  'isCoverImageEnabled' => bp_is_active( 'groups', 'cover_image' ),
  32              ),
  33          )
  34      );
  35  }
  36  add_filter( 'bp_blocks_editor_settings', 'bp_groups_editor_settings' );
  37  
  38  /**
  39   * Callback function to render the BP Group Block.
  40   *
  41   * @since 6.0.0
  42   *
  43   * @param array $attributes The block attributes.
  44   * @return string           HTML output.
  45   */
  46  function bp_groups_render_group_block( $attributes = array() ) {
  47      $bp = buddypress();
  48  
  49      $block_args = wp_parse_args(
  50          $attributes,
  51          array(
  52              'itemID'              => 0,
  53              'avatarSize'          => 'full',
  54              'displayDescription'  => true,
  55              'displayActionButton' => true,
  56              'displayCoverImage'   => true,
  57          )
  58      );
  59  
  60      if ( ! $block_args['itemID'] ) {
  61          return;
  62      }
  63  
  64      // Set the group ID and container classes.
  65      $group_id          = (int) $block_args['itemID'];
  66      $container_classes = array( 'bp-block-group' );
  67  
  68      // Group object.
  69      $group = groups_get_group( $group_id );
  70  
  71      if ( ! $group->id ) {
  72          return;
  73      }
  74  
  75      // Avatar variables.
  76      $avatar           = '';
  77      $avatar_container = '';
  78  
  79      // Cover image variable.
  80      $cover_image     = '';
  81      $cover_style     = '';
  82      $cover_container = '';
  83  
  84      // Group name/link/description variables.
  85      $group_name        = bp_get_group_name( $group );
  86      $group_link        = bp_get_group_permalink( $group );
  87      $group_description = '';
  88      $group_content     = '';
  89  
  90      // Group action button.
  91      $action_button         = '';
  92      $display_action_button = (bool) $block_args['displayActionButton'];
  93  
  94      if ( $bp->avatar && $bp->avatar->show_avatars && ! bp_disable_group_avatar_uploads() && in_array( $block_args['avatarSize'], array( 'thumb', 'full' ), true ) ) {
  95          $avatar = bp_core_fetch_avatar(
  96              array(
  97                  'item_id' => $group->id,
  98                  'object'  => 'group',
  99                  'type'    => $block_args['avatarSize'],
 100                  'html'    => false,
 101              )
 102          );
 103  
 104          $container_classes[] = 'avatar-' . $block_args['avatarSize'];
 105      } else {
 106          $container_classes[] = 'avatar-none';
 107      }
 108  
 109      if ( $avatar ) {
 110          $avatar_container = sprintf(
 111              '<div class="item-header-avatar">
 112                  <a href="%1$s">
 113                      <img src="%2$s" alt="%3$s" class="avatar">
 114                  </a>
 115              </div>',
 116              esc_url( $group_link ),
 117              esc_url( $avatar ),
 118              // Translators: %s is the group's name.
 119              sprintf( esc_html__( 'Group Profile photo of %s', 'buddypress' ), $group_name )
 120          );
 121      }
 122  
 123      $display_cover_image = (bool) $block_args['displayCoverImage'];
 124      if ( bp_is_active( 'groups', 'cover_image' ) && $display_cover_image ) {
 125          $cover_image = bp_attachments_get_attachment(
 126              'url',
 127              array(
 128                  'item_id'    => $group->id,
 129                  'object_dir' => 'groups',
 130              )
 131          );
 132  
 133          if ( $cover_image ) {
 134              $cover_style = sprintf(
 135                  ' style="background-image: url( %s );"',
 136                  esc_url( $cover_image )
 137              );
 138          }
 139  
 140          $cover_container = sprintf(
 141              '<div class="bp-group-cover-image"%s></div>',
 142              $cover_style
 143          );
 144  
 145          $container_classes[] = 'has-cover';
 146      }
 147  
 148      $display_description = (bool) $block_args['displayDescription'];
 149      if ( $display_description ) {
 150          $group_description = bp_get_group_description( $group );
 151          $group_content     = sprintf(
 152              '<div class="group-description-content">%s</div>',
 153              $group_description
 154          );
 155  
 156          $container_classes[] = 'has-description';
 157      }
 158  
 159      if ( $display_action_button ) {
 160          $action_button = sprintf(
 161              '<div class="bp-profile-button">
 162                  <a href="%1$s" class="button large primary button-primary" role="button">%2$s</a>
 163              </div>',
 164              esc_url( $group_link ),
 165              esc_html__( 'Visit Group', 'buddypress' )
 166          );
 167      }
 168  
 169      $output = sprintf(
 170          '<div class="%1$s">
 171              %2$s
 172              <div class="group-content">
 173                  %3$s
 174                  <div class="group-description">
 175                      <strong><a href="%4$s">%5$s</a></strong>
 176                      %6$s
 177                      %7$s
 178                  </div>
 179              </div>
 180          </div>',
 181          implode( ' ', array_map( 'sanitize_html_class', $container_classes ) ),
 182          $cover_container,
 183          $avatar_container,
 184          esc_url( $group_link ),
 185          esc_html( $group_name ),
 186          $group_content,
 187          $action_button
 188      );
 189  
 190      // Compact all interesting parameters.
 191      $params = array_merge( $block_args, compact( 'group_name', 'group_link', 'group_description', 'avatar', 'cover_image' ) );
 192  
 193      /**
 194       * Filter here to edit the output of the single group block.
 195       *
 196       * @since 6.0.0
 197       *
 198       * @param string          $output The HTML output of the block.
 199       * @param BP_Groups_Group $group  The group object.
 200       * @param array           $params The block extended parameters.
 201       */
 202      return apply_filters( 'bp_groups_render_group_block_output', $output, $group, $params );
 203  }


Generated: Sat Sep 26 01:01:31 2020 Cross-referenced by PHPXref 0.7.1