Skip to content

Commit

Permalink
Merge pull request #53 from hnuti-brontosaurus/dynamic-homepage-banner
Browse files Browse the repository at this point in the history
make homepage banner dynamic
  • Loading branch information
dakur authored Nov 29, 2024
2 parents 1439ef3 + 92ff9ed commit 21d2dce
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/workflows/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ jobs:
-i "functions.php" \
-i "header.php" \
-i "helpers.php" \
-i "homepage-banner.php" \
-i "index.php" \
-i "PostTypeInitializer.php" \
-i "screenshot.png" \
Expand Down
14 changes: 8 additions & 6 deletions UI/Homepage/HomepageController.latte
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

{block content}
<main class="homepage" role="main">
{capture $path}https://brontosaurus.cz/wp-content/uploads/2024/11/mikulcicky-luh-upravena.jpg{/capture}
<a class="hb-block-banner hb-mbe-5" href="https://darkyprirode.cz" rel="noopener" style="--hb-block-banner-background-url: url({$path|noescape})">
{if $banner !== null}
{var $tag = $banner->link !== null ? 'a' : 'div'}
<{$tag} class="hb-block-banner hb-mbe-5" {if $banner->link !== null}href="{$banner->link}" rel="noopener"{/if} style="--hb-block-banner-background-url: url({$banner->image|noescape})">
<div class="hb-block-banner__textContainer">
<div class="hb-block-banner__text hb-mbs-3">
<div class="hb-fs-xl hb-mbe-2 hb-fw-b">Věnuj dárek přírodě</div>
<div class="hb-mbe-2">Daruj budku nebo strom</div>
<div class="hb-fs-xl hb-mbe-2 hb-fw-b">{$banner->heading}</div>
<div class="hb-mbe-2" n:if="$banner->subheading !== null">{$banner->subheading}</div>
<span class="hb-block-banner__button hb-block-banner__button--standalone action action--primary action--paw">více info</span>
<span class="hb-block-banner__button hb-block-banner__button--inline hb-fs-xs" aria-hidden="true">více info »</span>
</div>
</div>
</a>
</{$tag}>
{/if}

<h1 class="homepage-heading">
<h1 n:class="'homepage-heading', $banner === null ? 'hb-mbs-4'">
Dobrovolnické a zážitkové akce
</h1>

Expand Down
20 changes: 20 additions & 0 deletions UI/Homepage/HomepageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function render(): void
}

$params = [
'banner' => self::banner(),
'aboutCrossroadPageLink' => $this->base->getLinkFor('o-brontosaurovi'),
'eventCollection' => $eventCollection,
];
Expand All @@ -75,4 +76,23 @@ public function render(): void
);
}

private static function banner(): ?\stdCLass
{
$heading = get_option('homepage-banner_heading');
$subheading = get_option('homepage-banner_subheading');
$image = get_option('homepage-banner_image');
$link = get_option('homepage-banner_link');

if ($image === '') {
return null;
}

return (object) [
'heading' => $heading,
'subheading' => $subheading ?: null,
'image' => $image,
'link' => $link ?: null,
];
}

}
2 changes: 2 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
/** @var Container $hb_container */
$hb_container = require_once __DIR__ . '/bootstrap.php';

require_once __DIR__ . '/homepage-banner.php';


(function (Container $container, WP_Theme $theme) {

Expand Down
120 changes: 120 additions & 0 deletions homepage-banner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php declare(strict_types = 1);

namespace HnutiBrontosaurus\Theme;

function banner_settings_page() {
add_options_page(
'Homepage banner',
'Homepage banner',
'publish_pages', // basically editor+ role
'homepage-banner',
'HnutiBrontosaurus\Theme\render_homepage_banner_settings',
);
}
add_action('admin_menu', 'HnutiBrontosaurus\Theme\banner_settings_page');

function enqueue_banner_settings_scripts($hook) {
if ($hook !== 'settings_page_homepage-banner') {
return;
}

// Enqueue WordPress media uploader script and styles.
wp_enqueue_media();
}
add_action('admin_enqueue_scripts', 'HnutiBrontosaurus\Theme\enqueue_banner_settings_scripts');



function render_homepage_banner_settings() {
// Check if the user has permission to save options.
if (!current_user_can('publish_pages')) {
return;
}

// Handle form submission.
if (isset($_POST['homepage-banner_save-settings'])) {
check_admin_referer('homepage-banner_nonce'); // Validate nonce.

update_option('homepage-banner_heading', sanitize_text_field($_POST['homepage-banner_heading']));
update_option('homepage-banner_subheading', sanitize_text_field($_POST['homepage-banner_subheading']));
update_option('homepage-banner_image', esc_url_raw($_POST['homepage-banner_image']));
update_option('homepage-banner_link', esc_url_raw($_POST['homepage-banner_link']));

echo '<div class="updated"><p>Úspěšně uloženo!</p></div>';
}

// Get saved values.
$heading = get_option('homepage-banner_heading', '');
$subheading = get_option('homepage-banner_subheading', '');
$image = get_option('homepage-banner_image', '');
$link = get_option('homepage-banner_link', '');
?>

<div class="wrap">
<h1>Homepage banner</h1>
<form method="post">
<?php wp_nonce_field('homepage-banner_nonce'); ?>
<table class="form-table">
<tr>
<th scope="row"><label for="homepage-banner_heading">Hlavní text:*</label></th>
<td><input type="text" id="homepage-banner_heading" name="homepage-banner_heading" value="<?php echo esc_attr($heading); ?>" class="regular-text" required></td>
</tr>
<tr>
<th scope="row"><label for="homepage-banner_subheading">Podtext:</label></th>
<td><input type="text" id="homepage-banner_subheading" name="homepage-banner_subheading" value="<?php echo esc_attr($subheading); ?>" class="regular-text"></td>
</tr>
<tr>
<th scope="row"><label for="homepage-banner_image">Banner:*</label></th>
<td>
<input type="text" id="homepage-banner_image" name="homepage-banner_image" value="<?php echo esc_url($image); ?>" class="regular-text" required>
<button type="button" class="button" id="homepage-banner_upload-image-button">Vybrat obrázek</button>
</td>
</tr>
<tr>
<th scope="row"><label for="homepage-banner_link">Odkaz:</label></th>
<td><input type="text" id="homepage-banner_link" name="homepage-banner_link" value="<?php echo esc_attr($link); ?>" class="regular-text"></td>
</tr>
</table>
<?php submit_button('Uložit', 'primary', 'homepage-banner_save-settings'); ?>
</form>
</div>

<script>
document.addEventListener("DOMContentLoaded", () => {
const uploadButton = document.getElementById("homepage-banner_upload-image-button");
const bannerImageField = document.getElementById("homepage-banner_image");

let mediaUploader;

uploadButton.addEventListener("click", (e) => {
e.preventDefault();

// If the media uploader already exists, open it.
if (mediaUploader) {
mediaUploader.open();
return;
}

// Create the media uploader.
mediaUploader = wp.media({
title: "Select Banner Image",
button: {
text: "Use this image",
},
multiple: false, // Only allow a single image.
});

// When an image is selected, update the input field.
mediaUploader.on("select", () => {
const attachment = mediaUploader.state().get("selection").first().toJSON();
bannerImageField.value = attachment.url;
});

// Open the media uploader.
mediaUploader.open();
});
});
</script>

<?php
}

0 comments on commit 21d2dce

Please sign in to comment.