Skip to content
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

Carousel: improve detection of containers where we should add data #13446

Merged
merged 5 commits into from
Sep 24, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions modules/carousel/jetpack-carousel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
kraftbj marked this conversation as resolved.
Show resolved Hide resolved

/*
* 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' )
Copy link
Member

Choose a reason for hiding this comment

The 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 wp-block- divs (including divs with no class at all).

Copy link
Member Author

Choose a reason for hiding this comment

The 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 wp-block-. The second parameter is here to add an additional exception: wp-block- is okay if it's a gallery block, like wp-block-jetpack-tiled-gallery.

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?

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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();
}
}

Expand Down