styles_for_block_core_search() – Builds an array of inline styles for the search block.

You appear to be a bot. Output may be restricted

Description

Builds an array of inline styles for the search block.

The result will contain one entry for shared styles such as those for the inner input or button and a second for the inner wrapper should the block be positioning the button "inside".

Usage

$array = styles_for_block_core_search( $attributes );

Parameters

$attributes
( array ) required – The block attributes.

Returns

array Style HTML attribute.

Source

File name: wordpress/wp-includes/blocks/search.php


Lines:

1 to 100 of 125
function styles_for_block_core_search( $attributes ) {
  $wrapper_styles   = array();
  $button_styles    = array();
  $input_styles     = array();
  $is_button_inside = ! empty( $attributes['buttonPosition'] ) &&
    'button-inside' === $attributes['buttonPosition'];

  // Add width styles.
  $has_width   = ! empty( $attributes['width'] ) && ! empty( $attributes['widthUnit'] );
  $button_only = ! empty( $attributes['buttonPosition'] ) && 'button-only' === $attributes['buttonPosition'];

  if ( $has_width && ! $button_only ) {
    $wrapper_styles[] = sprintf(
      'width: %d%s;',
      esc_attr( $attributes['width'] ),
      esc_attr( $attributes['widthUnit'] )
    );
  }

  // Add border width styles.
  $has_border_width = ! empty( $attributes['style']['border']['width'] );

  if ( $has_border_width ) {
    $border_width = $attributes['style']['border']['width'];

    if ( $is_button_inside ) {
      $wrapper_styles[] = sprintf( 'border-width: %s;', esc_attr( $border_width ) );
    } else {
      $button_styles[] = sprintf( 'border-width: %s;', esc_attr( $border_width ) );
      $input_styles[]  = sprintf( 'border-width: %s;', esc_attr( $border_width ) );
    }
  }

  // Add border radius styles.
  $has_border_radius = ! empty( $attributes['style']['border']['radius'] );

  if ( $has_border_radius ) {
    $default_padding = '4px';
    $border_radius   = $attributes['style']['border']['radius'];

    if ( is_array( $border_radius ) ) {
      // Apply styles for individual corner border radii.
      foreach ( $border_radius as $key => $value ) {
        if ( null !== $value ) {
          // Convert camelCase key to kebab-case.
          $name = strtolower( preg_replace( '/(?<!^)[A-Z]/', '-$0', $key ) );

          // Add shared styles for individual border radii for input & button.
          $border_style    = sprintf(
            'border-%s-radius: %s;',
            esc_attr( $name ),
            esc_attr( $value )
          );
          $input_styles[]  = $border_style;
          $button_styles[] = $border_style;

          // Add adjusted border radius styles for the wrapper element
          // if button is positioned inside.
          if ( $is_button_inside && intval( $value ) !== 0 ) {
            $wrapper_styles[] = sprintf(
              'border-%s-radius: calc(%s + %s);',
              esc_attr( $name ),
              esc_attr( $value ),
              $default_padding
            );
          }
        }
      }
    } else {
      // Numeric check is for backwards compatibility purposes.
      $border_radius   = is_numeric( $border_radius ) ? $border_radius . 'px' : $border_radius;
      $border_style    = sprintf( 'border-radius: %s;', esc_attr( $border_radius ) );
      $input_styles[]  = $border_style;
      $button_styles[] = $border_style;

      if ( $is_button_inside && intval( $border_radius ) !== 0 ) {
        // Adjust wrapper border radii to maintain visual consistency
        // with inner elements when button is positioned inside.
        $wrapper_styles[] = sprintf(
          'border-radius: calc(%s + %s);',
          esc_attr( $border_radius ),
          $default_padding
        );
      }
    }
  }

  // Add border color styles.
  $has_border_color = ! empty( $attributes['style']['border']['color'] );

  if ( $has_border_color ) {
    $border_color = $attributes['style']['border']['color'];

    // Apply wrapper border color if button placed inside.
    if ( $is_button_inside ) {
      $wrapper_styles[] = sprintf( 'border-color: %s;', esc_attr( $border_color ) );
    } else {
      $button_styles[] = sprintf( 'border-color: %s;', esc_attr( $border_color ) );
      $input_styles[]  = sprintf( 'border-color: %s;', esc_attr( $border_color ) );
    }

 View on GitHub View on Trac