WP_Interactivity_API::merge_style_property
Merges an individual style property in the style attribute of an HTML element, updating or removing the property when necessary.
If a property is modified, the old one is removed and the new one is added at the end of the list.
Example:
merge_style_property( 'color:green;', 'color', 'red' ) => 'color:red;' merge_style_property( 'background:green;', 'color', 'red' ) => 'background:green;color:red;' merge_style_property( 'color:green;', 'color', null ) => ''
Метод класса: WP_Interactivity_API{}
Хуков нет.
Возвращает
Строку. The new style attribute value after the specified property has been added, updated or removed.
Использование
// private - только в коде основоного (родительского) класса $result = $this->merge_style_property( $style_attribute_value, $style_property_name, $style_property_value ): string;
- $style_attribute_value(строка) (обязательный)
- The current style attribute value.
- $style_property_name(строка) (обязательный)
- The style property name to set.
- $style_property_value(строка|false|null) (обязательный)
- The value to set for the style property. With false, null or an empty string, it removes the style property.
Список изменений
| С версии 6.5.0 | Введена. |
Код WP_Interactivity_API::merge_style_property() WP Interactivity API::merge style property WP 6.9.1
private function merge_style_property( string $style_attribute_value, string $style_property_name, $style_property_value ): string {
$style_assignments = explode( ';', $style_attribute_value );
$result = array();
$style_property_value = ! empty( $style_property_value ) ? rtrim( trim( $style_property_value ), ';' ) : null;
$new_style_property = $style_property_value ? $style_property_name . ':' . $style_property_value . ';' : '';
// Generates an array with all the properties but the modified one.
foreach ( $style_assignments as $style_assignment ) {
if ( empty( trim( $style_assignment ) ) ) {
continue;
}
list( $name, $value ) = explode( ':', $style_assignment );
if ( trim( $name ) !== $style_property_name ) {
$result[] = trim( $name ) . ':' . trim( $value ) . ';';
}
}
// Adds the new/modified property at the end of the list.
$result[] = $new_style_property;
return implode( '', $result );
}