Skip to content

Commit 67bfb9e

Browse files
authored
Podcast Player: implement colors in server-side (#15195)
* podcast-player: add SS version of color name fn * podcast-player: apply CSS classes in SS * podcast-player: apply CSS to episodes list in SS * podcast-player: apply inline styles in SS * podcast-player: replace div by section * podcast-player: fix appying CSS classes * podcast-player: adjust code * podcast-player: minor code improvement * podcast-player: fix typo * podcast-player: refact -> adding helper function * podcast-player: remove get_color_class_name() * podcast-player: minor improvements according on feedback * podcast-player: simplify props to @ebenland
1 parent 68581ee commit 67bfb9e

File tree

1 file changed

+69
-16
lines changed

1 file changed

+69
-16
lines changed

extensions/blocks/podcast-player/podcast-player.php

Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function render_player( $player_data, $attributes ) {
103103
absint( $attributes['itemsToShow'] )
104104
);
105105

106-
// Genereate a unique id for the block instance.
106+
// Generate a unique id for the block instance.
107107
$instance_id = wp_unique_id( 'jetpack-podcast-player-block-' );
108108
$player_data['playerId'] = $instance_id;
109109

@@ -115,28 +115,42 @@ function render_player( $player_data, $attributes ) {
115115
$player_data
116116
);
117117

118+
$secondary_colors = get_colors( 'secondary', $attributes, 'color' );
119+
$background_colors = get_colors( 'background', $attributes, 'background-color' );
120+
121+
$player_classes_name = trim( "{$secondary_colors['class']} {$background_colors['class']}" );
122+
$player_inline_style = trim( "{$secondary_colors['style']} ${background_colors['style']}" );
123+
118124
$block_classname = Jetpack_Gutenberg::block_classes( FEATURE_NAME, $attributes, array( 'is-default' ) );
119125
$is_amp = ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() );
120126

121127
ob_start();
122128
?>
123129
<div class="<?php echo esc_attr( $block_classname ); ?>" id="<?php echo esc_attr( $instance_id ); ?>">
124-
<ol class="jetpack-podcast-player__episodes">
125-
<?php foreach ( $player_data['tracks'] as $attachment ) : ?>
126-
<li class="jetpack-podcast-player__episode">
127-
<a
128-
class="jetpack-podcast-player__episode-link"
129-
href="<?php echo esc_url( $attachment['link'] ); ?>"
130-
role="button"
131-
aria-pressed="false"
130+
<section
131+
class="<?php echo esc_attr( $player_classes_name ); ?>"
132+
style="<?php echo esc_attr( $player_inline_style ); ?>"
133+
>
134+
<ol class="jetpack-podcast-player__episodes">
135+
<?php foreach ( $player_data['tracks'] as $attachment ) : ?>
136+
<li
137+
class="jetpack-podcast-player__episode <?php echo esc_attr( $secondary_colors['class'] ); ?>"
138+
style="<?php echo esc_attr( $secondary_colors['style'] ); ?>"
132139
>
133-
<span class="jetpack-podcast-player__episode-status-icon"></span>
134-
<span class="jetpack-podcast-player__episode-title"><?php echo esc_html( $attachment['title'] ); ?></span>
135-
<time class="jetpack-podcast-player__episode-duration"><?php echo ( ! empty( $attachment['duration'] ) ? esc_html( $attachment['duration'] ) : '' ); ?></time>
136-
</a>
137-
</li>
138-
<?php endforeach; ?>
139-
</ol>
140+
<a
141+
class="jetpack-podcast-player__episode-link"
142+
href="<?php echo esc_url( $attachment['link'] ); ?>"
143+
role="button"
144+
aria-pressed="false"
145+
>
146+
<span class="jetpack-podcast-player__episode-status-icon"></span>
147+
<span class="jetpack-podcast-player__episode-title"><?php echo esc_html( $attachment['title'] ); ?></span>
148+
<time class="jetpack-podcast-player__episode-duration"><?php echo ( ! empty( $attachment['duration'] ) ? esc_html( $attachment['duration'] ) : '' ); ?></time>
149+
</a>
150+
</li>
151+
<?php endforeach; ?>
152+
</ol>
153+
</section>
140154
<?php if ( ! $is_amp ) : ?>
141155
<script type="application/json"><?php echo wp_json_encode( $player_props ); ?></script>
142156
<?php endif; ?>
@@ -161,3 +175,42 @@ class="jetpack-podcast-player__episode-link"
161175

162176
return ob_get_clean();
163177
}
178+
179+
/**
180+
* Given the color name, block attributes and the CSS property,
181+
* the function will return an array with the `class` and `style`
182+
* HTML attributes to be used straight in the markup.
183+
*
184+
* @example
185+
* $color = get_colors( 'secondary', $attributes, 'border-color'
186+
* => array( 'class' => 'has-secondary', 'style' => 'border-color: #333' )
187+
*
188+
* @param string $name Color attribute name, for instance `primary`, `secondary`, ...
189+
* @param array $attrs Block attributes.
190+
* @param string $property Color CSS property, fo instance `color`, `background-color`, ...
191+
* @return array Colors array.
192+
*/
193+
function get_colors( $name, $attrs, $property ) {
194+
$attr_color = "{$name}Color";
195+
$attr_custom = 'custom' . ucfirst( $attr_color );
196+
197+
$color = isset( $attrs[ $attr_color ] ) ? $attrs[ $attr_color ] : null;
198+
$custom_color = isset( $attrs[ $attr_custom ] ) ? $attrs[ $attr_custom ] : null;
199+
200+
$colors = array(
201+
'class' => '',
202+
'style' => '',
203+
);
204+
205+
if ( $color || $custom_color ) {
206+
$colors['class'] .= "has-{$name}";
207+
208+
if ( $color ) {
209+
$colors['class'] .= " has-{$color}-{$property}";
210+
} elseif ( $custom_color ) {
211+
$colors['style'] .= "{$property}: {$custom_color};";
212+
}
213+
}
214+
215+
return $colors;
216+
}

0 commit comments

Comments
 (0)