WP_Theme_JSON::get_styles_for_block() – Gets the CSS rules for a particular block from theme.json.

You appear to be a bot. Output may be restricted

Description

Gets the CSS rules for a particular block from theme.json.

Usage

$string = WP_Theme_JSON::get_styles_for_block( $block_metadata );

Parameters

$block_metadata
( array ) required – Metadata about the block to get styles for.

Returns

string Styles for the block.

Source

File name: wordpress/wp-includes/class-wp-theme-json.php


Lines:

1 to 100 of 134
  public function get_styles_for_block( $block_metadata ) {
    $node             = _wp_array_get( $this->theme_json, $block_metadata['path'], array() );
    $use_root_padding = isset( $this->theme_json['settings']['useRootPaddingAwareAlignments'] ) && true === $this->theme_json['settings']['useRootPaddingAwareAlignments'];
    $selector         = $block_metadata['selector'];
    $settings         = _wp_array_get( $this->theme_json, array( 'settings' ) );

    /*
		 * Process style declarations for block support features the current
		 * block contains selectors for. Values for a feature with a custom
		 * selector are filtered from the theme.json node before it is
		 * processed as normal.
		*/
    $feature_declarations = array();

    if ( ! empty( $block_metadata['features'] ) ) {
      foreach ( $block_metadata['features'] as $feature_name => $feature_selector ) {
        if ( ! empty( $node[ $feature_name ] ) ) {
          // Create temporary node containing only the feature data
          // to leverage existing `compute_style_properties` function.
          $feature = array( $feature_name => $node[ $feature_name ] );
          // Generate the feature's declarations only.
          $new_feature_declarations = static::compute_style_properties( $feature, $settings, null, $this->theme_json );

          // Merge new declarations with any that already exist for
          // the feature selector. This may occur when multiple block
          // support features use the same custom selector.
          if ( isset( $feature_declarations[ $feature_selector ] ) ) {
            foreach ( $new_feature_declarations as $new_feature_declaration ) {
              $feature_declarations[ $feature_selector ][] = $feature_declaration;
            }
          } else {
            $feature_declarations[ $feature_selector ] = $new_feature_declarations;
          }

          // Remove the feature from the block's node now the
          // styles will be included under the feature level selector.
          unset( $node[ $feature_name ] );
        }
      }
    }

    /*
		 * Get a reference to element name from path.
		 * $block_metadata['path'] = array( 'styles','elements','link' );
		 * Make sure that $block_metadata['path'] describes an element node, like [ 'styles', 'element', 'link' ].
		 * Skip non-element paths like just ['styles'].
		 */
    $is_processing_element = in_array( 'elements', $block_metadata['path'], true );

    $current_element = $is_processing_element ? $block_metadata['path'][ count( $block_metadata['path'] ) - 1 ] : null;

    $element_pseudo_allowed = array();

    // TODO: Replace array_key_exists() with isset() check once WordPress drops
    // support for PHP 5.6. See https://core.trac.wordpress.org/ticket/57067.
    if ( array_key_exists( $current_element, static::VALID_ELEMENT_PSEUDO_SELECTORS ) ) {
      $element_pseudo_allowed = static::VALID_ELEMENT_PSEUDO_SELECTORS[ $current_element ];
    }

    /*
		 * Check for allowed pseudo classes (e.g. ":hover") from the $selector ("a:hover").
		 * This also resets the array keys.
		 */
    $pseudo_matches = array_values(
      array_filter(
        $element_pseudo_allowed,
        function( $pseudo_selector ) use ( $selector ) {
          return str_contains( $selector, $pseudo_selector );
        }
      )
    );

    $pseudo_selector = isset( $pseudo_matches[0] ) ? $pseudo_matches[0] : null;

    /*
		 * If the current selector is a pseudo selector that's defined in the allow list for the current
		 * element then compute the style properties for it.
		 * Otherwise just compute the styles for the default selector as normal.
		 */
    if ( $pseudo_selector && isset( $node[ $pseudo_selector ] ) &&
      // TODO: Replace array_key_exists() with isset() check once WordPress drops
      // support for PHP 5.6. See https://core.trac.wordpress.org/ticket/57067.
      array_key_exists( $current_element, static::VALID_ELEMENT_PSEUDO_SELECTORS )
      && in_array( $pseudo_selector, static::VALID_ELEMENT_PSEUDO_SELECTORS[ $current_element ], true )
    ) {
      $declarations = static::compute_style_properties( $node[ $pseudo_selector ], $settings, null, $this->theme_json, $selector, $use_root_padding );
    } else {
      $declarations = static::compute_style_properties( $node, $settings, null, $this->theme_json, $selector, $use_root_padding );
    }

    $block_rules = '';

    /*
		 * 1. Separate the declarations that use the general selector
		 * from the ones using the duotone selector.
		 */
    $declarations_duotone = array();
    foreach ( $declarations as $index => $declaration ) {
      if ( 'filter' === $declaration['name'] ) {
        unset( $declarations[ $index ] );

 View on GitHub View on Trac