three hundred and thirty-six | | // Hardcode the selectors and processing until the HTML API is able to read CSS selectors and replace inner HTML. |
three hundred and thirty-seven | | // TODO: Use the HTML API instead. |
three hundred and thirty-eight | | if ( 'core/paragraph' === $this->name && 'content' === $attribute_name ) { |
three hundred and thirty-nine | | $selector = 'p'; |
three hundred and forty | | } |
three hundred and forty-one | | if ( 'core/heading' === $this->name && 'content' === $attribute_name ) { |
three hundred and forty-two | | $selector = 'h[1-6]'; |
three hundred and forty-three | | } |
three hundred and forty-four | | if ( 'core/button' === $this->name && 'text' === $attribute_name ) { |
three hundred and forty-five | | // Check if it is a <button> or <a> tag. |
three hundred and forty-six | | if ( preg_match( '/<button[^>]*>.*?<\/button>/', $block_content ) ) { |
three hundred and forty-seven | | $selector = 'button'; |
three hundred and forty-eight | | } else { |
three hundred and forty-nine | | $selector = 'a'; |
| three hundred and thirty-six | $block_reader = new WP_HTML_Tag_Processor( $block_content ); |
| three hundred and thirty-seven | |
| three hundred and thirty-eight | // TODO: Support for CSS selectors whenever they are ready in the HTML API. |
| three hundred and thirty-nine | // In the meantime, support comma-separated selectors by exploding them into an array. |
| three hundred and forty | $selectors = explode( ',', $block_type->attributes[ $attribute_name ]['selector'] ); |
| three hundred and forty-one | // Add a bookmark to the first tag to be able to iterate over the selectors. |
| three hundred and forty-two | $block_reader->next_tag(); |
| three hundred and forty-three | $block_reader->set_bookmark( 'iterate-selectors' ); |
| three hundred and forty-four | |
| three hundred and forty-five | // TODO: This shouldn't be needed when the `set_inner_html` function is ready. |
| three hundred and forty-six | // Store the parent tag and its attributes to be able to restore them later in the button. |
| three hundred and forty-seven | // The button block has a wrapper while the paragraph and heading blocks don't. |
| three hundred and forty-eight | if ( 'core/button' === $this->name ) { |
| three hundred and forty-nine | $button_wrapper = $block_reader->get_tag(); |
| three hundred and fifty | $button_wrapper_attribute_names = $block_reader->get_attribute_names_with_prefix( '' ); |
| three hundred and fifty-one | $button_wrapper_attrs = array(); |
| three hundred and fifty-two | foreach ( $button_wrapper_attribute_names as $name ) { |
| three hundred and fifty-three | $button_wrapper_attrs[ $name ] = $block_reader->get_attribute( $name ); |
three hundred and fifty-two | | if ( empty( $selector ) ) { |
three hundred and fifty-three | | return $block_content; |
| three hundred and fifty-six | |
| three hundred and fifty-seven | foreach ( $selectors as $selector ) { |
| three hundred and fifty-eight | // If the parent tag, or any of its children, matches the selector, replace the HTML. |
| three hundred and fifty-nine | if ( strcasecmp( $block_reader->get_tag( $selector ), $selector ) === 0 || $block_reader->next_tag( |
| three hundred and sixty | array( |
| three hundred and sixty-one | 'tag_name' => $selector, |
| three hundred and sixty-two | ) |
| three hundred and sixty-three | ) ) { |
| three hundred and sixty-four | $block_reader->release_bookmark( 'iterate-selectors' ); |
| three hundred and sixty-five | |
| three hundred and sixty-six | // TODO: Use `set_inner_html` method whenever it's ready in the HTML API. |
| three hundred and sixty-seven | // Until then, it is hardcoded for the paragraph, heading, and button blocks. |
| three hundred and sixty-eight | // Store the tag and its attributes to be able to restore them later. |
| three hundred and sixty-nine | $selector_attribute_names = $block_reader->get_attribute_names_with_prefix( '' ); |
| three hundred and seventy | $selector_attrs = array(); |
| three hundred and seventy-one | foreach ( $selector_attribute_names as $name ) { |
| three hundred and seventy-two | $selector_attrs[ $name ] = $block_reader->get_attribute( $name ); |
| three hundred and seventy-three | } |
| three hundred and seventy-four | $selector_markup = "<$selector>" . wp_kses_post( $source_value ) . "</$selector>"; |
| three hundred and seventy-five | $amended_content = new WP_HTML_Tag_Processor( $selector_markup ); |
| three hundred and seventy-six | $amended_content->next_tag(); |
| three hundred and seventy-seven | foreach ( $selector_attrs as $attribute_key => $attribute_value ) { |
| three hundred and seventy-eight | $amended_content->set_attribute( $attribute_key, $attribute_value ); |
| three hundred and seventy-nine | } |
| three hundred and eighty | if ( 'core/paragraph' === $this->name || 'core/heading' === $this->name ) { |
| three hundred and eighty-one | return $amended_content->get_updated_html(); |
| three hundred and eighty-two | } |
| three hundred and eighty-three | if ( 'core/button' === $this->name ) { |
| three hundred and eighty-four | $button_markup = "<$button_wrapper>{$amended_content->get_updated_html()}</$button_wrapper>"; |
| three hundred and eighty-five | $amended_button = new WP_HTML_Tag_Processor( $button_markup ); |
| three hundred and eighty-six | $amended_button->next_tag(); |
| three hundred and eighty-seven | foreach ( $button_wrapper_attrs as $attribute_key => $attribute_value ) { |
| three hundred and eighty-eight | $amended_button->set_attribute( $attribute_key, $attribute_value ); |
| three hundred and eighty-nine | } |
| three hundred and ninety | return $amended_button->get_updated_html(); |
| three hundred and ninety-one | } |
| three hundred and ninety-two | } else { |
| three hundred and ninety-three | $block_reader->seek( 'iterate-selectors' ); |
| three hundred and ninety-four | } |