Skip to content

Commit

Permalink
fix: harden usage and output of attribute values
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoo committed Sep 18, 2023
1 parent 966fc84 commit 20fb8cc
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 20 deletions.
16 changes: 9 additions & 7 deletions includes/class-newspack-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,13 +543,15 @@ public static function image_size_for_orientation( $orientation = 'landscape' )
),
);

foreach ( $sizes[ $orientation ] as $key => $dimensions ) {
$attachment = wp_get_attachment_image_src(
get_post_thumbnail_id( get_the_ID() ),
'newspack-article-block-' . $orientation . '-' . $key
);
if ( ! empty( $attachment ) && $dimensions[0] === $attachment[1] && $dimensions[1] === $attachment[2] ) {
return 'newspack-article-block-' . $orientation . '-' . $key;
if ( isset( $sizes[ $orientation ] ) ) {
foreach ( $sizes[ $orientation ] as $key => $dimensions ) {
$attachment = wp_get_attachment_image_src(
get_post_thumbnail_id( get_the_ID() ),
'newspack-article-block-' . $orientation . '-' . $key
);
if ( ! empty( $attachment ) && $dimensions[0] === $attachment[1] && $dimensions[1] === $attachment[2] ) {
return 'newspack-article-block-' . $orientation . '-' . $key;
}
}
}

Expand Down
19 changes: 10 additions & 9 deletions src/blocks/carousel/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,9 @@ function newspack_blocks_render_block_carousel( $attributes ) {
);
}

$slides_per_view = absint( ! empty( $attributes['slidesPerView'] ) ? $attributes['slidesPerView'] : 1 );
$slides_per_view = absint( $attributes['slidesPerView'] ) ?? 1;
$slides_to_show = $slides_per_view <= $counter ? $slides_per_view : $counter;
$aspect_ratio = floatval( $attributes['aspectRatio'] ) ?? 0.75;

if ( $is_amp ) {
$selector = sprintf(
Expand All @@ -272,15 +273,15 @@ function newspack_blocks_render_block_carousel( $attributes ) {

$carousel = sprintf(
'<amp-base-carousel class="wp-block-newspack-carousel__amp-carousel" width="%1$s" height="%2$s" heights="%3$s" layout="responsive" snap="true" data-next-button-aria-label="%4$s" data-prev-button-aria-label="%5$s" controls="auto" loop="true" %6$s id="wp-block-newspack-carousel__amp-carousel__%7$s" on="slideChange:wp-block-newspack-carousel__amp-pagination__%7$s.toggle(index=event.index, value=true)" advance-count="1" visible-count="%8$s">%9$s</amp-base-carousel>',
$attributes['slidesPerView'] * 1,
$attributes['aspectRatio'],
'(min-width: 1168px) ' . ( $attributes['aspectRatio'] / $slides_to_show * 100 ) . '% !important, (min-width: 782px) ' . ( $slides_to_show > 1 ? ( $attributes['aspectRatio'] / 2 * 100 ) . '% !important' : ( $attributes['aspectRatio'] * 100 ) . '% !important' ) . ', ' . ( $attributes['aspectRatio'] * 100 ) . '% !important',
esc_attr( $slides_per_view * 1 ),
esc_attr( $aspect_ratio ),
esc_attr( '(min-width: 1168px) ' . ( $aspect_ratio / $slides_to_show * 100 ) . '% !important, (min-width: 782px) ' . ( $slides_to_show > 1 ? ( $aspect_ratio / 2 * 100 ) . '% !important' : ( $aspect_ratio * 100 ) . '% !important' ) . ', ' . ( $aspect_ratio * 100 ) . '% !important' ),
esc_attr__( 'Next Slide', 'newspack-blocks' ),
esc_attr__( 'Previous Slide', 'newspack-blocks' ),
$autoplay ? 'auto-advance="true" auto-advance-interval=' . esc_attr( $delay * 1000 ) : '',
absint( $newspack_blocks_carousel_id ),
'(min-width: 1168px) ' . $slides_to_show . ', (min-width: 782px) ' . ( $slides_to_show > 1 ? 2 : 1 ) . ', ' . 1,
$slides
esc_attr( '(min-width: 1168px) ' . $slides_to_show . ', (min-width: 782px) ' . ( $slides_to_show > 1 ? 2 : 1 ) . ', ' . 1 ),
wp_kses_post( $slides )
);
$autoplay_ui = $autoplay ? newspack_blocks_carousel_block_autoplay_ui_amp( $newspack_blocks_carousel_id ) : '';
} else {
Expand All @@ -297,16 +298,16 @@ function newspack_blocks_render_block_carousel( $attributes ) {
);
$carousel = sprintf(
'<div class="swiper"><div class="swiper-wrapper">%s</div>%s</div>',
$slides,
wp_kses_post( $slides ),
$navigation
);
$autoplay_ui = $autoplay ? newspack_blocks_carousel_block_autoplay_ui( $newspack_blocks_carousel_id ) : '';
}
$data_attributes = [
'data-current-post-id=' . $post_id,
'data-slides-per-view=' . $attributes['slidesPerView'],
'data-slides-per-view=' . esc_attr( $slides_per_view ),
'data-slide-count=' . $counter,
'data-aspect-ratio=' . $attributes['aspectRatio'],
'data-aspect-ratio=' . esc_attr( $aspect_ratio ),
];

if ( $autoplay && ! $is_amp ) {
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/homepage-articles/templates/article.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function( $data ) {
$post_link = Newspack_Blocks::get_post_link( $post_id );

if ( 'behind' === $attributes['mediaPosition'] && $attributes['showImage'] && has_post_thumbnail() ) {
$styles = 'min-height: ' . $attributes['minHeight'] . 'vh; padding-top: ' . ( $attributes['minHeight'] / 5 ) . 'vh;';
$styles = 'min-height: ' . absint( $attributes['minHeight'] ) . 'vh; padding-top: ' . ( absint( $attributes['minHeight'] ) / 5 ) . 'vh;';
}
$image_size = 'newspack-article-block-uncropped';
if ( has_post_thumbnail() && 'uncropped' !== $attributes['imageShape'] ) {
Expand Down
4 changes: 2 additions & 2 deletions src/blocks/homepage-articles/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function newspack_blocks_hpb_maximum_image_width() {
$site_content_width = 1200;
$is_image_half_width = in_array( $attributes['mediaPosition'], [ 'left', 'right' ], true );
if ( 'grid' === $attributes['postLayout'] ) {
$columns = $attributes['columns'];
$columns = absint( $attributes['columns'] );
if ( $is_image_half_width ) {
// If the media position is on left or right, the image is 50% of the column width.
$columns = $columns * 2;
Expand Down Expand Up @@ -284,7 +284,7 @@ function newspack_blocks_register_homepage_articles() {
function newspack_blocks_format_avatars( $author_info ) {
$elements = array_map(
function ( $author ) {
return sprintf( '<a href="%s">%s</a>', $author->url, $author->avatar );
return sprintf( '<a href="%s">%s</a>', esc_url( $author->url ), wp_kses_post( $author->avatar ) );
},
$author_info
);
Expand Down
4 changes: 3 additions & 1 deletion src/blocks/video-playlist/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ function( $block ) {
}
);
foreach ( $youtube_blocks as $youtube_block ) {
$videos[] = $youtube_block['attrs']['url'];
if ( isset( $youtube_block['attrs']['url'] ) ) {
$videos[] = esc_url( $youtube_block['attrs']['url'] );
}
}
}

Expand Down

0 comments on commit 20fb8cc

Please sign in to comment.