Skip to content

Instantly share code, notes, and snippets.

@setola
Created February 20, 2015 15:47
Show Gist options
  • Save setola/1f17383fe671d6863e58 to your computer and use it in GitHub Desktop.
Save setola/1f17383fe671d6863e58 to your computer and use it in GitHub Desktop.

Revisions

  1. setola created this gist Feb 20, 2015.
    110 changes: 110 additions & 0 deletions custom-post-password-template.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,110 @@
    <?php
    /*
    Template Name: Custom Password
    */


    /**
    * Checks if the given code has a valid format ex: AB123456789
    * @param $code string the code
    * @return bool true only if code is valid
    */
    function checkCode($code){
    if(is_null($code)) return false;
    if(empty($code)) return false;
    if(strlen($code)!=11) return false;
    if(!is_numeric(substr($code,2,9))) return false;
    if(!ctype_alpha(substr($code,0,2))) return false;
    return true;
    }

    /**
    * Checks if the we can display the post content
    * @return bool
    */
    function can_view_content(){
    if(isset($_POST['post_password']) && checkCode($_POST['post_password'])) return true;

    require_once ABSPATH . 'wp-includes/class-phpass.php';
    $hasher = new PasswordHash( 8, true );

    $hash = ( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] );

    if ( 0 !== strpos( $hash, '$P$B' ) )
    return false;

    return $hasher->CheckPassword( 'yesyoucan', $hash );
    }





    if(isset($_POST['post_password'])){
    require_once ABSPATH . 'wp-includes/class-phpass.php';
    $hasher = new PasswordHash( 8, true );
    $expire = apply_filters( 'post_password_expires', time() + 10 * DAY_IN_SECONDS );
    $pass = checkCode($_POST['post_password']) ? 'yesyoucan' : 'noyoucant';
    setcookie( 'wp-postpass_' . COOKIEHASH, $hasher->HashPassword( ( $pass ) ), $expire, COOKIEPATH );
    }



    get_template_part('header');
    ?>

    <div id="main-container" class="container">
    <div class="row">
    <div class="main-content col-xs-12 col-sm-12 col-md-12 col-lg-12">
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

    <div id="title-container" class="clearfix">
    <header class="header">
    <h1 class="title"><?php the_title(); ?></h1>
    </header>
    </div>

    <div class="content">
    <?php
    if(can_view_content()) {
    the_content(__('Continue reading <span class="meta-nav">&rarr;</span>', BLU_THEME_TEXTDOMAIN));
    } else {
    global $input_with_label,
    $form_submit_button;

    $input_with_label
    ->set_markup('pre', '<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">')
    ->set_markup('post', '</div>')
    ->set_markup('label-class', 'col-sm-4 control-label')
    ->set_markup('input-wrapper-class', 'col-sm-8')
    ->set_markup('id', 'post-protected-' . get_the_ID())
    ->set_markup('name', 'post_password')
    ->set_markup('type', 'password')
    ->set_markup('help', __("To view this protected content, enter your card number:"))
    ->set_markup('label', __('ACI Card ID', BLU_FORMS_TEXTDOMAIN))
    ->set_markup('placeholder', __('Your ACI card ID', BLU_FORMS_TEXTDOMAIN));

    echo HtmlHelper::standard_tag(
    'form',
    $input_with_label->replace_markup()
    . $form_submit_button
    ->set_markup('label', __('Submit your request', BLU_FORMS_TEXTDOMAIN))
    ->replace_markup(),
    array(
    'id' => 'form-requestquote',
    'data-form-name' => 'requestquote',
    'class' => 'blu-form form-horizontal',
    'method' => 'post',
    'action' => ''
    )
    );
    }
    ?>
    </div>
    </article>
    </div>
    </div>
    </div>

    <?php get_template_part('footer');