links ) )->get();
if ( empty( $switcher_output ) ) {
return '';
}
$switcher_output = $this->apply_flag_styles_to_markup( $switcher_output, $attributes );
return $this->apply_block_wrapper_attributes(
$switcher_output,
get_block_wrapper_attributes()
);
}
/**
* Returns the path to the block JSON file directory.
* The directory name being used to register a block.
*
* @since 3.8
*
* @return string The path to the block.
*/
protected function get_path(): string {
return __DIR__;
}
/**
* Merges block wrapper attributes onto the switcher root element.
*
* Block supports (background, spacing, etc.) must apply to the switcher
* wrapper itself — not to an extra full-width `
` around it.
*
* @since 3.9
*
* @param string $html Switcher HTML.
* @param string $wrapper_attributes Attributes from `get_block_wrapper_attributes()`.
* @return string
*/
private function apply_block_wrapper_attributes( string $html, string $wrapper_attributes ): string {
if ( '' === trim( $wrapper_attributes ) ) {
return $html;
}
$processor = new \WP_HTML_Tag_Processor( $html );
if ( ! $processor->next_tag() ) {
return sprintf( '
%2$s
', $wrapper_attributes, $html );
}
foreach ( $this->parse_html_attributes( $wrapper_attributes ) as $name => $value ) {
if ( 'class' === $name ) {
$existing = $processor->get_attribute( 'class' ) ?? '';
$value = trim( $existing . ' ' . $value );
} elseif ( 'style' === $name ) {
$existing = $processor->get_attribute( 'style' ) ?? '';
/** @var string $existing `WP_HTML_Tag_Processor::get_attribute()` returns string|null for non-boolean attributes */
$value = '' === $existing ? $value : rtrim( $existing, ';' ) . ';' . $value;
}
$processor->set_attribute( $name, $value );
}
return $processor->get_updated_html();
}
/**
* Parses an HTML attributes string.
*
* @since 3.9
*
* @param string $attributes_string HTML attributes.
* @return array
*/
private function parse_html_attributes( string $attributes_string ): array {
$attributes = array();
$processor = new \WP_HTML_Tag_Processor( '' );
if ( ! $processor->next_tag() ) {
return $attributes;
}
foreach ( array( 'class', 'style', 'id' ) as $name ) {
/** @var string|null $value `WP_HTML_Tag_Processor::get_attribute()` returns string|null for non-boolean attributes */
$value = $processor->get_attribute( $name );
if ( null !== $value && '' !== $value ) {
$attributes[ $name ] = $value;
}
}
return $attributes;
}
}