Skip to content

Commit a969116

Browse files
committed
feat: support password protection for single pages
1 parent af48def commit a969116

File tree

6 files changed

+33
-5
lines changed

6 files changed

+33
-5
lines changed

src/php/example-page-template.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
$context['post'] = $timber_post;
99

1010
// Render HTML templates.
11-
Timber\Timber::render( 'pages/example-page.twig', $context );
11+
render_with_password_protection( $timber_post, 'pages/example-page.twig', $context );

src/php/functions.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@
7878
function add_to_context( $context ) {
7979
$context['menu'] = new \Timber\Menu( 'primary-menu' );
8080
$context['footer_sidebar'] = Timber\Timber::get_widgets( 'footer-area' );
81-
return $context;
81+
return $context;
8282
}
8383
add_filter( 'timber/context', 'add_to_context' );
84+
85+
/**
86+
* Render page content with password protection.
87+
*
88+
* @param object $post - The current post.
89+
* @param string|array $templates - The template(s) to render.
90+
* @param object $context - The Timber context used to render.
91+
*/
92+
function render_with_password_protection( $post, $templates, $context ) {
93+
if ( post_password_required( $post->ID ) ) {
94+
Timber::render( 'single-password.twig', $context );
95+
} else {
96+
Timber::render( $templates, $context );
97+
}
98+
}

src/php/page.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@
1414
$context = Timber\Timber::context();
1515
$timber_post = new Timber\Post();
1616
$context['post'] = $timber_post;
17-
Timber\Timber::render( array( 'page-' . $timber_post->post_name . '.twig', 'page.twig' ), $context );
17+
$templates = array( 'page-' . $timber_post->post_name . '.twig', 'page.twig' );
18+
19+
render_with_password_protection( $timber_post, $templates, $context );

src/php/single-example.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
$context = Timber\Timber::context();
88
$timber_post = new Timber\Post();
99
$context['post'] = $timber_post;
10-
Timber\Timber::render( array( 'page-' . $timber_post->post_name . '.twig', 'page.twig' ), $context );
10+
$templates = array( 'page-' . $timber_post->post_name . '.twig', 'page.twig' );
11+
12+
Timber\Timber::render( $timber_post, $templates, $context );

src/php/single.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@
99
$context = Timber\Timber::context();
1010
$timber_post = new Timber\Post();
1111
$context['post'] = $timber_post;
12-
Timber\Timber::render( array( 'page-' . $timber_post->post_name . '.twig', 'page.twig' ), $context );
12+
$templates = array( 'page-' . $timber_post->post_name . '.twig', 'page.twig' );
13+
14+
render_with_password_protection( $timber_post, $templates, $context );

src/php/views/single-password.twig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{% extends "base.twig" %}
2+
3+
{% block content %}
4+
<div class="obj-width-limiter">
5+
{{ function('get_the_password_form') }}
6+
</div>
7+
{% endblock %}

0 commit comments

Comments
 (0)