From d31057a009aa0cbf19a63a6502a48d2d31eb3c16 Mon Sep 17 00:00:00 2001 From: Aaron Jorbin Date: Wed, 14 Sep 2016 21:28:30 +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. Built from https://develop.svn.wordpress.org/trunk@38603 git-svn-id: http://core.svn.wordpress.org/trunk@38546 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/post-template.php | 28 +++++++++++++++++++++------- wp-includes/version.php | 2 +- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/wp-includes/post-template.php b/wp-includes/post-template.php index d915317722..c315df2a51 100644 --- a/wp-includes/post-template.php +++ b/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 ); } // diff --git a/wp-includes/version.php b/wp-includes/version.php index 88058f12e9..025f08d7bd 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.7-alpha-38602'; +$wp_version = '4.7-alpha-38603'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.