[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * BP Friends Blocks Functions.
   4   *
   5   * @package BuddyPress
   6   * @subpackage FriendsBlocks
   7   * @since 9.0.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Adds specific script data for the BP Friends blocks.
  15   *
  16   * Only used for the BP Friends block.
  17   *
  18   * @since 9.0.0
  19   */
  20  function bp_friends_blocks_add_script_data() {
  21      $friends_blocks = array_filter( buddypress()->friends->block_globals['bp/friends']->items );
  22  
  23      if ( ! $friends_blocks ) {
  24          return;
  25      }
  26  
  27      // Include the common JS template.
  28      echo bp_get_dynamic_template_part( 'assets/widgets/friends.php' );
  29  
  30      // List the block specific props.
  31      wp_add_inline_script(
  32          'bp-friends-script',
  33          sprintf( 'var bpFriendsBlocks = %s;', wp_json_encode( array_values( $friends_blocks ) ) ),
  34          'before'
  35      );
  36  }
  37  
  38  /**
  39   * Callback function to render the BP Friends Block.
  40   *
  41   * @since 9.0.0
  42   *
  43   * @param array $attributes The block attributes.
  44   * @return string           HTML output.
  45   */
  46  function bp_friends_render_friends_block( $attributes = array() ) {
  47      $block_args = bp_parse_args(
  48          $attributes,
  49          array(
  50              'maxFriends'    => 5,
  51              'friendDefault' => 'active',
  52              'linkTitle'     => false,
  53              'postId'        => 0, // If the postId attribute is defined, post author friends are needed.
  54          )
  55      );
  56  
  57      $user_id = 0;
  58      if ( $block_args['postId'] ) {
  59          $user_id = (int) get_post_field( 'post_author', $block_args['postId'] );
  60      } else {
  61          $user_id = bp_displayed_user_id();
  62  
  63          if ( ! $user_id && isset( $_SERVER['REQUEST_URI'] ) ) {
  64              $request_uri  = esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) );
  65              $request_path = wp_parse_url( $request_uri, PHP_URL_PATH );
  66              $regex        = addcslashes( sprintf( '%s/.*bp/friends', rest_get_url_prefix() ), '/' );
  67  
  68              if ( preg_match( "/{$regex}/", $request_path ) ) {
  69                  $user_id = bp_loggedin_user_id();
  70              }
  71          }
  72      }
  73  
  74      if ( ! $user_id ) {
  75          return '';
  76      }
  77  
  78      $classnames         = 'widget_bp_core_friends_widget buddypress widget';
  79      $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) );
  80  
  81      $max_friends = (int) $block_args['maxFriends'];
  82      $no_friends  = __( 'Sorry, no members were found.', 'buddypress' );
  83  
  84      /**
  85       * Filters the separator of the friends block nav links.
  86       *
  87       * @since 9.0.0
  88       *
  89       * @param string $separator Separator string. Default '|'.
  90       */
  91      $separator = apply_filters( 'bp_friends_block_nav_links_separator', '|' );
  92  
  93      // Make sure the widget ID is unique.
  94      $widget_id = uniqid( 'friends-list-' );
  95  
  96      $link = trailingslashit( bp_core_get_user_domain( $user_id ) . bp_get_friends_slug() );
  97  
  98      /* translators: %s is the member's display name */
  99      $title = sprintf( __( '%s\'s Friends', 'buddypress' ), bp_core_get_user_displayname( $user_id ) );
 100  
 101      // Set the Block's title.
 102      if ( true === $block_args['linkTitle'] ) {
 103          $widget_content = sprintf(
 104              '<h2 class="widget-title"><a href="%1$s">%2$s</a></h2>',
 105              esc_url( $link ),
 106              esc_html( $title )
 107          );
 108      } else {
 109          $widget_content = sprintf( '<h2 class="widget-title">%s</h2>', esc_html( $title ) );
 110      }
 111  
 112      $item_options = array(
 113          'newest'  => array(
 114              'class' => '',
 115              'label' => __( 'Newest', 'buddypress' ),
 116          ),
 117          'active'  => array(
 118              'class' => '',
 119              'label' => __( 'Active', 'buddypress' ),
 120          ),
 121          'popular' => array(
 122              'class' => '',
 123              'label' => __( 'Popular', 'buddypress' ),
 124          ),
 125      );
 126  
 127      $item_options_output = array();
 128      $separator_output    = sprintf( ' <span class="bp-separator" role="separator">%s</span> ', esc_html( $separator ) );
 129  
 130      foreach ( $item_options as $item_type => $item_attr ) {
 131          if ( $block_args['friendDefault'] === $item_type ) {
 132              $item_attr['class'] = ' class="selected"';
 133          }
 134  
 135          $item_options_output[] = sprintf(
 136              '<a href="%1$s" data-bp-sort="%2$s"%3$s>%4$s</a>',
 137              esc_url( $link ),
 138              esc_attr( $item_type ),
 139              $item_attr['class'],
 140              esc_html( $item_attr['label'] )
 141          );
 142      }
 143  
 144      $preview      = '';
 145      $default_args = array(
 146          'user_id'         => $user_id,
 147          'type'            => $block_args['friendDefault'],
 148          'per_page'        => $max_friends,
 149          'populate_extras' => true,
 150      );
 151  
 152      // Previewing the Block inside the editor.
 153      if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
 154          $bp_query = bp_core_get_users( $default_args );
 155          $preview  = sprintf( '<div class="widget-error">%s</div>', $no_friends );
 156  
 157          if ( is_array( $bp_query['users'] ) && 0 < count( $bp_query['users'] ) ) {
 158              $preview = '';
 159              foreach ( $bp_query['users'] as $user ) {
 160                  if ( 'newest' === $block_args['friendDefault'] ) {
 161                      /* translators: %s is time elapsed since the registration date happened */
 162                      $extra = sprintf( _x( 'Registered %s', 'The timestamp when the user registered', 'buddypress' ), bp_core_time_since( $user->user_registered ) );
 163                  } elseif ( 'popular' === $block_args['friendDefault'] && isset( $item_options['popular'] ) && isset( $user->total_friend_count ) ) {
 164                      /* translators: %s: total friend count */
 165                      $extra = sprintf( _n( '%s friend', '%s friends', $user->total_friend_count, 'buddypress' ), number_format_i18n( $user->total_friend_count ) );
 166                  } else {
 167                      /* translators: %s: a human time diff. */
 168                      $extra = sprintf( __( 'Active %s', 'buddypress' ), bp_core_time_since( $user->last_activity ) );
 169                  }
 170  
 171                  $preview .= bp_get_dynamic_template_part(
 172                      'assets/widgets/friends.php',
 173                      'php',
 174                      array(
 175                          'data.link'              => bp_core_get_user_domain( $user->ID, $user->user_nicename, $user->user_login ),
 176                          'data.name'              => $user->display_name,
 177                          'data.avatar_urls.thumb' => bp_core_fetch_avatar(
 178                              array(
 179                                  'item_id' => $user->ID,
 180                                  'html'    => false,
 181                              )
 182                          ),
 183                          'data.avatar_alt'        => esc_attr(
 184                              sprintf(
 185                                  /* translators: %s: member name */
 186                                  __( 'Profile picture of %s', 'buddypress' ),
 187                                  $user->display_name
 188                              )
 189                          ),
 190                          'data.id'                => $user->ID,
 191                          'data.extra'             => $extra,
 192                      )
 193                  );
 194              }
 195          }
 196      } else {
 197          // Get corresponding friends.
 198          $path = sprintf(
 199              '/%1$s/%2$s/%3$s',
 200              bp_rest_namespace(),
 201              bp_rest_version(),
 202              buddypress()->members->id
 203          );
 204  
 205          $default_path = add_query_arg(
 206              $default_args,
 207              $path
 208          );
 209  
 210          $preloaded_friends = array();
 211          if ( bp_is_running_wp( '5.0.0' ) ) {
 212              $preloaded_friends = rest_preload_api_request( '', $default_path );
 213          }
 214  
 215          buddypress()->friends->block_globals['bp/friends']->items[ $widget_id ] = (object) array(
 216              'selector'   => $widget_id,
 217              'query_args' => $default_args,
 218              'preloaded'  => reset( $preloaded_friends ),
 219          );
 220  
 221          // Only enqueue common/specific scripts and data once per page load.
 222          if ( ! has_action( 'wp_footer', 'bp_friends_blocks_add_script_data', 1 ) ) {
 223              wp_set_script_translations( 'bp-friends-script', 'buddypress' );
 224              wp_enqueue_script( 'bp-friends-script' );
 225              wp_localize_script(
 226                  'bp-friends-script',
 227                  'bpFriendsSettings',
 228                  array(
 229                      'path'  => ltrim( $path, '/' ),
 230                      'root'  => esc_url_raw( get_rest_url() ),
 231                      'nonce' => wp_create_nonce( 'wp_rest' ),
 232                  )
 233              );
 234  
 235              add_action( 'wp_footer', 'bp_friends_blocks_add_script_data', 1 );
 236          }
 237      }
 238  
 239      $widget_content .= sprintf(
 240          '<div class="item-options">
 241              %1$s
 242          </div>
 243          <ul id="%2$s" class="item-list" aria-live="polite" aria-relevant="all" aria-atomic="true">
 244              %3$s
 245          </ul>',
 246          implode( $separator_output, $item_options_output ),
 247          esc_attr( $widget_id ),
 248          $preview
 249      );
 250  
 251      // Adds a container to make sure the block is styled even when used into the Columns parent block.
 252      $widget_content = sprintf( '<div class="bp-dynamic-block-container">%s</div>', "\n" . $widget_content . "\n" );
 253  
 254      // Only add a block wrapper if not loaded into a Widgets sidebar.
 255      if ( ! did_action( 'dynamic_sidebar_before' ) ) {
 256          return sprintf(
 257              '<div %1$s>%2$s</div>',
 258              $wrapper_attributes,
 259              $widget_content
 260          );
 261      }
 262  
 263      return $widget_content;
 264  }


Generated: Thu Sep 23 01:01:38 2021 Cross-referenced by PHPXref 0.7.1