From 5d1ca8e2d032e2a094f529cff313781855618772 Mon Sep 17 00:00:00 2001 From: Aaron Jorbin Date: Wed, 14 Sep 2016 21:27:16 +0000 Subject: [PATCH] Posts: Add filter to allow overriding `post_password_required` return Post Passwords are incredibly inflexible. One Password per site at a time and other limitations that can't really be changed without a backwards compatibility break. This adds the ability for sites to change the password behavior such as doing per post passwords or allowing multiple passwords to be set in a browser. The possibilities are YUGE. Additionally, it allows for a behavior other than returning a html form when a password is needed. This is important for non website use cases (such as in a restful API). Fixes #38056. See #16483. Props rmccue. git-svn-id: https://develop.svn.wordpress.org/trunk@38603 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/post-template.php | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/post-template.php b/src/wp-includes/post-template.php index d915317722..c315df2a51 100644 --- a/src/wp-includes/post-template.php +++ b/src/wp-includes/post-template.php @@ -779,19 +779,33 @@ function get_body_class( $class = '' ) { function post_password_required( $post = null ) { $post = get_post($post); - if ( empty( $post->post_password ) ) - return false; + if ( empty( $post->post_password ) ) { + /** This filter is documented in wp-includes/post.php */ + return apply_filters( 'post_password_required', false, $post ); + } - if ( ! isset( $_COOKIE['wp-postpass_' . COOKIEHASH] ) ) - return true; + if ( ! isset( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ) ) { + /** This filter is documented in wp-includes/post.php */ + return apply_filters( 'post_password_required', true, $post ); + } $hasher = new PasswordHash( 8, true ); $hash = wp_unslash( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ); - if ( 0 !== strpos( $hash, '$P$B' ) ) - return true; + if ( 0 !== strpos( $hash, '$P$B' ) ) { + $required = true; + } else { + $required = ! $hasher->CheckPassword( $post->post_password, $hash ); + } - return ! $hasher->CheckPassword( $post->post_password, $hash ); + /** + * Filter whether a post requires the user to supply a password. + * + * @param bool $required Whether the user needs to supply a password. True if password has not been + * provided or is incorrect, false if password has been supplied or is not required. + * @param WP_Post $post Post data. + */ + return apply_filters( 'post_password_required', $required, $post ); } //