PluginProbe
Gutenberg / 17.5.0
Gutenberg v17.5.0
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / lib / experimental / interactivity-api / directives / wp-style.php

wp-style.php in Gutenberg 17.5.0, at lib/experimental/interactivity-api/directives/wp-style.php

74 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Process wp-style directive attribute.
4 *
5 * @package Gutenberg
6 * @subpackage Interactivity API
7 */
8
9 /**
10 * Process wp-style directive attribute.
11 *
12 * @param WP_Directive_Processor $tags Tags.
13 * @param WP_Directive_Context $context Directive context.
14 * @param string $ns Namespace.
15 */
16 function gutenberg_interactivity_process_wp_style( $tags, $context, $ns ) {
17 if ( $tags->is_tag_closer() ) {
18 return;
19 }
20
21 $prefixed_attributes = $tags->get_attribute_names_with_prefix( 'data-wp-style--' );
22
23 foreach ( $prefixed_attributes as $attr ) {
24 list( , $style_name ) = WP_Directive_Processor::parse_attribute_name( $attr );
25 if ( empty( $style_name ) ) {
26 continue;
27 }
28
29 $reference = $tags->get_attribute( $attr );
30 $style_value = gutenberg_interactivity_evaluate_reference( $reference, $ns, $context->get_context() );
31 if ( $style_value ) {
32 $style_attr = $tags->get_attribute( 'style' ) ?? '';
33 $style_attr = gutenberg_interactivity_set_style( $style_attr, $style_name, $style_value );
34 $tags->set_attribute( 'style', $style_attr );
35 } else {
36 // TODO: Do we want to unset styles if they're null?
37 }
38 }
39 }
40
41 /**
42 * Set style.
43 *
44 * @param string $style Existing style to amend.
45 * @param string $name Style property name.
46 * @param string $value Style property value.
47 * @return string Amended styles.
48 */
49 function gutenberg_interactivity_set_style( $style, $name, $value ) {
50 $style_assignments = explode( ';', $style );
51 $modified = false;
52 foreach ( $style_assignments as $style_assignment ) {
53 list( $style_name ) = explode( ':', $style_assignment );
54 if ( trim( $style_name ) === $name ) {
55 // TODO: Retain surrounding whitespace from $style_value, if any.
56 $style_assignment = $style_name . ': ' . $value;
57 $modified = true;
58 break;
59 }
60 }
61
62 if ( ! $modified ) {
63 $new_style_assignment = $name . ': ' . $value;
64 // If the last element is empty or whitespace-only, we insert
65 // the new "key: value" pair before it.
66 if ( empty( trim( end( $style_assignments ) ) ) ) {
67 array_splice( $style_assignments, - 1, 0, $new_style_assignment );
68 } else {
69 array_push( $style_assignments, $new_style_assignment );
70 }
71 }
72 return implode( ';', $style_assignments );
73 }
74