[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-core/classes/ -> class-bp-block.php (source)

   1  <?php
   2  /**
   3   * BP Block class.
   4   *
   5   * @package BuddyPress
   6   * @subpackage Core
   7   * @since 6.0.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  if ( ! defined( 'ABSPATH' ) ) {
  12      exit;
  13  }
  14  
  15  /**
  16   * BP Block Class.
  17   *
  18   * @since 6.0.0
  19   */
  20  class BP_Block {
  21      /**
  22       * WP Block Type object.
  23       *
  24       * @since 6.0.0
  25       * @var WP_Block_Type|WP_Error
  26       */
  27      public $block;
  28  
  29      /**
  30       * The script types registered.
  31       *
  32       * @since 6.0.0
  33       * @var array
  34       */
  35      private $registered_scripts;
  36  
  37      /**
  38       * The style types registered.
  39       *
  40       * @since 6.0.0
  41       * @var array
  42       */
  43      private $registered_styles;
  44  
  45      /**
  46       * Construct the BuddyPress Block.
  47       *
  48       * @since 6.0.0
  49       *
  50       * @param array $args {
  51       *     The registration arguments for the BP Block. Part of the arguments are the ones
  52       *     used by `WP_Block_Type`. Below are BP specific arguments.
  53       *
  54       *     @type string $name               The name of the block (eg: `bp/member`).
  55       *     @type string $editor_script_url  URL to the JavaScript main file of the BP Block
  56       *                                      to load into the Block Editor.
  57       *     @type array  $editor_script_deps The list of JavaScript dependency handles for the
  58       *                                      BP Block main file.
  59       *     @type string $script_url         URL to the JavaScript file to load into the Block
  60       *                                      Editor and on front-end.
  61       *     @type array  $script_deps        The list of JavaScript dependency handles for the
  62       *                                      JavaScript file to load into the Block Editor and
  63       *                                      on front-end.
  64       *     @type string $editor_style_url   URL to the CSS main file of the BP Block to load
  65       *                                      into the Block Editor.
  66       *     @type array  $editor_style_deps  The list of CSS dependency handles for the
  67       *                                      CSS main file.
  68       *     @type string $style_url          URL to the CSS file to load into the Block Editor
  69       *                                      and on front-end.
  70       *     @type array  $style_deps         The list of CSS dependency handles for the CSS file
  71       *                                      to load into the Block Editor and on front-end.
  72       * }
  73       */
  74  	public function __construct( $args ) {
  75          if ( ! did_action( 'bp_blocks_init' ) ) {
  76              _doing_it_wrong( __METHOD__, esc_html__( 'BP Blocks needs to be registered hooking `bp_blocks_init`', 'buddypress' ), '6.0.0' );
  77          }
  78  
  79          $min     = bp_core_get_minified_asset_suffix();
  80          $wp_args = array_intersect_key(
  81              $args,
  82              array(
  83                  'name'            => '',
  84                  'render_callback' => '',
  85                  'attributes'      => '',
  86                  'editor_script'   => '',
  87                  'script'          => '',
  88                  'editor_style'    => '',
  89                  'style'           => '',
  90              )
  91          );
  92  
  93          if ( ! isset( $wp_args['name'] ) || ! $wp_args['name'] || ! isset( $wp_args['editor_script'] ) || ! $wp_args['editor_script'] ) {
  94              $this->block = new WP_Error( 'missing_parameters', __( 'The `name` or `editor_script` required keys are missing.', 'buddypress' ) );
  95          } else {
  96              // Get specific BP Blocks arguments.
  97              $bp_args = array_intersect_key(
  98                  $args,
  99                  array(
 100                      'editor_script_url'  => '',
 101                      'editor_script_deps' => array(),
 102                      'script_url'         => '',
 103                      'script_deps'        => array(),
 104                      'editor_style_url'   => '',
 105                      'editor_style_deps'  => array(),
 106                      'style_url'          => '',
 107                      'style_deps'         => array(),
 108                  )
 109              );
 110  
 111              // Register the scripts.
 112              $version                  = bp_get_version();
 113              $this->registered_scripts = array();
 114  
 115              foreach ( array( 'editor_script', 'script' ) as $script_handle_key ) {
 116                  if ( ! isset( $wp_args[ $script_handle_key ] ) || ! $wp_args[ $script_handle_key ] ) {
 117                      continue;
 118                  }
 119  
 120                  if ( ! isset( $bp_args[ $script_handle_key . '_url' ] ) || ! $bp_args[ $script_handle_key . '_url' ] ) {
 121                      continue;
 122                  }
 123  
 124                  $deps = array();
 125                  if ( isset( $bp_args[ $script_handle_key . '_deps' ] ) && is_array( $bp_args[ $script_handle_key . '_deps' ] ) ) {
 126                      $deps = $bp_args[ $script_handle_key . '_deps' ];
 127                  }
 128  
 129                  $this->registered_scripts[ $script_handle_key ] = wp_register_script(
 130                      $wp_args[ $script_handle_key ],
 131                      $bp_args[ $script_handle_key . '_url' ],
 132                      $deps,
 133                      $version,
 134                      true
 135                  );
 136              }
 137  
 138              if ( ! isset( $this->registered_scripts['editor_script'] ) || ! $this->registered_scripts['editor_script'] ) {
 139                  $this->block = new WP_Error( 'script_registration_error', __( 'The required `editor_script` could not be registered.', 'buddypress' ) );
 140              } else {
 141                  // Register the styles.
 142                  $registered_styles = array();
 143  
 144                  foreach ( array( 'editor_style', 'style' ) as $style_handle_key ) {
 145                      if ( ! isset( $wp_args[ $style_handle_key ] ) || ! $wp_args[ $style_handle_key ] ) {
 146                          continue;
 147                      }
 148  
 149                      if ( ! isset( $bp_args[ $style_handle_key . '_url' ] ) || ! $bp_args[ $style_handle_key . '_url' ] ) {
 150                          continue;
 151                      }
 152  
 153                      if ( $min ) {
 154                          $minified_css  = str_replace( '.css', $min . '.css', $bp_args[ $style_handle_key . '_url' ] );
 155                          $css_file_path = str_replace( content_url(), WP_CONTENT_DIR, $minified_css );
 156  
 157                          if ( file_exists( $css_file_path ) ) {
 158                              $bp_args[ $style_handle_key . '_url' ] = $minified_css;
 159                          }
 160                      }
 161  
 162                      $deps = array();
 163                      if ( isset( $bp_args[ $style_handle_key . '_deps' ] ) && is_array( $bp_args[ $style_handle_key . '_deps' ] ) ) {
 164                          $deps = $bp_args[ $style_handle_key . '_deps' ];
 165                      }
 166  
 167                      $this->registered_styles[ $style_handle_key ] = wp_register_style(
 168                          $wp_args[ $style_handle_key ],
 169                          $bp_args[ $style_handle_key . '_url' ],
 170                          $deps,
 171                          $version
 172                      );
 173  
 174                      wp_style_add_data( $wp_args[ $style_handle_key ], 'rtl', 'replace' );
 175                      if ( $min ) {
 176                          wp_style_add_data( $wp_args[ $style_handle_key ], 'suffix', $min );
 177                      }
 178                  }
 179  
 180                  $name = $wp_args['name'];
 181                  unset( $wp_args['name'] );
 182  
 183                  // Set the Block Type.
 184                  $this->block = new WP_Block_Type( $name, $wp_args );
 185  
 186                  // Register the Block Type.
 187                  register_block_type( $this->block );
 188  
 189                  // Load Block translations if found.
 190                  if ( $this->block->editor_script ) {
 191                      /**
 192                       * Filter here to use a custom directory to look for the JSON translation file into.
 193                       *
 194                       * @since 6.0.0
 195                       *
 196                       * @param string $value         Absolute path to the directory to look for the JSON translation file into.
 197                       * @param string $editor_script The editor's script handle.
 198                       * @param string $name          The block's name.
 199                       */
 200                      $translation_dir = apply_filters( 'bp_block_translation_dir', null, $this->block->editor_script, $name );
 201  
 202                      /**
 203                       * Filter here to use a custom domain for the JSON translation file.
 204                       *
 205                       * @since 6.0.0
 206                       *
 207                       * @param string $value         The custom domain for the JSON translation file.
 208                       * @param string $editor_script The editor's script handle.
 209                       * @param string $name          The block's name.
 210                       */
 211                      $translation_domain = apply_filters( 'bp_block_translation_domain', 'buddypress', $this->block->editor_script, $name );
 212  
 213                      // Try to load the translation.
 214                      $translated = wp_set_script_translations( $this->block->editor_script, $translation_domain, $translation_dir );
 215                  }
 216              }
 217          }
 218      }
 219  }


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