[ 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   * Callback function to render the BP Group Block.
  17   *
  18   * @since 6.0.0
  19   *
  20   * @param array $attributes The block attributes.
  21   * @return string           HTML output.
  22   */
  23  function bp_groups_render_group_block( $attributes = array() ) {
  24      $bp = buddypress();
  25  
  26      $block_args = bp_parse_args(
  27          $attributes,
  28          array(
  29              'itemID'              => 0,
  30              'avatarSize'          => 'full',
  31              'displayDescription'  => true,
  32              'displayActionButton' => true,
  33              'displayCoverImage'   => true,
  34          )
  35      );
  36  
  37      if ( ! $block_args['itemID'] ) {
  38          return;
  39      }
  40  
  41      // Set the group ID and container classes.
  42      $group_id          = (int) $block_args['itemID'];
  43      $container_classes = array( 'bp-block-group' );
  44  
  45      // Group object.
  46      $group = groups_get_group( $group_id );
  47  
  48      if ( ! $group->id ) {
  49          return;
  50      }
  51  
  52      // Avatar variables.
  53      $avatar           = '';
  54      $avatar_container = '';
  55  
  56      // Cover image variable.
  57      $cover_image     = '';
  58      $cover_style     = '';
  59      $cover_container = '';
  60  
  61      // Group name/link/description variables.
  62      $group_name        = bp_get_group_name( $group );
  63      $group_link        = bp_get_group_permalink( $group );
  64      $group_description = '';
  65      $group_content     = '';
  66  
  67      // Group action button.
  68      $action_button         = '';
  69      $display_action_button = (bool) $block_args['displayActionButton'];
  70  
  71      if ( $bp->avatar && $bp->avatar->show_avatars && ! bp_disable_group_avatar_uploads() && in_array( $block_args['avatarSize'], array( 'thumb', 'full' ), true ) ) {
  72          $avatar = bp_core_fetch_avatar(
  73              array(
  74                  'item_id' => $group->id,
  75                  'object'  => 'group',
  76                  'type'    => $block_args['avatarSize'],
  77                  'html'    => false,
  78              )
  79          );
  80  
  81          $container_classes[] = 'avatar-' . $block_args['avatarSize'];
  82      } else {
  83          $container_classes[] = 'avatar-none';
  84      }
  85  
  86      if ( $avatar ) {
  87          $avatar_container = sprintf(
  88              '<div class="item-header-avatar">
  89                  <a href="%1$s">
  90                      <img loading="lazy" src="%2$s" alt="%3$s" class="avatar">
  91                  </a>
  92              </div>',
  93              esc_url( $group_link ),
  94              esc_url( $avatar ),
  95              /* Translators: %s is the group's name. */
  96              sprintf( esc_attr__( 'Group Profile photo of %s', 'buddypress' ), esc_html( $group_name ) )
  97          );
  98      }
  99  
 100      $display_cover_image = (bool) $block_args['displayCoverImage'];
 101      if ( bp_is_active( 'groups', 'cover_image' ) && $display_cover_image ) {
 102          $cover_image = bp_attachments_get_attachment(
 103              'url',
 104              array(
 105                  'item_id'    => $group->id,
 106                  'object_dir' => 'groups',
 107              )
 108          );
 109  
 110          if ( $cover_image ) {
 111              $cover_style = sprintf(
 112                  ' style="background-image: url( %s );"',
 113                  esc_url( $cover_image )
 114              );
 115          }
 116  
 117          $cover_container = sprintf(
 118              '<div class="bp-group-cover-image"%s></div>',
 119              $cover_style
 120          );
 121  
 122          $container_classes[] = 'has-cover';
 123      }
 124  
 125      $display_description = (bool) $block_args['displayDescription'];
 126      if ( $display_description ) {
 127          $group_description = bp_get_group_description( $group );
 128          $group_content     = sprintf(
 129              '<div class="group-description-content">%s</div>',
 130              $group_description
 131          );
 132  
 133          $container_classes[] = 'has-description';
 134      }
 135  
 136      if ( $display_action_button ) {
 137          $action_button = sprintf(
 138              '<div class="bp-profile-button">
 139                  <a href="%1$s" class="button large primary button-primary" role="button">%2$s</a>
 140              </div>',
 141              esc_url( $group_link ),
 142              esc_html__( 'Visit Group', 'buddypress' )
 143          );
 144      }
 145  
 146      $output = sprintf(
 147          '<div class="%1$s">
 148              %2$s
 149              <div class="group-content">
 150                  %3$s
 151                  <div class="group-description">
 152                      <strong><a href="%4$s">%5$s</a></strong>
 153                      %6$s
 154                      %7$s
 155                  </div>
 156              </div>
 157          </div>',
 158          implode( ' ', array_map( 'sanitize_html_class', $container_classes ) ),
 159          $cover_container,
 160          $avatar_container,
 161          esc_url( $group_link ),
 162          esc_html( $group_name ),
 163          $group_content,
 164          $action_button
 165      );
 166  
 167      // Compact all interesting parameters.
 168      $params = array_merge( $block_args, compact( 'group_name', 'group_link', 'group_description', 'avatar', 'cover_image' ) );
 169  
 170      /**
 171       * Filter here to edit the output of the single group block.
 172       *
 173       * @since 6.0.0
 174       *
 175       * @param string          $output The HTML output of the block.
 176       * @param BP_Groups_Group $group  The group object.
 177       * @param array           $params The block extended parameters.
 178       */
 179      return apply_filters( 'bp_groups_render_group_block_output', $output, $group, $params );
 180  }
 181  
 182  /**
 183   * Callback function to render the BP Groups Block.
 184   *
 185   * @since 7.0.0
 186   *
 187   * @param array $attributes The block attributes.
 188   * @return string           HTML output.
 189   */
 190  function bp_groups_render_groups_block( $attributes = array() ) {
 191      $bp = buddypress();
 192  
 193      $block_args = bp_parse_args(
 194          $attributes,
 195          array(
 196              'itemIDs'          => array(),
 197              'avatarSize'       => 'full',
 198              'displayGroupName' => true,
 199              'extraInfo'        => 'none',
 200              'layoutPreference' => 'list',
 201              'columns'          => '2',
 202          )
 203      );
 204  
 205      $group_ids = wp_parse_id_list( $block_args['itemIDs'] );
 206      if ( ! array_filter( $group_ids ) ) {
 207          return '';
 208      }
 209  
 210      $container_classes = sprintf( 'bp-block-groups avatar-%s', $block_args['avatarSize'] );
 211      if ( 'grid' === $block_args['layoutPreference'] ) {
 212          $container_classes .= sprintf( ' is-grid columns-%d', (int) $block_args['columns'] );
 213      }
 214  
 215      $query = groups_get_groups(
 216          array(
 217              'include' => $group_ids,
 218          )
 219      );
 220  
 221      // Initialize the output and the groups.
 222      $output = '';
 223      $groups = $query['groups'];
 224  
 225      foreach ( $groups as $group ) {
 226          $has_description    = false;
 227          $group_item_classes = 'group-content';
 228  
 229          if ( 'list' === $block_args['layoutPreference'] && 'description' === $block_args['extraInfo'] && isset( $group->description ) && $group->description ) {
 230              $has_description    = true;
 231              $group_item_classes = 'group-content has-description';
 232          }
 233  
 234          $output .= sprintf( '<div class="%s">', $group_item_classes );
 235  
 236          // Get Member link.
 237          $group_link = bp_get_group_permalink( $group );
 238  
 239          // Set the Avatar output.
 240          if ( $bp->avatar && $bp->avatar->show_avatars && ! bp_disable_group_avatar_uploads() && 'none' !== $block_args['avatarSize'] ) {
 241              $output .= sprintf(
 242                  '<div class="item-header-avatar">
 243                      <a href="%1$s">
 244                          <img loading="lazy" src="%2$s" alt="%3$s" class="avatar">
 245                      </a>
 246                  </div>',
 247                  esc_url( $group_link ),
 248                  esc_url(
 249                      bp_core_fetch_avatar(
 250                          array(
 251                              'item_id' => $group->id,
 252                              'object'  => 'group',
 253                              'type'    => $block_args['avatarSize'],
 254                              'html'    => false,
 255                          )
 256                      )
 257                  ),
 258                  /* Translators: %s is the group's name. */
 259                  sprintf( esc_attr__( 'Group Profile photo of %s', 'buddypress' ), esc_html( $group->name ) )
 260              );
 261          }
 262  
 263          $output .= '<div class="group-description">';
 264  
 265          if ( $block_args['displayGroupName'] ) {
 266              $output .= sprintf(
 267                  '<strong><a href="%1$s">%2$s</a></strong>',
 268                  esc_url( $group_link ),
 269                  esc_html( $group->name )
 270              );
 271          }
 272  
 273          // Add the latest activity the group posted.
 274          if ( $has_description && $group->description ) {
 275              $output .= sprintf(
 276                  '<div class="group-description-content">%s</div>',
 277                  bp_get_group_description( $group )
 278              );
 279          } elseif ( 'active' === $block_args['extraInfo'] ) {
 280              $output .= sprintf(
 281                  '<time datetime="%1$s">%2$s</time>',
 282                  esc_attr( bp_core_get_iso8601_date( $group->last_activity ) ),
 283                  /* translators: %s: last activity timestamp (e.g. "Active 1 hour ago") */
 284                  sprintf( esc_html__( 'Active %s', 'buddypress' ), bp_get_group_last_active( $group ) )
 285              );
 286          } elseif ( 'popular' === $block_args['extraInfo'] ) {
 287              $total_member_count = $group->total_member_count;
 288  
 289              $output .= sprintf(
 290                  '<div class="group-meta">%s</div>',
 291                  /* translators: %d: the number of group members. */
 292                  esc_html( sprintf( _n( '%d member', '%d members', $total_member_count, 'buddypress' ), $total_member_count ) )
 293              );
 294          }
 295  
 296          $output .= '</div></div>';
 297      }
 298  
 299      // Set the final output.
 300      $output = sprintf( '<div class="%1$s">%2$s</div>', $container_classes, $output );
 301  
 302      /**
 303       * Filter here to edit the output of the groups block.
 304       *
 305       * @since 7.0.0
 306       *
 307       * @param string $output     The HTML output of the block.
 308       * @param array  $block_args The block arguments.
 309       * @param array  $groups     The list of BP_Groups_Group objects.
 310       */
 311      return apply_filters( 'bp_groups_render_groups_block_output', $output, $block_args, $groups );
 312  }
 313  
 314  /**
 315   * Adds specific script data for the BP Groups blocks.
 316   *
 317   * Only used for the BP Dynamic Groups block.
 318   *
 319   * @since 9.0.0
 320   */
 321  function bp_groups_blocks_add_script_data() {
 322      $dynamic_groups_blocks = array_filter( buddypress()->groups->block_globals['bp/dynamic-groups']->items );
 323  
 324      if ( ! $dynamic_groups_blocks ) {
 325          return;
 326      }
 327  
 328      // Include the common JS template.
 329      echo bp_get_dynamic_template_part( 'assets/widgets/dynamic-groups.php' );
 330  
 331      // List the block specific props.
 332      wp_add_inline_script(
 333          'bp-dynamic-groups-script',
 334          sprintf( 'var bpDynamicGroupsBlocks = %s;', wp_json_encode( array_values( $dynamic_groups_blocks ) ) ),
 335          'before'
 336      );
 337  }
 338  
 339  /**
 340   * Callback function to render the Dynamic Groups Block.
 341   *
 342   * @since 9.0.0
 343   *
 344   * @param array $attributes The block attributes.
 345   * @return string           HTML output.
 346   */
 347  function bp_groups_render_dynamic_groups_block( $attributes = array() ) {
 348      $block_args = bp_parse_args(
 349          $attributes,
 350          array(
 351              'title'        => __( 'Groups', 'buddypress' ),
 352              'maxGroups'    => 5,
 353              'groupDefault' => 'active',
 354              'linkTitle'    => false,
 355          )
 356      );
 357  
 358      $classnames         = 'widget_bp_groups_widget buddypress widget';
 359      $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) );
 360  
 361      $max_groups = (int) $block_args['maxGroups'];
 362      $no_groups  = __( 'There are no groups to display.', 'buddypress' );
 363  
 364      /** This filter is documented in buddypress/src/bp-groups/classes/class-bp-groups-widget.php */
 365      $separator = apply_filters( 'bp_groups_widget_separator', '|' );
 366  
 367      // Make sure the widget ID is unique.
 368      $widget_id             = uniqid( 'groups-list-' );
 369      $groups_directory_link = bp_get_groups_directory_permalink();
 370  
 371      // Set the Block's title.
 372      if ( true === $block_args['linkTitle'] ) {
 373          $widget_content = sprintf(
 374              '<h2 class="widget-title"><a href="%1$s">%2$s</a></h2>',
 375              esc_url( $groups_directory_link ),
 376              esc_html( $block_args['title'] )
 377          );
 378      } else {
 379          $widget_content = sprintf( '<h2 class="widget-title">%s</h2>', esc_html( $block_args['title'] ) );
 380      }
 381  
 382      $item_options = array(
 383          'newest'       => array(
 384              'class' => '',
 385              'label' => __( 'Newest', 'buddypress' ),
 386          ),
 387          'active'       => array(
 388              'class' => '',
 389              'label' => __( 'Active', 'buddypress' ),
 390          ),
 391          'popular'      => array(
 392              'class' => '',
 393              'label' => __( 'Popular', 'buddypress' ),
 394          ),
 395          'alphabetical' => array(
 396              'class' => '',
 397              'label' => __( 'Alphabetical', 'buddypress' ),
 398          ),
 399      );
 400  
 401      $item_options_output = array();
 402      $separator_output    = sprintf( ' <span class="bp-separator" role="separator">%s</span> ', esc_html( $separator ) );
 403  
 404      foreach ( $item_options as $item_type => $item_attr ) {
 405          if ( $block_args['groupDefault'] === $item_type ) {
 406              $item_attr['class'] = ' class="selected"';
 407          }
 408  
 409          $item_options_output[] = sprintf(
 410              '<a href="%1$s" data-bp-sort="%2$s"%3$s>%4$s</a>',
 411              esc_url( $groups_directory_link ),
 412              esc_attr( $item_type ),
 413              $item_attr['class'],
 414              esc_html( $item_attr['label'] )
 415          );
 416      }
 417  
 418      $preview      = '';
 419      $default_args = array(
 420          'type'            => $block_args['groupDefault'],
 421          'per_page'        => $max_groups,
 422          'populate_extras' => true,
 423      );
 424  
 425      // Previewing the Block inside the editor.
 426      if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
 427          $bp_query = groups_get_groups( $default_args );
 428          $preview  = sprintf( '<div class="widget-error">%s</div>', $no_groups );
 429  
 430          if ( is_array( $bp_query['groups'] ) && 0 < count( $bp_query['groups'] ) ) {
 431              $preview = '';
 432              foreach ( $bp_query['groups'] as $group ) {
 433                  if ( 'newest' === $block_args['groupDefault'] ) {
 434                      /* translators: %s is time elapsed since the group was created */
 435                      $extra = sprintf( __( 'Created %s', 'buddypress' ), bp_get_group_date_created( $group ) );
 436                  } elseif ( 'popular' === $block_args['groupDefault'] ) {
 437                      $count = (int) $group->total_member_count;
 438  
 439                      /* translators: %s is the number of Group members */
 440                      $extra = sprintf( _n( '%s member', '%s members', $count, 'buddypress' ), bp_core_number_format( $count ) );
 441                  } else {
 442                      /* translators: %s: a human time diff. */
 443                      $extra = sprintf( __( 'Active %s', 'buddypress' ), bp_get_group_last_active( $group ) );
 444                  }
 445  
 446                  $preview .= bp_get_dynamic_template_part(
 447                      'assets/widgets/dynamic-groups.php',
 448                      'php',
 449                      array(
 450                          'data.link'              => bp_get_group_permalink( $group ),
 451                          'data.name'              => bp_get_group_name( $group ),
 452                          'data.avatar_urls.thumb' => bp_core_fetch_avatar(
 453                              array(
 454                                  'item_id' => $group->id,
 455                                  'html'    => false,
 456                                  'object'  => 'group',
 457                              )
 458                          ),
 459                          'data.avatar_alt'        => esc_attr(
 460                              sprintf(
 461                                  /* Translators: %s is the group's name. */
 462                                  __( 'Group Profile photo of %s', 'buddypress' ),
 463                                  $group->name
 464                              )
 465                          ),
 466                          'data.id'                => $group->id,
 467                          'data.extra'             => $extra,
 468                      )
 469                  );
 470              }
 471          }
 472      } else {
 473          // Get corresponding members.
 474          $path = sprintf(
 475              '/%1$s/%2$s/%3$s',
 476              bp_rest_namespace(),
 477              bp_rest_version(),
 478              buddypress()->groups->id
 479          );
 480  
 481          $default_path = add_query_arg(
 482              $default_args,
 483              $path
 484          );
 485  
 486          $preloaded_groups = array();
 487          if ( bp_is_running_wp( '5.0.0' ) ) {
 488              $preloaded_groups = rest_preload_api_request( '', $default_path );
 489          }
 490  
 491          buddypress()->groups->block_globals['bp/dynamic-groups']->items[ $widget_id ] = (object) array(
 492              'selector'   => $widget_id,
 493              'query_args' => $default_args,
 494              'preloaded'  => reset( $preloaded_groups ),
 495          );
 496  
 497          // Only enqueue common/specific scripts and data once per page load.
 498          if ( ! has_action( 'wp_footer', 'bp_groups_blocks_add_script_data', 1 ) ) {
 499              wp_set_script_translations( 'bp-dynamic-groups-script', 'buddypress' );
 500              wp_enqueue_script( 'bp-dynamic-groups-script' );
 501              wp_localize_script(
 502                  'bp-dynamic-groups-script',
 503                  'bpDynamicGroupsSettings',
 504                  array(
 505                      'path'  => ltrim( $path, '/' ),
 506                      'root'  => esc_url_raw( get_rest_url() ),
 507                      'nonce' => wp_create_nonce( 'wp_rest' ),
 508                  )
 509              );
 510  
 511              add_action( 'wp_footer', 'bp_groups_blocks_add_script_data', 1 );
 512          }
 513      }
 514  
 515      $widget_content .= sprintf(
 516          '<div class="item-options">
 517              %1$s
 518          </div>
 519          <ul id="%2$s" class="item-list" aria-live="polite" aria-relevant="all" aria-atomic="true">
 520              %3$s
 521          </ul>',
 522          implode( $separator_output, $item_options_output ),
 523          esc_attr( $widget_id ),
 524          $preview
 525      );
 526  
 527      // Adds a container to make sure the block is styled even when used into the Columns parent block.
 528      $widget_content = sprintf( '<div class="bp-dynamic-block-container">%s</div>', "\n" . $widget_content . "\n" );
 529  
 530      // Only add a block wrapper if not loaded into a Widgets sidebar.
 531      if ( ! did_action( 'dynamic_sidebar_before' ) ) {
 532          return sprintf(
 533              '<div %1$s>%2$s</div>',
 534              $wrapper_attributes,
 535              $widget_content
 536          );
 537      }
 538  
 539      return $widget_content;
 540  }


Generated: Sat Sep 18 01:01:37 2021 Cross-referenced by PHPXref 0.7.1