[ 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      $path = sprintf(
 329          '/%1$s/%2$s/%3$s',
 330          bp_rest_namespace(),
 331          bp_rest_version(),
 332          buddypress()->groups->id
 333      );
 334  
 335      wp_localize_script(
 336          'bp-dynamic-groups-script',
 337          'bpDynamicGroupsSettings',
 338          array(
 339              'path'  => ltrim( $path, '/' ),
 340              'root'  => esc_url_raw( get_rest_url() ),
 341              'nonce' => wp_create_nonce( 'wp_rest' ),
 342          )
 343      );
 344  
 345      // Include the common JS template.
 346      echo bp_get_dynamic_template_part( 'assets/widgets/dynamic-groups.php' );
 347  
 348      // List the block specific props.
 349      wp_add_inline_script(
 350          'bp-dynamic-groups-script',
 351          sprintf( 'var bpDynamicGroupsBlocks = %s;', wp_json_encode( array_values( $dynamic_groups_blocks ) ) ),
 352          'before'
 353      );
 354  }
 355  
 356  /**
 357   * Callback function to render the Dynamic Groups Block.
 358   *
 359   * @since 9.0.0
 360   *
 361   * @param array $attributes The block attributes.
 362   * @return string           HTML output.
 363   */
 364  function bp_groups_render_dynamic_groups_block( $attributes = array() ) {
 365      $block_args = bp_parse_args(
 366          $attributes,
 367          array(
 368              'title'        => __( 'Groups', 'buddypress' ),
 369              'maxGroups'    => 5,
 370              'groupDefault' => 'active',
 371              'linkTitle'    => false,
 372          )
 373      );
 374  
 375      $classnames         = 'widget_bp_groups_widget buddypress widget';
 376      $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) );
 377  
 378      $max_groups = (int) $block_args['maxGroups'];
 379      $no_groups  = __( 'There are no groups to display.', 'buddypress' );
 380  
 381      /** This filter is documented in buddypress/src/bp-groups/classes/class-bp-groups-widget.php */
 382      $separator = apply_filters( 'bp_groups_widget_separator', '|' );
 383  
 384      // Make sure the widget ID is unique.
 385      $widget_id             = uniqid( 'groups-list-' );
 386      $groups_directory_link = bp_get_groups_directory_permalink();
 387  
 388      // Set the Block's title.
 389      if ( true === $block_args['linkTitle'] ) {
 390          $widget_content = sprintf(
 391              '<h2 class="widget-title"><a href="%1$s">%2$s</a></h2>',
 392              esc_url( $groups_directory_link ),
 393              esc_html( $block_args['title'] )
 394          );
 395      } else {
 396          $widget_content = sprintf( '<h2 class="widget-title">%s</h2>', esc_html( $block_args['title'] ) );
 397      }
 398  
 399      $item_options = array(
 400          'newest'       => array(
 401              'class' => '',
 402              'label' => __( 'Newest', 'buddypress' ),
 403          ),
 404          'active'       => array(
 405              'class' => '',
 406              'label' => __( 'Active', 'buddypress' ),
 407          ),
 408          'popular'      => array(
 409              'class' => '',
 410              'label' => __( 'Popular', 'buddypress' ),
 411          ),
 412          'alphabetical' => array(
 413              'class' => '',
 414              'label' => __( 'Alphabetical', 'buddypress' ),
 415          ),
 416      );
 417  
 418      $item_options_output = array();
 419      $separator_output    = sprintf( ' <span class="bp-separator" role="separator">%s</span> ', esc_html( $separator ) );
 420  
 421      foreach ( $item_options as $item_type => $item_attr ) {
 422          if ( $block_args['groupDefault'] === $item_type ) {
 423              $item_attr['class'] = ' class="selected"';
 424          }
 425  
 426          $item_options_output[] = sprintf(
 427              '<a href="%1$s" data-bp-sort="%2$s"%3$s>%4$s</a>',
 428              esc_url( $groups_directory_link ),
 429              esc_attr( $item_type ),
 430              $item_attr['class'],
 431              esc_html( $item_attr['label'] )
 432          );
 433      }
 434  
 435      $preview      = '';
 436      $default_args = array(
 437          'type'            => $block_args['groupDefault'],
 438          'per_page'        => $max_groups,
 439          'populate_extras' => true,
 440      );
 441  
 442      // Previewing the Block inside the editor.
 443      if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
 444          $bp_query = groups_get_groups( $default_args );
 445          $preview  = sprintf( '<div class="widget-error">%s</div>', $no_groups );
 446  
 447          if ( is_array( $bp_query['groups'] ) && 0 < count( $bp_query['groups'] ) ) {
 448              $preview = '';
 449              foreach ( $bp_query['groups'] as $group ) {
 450                  if ( 'newest' === $block_args['groupDefault'] ) {
 451                      /* translators: %s is time elapsed since the group was created */
 452                      $extra = sprintf( __( 'Created %s', 'buddypress' ), bp_get_group_date_created( $group ) );
 453                  } elseif ( 'popular' === $block_args['groupDefault'] ) {
 454                      $count = (int) $group->total_member_count;
 455  
 456                      /* translators: %s is the number of Group members */
 457                      $extra = sprintf( _n( '%s member', '%s members', $count, 'buddypress' ), bp_core_number_format( $count ) );
 458                  } else {
 459                      /* translators: %s: last activity timestamp (e.g. "Active 1 hour ago") */
 460                      $extra = sprintf( __( 'Active %s', 'buddypress' ), bp_get_group_last_active( $group ) );
 461                  }
 462  
 463                  $preview .= bp_get_dynamic_template_part(
 464                      'assets/widgets/dynamic-groups.php',
 465                      'php',
 466                      array(
 467                          'data.link'              => bp_get_group_permalink( $group ),
 468                          'data.name'              => bp_get_group_name( $group ),
 469                          'data.avatar_urls.thumb' => bp_core_fetch_avatar(
 470                              array(
 471                                  'item_id' => $group->id,
 472                                  'html'    => false,
 473                                  'object'  => 'group',
 474                              )
 475                          ),
 476                          'data.avatar_alt'        => esc_attr(
 477                              sprintf(
 478                                  /* Translators: %s is the group's name. */
 479                                  __( 'Group Profile photo of %s', 'buddypress' ),
 480                                  $group->name
 481                              )
 482                          ),
 483                          'data.id'                => $group->id,
 484                          'data.extra'             => $extra,
 485                      )
 486                  );
 487              }
 488          }
 489      } elseif ( defined( 'WP_USE_THEMES' ) ) {
 490          // Get corresponding members.
 491          $path = sprintf(
 492              '/%1$s/%2$s/%3$s',
 493              bp_rest_namespace(),
 494              bp_rest_version(),
 495              buddypress()->groups->id
 496          );
 497  
 498          $default_path = add_query_arg(
 499              $default_args,
 500              $path
 501          );
 502  
 503          $preloaded_groups = rest_preload_api_request( '', $default_path );
 504  
 505          buddypress()->groups->block_globals['bp/dynamic-groups']->items[ $widget_id ] = (object) array(
 506              'selector'   => $widget_id,
 507              'query_args' => $default_args,
 508              'preloaded'  => reset( $preloaded_groups ),
 509          );
 510  
 511          // Only enqueue common/specific scripts and data once per page load.
 512          if ( ! has_action( 'wp_footer', 'bp_groups_blocks_add_script_data', 1 ) ) {
 513              wp_set_script_translations( 'bp-dynamic-groups-script', 'buddypress' );
 514              wp_enqueue_script( 'bp-dynamic-groups-script' );
 515  
 516              add_action( 'wp_footer', 'bp_groups_blocks_add_script_data', 1 );
 517          }
 518      }
 519  
 520      $widget_content .= sprintf(
 521          '<div class="item-options">
 522              %1$s
 523          </div>
 524          <ul id="%2$s" class="item-list" aria-live="polite" aria-relevant="all" aria-atomic="true">
 525              %3$s
 526          </ul>',
 527          implode( $separator_output, $item_options_output ),
 528          esc_attr( $widget_id ),
 529          $preview
 530      );
 531  
 532      // Adds a container to make sure the block is styled even when used into the Columns parent block.
 533      $widget_content = sprintf( '<div class="bp-dynamic-block-container">%s</div>', "\n" . $widget_content . "\n" );
 534  
 535      // Only add a block wrapper if not loaded into a Widgets sidebar.
 536      if ( ! did_action( 'dynamic_sidebar_before' ) ) {
 537          return sprintf(
 538              '<div %1$s>%2$s</div>',
 539              $wrapper_attributes,
 540              $widget_content
 541          );
 542      }
 543  
 544      return $widget_content;
 545  }


Generated: Fri Jan 28 01:00:57 2022 Cross-referenced by PHPXref 0.7.1