[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/blocks/ -> index.php (source)

   1  <?php
   2  /**
   3   * Used to set up all core blocks used with the block editor.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  define( 'BLOCKS_PATH', ABSPATH . WPINC . '/blocks/' );
   9  
  10  // Include files required for core blocks registration.
  11  require  BLOCKS_PATH . 'legacy-widget.php';
  12  require  BLOCKS_PATH . 'widget-group.php';
  13  require  BLOCKS_PATH . 'require-dynamic-blocks.php';
  14  
  15  /**
  16   * Registers core block style handles.
  17   *
  18   * While {@see register_block_style_handle()} is typically used for that, the way it is
  19   * implemented is inefficient for core block styles. Registering those style handles here
  20   * avoids unnecessary logic and filesystem lookups in the other function.
  21   *
  22   * @since 6.3.0
  23   *
  24   * @global string $wp_version The WordPress version string.
  25   */
  26  function register_core_block_style_handles() {
  27      global $wp_version;
  28  
  29      if ( ! wp_should_load_separate_core_block_assets() ) {
  30          return;
  31      }
  32  
  33      $blocks_url   = includes_url( 'blocks/' );
  34      $suffix       = wp_scripts_get_suffix();
  35      $wp_styles    = wp_styles();
  36      $style_fields = array(
  37          'style'       => 'style',
  38          'editorStyle' => 'editor',
  39      );
  40  
  41      static $core_blocks_meta;
  42      if ( ! $core_blocks_meta ) {
  43          $core_blocks_meta = require  BLOCKS_PATH . 'blocks-json.php';
  44      }
  45  
  46      $files          = false;
  47      $transient_name = 'wp_core_block_css_files';
  48  
  49      /*
  50       * Ignore transient cache when the development mode is set to 'core'. Why? To avoid interfering with
  51       * the core developer's workflow.
  52       */
  53      $can_use_cached = ! wp_is_development_mode( 'core' );
  54  
  55      if ( $can_use_cached ) {
  56          $cached_files = get_transient( $transient_name );
  57  
  58          // Check the validity of cached values by checking against the current WordPress version.
  59          if (
  60              is_array( $cached_files )
  61              && isset( $cached_files['version'] )
  62              && $cached_files['version'] === $wp_version
  63              && isset( $cached_files['files'] )
  64          ) {
  65              $files = $cached_files['files'];
  66          }
  67      }
  68  
  69      if ( ! $files ) {
  70          $files = glob( wp_normalize_path( BLOCKS_PATH . '**/**.css' ) );
  71  
  72          // Normalize BLOCKS_PATH prior to substitution for Windows environments.
  73          $normalized_blocks_path = wp_normalize_path( BLOCKS_PATH );
  74  
  75          $files = array_map(
  76              static function ( $file ) use ( $normalized_blocks_path ) {
  77                  return str_replace( $normalized_blocks_path, '', $file );
  78              },
  79              $files
  80          );
  81  
  82          // Save core block style paths in cache when not in development mode.
  83          if ( $can_use_cached ) {
  84              set_transient(
  85                  $transient_name,
  86                  array(
  87                      'version' => $wp_version,
  88                      'files'   => $files,
  89                  )
  90              );
  91          }
  92      }
  93  
  94      $register_style = static function ( $name, $filename, $style_handle ) use ( $blocks_url, $suffix, $wp_styles, $files ) {
  95          $style_path = "{$name}/{$filename}{$suffix}.css";
  96          $path       = wp_normalize_path( BLOCKS_PATH . $style_path );
  97  
  98          if ( ! in_array( $style_path, $files, true ) ) {
  99              $wp_styles->add(
 100                  $style_handle,
 101                  false
 102              );
 103              return;
 104          }
 105  
 106          $wp_styles->add( $style_handle, $blocks_url . $style_path );
 107          $wp_styles->add_data( $style_handle, 'path', $path );
 108  
 109          $rtl_file = "{$name}/{$filename}-rtl{$suffix}.css";
 110          if ( is_rtl() && in_array( $rtl_file, $files, true ) ) {
 111              $wp_styles->add_data( $style_handle, 'rtl', 'replace' );
 112              $wp_styles->add_data( $style_handle, 'suffix', $suffix );
 113              $wp_styles->add_data( $style_handle, 'path', str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $path ) );
 114          }
 115      };
 116  
 117      foreach ( $core_blocks_meta as $name => $schema ) {
 118          /** This filter is documented in wp-includes/blocks.php */
 119          $schema = apply_filters( 'block_type_metadata', $schema );
 120  
 121          // Backfill these properties similar to `register_block_type_from_metadata()`.
 122          if ( ! isset( $schema['style'] ) ) {
 123              $schema['style'] = "wp-block-{$name}";
 124          }
 125          if ( ! isset( $schema['editorStyle'] ) ) {
 126              $schema['editorStyle'] = "wp-block-{$name}-editor";
 127          }
 128  
 129          // Register block theme styles.
 130          $register_style( $name, 'theme', "wp-block-{$name}-theme" );
 131  
 132          foreach ( $style_fields as $style_field => $filename ) {
 133              $style_handle = $schema[ $style_field ];
 134              if ( is_array( $style_handle ) ) {
 135                  continue;
 136              }
 137              $register_style( $name, $filename, $style_handle );
 138          }
 139      }
 140  }
 141  add_action( 'init', 'register_core_block_style_handles', 9 );
 142  
 143  /**
 144   * Registers core block types using metadata files.
 145   * Dynamic core blocks are registered separately.
 146   *
 147   * @since 5.5.0
 148   */
 149  function register_core_block_types_from_metadata() {
 150      $block_folders = require  BLOCKS_PATH . 'require-static-blocks.php';
 151      foreach ( $block_folders as $block_folder ) {
 152          register_block_type_from_metadata(
 153              BLOCKS_PATH . $block_folder
 154          );
 155      }
 156  }
 157  add_action( 'init', 'register_core_block_types_from_metadata' );


Generated : Fri Apr 26 08:20:02 2024 Cross-referenced by PHPXref