[ 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'] ) {
 225          if ( bp_get_signup_allowed() ) {
 226              $content .= sprintf(
 227                  '<p class="bp-login-widget-register-link"><a href="%1$s">%2$s</a></p>',
 228                  esc_url( bp_get_signup_page() ),
 229                  esc_html__( 'Register', 'buddypress' )
 230              );
 231          }
 232  
 233          if ( isset( $args['include_pwd_link'] ) && true === $args['include_pwd_link'] ) {
 234              $content .= sprintf(
 235                  '<p class="bp-login-widget-pwd-link"><a href="%1$s">%2$s</a></p>',
 236                  esc_url( wp_lostpassword_url( bp_get_root_domain() ) ),
 237                  esc_html__( 'Lost your password?', 'buddypress' )
 238              );
 239          }
 240      }
 241  
 242      $action_output = '';
 243      if ( has_action( 'bp_login_widget_form' ) ) {
 244          ob_start();
 245          /**
 246           * Fires inside the display of the login widget form.
 247           *
 248           * @since 2.4.0
 249           */
 250          do_action( 'bp_login_widget_form' );
 251          $action_output = ob_get_clean();
 252      }
 253  
 254      if ( $action_output ) {
 255          $content .= $action_output;
 256      }
 257  
 258      return $content;
 259  }
 260  
 261  /**
 262   * Callback function to render the BP Login Form.
 263   *
 264   * @since 9.0.0
 265   *
 266   * @param array $attributes The block attributes.
 267   * @return string           HTML output.
 268   */
 269  function bp_block_render_login_form_block( $attributes = array() ) {
 270      $block_args = bp_parse_args(
 271          $attributes,
 272          array(
 273              'title'         => '',
 274              'forgotPwdLink' => false,
 275          )
 276      );
 277  
 278      $title = $block_args['title'];
 279  
 280      $classnames         = 'widget_bp_core_login_widget buddypress widget';
 281      $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) );
 282  
 283      $widget_content = '';
 284  
 285      if ( $title ) {
 286          $widget_content .= sprintf(
 287              '<h2 class="widget-title">%s</h2>',
 288              esc_html( $title )
 289          );
 290      }
 291  
 292      if ( is_user_logged_in() ) {
 293          $action_output = '';
 294          if ( has_action( 'bp_before_login_widget_loggedin' ) ) {
 295              ob_start();
 296              /**
 297               * Fires before the display of widget content if logged in.
 298               *
 299               * @since 1.9.0
 300               */
 301              do_action( 'bp_before_login_widget_loggedin' );
 302              $action_output = ob_get_clean();
 303          }
 304  
 305          if ( $action_output ) {
 306              $widget_content .= $action_output;
 307          }
 308  
 309          $widget_content .= sprintf(
 310              '<div class="bp-login-widget-user-avatar">
 311                  <a href="%1$s">
 312                      %2$s
 313                  </a>
 314              </div>',
 315              bp_loggedin_user_domain(),
 316              bp_get_loggedin_user_avatar(
 317                  array(
 318                      'type'   => 'thumb',
 319                      'width'  => 50,
 320                      'height' => 50,
 321                  )
 322              )
 323          );
 324  
 325          $widget_content .= sprintf(
 326              '<div class="bp-login-widget-user-links">
 327                  <div class="bp-login-widget-user-link">%1$s</div>
 328                  <div class="bp-login-widget-user-logout"><a class="logout" href="%2$s">%3$s</a></div>
 329              </div>',
 330              bp_core_get_userlink( bp_loggedin_user_id() ),
 331              wp_logout_url( bp_get_requested_url() ),
 332              __( 'Log Out', 'buddypress' )
 333          );
 334  
 335          $action_output = '';
 336          if ( has_action( 'bp_after_login_widget_loggedin' ) ) {
 337              ob_start();
 338              /**
 339               * Fires after the display of widget content if logged in.
 340               *
 341               * @since 1.9.0
 342               */
 343              do_action( 'bp_after_login_widget_loggedin' );
 344              $action_output = ob_get_clean();
 345          }
 346  
 347          if ( $action_output ) {
 348              $widget_content .= $action_output;
 349          }
 350      } else {
 351          $action_output = '';
 352          $pwd_link      = (bool) $block_args['forgotPwdLink'];
 353  
 354          if ( has_action( 'bp_before_login_widget_loggedout' ) ) {
 355              ob_start();
 356              /**
 357               * Fires before the display of widget content if logged out.
 358               *
 359               * @since 1.9.0
 360               */
 361              do_action( 'bp_before_login_widget_loggedout' );
 362              $action_output = ob_get_clean();
 363          }
 364  
 365          if ( $action_output ) {
 366              $widget_content .= $action_output;
 367          }
 368  
 369          add_filter( 'login_form_bottom', 'bp_blocks_get_login_widget_registration_link', 10, 2 );
 370  
 371          $widget_content .= wp_login_form(
 372              array(
 373                  'echo'             => false,
 374                  'form_id'          => 'bp-login-widget-form',
 375                  'id_username'      => 'bp-login-widget-user-login',
 376                  'label_username'   => __( 'Username', 'buddypress' ),
 377                  'id_password'      => 'bp-login-widget-user-pass',
 378                  'label_password'   => __( 'Password', 'buddypress' ),
 379                  'id_remember'      => 'bp-login-widget-rememberme',
 380                  'id_submit'        => 'bp-login-widget-submit',
 381                  'include_pwd_link' => $pwd_link,
 382              )
 383          );
 384  
 385          remove_filter( 'login_form_bottom', 'bp_blocks_get_login_widget_registration_link', 10, 2 );
 386  
 387          $action_output = '';
 388          if ( has_action( 'bp_after_login_widget_loggedout' ) ) {
 389              ob_start();
 390              /**
 391               * Fires after the display of widget content if logged out.
 392               *
 393               * @since 1.9.0
 394               */
 395              do_action( 'bp_after_login_widget_loggedout' );
 396              $action_output = ob_get_clean();
 397          }
 398  
 399          if ( $action_output ) {
 400              $widget_content .= $action_output;
 401          }
 402      }
 403  
 404      if ( ! did_action( 'dynamic_sidebar_before' ) ) {
 405          return sprintf(
 406              '<div %1$s>%2$s</div>',
 407              $wrapper_attributes,
 408              $widget_content
 409          );
 410      }
 411  
 412      return $widget_content;
 413  }


Generated: Thu Oct 21 01:00:54 2021 Cross-referenced by PHPXref 0.7.1