[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * BP Messages Blocks Functions.
   4   *
   5   * @package BuddyPress
   6   * @subpackage MessagesBlocks
   7   * @since 9.0.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Callback function to render the BP Sitewide Notices Block.
  15   *
  16   * @since 9.0.0
  17   *
  18   * @param array $attributes The block attributes.
  19   * @return string HTML output.
  20   */
  21  function bp_messages_render_sitewide_notices_block( $attributes = array() ) {
  22      $block_args = bp_parse_args(
  23          $attributes,
  24          array(
  25              'title' => '',
  26          ),
  27          'widget_object_sitewide_messages'
  28      );
  29  
  30      if ( ! is_user_logged_in() ) {
  31          return;
  32      }
  33  
  34      $feedback_tpl  = '<div class="components-placeholder">' . "\n";
  35      $feedback_tpl .= '<div class="components-placeholder__label">%1$s</div>' . "\n";
  36      $feedback_tpl .= '<div class="components-placeholder__fieldset">%2$s</div>' . "\n";
  37      $feedback_tpl .= '</div>';
  38  
  39      // Don't display the block if there are no Notices to show.
  40      $notice = \BP_Messages_Notice::get_active();
  41      if ( empty( $notice->id ) ) {
  42          // Previewing the Block inside the editor.
  43          if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
  44              return sprintf(
  45                  $feedback_tpl,
  46                  esc_html__( 'Preview unavailable', 'buddypress' ),
  47                  esc_html__( 'No active sitewide notices.', 'buddypress' )
  48              );
  49          }
  50  
  51          return;
  52      }
  53  
  54      // Only enqueue common/specific scripts and data once per page load.
  55      if ( ! wp_script_is( 'bp-sitewide-notices-script', 'enqueued' ) ) {
  56          $path = sprintf(
  57              '/%1$s/%2$s/sitewide-notices/',
  58              bp_rest_namespace(),
  59              bp_rest_version()
  60          );
  61          wp_enqueue_script( 'bp-sitewide-notices-script' );
  62          wp_localize_script(
  63              'bp-sitewide-notices-script',
  64              'bpSitewideNoticeBlockSettings',
  65              array(
  66                  'path'        => ltrim( $path, '/' ),
  67                  'dismissPath' => ltrim( $path, '/' ) . 'dismiss',
  68                  'root'        => esc_url_raw( get_rest_url() ),
  69                  'nonce'       => wp_create_nonce( 'wp_rest' ),
  70              )
  71          );
  72      }
  73  
  74      $closed_notices = (array) bp_get_user_meta( bp_loggedin_user_id(), 'closed_notices', true );
  75  
  76      if ( in_array( $notice->id, $closed_notices, true ) ) {
  77          // Previewing the Block inside the editor.
  78          if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
  79              return sprintf(
  80                  $feedback_tpl,
  81                  esc_html__( 'Preview unavailable', 'buddypress' ),
  82                  esc_html__( 'You dismissed the sitewide notice.', 'buddypress' )
  83              );
  84          }
  85  
  86          return;
  87      }
  88  
  89      // There is an active, non-dismissed notice to show.
  90      $title = $block_args['title'];
  91  
  92      $classnames         = 'widget_bp_core_sitewide_messages buddypress widget';
  93      $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) );
  94  
  95      $widget_content = '<div class="bp-sitewide-notice-block">';
  96  
  97      if ( $title ) {
  98          $widget_content .= sprintf(
  99              '<h2 class="widget-title">%s</h2>',
 100              esc_html( $title )
 101          );
 102      }
 103  
 104      $widget_content .= sprintf(
 105          '<div class="bp-sitewide-notice-message info bp-notice" rel="n-%1$d">
 106              <strong>%2$s</strong>
 107              <a href="%3$s" class="bp-tooltip button dismiss-notice" data-bp-tooltip="%4$s" data-bp-sitewide-notice-id="%5$d"><span class="bp-screen-reader-text">%6$s</span> <span aria-hidden="true">&#x2716;</span></a>
 108              %7$s
 109          </div>',
 110          esc_attr( $notice->id ),
 111          bp_get_message_notice_subject( $notice ),
 112          esc_url( bp_get_message_notice_dismiss_link() ),
 113          esc_attr__( 'Dismiss this notice', 'buddypress' ),
 114          esc_attr( $notice->id ),
 115          esc_html__( 'Dismiss this notice', 'buddypress' ),
 116          bp_get_message_notice_text( $notice )
 117      );
 118  
 119      $widget_content .= '</div>';
 120  
 121      if ( ! did_action( 'dynamic_sidebar_before' ) ) {
 122          return sprintf(
 123              '<div %1$s>%2$s</div>',
 124              $wrapper_attributes,
 125              $widget_content
 126          );
 127      }
 128  
 129      return $widget_content;
 130  }


Generated: Wed Apr 24 01:01:03 2024 Cross-referenced by PHPXref 0.7.1