Skip to content

Commit

Permalink
Podcast Player Block: avoid use of extract() (#21199)
Browse files Browse the repository at this point in the history
Co-authored-by: Konstantin Obenland <[email protected]>
Co-authored-by: Samiff <[email protected]>
  • Loading branch information
3 people authored Sep 27, 2021
1 parent c80709b commit d962525
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

Podcast Player Block: avoid use of extract()
Original file line number Diff line number Diff line change
Expand Up @@ -299,18 +299,6 @@ function render( $name, $template_props = array(), $print = true ) {
return '';
}

/*
* Optionally provided an assoc array of data to pass to template.
* IMPORTANT: It will be extracted into variables.
*/
if ( is_array( $template_props ) ) {
/*
* It ignores the `discouraging` sniffer rule for extract, since it's needed
* to make the templating system works.
*/
extract( $template_props ); // phpcs:ignore WordPress.PHP.DontExtract.extract_extract
}

if ( $print ) {
include $template_path;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,18 @@
/**
* Template variables.
*
* @var array $attachment
* @var array $primary_colors
* @var array $secondary_colors
* @var bool $is_active
* @var array $template_props
*/

$track_title = $attachment['title'];
$track_link = empty( $attachment['link'] ) ? $attachment['src'] : $attachment['link'];
$track_duration = ! empty( $attachment['duration'] ) ? $attachment['duration'] : '';
$track_title = $template_props['attachment']['title'];
$track_link = empty( $template_props['attachment']['link'] ) ? $template_props['attachment']['src'] : $template_props['attachment']['link'];
$track_duration = ! empty( $template_props['attachment']['duration'] ) ? $template_props['attachment']['duration'] : '';

$class = 'jetpack-podcast-player__track ' . $secondary_colors['class'];
$style = $secondary_colors['style'];
if ( $is_active ) {
$class = 'jetpack-podcast-player__track is-active ' . $primary_colors['class'];
$style = $primary_colors['style'];
$class = 'jetpack-podcast-player__track ' . $template_props['secondary_colors']['class'];
$style = $template_props['secondary_colors']['style'];
if ( $template_props['is_active'] ) {
$class = 'jetpack-podcast-player__track is-active ' . $template_props['primary_colors']['class'];
$style = $template_props['primary_colors']['style'];
}

?>
Expand All @@ -37,7 +34,7 @@ class="<?php echo esc_attr( trim( $class ) ); ?>"
class="jetpack-podcast-player__track-link jetpack-podcast-player__link"
href="<?php echo esc_url( $track_link ); ?>"
role="button"
<?php echo $is_active ? 'aria-current="track"' : ''; ?>
<?php echo $template_props['is_active'] ? 'aria-current="track"' : ''; ?>
>
<span class="jetpack-podcast-player__track-status-icon"></span>
<span class="jetpack-podcast-player__track-title"><?php echo esc_html( $track_title ); ?></span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,23 @@
/**
* Template variables.
*
* @var string $player_id
* @var string $title
* @var string $link
* @var array $track
* @var array $primary_colors
* @var string $template_props
*/

if ( ! isset( $title ) && empty( $track['title'] ) ) {
if ( ! isset( $template_props['title'] ) && empty( $template_props['track']['title'] ) ) {
return;
}

$track_link = empty( $track['link'] ) ? $track['src'] : $track['link'];
$track_link = empty( $template_props['track']['link'] ) ? $template_props['track']['src'] : $template_props['track']['link'];
?>

<h2 id="<?php echo esc_attr( $player_id ); ?>__title" class="jetpack-podcast-player__title">
<h2 id="<?php echo esc_attr( $template_props['player_id'] ); ?>__title" class="jetpack-podcast-player__title">
<span
class="jetpack-podcast-player__current-track-title <?php echo esc_attr( $primary_colors['class'] ); ?>"
<?php echo isset( $primary_colors['style'] ) ? 'style="' . esc_attr( $primary_colors['style'] ) . '"' : ''; ?>
class="jetpack-podcast-player__current-track-title <?php echo esc_attr( $template_props['primary_colors']['class'] ); ?>"
<?php echo isset( $template_props['primary_colors']['style'] ) ? 'style="' . esc_attr( $template_props['primary_colors']['style'] ) . '"' : ''; ?>
>
<?php
echo esc_html( $track['title'] );
echo esc_html( $template_props['track']['title'] );
if ( ! empty( $track_link ) ) :
// Prevent whitespace between title and link to cause a jump when JS kicks in.
// phpcs:disable Squiz.PHP.EmbeddedPhp.ContentAfterEnd
Expand All @@ -47,15 +43,15 @@ class="jetpack-podcast-player__track-title-link"
<?php endif; // phpcs:enable ?>
</span>

<?php if ( ! empty( $title ) ) : ?>
<?php if ( ! empty( $template_props['title'] ) ) : ?>
<span class="jetpack-podcast-player--visually-hidden"> - </span>

<?php
render(
'podcast-title',
array(
'title' => $title,
'link' => $link,
'title' => $template_props['title'],
'link' => $template_props['link'],
)
);
?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@
* Template variables.
*
* @var array $template_props
* @var string $player_id
* @var string $title
* @var string $link
* @var array $primary_colors
*/

/**
* Block attributes
* Block attributes.
*/
$attributes = (array) $template_props['attributes']; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
$show_cover_art = (bool) $attributes['showCoverArt'];
Expand All @@ -32,9 +28,9 @@

<div class="jetpack-podcast-player__header">
<div class="jetpack-podcast-player__current-track-info">
<?php if ( $show_cover_art && isset( $cover ) ) : ?>
<?php if ( $show_cover_art && isset( $template_props['cover'] ) ) : ?>
<div class="jetpack-podcast-player__cover">
<img class="jetpack-podcast-player__cover-image" src="<?php echo esc_url( $cover ); ?>" alt="" />
<img class="jetpack-podcast-player__cover-image" src="<?php echo esc_url( $template_props['cover'] ); ?>" alt="" />
</div>
<?php endif; ?>

Expand All @@ -43,11 +39,11 @@
render(
'podcast-header-title',
array(
'player_id' => $player_id,
'title' => $title,
'link' => $link,
'player_id' => $template_props['player_id'],
'title' => $template_props['title'],
'link' => $template_props['link'],
'track' => $track,
'primary_colors' => $primary_colors,
'primary_colors' => $template_props['primary_colors'],
)
);
}
Expand All @@ -58,7 +54,7 @@
if ( $show_episode_description && ! empty( $track ) && isset( $track['description'] ) ) :
?>
<div
id="<?php echo esc_attr( $player_id ); ?>__track-description"
id="<?php echo esc_attr( $template_props['player_id'] ); ?>__track-description"
class="jetpack-podcast-player__track-description"
>
<?php echo esc_html( $track['description'] ); ?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,27 @@
/**
* Template variables.
*
* @var string $title
* @var string $link
* @var string $template_props
*/

if ( empty( $title ) ) {
if ( empty( $template_props['title'] ) ) {
return;
}

?>
<span class="jetpack-podcast-player__podcast-title">
<?php
if ( ! empty( $link ) ) :
?>
<?php if ( ! empty( $template_props['link'] ) ) : ?>
<a
class="jetpack-podcast-player__link"
href="<?php echo esc_url( $link ); ?>"
href="<?php echo esc_url( $template_props['link'] ); ?>"
target="_blank"
rel="noopener noreferrer nofollow"
>
<?php echo esc_html( $title ); ?>
<?php echo esc_html( $template_props['title'] ); ?>
</a>
<?php
else :
echo esc_html( $title );
endif;
?>
else :
echo esc_html( $template_props['title'] );
endif;
?>
</span>

0 comments on commit d962525

Please sign in to comment.