Permalink
Browse files

Added password protection functionality and context support for @s he…

…lper
  • Loading branch information...
Nomafin committed Jul 27, 2017
1 parent 76043e5 commit f68a5978e62ab53e7518abb1af768f644cc9cdc4
Showing with 67 additions and 3 deletions.
  1. +19 −2 README.md
  2. +35 −0 helpers/password.php
  3. +9 −1 helpers/s.php
  4. +4 −0 partials/password_form.dust
View
@@ -451,6 +451,21 @@ custom_pagination_link( $page_link ) {
add_filter( 'dustpress/pagination/page_link', 'custom_pagination_link' );
```
### password
`password` implements WordPress' native password protection functionality. It takes one parameter, `id`, that tells it what post's password to require. It defaults to current post's id.
Put your password protected content inside the `password` context and it will be replaced with a password form until the correct password has been given.
Example:
```
{@password}
<p>This content is password protected.</p>
{/password}
```
Unlike native WordPress, where the form's layout can be changed via filter, you can override it with your own Dust template. The default template can be found in DustPress core under `partials/` directory. Copy it somewhere under your theme's `partials/` directory and make the modifications you want to.
### permalink
`permalink` helper emulates WordPress' native `get_permalink()` function. It takes one parameter, `id`, that tells it what post's permalink to give. It defaults to current post's id.
@@ -462,7 +477,7 @@ Example:
### s
`s` helper emulates WordPress' native `__` function and it is used in internationalization. It takes two parameters: `s` that is the string to be translated and `td` that is the text domain, only `s` is mandatory.
`s` helper emulates WordPress' native `__` or `_x` functions and it is used in internationalization. It takes one to three parameters: `s` that is the string to be translated and the only required parameter. `td` is the text domain and `x` is the context to provide with the translation.
Note that the use of `s` helper does not bring the string available to for example WPML's string scanning function.
@@ -471,7 +486,7 @@ Example:
{@s s="Home page" td="my-page" /}
```
You can use a [translation parser](https://github.com/devgeniem/dustpress/wiki/Translation-parser) to find all strings defined with {@s} and write them to a file in a format that can be scanned with POedit.
You can use the [translation parser script](https://github.com/devgeniem/dustpress/wiki/Translation-parser) to find all strings defined with {@s} and to write them to a file in a format that can be scanned with POedit.
### sep
@@ -517,6 +532,8 @@ Examples:
`strtodate` formats the date it is given to a format it is given. It takes three parameters: `value`, `format` and `now`. The function emulates the behaviour of a PHP code: `date( $format, strtotime( $value, $now ) )`.
If no format parameter is given, the default date format for the site will be used ( `get_option( 'date_format' )`).
Example:
```
{@strtodate value=post_date format="d.m.Y H:i:s" /}
View
@@ -0,0 +1,35 @@
<?php
namespace DustPress;
class Password extends Helper {
public function init() {
if ( isset( $this->params->id ) ) {
$id = $this->params->id;
}
else {
$id = get_the_ID();
}
$post = get_post( $id );
// If a password is not needed, render the content block as is
if ( empty( $post->post_password ) || ! post_password_required( $id ) ) {
return $this->chunk->render( $this->bodies->block, $this->context );
}
else {
if ( post_password_required( $id ) ) {
// Populate data object to be passed to the password form template
$data = new \stdClass();
$data->url = esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) );
$data->label = 'pwbox-'. $id;
return $this->chunk->write( dustpress()->render([
'partial' => 'password_form',
'data' => $data,
'echo' => false
]) );
}
}
}
}
$this->dust->helpers['password'] = new Password();
View
@@ -6,7 +6,15 @@ class S extends Helper
public function output() {
if ( isset( $this->params->s ) ) {
if ( isset( $this->params->td ) ) {
return __( $this->params->s, $this->params->td );
if ( isset( $this->params->x ) ) {
return _x( $this->params->s, $this->params->x, $this->params->td );
}
else {
return __( $this->params->s, $this->params->td );
}
}
else if ( isset( $this->params->x ) ) {
return _x( $this->params->s, $this->params->x );
}
else {
return __( $this->params->s );
@@ -0,0 +1,4 @@
<form action="{url}" class="post-password-form" method="post">
<p>{@s s="This content is password protected. To view it please enter your password below:" /}</p>
<p><label for="{label}">{@s s="Password:" /} <input name="post_password" id="{label}" type="password" size="20" /></label> <input type="submit" name="Submit" value="{@s s="Enter" x="post password form" /}" /></p>
</form>

0 comments on commit f68a597

Please sign in to comment.