Make WordPress Core

Changeset 38603


Ignore:
Timestamp:
09/14/2016 09:27:16 PM (8 years ago)
Author:
jorbin
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post-template.php

    r38574 r38603  
    780780    $post = get_post($post);
    781781
    782     if ( empty( $post->post_password ) )
    783         return false;
    784 
    785     if ( ! isset( $_COOKIE['wp-postpass_' . COOKIEHASH] ) )
    786         return true;
     782    if ( empty( $post->post_password ) ) {
     783        /** This filter is documented in wp-includes/post.php */
     784        return apply_filters( 'post_password_required', false, $post );
     785    }
     786
     787    if ( ! isset( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ) ) {
     788        /** This filter is documented in wp-includes/post.php */
     789        return apply_filters( 'post_password_required', true, $post );
     790    }
    787791
    788792    $hasher = new PasswordHash( 8, true );
    789793
    790794    $hash = wp_unslash( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] );
    791     if ( 0 !== strpos( $hash, '$P$B' ) )
    792         return true;
    793 
    794     return ! $hasher->CheckPassword( $post->post_password, $hash );
     795    if ( 0 !== strpos( $hash, '$P$B' ) ) {
     796        $required = true;
     797    } else {
     798        $required = ! $hasher->CheckPassword( $post->post_password, $hash );
     799    }
     800
     801    /**
     802     * Filter whether a post requires the user to supply a password.
     803     *
     804     * @param bool    $required Whether the user needs to supply a password. True if password has not been
     805     *                          provided or is incorrect, false if password has been supplied or is not required.
     806     * @param WP_Post $post Post data.
     807     */
     808    return apply_filters( 'post_password_required', $required, $post );
    795809}
    796810
Note: See TracChangeset for help on using the changeset viewer.