-
Notifications
You must be signed in to change notification settings - Fork 847
Carousel: improve detection of containers where we should add data #13446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ebb2a70
fef2a1a
ef5cb8a
b2a0560
6c293bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -540,9 +540,59 @@ class_exists( 'Jetpack_AMP_Support' ) | |
| * @param array $extra_data Array of data about the site and the post. | ||
| */ | ||
| $extra_data = apply_filters( 'jp_carousel_add_data_to_container', $extra_data ); | ||
|
|
||
| foreach ( (array) $extra_data as $data_key => $data_values ) { | ||
| $html = str_replace( '<div ', '<div ' . esc_attr( $data_key ) . "='" . json_encode( $data_values ) . "' ", $html ); | ||
| $html = str_replace( '<ul class="wp-block-gallery', '<ul ' . esc_attr( $data_key ) . "='" . json_encode( $data_values ) . "' class=\"wp-block-gallery", $html ); | ||
| // Do not go any further if DOMDocument is disabled on the server. | ||
| if ( ! class_exists( 'DOMDocument' ) ) { | ||
| return $html; | ||
| } | ||
|
|
||
| // Let's grab all containers from the HTML. | ||
| $dom_doc = new DOMDocument(); | ||
|
|
||
| /* | ||
| * The @ is not enough to suppress errors when dealing with libxml, | ||
| * we have to tell it directly how we want to handle errors. | ||
| */ | ||
| $old_libxml_disable_entity_loader = libxml_disable_entity_loader( true ); | ||
| $old_libxml_use_internal_errors = libxml_use_internal_errors( true ); | ||
| @$dom_doc->loadHTML( $html ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged | ||
| libxml_use_internal_errors( $old_libxml_use_internal_errors ); | ||
| libxml_disable_entity_loader( $old_libxml_disable_entity_loader ); | ||
|
|
||
| // Let's look for lists and divs. | ||
| $ul_tags = $dom_doc->getElementsByTagName( 'ul' ); | ||
| $div_tags = $dom_doc->getElementsByTagName( 'div' ); | ||
|
|
||
| // Loop through each ul, and add the data to it if it is a gallery block. | ||
| foreach ( $ul_tags as $ul_tag ) { | ||
| if ( false !== strpos( $ul_tag->getAttribute( 'class' ), 'wp-block-gallery' ) ) { | ||
| $ul_tag->setAttribute( | ||
| $data_key, | ||
| wp_json_encode( $data_values ) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| * Loop through each div and add the data, only when it's a gallery block div. | ||
| * We want to avoid adding data to divs like wp-block-columns. | ||
| * We do however want data on divs like wp-block-jetpack-tiled-gallery. | ||
| */ | ||
| foreach ( $div_tags as $div_tag ) { | ||
| if ( | ||
| false === strpos( $div_tag->getAttribute( 'class' ), 'wp-block-' ) | ||
| || false !== strpos( $div_tag->getAttribute( 'class' ), 'gallery' ) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be As written, we add the attribute to all divs that are not
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's the current behaviour, I didn't want to change that. We currently add the attributes to all divs. With this change, it will be added to all divs, except if they include a class including Maybe there would be a more readable way to do that? Or should we instead move to more of a whitelist where we don't add the data to all the divs, but only to some specific ones?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd support keeping the behavior the way it is for the sake of the PR, but open an enhancement issue to explore refactoring it.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Logged in #13499 |
||
| ) { | ||
| $div_tag->setAttribute( | ||
| $data_key, | ||
| wp_json_encode( $data_values ) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| // Save our updated HTML. | ||
| $html = $dom_doc->saveHTML(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.