wp/wp-includes/block-supports/position.php
changeset 21 48c4eec2b7e6
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
       
     1 <?php
       
     2 /**
       
     3  * Position block support flag.
       
     4  *
       
     5  * @package WordPress
       
     6  * @since 6.2.0
       
     7  */
       
     8 
       
     9 /**
       
    10  * Registers the style block attribute for block types that support it.
       
    11  *
       
    12  * @since 6.2.0
       
    13  * @access private
       
    14  *
       
    15  * @param WP_Block_Type $block_type Block Type.
       
    16  */
       
    17 function wp_register_position_support( $block_type ) {
       
    18 	$has_position_support = block_has_support( $block_type, 'position', false );
       
    19 
       
    20 	// Set up attributes and styles within that if needed.
       
    21 	if ( ! $block_type->attributes ) {
       
    22 		$block_type->attributes = array();
       
    23 	}
       
    24 
       
    25 	if ( $has_position_support && ! array_key_exists( 'style', $block_type->attributes ) ) {
       
    26 		$block_type->attributes['style'] = array(
       
    27 			'type' => 'object',
       
    28 		);
       
    29 	}
       
    30 }
       
    31 
       
    32 /**
       
    33  * Renders position styles to the block wrapper.
       
    34  *
       
    35  * @since 6.2.0
       
    36  * @access private
       
    37  *
       
    38  * @param  string $block_content Rendered block content.
       
    39  * @param  array  $block         Block object.
       
    40  * @return string                Filtered block content.
       
    41  */
       
    42 function wp_render_position_support( $block_content, $block ) {
       
    43 	$block_type           = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
       
    44 	$has_position_support = block_has_support( $block_type, 'position', false );
       
    45 
       
    46 	if (
       
    47 		! $has_position_support ||
       
    48 		empty( $block['attrs']['style']['position'] )
       
    49 	) {
       
    50 		return $block_content;
       
    51 	}
       
    52 
       
    53 	$global_settings          = wp_get_global_settings();
       
    54 	$theme_has_sticky_support = isset( $global_settings['position']['sticky'] ) ? $global_settings['position']['sticky'] : false;
       
    55 	$theme_has_fixed_support  = isset( $global_settings['position']['fixed'] ) ? $global_settings['position']['fixed'] : false;
       
    56 
       
    57 	// Only allow output for position types that the theme supports.
       
    58 	$allowed_position_types = array();
       
    59 	if ( true === $theme_has_sticky_support ) {
       
    60 		$allowed_position_types[] = 'sticky';
       
    61 	}
       
    62 	if ( true === $theme_has_fixed_support ) {
       
    63 		$allowed_position_types[] = 'fixed';
       
    64 	}
       
    65 
       
    66 	$style_attribute = isset( $block['attrs']['style'] ) ? $block['attrs']['style'] : null;
       
    67 	$class_name      = wp_unique_id( 'wp-container-' );
       
    68 	$selector        = ".$class_name";
       
    69 	$position_styles = array();
       
    70 	$position_type   = isset( $style_attribute['position']['type'] ) ? $style_attribute['position']['type'] : '';
       
    71 	$wrapper_classes = array();
       
    72 
       
    73 	if (
       
    74 		in_array( $position_type, $allowed_position_types, true )
       
    75 	) {
       
    76 		$wrapper_classes[] = $class_name;
       
    77 		$wrapper_classes[] = 'is-position-' . $position_type;
       
    78 		$sides             = array( 'top', 'right', 'bottom', 'left' );
       
    79 
       
    80 		foreach ( $sides as $side ) {
       
    81 			$side_value = isset( $style_attribute['position'][ $side ] ) ? $style_attribute['position'][ $side ] : null;
       
    82 			if ( null !== $side_value ) {
       
    83 				/*
       
    84 				 * For fixed or sticky top positions,
       
    85 				 * ensure the value includes an offset for the logged in admin bar.
       
    86 				 */
       
    87 				if (
       
    88 					'top' === $side &&
       
    89 					( 'fixed' === $position_type || 'sticky' === $position_type )
       
    90 				) {
       
    91 					// Ensure 0 values can be used in `calc()` calculations.
       
    92 					if ( '0' === $side_value || 0 === $side_value ) {
       
    93 						$side_value = '0px';
       
    94 					}
       
    95 
       
    96 					// Ensure current side value also factors in the height of the logged in admin bar.
       
    97 					$side_value = "calc($side_value + var(--wp-admin--admin-bar--position-offset, 0px))";
       
    98 				}
       
    99 
       
   100 				$position_styles[] =
       
   101 					array(
       
   102 						'selector'     => $selector,
       
   103 						'declarations' => array(
       
   104 							$side => $side_value,
       
   105 						),
       
   106 					);
       
   107 			}
       
   108 		}
       
   109 
       
   110 		$position_styles[] =
       
   111 			array(
       
   112 				'selector'     => $selector,
       
   113 				'declarations' => array(
       
   114 					'position' => $position_type,
       
   115 					'z-index'  => '10',
       
   116 				),
       
   117 			);
       
   118 	}
       
   119 
       
   120 	if ( ! empty( $position_styles ) ) {
       
   121 		/*
       
   122 		 * Add to the style engine store to enqueue and render position styles.
       
   123 		 */
       
   124 		wp_style_engine_get_stylesheet_from_css_rules(
       
   125 			$position_styles,
       
   126 			array(
       
   127 				'context'  => 'block-supports',
       
   128 				'prettify' => false,
       
   129 			)
       
   130 		);
       
   131 
       
   132 		// Inject class name to block container markup.
       
   133 		$content = new WP_HTML_Tag_Processor( $block_content );
       
   134 		$content->next_tag();
       
   135 		foreach ( $wrapper_classes as $class ) {
       
   136 			$content->add_class( $class );
       
   137 		}
       
   138 		return (string) $content;
       
   139 	}
       
   140 
       
   141 	return $block_content;
       
   142 }
       
   143 
       
   144 // Register the block support.
       
   145 WP_Block_Supports::get_instance()->register(
       
   146 	'position',
       
   147 	array(
       
   148 		'register_attribute' => 'wp_register_position_support',
       
   149 	)
       
   150 );
       
   151 add_filter( 'render_block', 'wp_render_position_support', 10, 2 );