[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * BP Members Blocks Functions. 4 * 5 * @package BuddyPress 6 * @subpackage MembersBlocks 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 Member Block. 17 * 18 * @since 6.0.0 19 * 20 * @param array $attributes The block attributes. 21 * @return string HTML output. 22 */ 23 function bp_members_render_member_block( $attributes = array() ) { 24 $bp = buddypress(); 25 26 $block_args = bp_parse_args( 27 $attributes, 28 array( 29 'itemID' => 0, 30 'avatarSize' => 'full', 31 'displayMentionSlug' => true, 32 'displayActionButton' => true, 33 'displayCoverImage' => true, 34 ) 35 ); 36 37 if ( ! $block_args['itemID'] ) { 38 return; 39 } 40 41 // Set the member ID and container classes. 42 $member_id = (int) $block_args['itemID']; 43 $container_classes = array( 'bp-block-member' ); 44 45 // Mention variables. 46 $username = bp_core_get_username( $member_id ); 47 $at_mention = ''; 48 49 // Avatar variables. 50 $avatar = ''; 51 $avatar_container = ''; 52 53 // Cover image variable. 54 $cover_image = ''; 55 $cover_style = ''; 56 $cover_container = ''; 57 58 // Member name variables. 59 $display_name = bp_core_get_user_displayname( $member_id ); 60 $member_link = bp_core_get_user_domain( $member_id ); 61 62 // Member action button. 63 $action_button = ''; 64 $display_action_button = (bool) $block_args['displayActionButton']; 65 66 if ( $bp->avatar && $bp->avatar->show_avatars && in_array( $block_args['avatarSize'], array( 'thumb', 'full' ), true ) ) { 67 $avatar = bp_core_fetch_avatar( 68 array( 69 'item_id' => $member_id, 70 'object' => 'user', 71 'type' => $block_args['avatarSize'], 72 'html' => false, 73 ) 74 ); 75 76 $container_classes[] = 'avatar-' . $block_args['avatarSize']; 77 } else { 78 $container_classes[] = 'avatar-none'; 79 } 80 81 if ( $avatar ) { 82 $avatar_container = sprintf( 83 '<div class="item-header-avatar"> 84 <a href="%1$s"> 85 <img loading="lazy" src="%2$s" alt="%3$s" class="avatar"> 86 </a> 87 </div>', 88 esc_url( $member_link ), 89 esc_url( $avatar ), 90 /* translators: %s: member name */ 91 sprintf( esc_html__( 'Profile photo of %s', 'buddypress' ), $display_name ) 92 ); 93 } 94 95 $display_cover_image = (bool) $block_args['displayCoverImage']; 96 if ( bp_is_active( 'members', 'cover_image' ) && $display_cover_image ) { 97 $cover_image = bp_attachments_get_attachment( 98 'url', 99 array( 100 'item_id' => $member_id, 101 ) 102 ); 103 104 if ( $cover_image ) { 105 $cover_style = sprintf( 106 ' style="background-image: url( %s );"', 107 esc_url( $cover_image ) 108 ); 109 } 110 111 $cover_container = sprintf( 112 '<div class="bp-member-cover-image"%s></div>', 113 $cover_style 114 ); 115 116 $container_classes[] = 'has-cover'; 117 } 118 119 $display_mention_slug = (bool) $block_args['displayMentionSlug']; 120 if ( bp_is_active( 'activity' ) && bp_activity_do_mentions() && $display_mention_slug ) { 121 $at_mention = sprintf( 122 '<span class="user-nicename">@%s</span>', 123 esc_html( $username ) 124 ); 125 } 126 127 if ( $display_action_button ) { 128 $action_button = sprintf( 129 '<div class="bp-profile-button"> 130 <a href="%1$s" class="button large primary button-primary" role="button">%2$s</a> 131 </div>', 132 esc_url( $member_link ), 133 esc_html__( 'View Profile', 'buddypress' ) 134 ); 135 } 136 137 $output = sprintf( 138 '<div class="%1$s"> 139 %2$s 140 <div class="member-content"> 141 %3$s 142 <div class="member-description"> 143 <strong><a href="%4$s">%5$s</a></strong> 144 %6$s 145 %7$s 146 </div> 147 </div> 148 </div>', 149 implode( ' ', array_map( 'sanitize_html_class', $container_classes ) ), 150 $cover_container, 151 $avatar_container, 152 esc_url( $member_link ), 153 esc_html( $display_name ), 154 $at_mention, 155 $action_button 156 ); 157 158 // Compact all interesting parameters. 159 $params = array_merge( $block_args, compact( 'username', 'display_name', 'member_link', 'avatar', 'cover_image' ) ); 160 161 /** 162 * Filter here to edit the output of the single member block. 163 * 164 * @since 6.0.0 165 * 166 * @param string $output The HTML output of the block. 167 * @param array $params The block extended parameters. 168 */ 169 return apply_filters( 'bp_members_render_member_block_output', $output, $params ); 170 } 171 172 /** 173 * Callback function to render the BP Members Block. 174 * 175 * @since 7.0.0 176 * 177 * @param array $attributes The block attributes. 178 * @return string HTML output. 179 */ 180 function bp_members_render_members_block( $attributes = array() ) { 181 $bp = buddypress(); 182 183 $block_args = bp_parse_args( 184 $attributes, 185 array( 186 'itemIDs' => array(), 187 'avatarSize' => 'full', 188 'displayMentionSlug' => true, 189 'displayUserName' => true, 190 'extraData' => 'none', 191 'layoutPreference' => 'list', 192 'columns' => '2', 193 ) 194 ); 195 196 $member_ids = wp_parse_id_list( $block_args['itemIDs'] ); 197 if ( ! array_filter( $member_ids ) ) { 198 return ''; 199 } 200 201 $container_classes = sprintf( 'bp-block-members avatar-%s', $block_args['avatarSize'] ); 202 if ( 'grid' === $block_args['layoutPreference'] ) { 203 $container_classes .= sprintf( ' is-grid columns-%d', (int) $block_args['columns'] ); 204 } 205 206 $query_args = array( 207 'user_ids' => $member_ids, 208 ); 209 210 if ( 'none' !== $block_args['extraData'] ) { 211 $query_args['populate_extras'] = true; 212 } 213 214 $query = bp_core_get_users( $query_args ); 215 216 // Initialize the output and the members. 217 $output = ''; 218 $members = $query['users']; 219 220 foreach ( $members as $member ) { 221 $has_activity = false; 222 $member_item_classes = 'member-content'; 223 224 if ( 'list' === $block_args['layoutPreference'] && 'latest_update' === $block_args['extraData'] && isset( $member->latest_update ) && $member->latest_update ) { 225 $has_activity = true; 226 $member_item_classes = 'member-content has-activity'; 227 } 228 229 $output .= sprintf( '<div class="%s">', $member_item_classes ); 230 231 // Get Member link. 232 $member_link = bp_core_get_user_domain( $member->ID ); 233 234 // Set the Avatar output. 235 if ( $bp->avatar && $bp->avatar->show_avatars && 'none' !== $block_args['avatarSize'] ) { 236 $output .= sprintf( 237 '<div class="item-header-avatar"> 238 <a href="%1$s"> 239 <img loading="lazy" class="avatar" alt="%2$s" src="%3$s" /> 240 </a> 241 </div>', 242 esc_url( $member_link ), 243 /* translators: %s: member name */ 244 sprintf( esc_attr__( 'Profile photo of %s', 'buddypress' ), $member->display_name ), 245 esc_url( 246 bp_core_fetch_avatar( 247 array( 248 'item_id' => $member->ID, 249 'object' => 'user', 250 'type' => $block_args['avatarSize'], 251 'html' => false, 252 ) 253 ) 254 ) 255 ); 256 } 257 258 $output .= '<div class="member-description">'; 259 260 // Add the latest activity the member posted. 261 if ( $has_activity ) { 262 $activity_content = ''; 263 $activity_data = maybe_unserialize( $member->latest_update ); 264 265 if ( isset( $activity_data['content'] ) ) { 266 $activity_content = apply_filters( 'bp_get_activity_content', $activity_data['content'] ); 267 } 268 269 $display_name = ''; 270 if ( $block_args['displayUserName'] ) { 271 $display_name = $member->display_name; 272 } 273 274 $mention_name = ''; 275 if ( bp_is_active( 'activity' ) && bp_activity_do_mentions() && $block_args['displayMentionSlug'] ) { 276 $mention_name = '(@' . $member->user_nicename . ')'; 277 } 278 279 $output .= sprintf( 280 '<blockquote class="wp-block-quote"> 281 %1$s 282 <cite> 283 <span>%2$s</span> 284 %3$s 285 </cite> 286 </blockquote>', 287 $activity_content, 288 esc_html( $display_name ), 289 esc_html( $mention_name ) 290 ); 291 } else { 292 if ( $block_args['displayUserName'] ) { 293 $output .= sprintf( 294 '<strong><a href="%1$s">%2$s</a></strong>', 295 esc_url( $member_link ), 296 esc_html( $member->display_name ) 297 ); 298 } 299 300 if ( bp_is_active( 'activity' ) && bp_activity_do_mentions() && $block_args['displayMentionSlug'] ) { 301 $output .= sprintf( 302 '<span class="user-nicename">@%s</span>', 303 esc_html( $member->user_nicename ) 304 ); 305 } 306 307 if ( 'last_activity' === $block_args['extraData'] ) { 308 $output .= sprintf( 309 '<time datetime="%1$s">%2$s</time>', 310 esc_attr( bp_core_get_iso8601_date( $member->last_activity ) ), 311 /* translators: %s: last activity timestamp (e.g. "Active 1 hour ago") */ 312 sprintf( esc_html__( 'Active %s', 'buddypress' ), bp_core_time_since( $member->last_activity ) ) 313 ); 314 } 315 } 316 317 $output .= '</div></div>'; 318 } 319 320 // Set the final output. 321 $output = sprintf( '<div class="%1$s">%2$s</div>', $container_classes, $output ); 322 323 /** 324 * Filter here to edit the output of the members block. 325 * 326 * @since 7.0.0 327 * 328 * @param string $output The HTML output of the block. 329 * @param array $block_args The block arguments. 330 * @param array $members The list of WP_User objects. 331 */ 332 return apply_filters( 'bp_members_render_members_block_output', $output, $block_args, $members ); 333 } 334 335 /** 336 * Adds specific script data for the BP Members blocks. 337 * 338 * Only used for the BP Dynamic Members block. 339 * 340 * @since 9.0.0 341 */ 342 function bp_members_blocks_add_script_data() { 343 $dynamic_members_blocks = array_filter( buddypress()->members->block_globals['bp/dynamic-members']->items ); 344 345 if ( ! $dynamic_members_blocks ) { 346 return; 347 } 348 349 $path = sprintf( 350 '/%1$s/%2$s/%3$s', 351 bp_rest_namespace(), 352 bp_rest_version(), 353 buddypress()->members->id 354 ); 355 356 wp_localize_script( 357 'bp-dynamic-members-script', 358 'bpDynamicMembersSettings', 359 array( 360 'path' => ltrim( $path, '/' ), 361 'root' => esc_url_raw( get_rest_url() ), 362 'nonce' => wp_create_nonce( 'wp_rest' ), 363 ) 364 ); 365 366 // Include the common JS template. 367 echo bp_get_dynamic_template_part( 'assets/widgets/dynamic-members.php' ); 368 369 // List the block specific props. 370 wp_add_inline_script( 371 'bp-dynamic-members-script', 372 sprintf( 'var bpDynamicMembersBlocks = %s;', wp_json_encode( array_values( $dynamic_members_blocks ) ) ), 373 'before' 374 ); 375 } 376 377 /** 378 * Callback function to render the Dynamic Members Block. 379 * 380 * @since 9.0.0 381 * 382 * @param array $attributes The block attributes. 383 * @return string HTML output. 384 */ 385 function bp_members_render_dynamic_members_block( $attributes = array() ) { 386 $block_args = bp_parse_args( 387 $attributes, 388 array( 389 'title' => __( 'Members', 'buddypress' ), 390 'maxMembers' => 5, 391 'memberDefault' => 'active', 392 'linkTitle' => false, 393 ) 394 ); 395 396 $classnames = 'widget_bp_core_members_widget buddypress widget'; 397 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) ); 398 399 $max_members = (int) $block_args['maxMembers']; 400 $no_members = __( 'No members found.', 'buddypress' ); 401 402 /** This filter is documented in buddypress/src/bp-members/classes/class-bp-core-members-widget.php */ 403 $separator = apply_filters( 'bp_members_widget_separator', '|' ); 404 405 // Make sure the widget ID is unique. 406 $widget_id = uniqid( 'members-list-' ); 407 $members_directory_link = bp_get_members_directory_permalink(); 408 409 // Set the Block's title. 410 if ( true === $block_args['linkTitle'] ) { 411 $widget_content = sprintf( 412 '<h2 class="widget-title"><a href="%1$s">%2$s</a></h2>', 413 esc_url( $members_directory_link ), 414 esc_html( $block_args['title'] ) 415 ); 416 } else { 417 $widget_content = sprintf( '<h2 class="widget-title">%s</h2>', esc_html( $block_args['title'] ) ); 418 } 419 420 $item_options = array( 421 'newest' => array( 422 'class' => '', 423 'label' => __( 'Newest', 'buddypress' ), 424 ), 425 'active' => array( 426 'class' => '', 427 'label' => __( 'Active', 'buddypress' ), 428 ), 429 ); 430 431 if ( bp_is_active( 'friends' ) ) { 432 $item_options['popular'] = array( 433 'class' => '', 434 'label' => __( 'Popular', 'buddypress' ), 435 ); 436 } 437 438 $item_options_output = array(); 439 $separator_output = sprintf( ' <span class="bp-separator" role="separator">%s</span> ', esc_html( $separator ) ); 440 441 foreach ( $item_options as $item_type => $item_attr ) { 442 if ( $block_args['memberDefault'] === $item_type ) { 443 $item_attr['class'] = ' class="selected"'; 444 } 445 446 $item_options_output[] = sprintf( 447 '<a href="%1$s" data-bp-sort="%2$s"%3$s>%4$s</a>', 448 esc_url( $members_directory_link ), 449 esc_attr( $item_type ), 450 $item_attr['class'], 451 esc_html( $item_attr['label'] ) 452 ); 453 } 454 455 $preview = ''; 456 $default_args = array( 457 'type' => $block_args['memberDefault'], 458 'per_page' => $max_members, 459 'populate_extras' => true, 460 ); 461 462 // Previewing the Block inside the editor. 463 if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { 464 $bp_query = bp_core_get_users( $default_args ); 465 $preview = sprintf( '<div class="widget-error">%s</div>', $no_members ); 466 467 if ( is_array( $bp_query['users'] ) && 0 < count( $bp_query['users'] ) ) { 468 $preview = ''; 469 foreach ( $bp_query['users'] as $user ) { 470 if ( 'newest' === $block_args['memberDefault'] ) { 471 /* translators: %s is time elapsed since the registration date happened */ 472 $extra = sprintf( _x( 'Registered %s', 'The timestamp when the user registered', 'buddypress' ), bp_core_time_since( $user->user_registered ) ); 473 } elseif ( 'popular' === $block_args['memberDefault'] && isset( $item_options['popular'] ) && isset( $user->total_friend_count ) ) { 474 /* translators: %s: total friend count */ 475 $extra = sprintf( _n( '%s friend', '%s friends', $user->total_friend_count, 'buddypress' ), number_format_i18n( $user->total_friend_count ) ); 476 } else { 477 /* translators: %s: last activity timestamp (e.g. "Active 1 hour ago") */ 478 $extra = sprintf( __( 'Active %s', 'buddypress' ), bp_core_time_since( $user->last_activity ) ); 479 } 480 481 $preview .= bp_get_dynamic_template_part( 482 'assets/widgets/dynamic-members.php', 483 'php', 484 array( 485 'data.link' => bp_core_get_user_domain( $user->ID, $user->user_nicename, $user->user_login ), 486 'data.name' => $user->display_name, 487 'data.avatar_urls.thumb' => bp_core_fetch_avatar( 488 array( 489 'item_id' => $user->ID, 490 'html' => false, 491 ) 492 ), 493 'data.avatar_alt' => esc_attr( 494 sprintf( 495 /* translators: %s: member name */ 496 __( 'Profile picture of %s', 'buddypress' ), 497 $user->display_name 498 ) 499 ), 500 'data.id' => $user->ID, 501 'data.extra' => $extra, 502 ) 503 ); 504 } 505 } 506 } elseif ( defined( 'WP_USE_THEMES' ) ) { 507 // Get corresponding members. 508 $path = sprintf( 509 '/%1$s/%2$s/%3$s', 510 bp_rest_namespace(), 511 bp_rest_version(), 512 buddypress()->members->id 513 ); 514 515 $default_path = add_query_arg( 516 $default_args, 517 $path 518 ); 519 520 $preloaded_members = rest_preload_api_request( '', $default_path ); 521 522 buddypress()->members->block_globals['bp/dynamic-members']->items[ $widget_id ] = (object) array( 523 'selector' => $widget_id, 524 'query_args' => $default_args, 525 'preloaded' => reset( $preloaded_members ), 526 ); 527 528 // Only enqueue common/specific scripts and data once per page load. 529 if ( ! has_action( 'wp_footer', 'bp_members_blocks_add_script_data', 1 ) ) { 530 wp_set_script_translations( 'bp-dynamic-members-script', 'buddypress' ); 531 wp_enqueue_script( 'bp-dynamic-members-script' ); 532 533 add_action( 'wp_footer', 'bp_members_blocks_add_script_data', 1 ); 534 } 535 } 536 537 $widget_content .= sprintf( 538 '<div class="item-options"> 539 %1$s 540 </div> 541 <ul id="%2$s" class="item-list" aria-live="polite" aria-relevant="all" aria-atomic="true"> 542 %3$s 543 </ul>', 544 implode( $separator_output, $item_options_output ), 545 esc_attr( $widget_id ), 546 $preview 547 ); 548 549 // Adds a container to make sure the block is styled even when used into the Columns parent block. 550 $widget_content = sprintf( '<div class="bp-dynamic-block-container">%s</div>', "\n" . $widget_content . "\n" ); 551 552 // Only add a block wrapper if not loaded into a Widgets sidebar. 553 if ( ! did_action( 'dynamic_sidebar_before' ) ) { 554 return sprintf( 555 '<div %1$s>%2$s</div>', 556 $wrapper_attributes, 557 $widget_content 558 ); 559 } 560 561 return $widget_content; 562 } 563 564 /** 565 * Common function to render the Recently Active & Online Members Blocks. 566 * 567 * @since 9.0.0 568 * 569 * @param array $block_args { 570 * Optional. An array of Block arguments. 571 * 572 * @type string $title The title of the Block. 573 * @type int $maxMembers The maximum number of members to show. Defaults to `0`. 574 * @type string $noMembers The string to output when there are no members to show. 575 * @type string $classname The name of the CSS class to use. 576 * @type string $type The type of filter to perform. Possible values are `online`, `active`, 577 * `newest`, `alphabetical`, `random` or `popular`. 578 * } 579 * @return string HTML output. 580 */ 581 function bp_members_render_members_avatars_block( $block_args = array() ) { 582 $args = bp_parse_args( 583 $block_args, 584 array( 585 'title' => '', 586 'maxMembers' => 0, 587 'noMembers' => '', 588 'classname' => '', 589 'type' => 'active', 590 ), 591 '' 592 ); 593 594 $title = $args['title']; 595 $max_members = (int) $args['maxMembers']; 596 $no_members = $args['noMembers']; 597 $classname = sanitize_key( $args['classname'] ); 598 $wrapper_attributes = get_block_wrapper_attributes( 599 array( 600 'class' => sprintf( '%s buddypress widget', $classname ), 601 ) 602 ); 603 $type = sanitize_key( $args['type'] ); 604 605 if ( $title ) { 606 $widget_content = sprintf( '<h2 class="widget-title">%s</h2>', esc_html( $title ) ); 607 } else { 608 $widget_content = ''; 609 } 610 611 // Query Users. 612 $query = bp_core_get_users( 613 array( 614 'user_id' => 0, 615 'type' => $type, 616 'per_page' => $max_members, 617 'max' => $max_members, 618 'populate_extras' => true, 619 'search_terms' => false, 620 ) 621 ); 622 623 // Build the output for online members. 624 if ( isset( $query['total'] ) && 1 <= (int) $query['total'] ) { 625 $members = $query['users']; 626 $member_avatars = array(); 627 628 foreach ( $members as $member ) { 629 $member_avatars[] = sprintf( 630 '<div class="item-avatar"> 631 <a href="%1$s" class="bp-tooltip" data-bp-tooltip="%2$s"> 632 <img loading="lazy" src="%3$s" class="avatar user-%4$s-avatar avatar-50 photo" width="50" height="50" alt="%5$s"> 633 </a> 634 </div>', 635 esc_url( bp_core_get_user_domain( $member->ID, $member->user_nicename, $member->user_login ) ), 636 esc_html( $member->display_name ), 637 bp_core_fetch_avatar( 638 array( 639 'item_id' => $member->ID, 640 'html' => false, 641 ) 642 ), 643 esc_attr( $member->ID ), 644 esc_html( 645 sprintf( 646 /* translators: %s: member name */ 647 __( 'Profile picture of %s', 'buddypress' ), 648 $member->display_name 649 ) 650 ) 651 ); 652 } 653 654 $widget_content .= sprintf( 655 '<div class="avatar-block"> 656 %s 657 </div>', 658 implode( "\n", $member_avatars ) 659 ); 660 } else { 661 $widget_content .= sprintf( 662 '<div class="widget-error"> 663 %s 664 </div>', 665 esc_html( $no_members ) 666 ); 667 } 668 669 // Only add a block wrapper if not loaded into a Widgets sidebar. 670 if ( ! did_action( 'dynamic_sidebar_before' ) ) { 671 return sprintf( 672 '<div %1$s>%2$s</div>', 673 $wrapper_attributes, 674 $widget_content 675 ); 676 } 677 678 return $widget_content; 679 } 680 681 /** 682 * Callback function to render the Online Members Block. 683 * 684 * @since 9.0.0 685 * 686 * @param array $attributes The block attributes. 687 * @return string HTML output. 688 */ 689 function bp_members_render_online_members_block( $attributes = array() ) { 690 $block_args = bp_parse_args( 691 $attributes, 692 array( 693 'title' => __( 'Who\'s Online', 'buddypress' ), 694 'maxMembers' => 15, 695 'noMembers' => __( 'There are no users currently online', 'buddypress' ), 696 'classname' => 'widget_bp_core_whos_online_widget', 697 ), 698 'members_widget_settings' 699 ); 700 701 $block_args['type'] = 'online'; 702 703 return bp_members_render_members_avatars_block( $block_args ); 704 } 705 706 /** 707 * Callback function to render the Recently Active Members Block. 708 * 709 * @since 9.0.0 710 * 711 * @param array $attributes The block attributes. 712 * @return string HTML output. 713 */ 714 function bp_members_render_active_members_block( $attributes = array() ) { 715 $block_args = bp_parse_args( 716 $attributes, 717 array( 718 'title' => __( 'Recently Active Members', 'buddypress' ), 719 'maxMembers' => 15, 720 'noMembers' => __( 'There are no recently active members', 'buddypress' ), 721 'classname' => 'widget_bp_core_recently_active_widget', 722 ), 723 'recently_active_members_widget_settings' 724 ); 725 726 $block_args['type'] = 'active'; 727 728 return bp_members_render_members_avatars_block( $block_args ); 729 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sat Dec 7 01:00:57 2024 | Cross-referenced by PHPXref 0.7.1 |