Commit d31057a0 authored by Aaron Jorbin's avatar Aaron Jorbin
Browse files

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
parent dd6da701
...@@ -779,19 +779,33 @@ function get_body_class( $class = '' ) { ...@@ -779,19 +779,33 @@ function get_body_class( $class = '' ) {
function post_password_required( $post = null ) { function post_password_required( $post = null ) {
$post = get_post($post); $post = get_post($post);
if ( empty( $post->post_password ) ) if ( empty( $post->post_password ) ) {
return false; /** This filter is documented in wp-includes/post.php */
return apply_filters( 'post_password_required', false, $post );
}
if ( ! isset( $_COOKIE['wp-postpass_' . COOKIEHASH] ) ) if ( ! isset( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ) ) {
return true; /** This filter is documented in wp-includes/post.php */
return apply_filters( 'post_password_required', true, $post );
}
$hasher = new PasswordHash( 8, true ); $hasher = new PasswordHash( 8, true );
$hash = wp_unslash( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ); $hash = wp_unslash( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] );
if ( 0 !== strpos( $hash, '$P$B' ) ) if ( 0 !== strpos( $hash, '$P$B' ) ) {
return true; $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 );
} }
// //
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
* *
* @global string $wp_version * @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. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment