[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/block-supports/ -> settings.php (source)

   1  <?php
   2  /**
   3   * Block level presets support.
   4   *
   5   * @package WordPress
   6   * @since 6.2.0
   7   */
   8  
   9  /**
  10   * Get the class name used on block level presets.
  11   *
  12   * @internal
  13   *
  14   * @since 6.2.0
  15   * @access private
  16   *
  17   * @param array $block Block object.
  18   * @return string      The unique class name.
  19   */
  20  function _wp_get_presets_class_name( $block ) {
  21      return 'wp-settings-' . md5( serialize( $block ) );
  22  }
  23  
  24  /**
  25   * Update the block content with block level presets class name.
  26   *
  27   * @internal
  28   *
  29   * @since 6.2.0
  30   * @access private
  31   *
  32   * @param  string $block_content Rendered block content.
  33   * @param  array  $block         Block object.
  34   * @return string                Filtered block content.
  35   */
  36  function _wp_add_block_level_presets_class( $block_content, $block ) {
  37      if ( ! $block_content ) {
  38          return $block_content;
  39      }
  40  
  41      // return early if the block doesn't have support for settings.
  42      $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
  43      if ( ! block_has_support( $block_type, '__experimentalSettings', false ) ) {
  44          return $block_content;
  45      }
  46  
  47      // return early if no settings are found on the block attributes.
  48      $block_settings = $block['attrs']['settings'] ?? null;
  49      if ( empty( $block_settings ) ) {
  50          return $block_content;
  51      }
  52  
  53      // Like the layout hook this assumes the hook only applies to blocks with a single wrapper.
  54      // Add the class name to the first element, presuming it's the wrapper, if it exists.
  55      $tags = new WP_HTML_Tag_Processor( $block_content );
  56      if ( $tags->next_tag() ) {
  57          $tags->add_class( _wp_get_presets_class_name( $block ) );
  58      }
  59  
  60      return $tags->get_updated_html();
  61  }
  62  
  63  /**
  64   * Render the block level presets stylesheet.
  65   *
  66   * @internal
  67   *
  68   * @since 6.2.0
  69   * @since 6.3.0 Updated preset styles to use Selectors API.
  70   * @access private
  71   *
  72   * @param string|null $pre_render   The pre-rendered content. Default null.
  73   * @param array       $block The block being rendered.
  74   * @return null
  75   */
  76  function _wp_add_block_level_preset_styles( $pre_render, $block ) {
  77      // Return early if the block has not support for descendent block styles.
  78      $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
  79      if ( ! block_has_support( $block_type, '__experimentalSettings', false ) ) {
  80          return null;
  81      }
  82  
  83      // return early if no settings are found on the block attributes.
  84      $block_settings = $block['attrs']['settings'] ?? null;
  85      if ( empty( $block_settings ) ) {
  86          return null;
  87      }
  88  
  89      $class_name = '.' . _wp_get_presets_class_name( $block );
  90  
  91      // the root selector for preset variables needs to target every possible block selector
  92      // in order for the general setting to override any bock specific setting of a parent block or
  93      // the site root.
  94      $variables_root_selector = '*,[class*="wp-block"]';
  95      $registry                = WP_Block_Type_Registry::get_instance();
  96      $blocks                  = $registry->get_all_registered();
  97      foreach ( $blocks as $block_type ) {
  98          /*
  99           * We only want to append selectors for blocks using custom selectors
 100           * i.e. not `wp-block-<name>`.
 101           */
 102          $has_custom_selector =
 103              ( isset( $block_type->supports['__experimentalSelector'] ) && is_string( $block_type->supports['__experimentalSelector'] ) ) ||
 104              ( isset( $block_type->selectors['root'] ) && is_string( $block_type->selectors['root'] ) );
 105  
 106          if ( $has_custom_selector ) {
 107              $variables_root_selector .= ',' . wp_get_block_css_selector( $block_type );
 108          }
 109      }
 110      $variables_root_selector = WP_Theme_JSON::scope_selector( $class_name, $variables_root_selector );
 111  
 112      // Remove any potentially unsafe styles.
 113      $theme_json_shape  = WP_Theme_JSON::remove_insecure_properties(
 114          array(
 115              'version'  => WP_Theme_JSON::LATEST_SCHEMA,
 116              'settings' => $block_settings,
 117          )
 118      );
 119      $theme_json_object = new WP_Theme_JSON( $theme_json_shape );
 120  
 121      $styles = '';
 122  
 123      // include preset css variables declaration on the stylesheet.
 124      $styles .= $theme_json_object->get_stylesheet(
 125          array( 'variables' ),
 126          null,
 127          array(
 128              'root_selector' => $variables_root_selector,
 129              'scope'         => $class_name,
 130          )
 131      );
 132  
 133      // include preset css classes on the stylesheet.
 134      $styles .= $theme_json_object->get_stylesheet(
 135          array( 'presets' ),
 136          null,
 137          array(
 138              'root_selector' => $class_name . ',' . $class_name . ' *',
 139              'scope'         => $class_name,
 140          )
 141      );
 142  
 143      if ( ! empty( $styles ) ) {
 144          wp_enqueue_block_support_styles( $styles );
 145      }
 146  
 147      return null;
 148  }
 149  
 150  add_filter( 'render_block', '_wp_add_block_level_presets_class', 10, 2 );
 151  add_filter( 'pre_render_block', '_wp_add_block_level_preset_styles', 10, 2 );


Generated : Wed Aug 19 08:20:25 2026 Cross-referenced by PHPXref