Skip to content

Commit

Permalink
feat: support password protection for single pages
Browse files Browse the repository at this point in the history
  • Loading branch information
dustin-jw committed Sep 26, 2023
1 parent af48def commit a969116
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/php/example-page-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
$context['post'] = $timber_post;

// Render HTML templates.
Timber\Timber::render( 'pages/example-page.twig', $context );
render_with_password_protection( $timber_post, 'pages/example-page.twig', $context );
17 changes: 16 additions & 1 deletion src/php/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@
function add_to_context( $context ) {
$context['menu'] = new \Timber\Menu( 'primary-menu' );
$context['footer_sidebar'] = Timber\Timber::get_widgets( 'footer-area' );
return $context;
return $context;
}
add_filter( 'timber/context', 'add_to_context' );

/**
* Render page content with password protection.
*
* @param object $post - The current post.
* @param string|array $templates - The template(s) to render.
* @param object $context - The Timber context used to render.
*/
function render_with_password_protection( $post, $templates, $context ) {
if ( post_password_required( $post->ID ) ) {
Timber::render( 'single-password.twig', $context );
} else {
Timber::render( $templates, $context );
}
}
4 changes: 3 additions & 1 deletion src/php/page.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@
$context = Timber\Timber::context();
$timber_post = new Timber\Post();
$context['post'] = $timber_post;
Timber\Timber::render( array( 'page-' . $timber_post->post_name . '.twig', 'page.twig' ), $context );
$templates = array( 'page-' . $timber_post->post_name . '.twig', 'page.twig' );

render_with_password_protection( $timber_post, $templates, $context );
4 changes: 3 additions & 1 deletion src/php/single-example.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
$context = Timber\Timber::context();
$timber_post = new Timber\Post();
$context['post'] = $timber_post;
Timber\Timber::render( array( 'page-' . $timber_post->post_name . '.twig', 'page.twig' ), $context );
$templates = array( 'page-' . $timber_post->post_name . '.twig', 'page.twig' );

Timber\Timber::render( $timber_post, $templates, $context );
4 changes: 3 additions & 1 deletion src/php/single.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
$context = Timber\Timber::context();
$timber_post = new Timber\Post();
$context['post'] = $timber_post;
Timber\Timber::render( array( 'page-' . $timber_post->post_name . '.twig', 'page.twig' ), $context );
$templates = array( 'page-' . $timber_post->post_name . '.twig', 'page.twig' );

render_with_password_protection( $timber_post, $templates, $context );
7 changes: 7 additions & 0 deletions src/php/views/single-password.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% extends "base.twig" %}

{% block content %}
<div class="obj-width-limiter">
{{ function('get_the_password_form') }}
</div>
{% endblock %}

0 comments on commit a969116

Please sign in to comment.