post_password_required(). fixes #7679

git-svn-id: https://develop.svn.wordpress.org/trunk@8800 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan Boren 2008-09-03 19:54:14 +00:00
parent 341cede30b
commit a487b60f71
7 changed files with 45 additions and 31 deletions

View File

@ -33,7 +33,7 @@ $commenter = wp_get_current_commenter();
extract($commenter);
$comments = get_approved_comments($id);
$commentstatus = get_post($id);
if (!empty($commentstatus->post_password) && $_COOKIE['wp-postpass_'. COOKIEHASH] != $commentstatus->post_password) { // and it doesn't match the cookie
if ( post_password_required($commentstatus) ) { // and it doesn't match the cookie
echo(get_the_password_form());
} else { ?>

View File

@ -1,4 +1,4 @@
<?php if ( !empty($post->post_password) && $_COOKIE['wp-postpass_' . COOKIEHASH] != $post->post_password) : ?>
<?php if ( post_password_required() ) : ?>
<p><?php _e('Enter your password to view comments.'); ?></p>
<?php return; endif; ?>

View File

@ -33,7 +33,7 @@ $commenter = wp_get_current_commenter();
extract($commenter);
$comments = get_approved_comments($id);
$post = get_post($id);
if (!empty($post->post_password) && $_COOKIE['wp-postpass_'. COOKIEHASH] != $post->post_password) { // and it doesn't match the cookie
if ( post_password_required($post) ) { // and it doesn't match the cookie
echo(get_the_password_form());
} else { ?>

View File

@ -2,15 +2,10 @@
if (!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
die ('Please do not load this page directly. Thanks!');
if (!empty($post->post_password)) { // if there's a password
if ($_COOKIE['wp-postpass_' . COOKIEHASH] != $post->post_password) { // and it doesn't match the cookie
?>
<p class="nocomments">This post is password protected. Enter the password to view comments.</p>
<?php
return;
}
if ( post_password_required() ) { ?>
<p class="nocomments">This post is password protected. Enter the password to view comments.</p>
<?php
return;
}
/* This variable is for alternating comment background */

View File

@ -802,11 +802,9 @@ function comments_popup_link( $zero = 'No Comments', $one = '1 Comment', $more =
return;
}
if ( !empty($post->post_password) ) { // if there's a password
if ( !isset($_COOKIE['wp-postpass_' . COOKIEHASH]) || $_COOKIE['wp-postpass_' . COOKIEHASH] != $post->post_password ) { // and it doesn't match the cookie
echo __('Enter your password to view comments');
return;
}
if ( post_password_required() ) {
echo __('Enter your password to view comments');
return;
}
echo '<a href="';

View File

@ -394,8 +394,7 @@ function html_type_rss() {
* @uses get_post_custom() To get the current post enclosure metadata.
*/
function rss_enclosure() {
global $post;
if ( !empty($post->post_password) && (!isset($_COOKIE['wp-postpass_'.COOKIEHASH]) || $_COOKIE['wp-postpass_'.COOKIEHASH] != $post->post_password) )
if ( post_password_required() )
return;
foreach ( (array) get_post_custom() as $key => $val) {
@ -426,8 +425,7 @@ function rss_enclosure() {
* @uses get_post_custom() To get the current post enclosure metadata.
*/
function atom_enclosure() {
global $post;
if ( !empty($post->post_password) && ($_COOKIE['wp-postpass_'.COOKIEHASH] != $post->post_password) )
if ( post_password_required() )
return;
foreach ( (array) get_post_custom() as $key => $val ) {

View File

@ -90,11 +90,9 @@ function get_the_content($more_link_text = NULL, $stripteaser = 0, $more_file =
$output = '';
if ( !empty($post->post_password) ) { // if there's a password
if ( !isset($_COOKIE['wp-postpass_'.COOKIEHASH]) || stripslashes($_COOKIE['wp-postpass_'.COOKIEHASH]) != $post->post_password ) { // and it doesn't match the cookie
$output = get_the_password_form();
return $output;
}
if ( post_password_required($post) ) { // If post password required and it doesn't match the cookie
$output = get_the_password_form();
return $output;
}
if ( $more_file != '' )
@ -145,11 +143,9 @@ function get_the_excerpt($deprecated = '') {
global $post;
$output = '';
$output = $post->post_excerpt;
if ( !empty($post->post_password) ) { // if there's a password
if ( !isset($_COOKIE['wp-postpass_'.COOKIEHASH]) || $_COOKIE['wp-postpass_'.COOKIEHASH] != $post->post_password ) { // and it doesn't match the cookie
$output = __('There is no excerpt because this is a protected post.');
return $output;
}
if ( post_password_required($post) ) {
$output = __('There is no excerpt because this is a protected post.');
return $output;
}
return apply_filters('get_the_excerpt', $output);
@ -227,6 +223,33 @@ function get_post_class( $class = '', $post_id = null ) {
return apply_filters('post_class', $classes, $class, $post_id);
}
/**
* Determines if post requires a password and if the correct password has been provided
*
* {@internal Missing Long Description}}
*
* @package WordPress
* @subpackage Post
* @since 2.7
*
* @param int|object $post An optional post. Global $post used if not provided.
* @return bool false if a password is not required or the correct password cookie is present, true otherwise
*/
function post_password_required( $post = null ) {
$post = get_post($post);
if ( empty($post->post_password) )
return false;
if ( !isset($_COOKIE['wp-postpass_' . COOKIEHASH]) )
return true;
if ( $_COOKIE['wp-postpass_' . COOKIEHASH] != $post->post_password )
return true;
return false;
}
/**
* Echo "sticky" CSS class if a post is sticky
*