Skip to content

Commit

Permalink
Merge pull request #75 from WordPress/fix/prevent-fatals-if-meta-data…
Browse files Browse the repository at this point in the history
…-not-exist

Prevent fatals if post_meta is null.
  • Loading branch information
cbravobernal authored Oct 11, 2024
2 parents 35b5957 + 2d9e5f0 commit 88b03a1
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 42 deletions.
43 changes: 24 additions & 19 deletions src/blocks/interactive/movie-trailer-button/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,39 @@
$post = get_post();
$play_icon = file_get_contents( get_template_directory() . '/assets/play.svg' );
$videos = get_post_meta( $post->ID, 'wpmovies_videos', true );
$trailers = array_filter(
json_decode( $videos, true ),

if ( ! $videos ) {
return;
}

$trailers = array_filter(
json_decode( $videos, true ) ?: array(),
function( $video ) {
return 'Trailer' === $video['type'];
return isset( $video['type'] ) && 'Trailer' === $video['type'];
}
);

if ( count( $trailers ) !== 0 ) {
$trailer_url = reset( $trailers )['url'];
$trailer_id = substr( $trailer_url, strpos( $trailer_url, '?v=' ) + 3 );
$context = array( 'videoId' => $trailer_id );
if ( $trailers && count( $trailers ) !== 0 ) {
$trailer_url = reset( $trailers )['url'];
$trailer_id = substr( $trailer_url, strpos( $trailer_url, '?v=' ) + 3 );
$context = array( 'videoId' => $trailer_id );
?>

<div
data-wp-interactive="wpmovies"
<?php echo $wrapper_attributes; ?>
<?php echo wp_interactivity_data_wp_context( $context ); ?>
>
<div
data-wp-interactive="wpmovies"
<?php echo $wrapper_attributes; ?>
<?php echo wp_interactivity_data_wp_context( $context ); ?>
class="wpmovies-page-button-parent"
data-wp-on--click="actions.setVideo"
aria-controls="wp-movies-video-player"
>
<div
class="wpmovies-page-button-parent"
data-wp-on--click="actions.setVideo"
aria-controls="wp-movies-video-player"
>
<div class="wpmovies-page-button-child">
<?php echo $play_icon; ?>
<span>Play trailer</span>
</div>
<div class="wpmovies-page-button-child">
<?php echo $play_icon; ?>
<span><?php _e( 'Play trailer', 'wpmovies' ); ?></span>
</div>
</div>
</div>

<?php } ?>
54 changes: 31 additions & 23 deletions src/blocks/non-interactive/movie-score/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@
* @package wpmovies
*/

$post = get_post();
$score = get_post_meta( $post->ID, 'wpmovies_vote_average', true );
$score_color = '#5EFD26'; // Green.
if ( $score < 7 && $score >= 3 ) {
$score_color = '#fad900'; // Yellow.
} elseif ( $score < 3 ) {
$score_color = '#de1600'; // Red.
};
$degrees_css = $score * 180 / 10;

$post = get_post();
$score = get_post_meta( $post->ID, 'wpmovies_vote_average', true );
$score = $score !== '' ? floatval( $score ) : null;
$score_color = '#5EFD26'; // Green.

if ( $score === null ) {
$score_color = '#808080'; // Gray for no score.
$degrees_css = 0;
} else {
if ( $score < 7 && $score >= 3 ) {
$score_color = '#fad900'; // Yellow.
} elseif ( $score < 3 ) {
$score_color = '#de1600'; // Red.
}
$degrees_css = $score * 180 / 10;
}

$wrapper_attributes = get_block_wrapper_attributes(
array(
Expand All @@ -22,17 +30,17 @@
)
);
?>

<div <?php echo $wrapper_attributes; ?>>
<div class="wpmovies-score-circle">
<div class="wpmovies-score-mask wpmovies-score-full" <?php echo 'style="transform: rotate(' . $degrees_css . 'deg)"'; ?>>
<div class="wpmovies-score-fill" <?php echo 'style="background-color: ' . $score_color . ';transform: rotate(' . $degrees_css . 'deg)"'; ?>></div>
</div>
<div class="wpmovies-score-mask wpmovies-score-half">
<div class="wpmovies-score-fill" <?php echo 'style="background-color: ' . $score_color . ';transform: rotate(' . $degrees_css . 'deg)"'; ?>></div>
</div>
<div class="wpmovies-score-inside-circle">
<span><?php echo round( $score * 10 ) . '%'; ?></span>
</div>
</div>
</div>
<div <?php echo $wrapper_attributes; ?>>
<div class="wpmovies-score-circle">
<div class="wpmovies-score-mask wpmovies-score-full" <?php echo 'style="transform: rotate(' . $degrees_css . 'deg)"'; ?>>
<div class="wpmovies-score-fill" <?php echo 'style="background-color: ' . $score_color . ';transform: rotate(' . $degrees_css . 'deg)"'; ?>></div>
</div>
<div class="wpmovies-score-mask wpmovies-score-half">
<div class="wpmovies-score-fill" <?php echo 'style="background-color: ' . $score_color . ';transform: rotate(' . $degrees_css . 'deg)"'; ?>></div>
</div>
<div class="wpmovies-score-inside-circle">
<span><?php echo round( $score * 10 ) . '%'; ?></span>
</div>
</div>
</div>

0 comments on commit 88b03a1

Please sign in to comment.