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


Generated: Thu Nov 21 01:00:57 2024 Cross-referenced by PHPXref 0.7.1