Skip to content

Commit 6d9e03d

Browse files
committed
make homepage banner dynamic
1 parent 1439ef3 commit 6d9e03d

File tree

5 files changed

+152
-6
lines changed

5 files changed

+152
-6
lines changed

.github/workflows/default.yml

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ jobs:
117117
-i "functions.php" \
118118
-i "header.php" \
119119
-i "helpers.php" \
120+
-i "homepage-banner.php" \
120121
-i "index.php" \
121122
-i "PostTypeInitializer.php" \
122123
-i "screenshot.png" \

UI/Homepage/HomepageController.latte

+8-6
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@
22

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

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

UI/Homepage/HomepageController.php

+20
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public function render(): void
5858
}
5959

6060
$params = [
61+
'banner' => self::banner(),
6162
'aboutCrossroadPageLink' => $this->base->getLinkFor('o-brontosaurovi'),
6263
'eventCollection' => $eventCollection,
6364
];
@@ -75,4 +76,23 @@ public function render(): void
7576
);
7677
}
7778

79+
private static function banner(): ?\stdCLass
80+
{
81+
$heading = get_option('homepage-banner_heading');
82+
$subheading = get_option('homepage-banner_subheading');
83+
$image = get_option('homepage-banner_image');
84+
$link = get_option('homepage-banner_link');
85+
86+
if ($image === '') {
87+
return null;
88+
}
89+
90+
return (object) [
91+
'heading' => $heading,
92+
'subheading' => $subheading ?: null,
93+
'image' => $image,
94+
'link' => $link ?: null,
95+
];
96+
}
97+
7898
}

functions.php

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
/** @var Container $hb_container */
1919
$hb_container = require_once __DIR__ . '/bootstrap.php';
2020

21+
require_once __DIR__ . '/homepage-banner.php';
22+
2123

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

homepage-banner.php

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace HnutiBrontosaurus\Theme;
4+
5+
function banner_settings_page() {
6+
add_options_page(
7+
'Homepage banner',
8+
'Homepage banner',
9+
'publish_pages', // basically editor+ role
10+
'homepage-banner',
11+
'HnutiBrontosaurus\Theme\render_homepage_banner_settings',
12+
);
13+
}
14+
add_action('admin_menu', 'HnutiBrontosaurus\Theme\banner_settings_page');
15+
16+
function enqueue_banner_settings_scripts($hook) {
17+
// Check if we're on the banner settings page to load scripts only there.
18+
if ($hook !== 'toplevel_page_banner-settings') {
19+
return;
20+
}
21+
22+
// Enqueue WordPress media uploader script and styles.
23+
wp_enqueue_media();
24+
}
25+
add_action('admin_enqueue_scripts', 'HnutiBrontosaurus\Theme\enqueue_banner_settings_scripts');
26+
27+
28+
29+
function render_homepage_banner_settings() {
30+
// Check if the user has permission to save options.
31+
if (!current_user_can('publish_pages')) {
32+
return;
33+
}
34+
35+
// Handle form submission.
36+
if (isset($_POST['homepage-banner_save-settings'])) {
37+
check_admin_referer('homepage-banner_nonce'); // Validate nonce.
38+
39+
update_option('homepage-banner_heading', sanitize_text_field($_POST['homepage-banner_heading']));
40+
update_option('homepage-banner_subheading', sanitize_text_field($_POST['homepage-banner_subheading']));
41+
update_option('homepage-banner_image', esc_url_raw($_POST['homepage-banner_image']));
42+
update_option('homepage-banner_link', esc_url_raw($_POST['homepage-banner_link']));
43+
44+
echo '<div class="updated"><p>Úspěšně uloženo!</p></div>';
45+
}
46+
47+
// Get saved values.
48+
$heading = get_option('homepage-banner_heading', '');
49+
$subheading = get_option('homepage-banner_subheading', '');
50+
$image = get_option('homepage-banner_image', '');
51+
$link = get_option('homepage-banner_link', '');
52+
?>
53+
54+
<div class="wrap">
55+
<h1>Homepage banner</h1>
56+
<form method="post">
57+
<?php wp_nonce_field('homepage-banner_nonce'); ?>
58+
<table class="form-table">
59+
<tr>
60+
<th scope="row"><label for="homepage-banner_heading">Hlavní text:*</label></th>
61+
<td><input type="text" id="homepage-banner_heading" name="homepage-banner_heading" value="<?php echo esc_attr($heading); ?>" class="regular-text" required></td>
62+
</tr>
63+
<tr>
64+
<th scope="row"><label for="homepage-banner_subheading">Podtext:</label></th>
65+
<td><input type="text" id="homepage-banner_subheading" name="homepage-banner_subheading" value="<?php echo esc_attr($subheading); ?>" class="regular-text"></td>
66+
</tr>
67+
<tr>
68+
<th scope="row"><label for="homepage-banner_image">Banner:*</label></th>
69+
<td>
70+
<input type="text" id="homepage-banner_image" name="homepage-banner_image" value="<?php echo esc_url($image); ?>" class="regular-text" required>
71+
<button type="button" class="button" id="homepage-banner_upload-image-button">Vybrat obrázek</button>
72+
</td>
73+
</tr>
74+
<tr>
75+
<th scope="row"><label for="homepage-banner_link">Odkaz:</label></th>
76+
<td><input type="text" id="homepage-banner_link" name="homepage-banner_link" value="<?php echo esc_attr($link); ?>" class="regular-text"></td>
77+
</tr>
78+
</table>
79+
<?php submit_button('Uložit', 'primary', 'homepage-banner_save-settings'); ?>
80+
</form>
81+
</div>
82+
83+
<script>
84+
document.addEventListener("DOMContentLoaded", () => {
85+
const uploadButton = document.getElementById("homepage-banner_upload-image-button");
86+
const bannerImageField = document.getElementById("homepage-banner_image");
87+
88+
let mediaUploader;
89+
90+
uploadButton.addEventListener("click", (e) => {
91+
e.preventDefault();
92+
93+
// If the media uploader already exists, open it.
94+
if (mediaUploader) {
95+
mediaUploader.open();
96+
return;
97+
}
98+
99+
// Create the media uploader.
100+
mediaUploader = wp.media({
101+
title: "Select Banner Image",
102+
button: {
103+
text: "Use this image",
104+
},
105+
multiple: false, // Only allow a single image.
106+
});
107+
108+
// When an image is selected, update the input field.
109+
mediaUploader.on("select", () => {
110+
const attachment = mediaUploader.state().get("selection").first().toJSON();
111+
bannerImageField.value = attachment.url;
112+
});
113+
114+
// Open the media uploader.
115+
mediaUploader.open();
116+
});
117+
});
118+
</script>
119+
120+
<?php
121+
}

0 commit comments

Comments
 (0)