[ 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 The registration arguments for the BP Block.
  51       */
  52  	public function __construct( $args ) {
  53          if ( ! did_action( 'bp_blocks_init' ) ) {
  54              _doing_it_wrong( __METHOD__, esc_html__( 'BP Blocks needs to be registered hooking `bp_blocks_init`', 'buddypress' ), '6.0.0' );
  55          }
  56  
  57          $min     = bp_core_get_minified_asset_suffix();
  58          $wp_args = array_intersect_key(
  59              $args,
  60              array(
  61                  'name'            => '',
  62                  'render_callback' => '',
  63                  'attributes'      => '',
  64                  'editor_script'   => '',
  65                  'script'          => '',
  66                  'editor_style'    => '',
  67                  'style'           => '',
  68              )
  69          );
  70  
  71          if ( ! isset( $wp_args['name'] ) || ! $wp_args['name'] || ! isset( $wp_args['editor_script'] ) || ! $wp_args['editor_script'] ) {
  72              $this->block = new WP_Error( 'missing_parameters', __( 'The `name` or `editor_script` required keys are missing.', 'buddypress' ) );
  73          } else {
  74              // Get specific BP Blocks arguments.
  75              $bp_args = array_intersect_key(
  76                  $args,
  77                  array(
  78                      'editor_script_url'  => '',
  79                      'editor_script_deps' => array(),
  80                      'script_url'         => '',
  81                      'script_deps'        => array(),
  82                      'editor_style_url'   => '',
  83                      'editor_style_deps'  => array(),
  84                      'style_url'          => '',
  85                      'style_deps'         => array(),
  86                  )
  87              );
  88  
  89              // Register the scripts.
  90              $version                  = bp_get_version();
  91              $this->registered_scripts = array();
  92  
  93              foreach ( array( 'editor_script', 'script' ) as $script_handle_key ) {
  94                  if ( ! isset( $wp_args[ $script_handle_key ] ) || ! $wp_args[ $script_handle_key ] ) {
  95                      continue;
  96                  }
  97  
  98                  if ( ! isset( $bp_args[ $script_handle_key . '_url' ] ) || ! $bp_args[ $script_handle_key . '_url' ] ) {
  99                      continue;
 100                  }
 101  
 102                  $deps = array();
 103                  if ( isset( $bp_args[ $script_handle_key . '_deps' ] ) && is_array( $bp_args[ $script_handle_key . '_deps' ] ) ) {
 104                      $deps = $bp_args[ $script_handle_key . '_deps' ];
 105                  }
 106  
 107                  $this->registered_scripts[ $script_handle_key ] = wp_register_script(
 108                      $wp_args[ $script_handle_key ],
 109                      $bp_args[ $script_handle_key . '_url' ],
 110                      $deps,
 111                      $version,
 112                      true
 113                  );
 114              }
 115  
 116              if ( ! isset( $this->registered_scripts['editor_script'] ) || ! $this->registered_scripts['editor_script'] ) {
 117                  $this->block = new WP_Error( 'script_registration_error', __( 'The required `editor_script` could not be registered.', 'buddypress' ) );
 118              } else {
 119                  // Register the styles.
 120                  $registered_styles = array();
 121  
 122                  foreach ( array( 'editor_style', 'style' ) as $style_handle_key ) {
 123                      if ( ! isset( $wp_args[ $style_handle_key ] ) || ! $wp_args[ $style_handle_key ] ) {
 124                          continue;
 125                      }
 126  
 127                      if ( ! isset( $bp_args[ $style_handle_key . '_url' ] ) || ! $bp_args[ $style_handle_key . '_url' ] ) {
 128                          continue;
 129                      }
 130  
 131                      if ( $min ) {
 132                          $minified_css  = str_replace( '.css', $min . '.css', $bp_args[ $style_handle_key . '_url' ] );
 133                          $css_file_path = str_replace( content_url(), WP_CONTENT_DIR, $minified_css );
 134  
 135                          if ( file_exists( $css_file_path ) ) {
 136                              $bp_args[ $style_handle_key . '_url' ] = $minified_css;
 137                          }
 138                      }
 139  
 140                      $deps = array();
 141                      if ( isset( $bp_args[ $style_handle_key . '_deps' ] ) && is_array( $bp_args[ $style_handle_key . '_deps' ] ) ) {
 142                          $deps = $bp_args[ $style_handle_key . '_deps' ];
 143                      }
 144  
 145                      $this->registered_styles[ $style_handle_key ] = wp_register_style(
 146                          $wp_args[ $style_handle_key ],
 147                          $bp_args[ $style_handle_key . '_url' ],
 148                          $deps,
 149                          $version
 150                      );
 151  
 152                      wp_style_add_data( $wp_args[ $style_handle_key ], 'rtl', 'replace' );
 153                      if ( $min ) {
 154                          wp_style_add_data( $wp_args[ $style_handle_key ], 'suffix', $min );
 155                      }
 156                  }
 157  
 158                  $name = $wp_args['name'];
 159                  unset( $wp_args['name'] );
 160  
 161                  // Set the Block Type.
 162                  $this->block = new WP_Block_Type( $name, $wp_args );
 163  
 164                  // Register the Block Type.
 165                  register_block_type( $this->block );
 166  
 167                  // Load Block translations if found.
 168                  if ( $this->block->editor_script ) {
 169                      /**
 170                       * Filter here to use a custom directory to look for the JSON translation file into.
 171                       *
 172                       * @since 6.0.0
 173                       *
 174                       * @param string $value         Absolute path to the directory to look for the JSON translation file into.
 175                       * @param string $editor_script The editor's script handle.
 176                       * @param string $name          The block's name.
 177                       */
 178                      $translation_dir = apply_filters( 'bp_block_translation_dir', null, $this->block->editor_script, $name );
 179  
 180                      /**
 181                       * Filter here to use a custom domain for the JSON translation file.
 182                       *
 183                       * @since 6.0.0
 184                       *
 185                       * @param string $value         The custom domain for the JSON translation file.
 186                       * @param string $editor_script The editor's script handle.
 187                       * @param string $name          The block's name.
 188                       */
 189                      $translation_domain = apply_filters( 'bp_block_translation_domain', 'buddypress', $this->block->editor_script, $name );
 190  
 191                      // Try to load the translation.
 192                      $translated = wp_set_script_translations( $this->block->editor_script, $translation_domain, $translation_dir );
 193                  }
 194              }
 195          }
 196      }
 197  }


Generated: Sun Jul 12 01:01:53 2020 Cross-referenced by PHPXref 0.7.1