Myzwer / foothillschurch

Bootcamp II is a wordpress theme (as well as an inside joke) designed to suit the needs of foothillschurch.com. It makes use of webpack, Babel, Sass, Tailwind, Browsersync, PostCSS, ESLint, Stylelint, Prettier and more. It is meant for that site, but if you can use it by all means go for it.
1 stars 1 forks source link

Use null coalescing operator for fallback values #47

Closed rain2o closed 1 month ago

rain2o commented 1 month ago

In cases like this, where you are using the value if it exists, otherwise setting a fallback:

https://github.com/Myzwer/foothillschurch/blob/effc5bbf81497ae99f0561ab7849412fe9272bfd/linktree.php#L19-L25

You can use null coalescing to simplify it. This also means you only call the function once.

$logo = get_field( 'logo' ) ?? get_template_directory_uri() . '/assets/src/img/circle-brand.png';
$title = get_field( 'title' ) ?? 'Foothills Church';
rain2o commented 1 month ago

Another location you could do this https://github.com/Myzwer/foothillschurch/blob/effc5bbf81497ae99f0561ab7849412fe9272bfd/messages.php#L105

'paged'          => get_query_var( 'paged' ) ?? 1,
rain2o commented 1 month ago

Here too https://github.com/Myzwer/foothillschurch/blob/effc5bbf81497ae99f0561ab7849412fe9272bfd/resources-bank.php#L99

rain2o commented 1 month ago

This whole section https://github.com/Myzwer/foothillschurch/blob/effc5bbf81497ae99f0561ab7849412fe9272bfd/single-event.php#L47-L69

Could be reduced to 1 or 3 lines, depending on if you want to break it up.

<h2 class="text-lg font-bold uppercase">
    <?php echo get_field('event_start_time') ?? "TBD"; ?> - <?php echo get_field('event_end_time') ?? "TBD"; ?>
</h2>
rain2o commented 1 month ago

Another place you can implement this optimization https://github.com/Myzwer/foothillschurch/blob/eb77c6d3119be03e7efd20408e627b1ab9af7aed/components/blocks/event-type.php#L25

$count    = get_sub_field( "num_events" ) ?? 3;

and here in the same file https://github.com/Myzwer/foothillschurch/blob/eb77c6d3119be03e7efd20408e627b1ab9af7aed/components/blocks/event-type.php#L62

rain2o commented 1 month ago

Also here, can simplify a few things with these conditions.

https://github.com/Myzwer/foothillschurch/blob/effc5bbf81497ae99f0561ab7849412fe9272bfd/components/headers/default/_button.php#L35-L46

and

https://github.com/Myzwer/foothillschurch/blob/effc5bbf81497ae99f0561ab7849412fe9272bfd/components/headers/default/_image.php#L38-L49

for example

$link = get_sub_field('button_link') ?? get_sub_field('button_link_file');
// Get tab status
$new_tab = get_sub_field('new_tab');
$attrs = null;
if ($tab == "yes") {
    $attrs = 'target="_blank"';
} elseif ($tab == "cc") {
    $attrs = "data-open-in-church-center-modal='true'";
}