[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Core BP Blocks functions.
   4   *
   5   * @package BuddyPress
   6   * @subpackage Core
   7   * @since 6.0.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * BuddyPress blocks require WordPress >= 5.0.0 & the BP REST API.
  15   *
  16   * @since 6.0.0
  17   *
  18   * @return bool True if the current installation supports BP Blocks.
  19   *              False otherwise.
  20   */
  21  function bp_support_blocks() {
  22      return bp_is_running_wp( '5.0.0' ) && bp_rest_api_is_available();
  23  }
  24  
  25  /**
  26   * Registers the BP Block components.
  27   *
  28   * @since 6.0.0
  29   * @since 9.0.0 Adds a dependency to `wp-server-side-render` if WP >= 5.3.
  30   *              Uses a dependency to `wp-editor` otherwise.
  31   */
  32  function bp_register_block_components() {
  33      $server_side_renderer_dep = 'wp-server-side-render';
  34      if ( bp_is_running_wp( '5.3.0', '<' ) ) {
  35          $server_side_renderer_dep = 'wp-editor';
  36      }
  37  
  38      wp_register_script(
  39          'bp-block-components',
  40          plugins_url( 'js/block-components.js', __FILE__ ),
  41          array(
  42              'wp-element',
  43              'wp-components',
  44              'wp-i18n',
  45              'wp-api-fetch',
  46              'wp-url',
  47              $server_side_renderer_dep,
  48          ),
  49          bp_get_version(),
  50          false
  51      );
  52  
  53      // Adds BP Block Components to the `bp` global.
  54      wp_add_inline_script(
  55          'bp-block-components',
  56          'window.bp = window.bp || {};
  57          bp.blockComponents = bpBlock.blockComponents;
  58          delete bpBlock;',
  59          'after'
  60      );
  61  }
  62  add_action( 'bp_blocks_init', 'bp_register_block_components', 1 );
  63  
  64  /**
  65   * Registers the BP Block Assets.
  66   *
  67   * @since 9.0.0
  68   */
  69  function bp_register_block_assets() {
  70      wp_register_script(
  71          'bp-block-data',
  72          plugins_url( 'js/block-data.js', __FILE__ ),
  73          array(
  74              'wp-data',
  75              'wp-api-fetch',
  76              'lodash',
  77          ),
  78          bp_get_version(),
  79          false
  80      );
  81  
  82      // Adds BP Block Assets to the `bp` global.
  83      wp_add_inline_script(
  84          'bp-block-data',
  85          sprintf(
  86              'window.bp = window.bp || {};
  87              bp.blockData = bpBlock.blockData;
  88              bp.blockData.embedScriptURL = \'%s\';
  89              delete bpBlock;',
  90              esc_url_raw( includes_url( 'js/wp-embed.min.js' ) )
  91          ),
  92          'after'
  93      );
  94  }
  95  add_action( 'bp_blocks_init', 'bp_register_block_assets', 2 );
  96  
  97  /**
  98   * Filters the Block Editor settings to gather BuddyPress ones into a `bp` key.
  99   *
 100   * @since 6.0.0
 101   *
 102   * @param array $editor_settings Default editor settings.
 103   * @return array The editor settings including BP blocks specific ones.
 104   */
 105  function bp_blocks_editor_settings( $editor_settings = array() ) {
 106      /**
 107       * Filter here to include your BP Blocks specific settings.
 108       *
 109       * @since 6.0.0
 110       *
 111       * @param array $bp_editor_settings BP blocks specific editor settings.
 112       */
 113      $bp_editor_settings = (array) apply_filters( 'bp_blocks_editor_settings', array() );
 114  
 115      if ( $bp_editor_settings ) {
 116          $editor_settings['bp'] = $bp_editor_settings;
 117      }
 118  
 119      return $editor_settings;
 120  }
 121  
 122  /**
 123   * Select the right `block_editor_settings` filter according to WP version.
 124   *
 125   * @since 8.0.0
 126   */
 127  function bp_block_init_editor_settings_filter() {
 128      if ( function_exists( 'get_block_editor_settings' ) ) {
 129          add_filter( 'block_editor_settings_all', 'bp_blocks_editor_settings' );
 130      } else {
 131          add_filter( 'block_editor_settings', 'bp_blocks_editor_settings' );
 132      }
 133  }
 134  add_action( 'bp_init', 'bp_block_init_editor_settings_filter' );
 135  
 136  /**
 137   * Preload the Active BuddyPress Components.
 138   *
 139   * @since 9.0.0
 140   *
 141   * @param string[] $paths The Block Editors preload paths.
 142   * @return string[] The Block Editors preload paths.
 143   */
 144  function bp_blocks_preload_paths( $paths = array() ) {
 145      return array_merge(
 146          $paths,
 147          array(
 148              '/buddypress/v1/components?status=active',
 149          )
 150      );
 151  }
 152  add_filter( 'block_editor_rest_api_preload_paths', 'bp_blocks_preload_paths' );
 153  
 154  /**
 155   * Register a BuddyPress block type.
 156   *
 157   * @since 6.0.0
 158   *
 159   * @param array $args The registration arguments for the block type.
 160   * @return BP_Block   The BuddyPress block type object.
 161   */
 162  function bp_register_block( $args = array() ) {
 163      return new BP_Block( $args );
 164  }
 165  
 166  /**
 167   * Gets a Widget Block list of classnames.
 168   *
 169   * @since 9.0.0
 170   *
 171   * @param string $block_name The Block name.
 172   * @return array The list of widget classnames for the Block.
 173   */
 174  function bp_blocks_get_widget_block_classnames( $block_name = '' ) {
 175      $components         = bp_core_get_active_components( array(), 'objects' );
 176      $components['core'] = buddypress()->core;
 177      $classnames         = array();
 178  
 179      foreach ( $components as $component ) {
 180          if ( isset( $component->block_globals[ $block_name ] ) ) {
 181              $block_props = $component->block_globals[ $block_name ]->props;
 182  
 183              if ( isset( $block_props['widget_classnames'] ) && $block_props['widget_classnames'] ) {
 184                  $classnames = (array) $block_props['widget_classnames'];
 185                  break;
 186              }
 187          }
 188      }
 189  
 190      return $classnames;
 191  }
 192  
 193  /**
 194   * Make sure the BP Widget Block classnames are included into Widget Blocks.
 195   *
 196   * @since 9.0.0
 197   *
 198   * @param string $classname The classname to be used in the block widget's container HTML.
 199   * @param string $block_name The name of the block.
 200   * @return string The classname to be used in the block widget's container HTML.
 201   */
 202  function bp_widget_block_dynamic_classname( $classname, $block_name ) {
 203      $bp_classnames = bp_blocks_get_widget_block_classnames( $block_name );
 204  
 205      if ( $bp_classnames ) {
 206          $bp_classnames = array_map( 'sanitize_html_class', $bp_classnames );
 207          $classname    .= ' ' . implode( ' ', $bp_classnames );
 208      }
 209  
 210      return $classname;
 211  }
 212  add_filter( 'widget_block_dynamic_classname', 'bp_widget_block_dynamic_classname', 10, 2 );
 213  
 214  /**
 215   * Create a link to the registration form for use on the bottom of the login form widget.
 216   *
 217   * @since 9.0.0
 218   *
 219   * @param string $content Content to display. Default empty.
 220   * @param array  $args    Array of login form arguments.
 221   * @return string         HTML output.
 222   */
 223  function bp_blocks_get_login_widget_registration_link( $content = '', $args = array() ) {
 224      if ( isset( $args['form_id'] ) && 'bp-login-widget-form' === $args['form_id'] && bp_get_signup_allowed() ) {
 225          $content .= sprintf(
 226              '<p class="bp-login-widget-register-link"><a href="%1$s">%2$s</a></p>',
 227              esc_url( bp_get_signup_page() ),
 228              esc_html__( 'Register', 'buddypress' )
 229          );
 230      }
 231  
 232      $action_output = '';
 233      if ( has_action( 'bp_login_widget_form' ) ) {
 234          ob_start();
 235          /**
 236           * Fires inside the display of the login widget form.
 237           *
 238           * @since 2.4.0
 239           */
 240          do_action( 'bp_login_widget_form' );
 241          $action_output = ob_get_clean();
 242      }
 243  
 244      if ( $action_output ) {
 245          $content .= $action_output;
 246      }
 247  
 248      return $content;
 249  }
 250  
 251  /**
 252   * Callback function to render the BP Login Form.
 253   *
 254   * @since 9.0.0
 255   *
 256   * @param array $attributes The block attributes.
 257   * @return string           HTML output.
 258   */
 259  function bp_block_render_login_form_block( $attributes = array() ) {
 260      $block_args = wp_parse_args(
 261          $attributes,
 262          array(
 263              'title' => '',
 264          )
 265      );
 266  
 267      $title = $block_args['title'];
 268  
 269      $classnames         = 'widget_bp_core_login_widget buddypress widget';
 270      $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) );
 271  
 272      $widget_content = '';
 273  
 274      if ( $title ) {
 275          $widget_content .= sprintf(
 276              '<h2 class="widget-title">%s</h2>',
 277              esc_html( $title )
 278          );
 279      }
 280  
 281      if ( is_user_logged_in() ) {
 282          $action_output = '';
 283          if ( has_action( 'bp_before_login_widget_loggedin' ) ) {
 284              ob_start();
 285              /**
 286               * Fires before the display of widget content if logged in.
 287               *
 288               * @since 1.9.0
 289               */
 290              do_action( 'bp_before_login_widget_loggedin' );
 291              $action_output = ob_get_clean();
 292          }
 293  
 294          if ( $action_output ) {
 295              $widget_content .= $action_output;
 296          }
 297  
 298          $widget_content .= sprintf(
 299              '<div class="bp-login-widget-user-avatar">
 300                  <a href="%1$s">
 301                      %2$s
 302                  </a>
 303              </div>',
 304              bp_loggedin_user_domain(),
 305              bp_get_loggedin_user_avatar(
 306                  array(
 307                      'type'   => 'thumb',
 308                      'width'  => 50,
 309                      'height' => 50,
 310                  )
 311              )
 312          );
 313  
 314          $widget_content .= sprintf(
 315              '<div class="bp-login-widget-user-links">
 316                  <div class="bp-login-widget-user-link">%1$s</div>
 317                  <div class="bp-login-widget-user-logout"><a class="logout" href="%2$s">%3$s</a></div>
 318              </div>',
 319              bp_core_get_userlink( bp_loggedin_user_id() ),
 320              wp_logout_url( bp_get_requested_url() ),
 321              __( 'Log Out', 'buddypress' )
 322          );
 323  
 324          $action_output = '';
 325          if ( has_action( 'bp_after_login_widget_loggedin' ) ) {
 326              ob_start();
 327              /**
 328               * Fires after the display of widget content if logged in.
 329               *
 330               * @since 1.9.0
 331               */
 332              do_action( 'bp_after_login_widget_loggedin' );
 333              $action_output = ob_get_clean();
 334          }
 335  
 336          if ( $action_output ) {
 337              $widget_content .= $action_output;
 338          }
 339      } else {
 340          $action_output = '';
 341          if ( has_action( 'bp_before_login_widget_loggedout' ) ) {
 342              ob_start();
 343              /**
 344               * Fires before the display of widget content if logged out.
 345               *
 346               * @since 1.9.0
 347               */
 348              do_action( 'bp_before_login_widget_loggedout' );
 349              $action_output = ob_get_clean();
 350          }
 351  
 352          if ( $action_output ) {
 353              $widget_content .= $action_output;
 354          }
 355  
 356          add_filter( 'login_form_bottom', 'bp_blocks_get_login_widget_registration_link', 10, 2 );
 357  
 358          $widget_content .= wp_login_form(
 359              array(
 360                  'echo'           => false,
 361                  'form_id'        => 'bp-login-widget-form',
 362                  'id_username'    => 'bp-login-widget-user-login',
 363                  'label_username' => __( 'Username', 'buddypress' ),
 364                  'id_password'    => 'bp-login-widget-user-pass',
 365                  'label_password' => __( 'Password', 'buddypress' ),
 366                  'id_remember'    => 'bp-login-widget-rememberme',
 367                  'id_submit'      => 'bp-login-widget-submit',
 368              )
 369          );
 370  
 371          remove_filter( 'login_form_bottom', 'bp_blocks_get_login_widget_registration_link', 10, 2 );
 372  
 373          $action_output = '';
 374          if ( has_action( 'bp_after_login_widget_loggedout' ) ) {
 375              ob_start();
 376              /**
 377               * Fires after the display of widget content if logged out.
 378               *
 379               * @since 1.9.0
 380               */
 381              do_action( 'bp_after_login_widget_loggedout' );
 382              $action_output = ob_get_clean();
 383          }
 384  
 385          if ( $action_output ) {
 386              $widget_content .= $action_output;
 387          }
 388      }
 389  
 390      if ( ! did_action( 'dynamic_sidebar_before' ) ) {
 391          return sprintf(
 392              '<div %1$s>%2$s</div>',
 393              $wrapper_attributes,
 394              $widget_content
 395          );
 396      }
 397  
 398      return $widget_content;
 399  }


Generated: Tue Aug 3 01:01:41 2021 Cross-referenced by PHPXref 0.7.1