[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * BP Blogs Blocks Functions.
   4   *
   5   * @package BuddyPress
   6   * @subpackage BlogsBlocks
   7   * @since 9.0.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  if ( ! defined( 'ABSPATH' ) ) {
  12      exit;
  13  }
  14  
  15  /**
  16   * Callback function to render the Recent Posts Block.
  17   *
  18   * @since 9.0.0
  19   *
  20   * @param array $attributes The block attributes.
  21   * @return string           HTML output.
  22   */
  23  function bp_blogs_render_recent_posts_block( $attributes = array() ) {
  24      $block_args = bp_parse_args(
  25          $attributes,
  26          array(
  27              'title'     => __( 'Recent Networkwide Posts', 'buddypress' ),
  28              'maxPosts'  => 10,
  29              'linkTitle' => false,
  30          )
  31      );
  32  
  33      $classnames           = 'widget_bp_blogs_widget buddypress widget';
  34      $wrapper_attributes   = get_block_wrapper_attributes( array( 'class' => $classnames ) );
  35      $blogs_directory_link = bp_get_blogs_directory_permalink();
  36      $max_posts            = (int) $block_args['maxPosts'];
  37      $no_posts             = __( 'Sorry, there were no posts found.', 'buddypress' );
  38  
  39      // Set the Block's title.
  40      if ( true === $block_args['linkTitle'] ) {
  41          $widget_content = sprintf(
  42              '<h2 class="widget-title"><a href="%1$s">%2$s</a></h2>',
  43              esc_url( $blogs_directory_link ),
  44              esc_html( $block_args['title'] )
  45          );
  46      } else {
  47          $widget_content = sprintf( '<h2 class="widget-title">%s</h2>', esc_html( $block_args['title'] ) );
  48      }
  49  
  50      $blog_activities = bp_activity_get(
  51          array(
  52              'max'      => $max_posts,
  53              'per_page' => $max_posts,
  54              'user_id'  => 0,
  55              'scope'    => false,
  56              'filter'   => array(
  57                  'object'     => false,
  58                  'primary_id' => false,
  59                  'action'     => 'new_blog_post',
  60              ),
  61          )
  62      );
  63  
  64      $blog_activities = reset( $blog_activities );
  65  
  66      if ( ! $blog_activities ) {
  67          $widget_content .= sprintf( '<div class="widget-error">%s</div>', $no_posts );
  68      } else {
  69          // Avoid conflicts with other activity loops.
  70          $reset_activities_template = null;
  71          if ( ! empty( $GLOBALS['activities_template'] ) ) {
  72              $reset_activities_template = $GLOBALS['activities_template'];
  73          }
  74  
  75          $GLOBALS['activities_template'] = new \stdClass();
  76          $activities                     = array();
  77  
  78          foreach ( $blog_activities as $blog_activity ) {
  79              $activity_content                         = '';
  80              $GLOBALS['activities_template']->activity = $blog_activity;
  81  
  82              if ( $blog_activity->content ) {
  83                  /** This filter is documented in bp-activity/bp-activity-template.php. */
  84                  $activity_content = apply_filters_ref_array( 'bp_get_activity_content_body', array( $blog_activity->content, &$blog_activity ) );
  85                  $activity_content = sprintf(
  86                      '<div class="activity-inner">%s</div>',
  87                      $activity_content
  88                  );
  89              }
  90  
  91              /** This filter is documented in bp-activity/bp-activity-template.php. */
  92              $actity_action = apply_filters_ref_array(
  93                  'bp_get_activity_action',
  94                  array(
  95                      bp_insert_activity_meta( $blog_activity->action ),
  96                      &$blog_activity,
  97                      array( 'no_timestamp' => false ),
  98                  )
  99              );
 100  
 101              $activities[] = sprintf(
 102                  '<li>
 103                      <div class="activity-content">
 104                          <div class="activity-header">%1$s</div>
 105                          %2$s
 106                      </div>
 107                  </li>',
 108                  $actity_action,
 109                  $activity_content
 110              );
 111          }
 112  
 113          // Reset the global template loop.
 114          $GLOBALS['activities_template'] = $reset_activities_template;
 115  
 116          $widget_content .= sprintf(
 117              '<ul class="activity-list item-list">
 118                  %s
 119              </ul>',
 120              implode( "\n", $activities )
 121          );
 122      }
 123  
 124      // Adds a container to make sure the block is styled even when used into the Columns parent block.
 125      $widget_content = sprintf( '<div class="bp-recent-posts-block-container">%s</div>', "\n" . $widget_content . "\n" );
 126  
 127      // Only add a block wrapper if not loaded into a Widgets sidebar.
 128      if ( ! did_action( 'dynamic_sidebar_before' ) ) {
 129          return sprintf(
 130              '<div %1$s>%2$s</div>',
 131              $wrapper_attributes,
 132              $widget_content
 133          );
 134      }
 135  
 136      return $widget_content;
 137  }


Generated: Thu Apr 25 01:01:12 2024 Cross-referenced by PHPXref 0.7.1