post_password_required(). fixes #7679

git-svn-id: http://svn.automattic.com/wordpress/trunk@8800 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2008-09-03 19:54:14 +00:00
parent c7e516b25d
commit cddfd974e0
7 changed files with 45 additions and 31 deletions

View File

@ -33,7 +33,7 @@ $commenter = wp_get_current_commenter();
extract($commenter); extract($commenter);
$comments = get_approved_comments($id); $comments = get_approved_comments($id);
$commentstatus = get_post($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()); echo(get_the_password_form());
} else { ?> } 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> <p><?php _e('Enter your password to view comments.'); ?></p>
<?php return; endif; ?> <?php return; endif; ?>

View File

@ -33,7 +33,7 @@ $commenter = wp_get_current_commenter();
extract($commenter); extract($commenter);
$comments = get_approved_comments($id); $comments = get_approved_comments($id);
$post = get_post($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()); echo(get_the_password_form());
} else { ?> } else { ?>

View File

@ -2,15 +2,10 @@
if (!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME'])) if (!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
die ('Please do not load this page directly. Thanks!'); die ('Please do not load this page directly. Thanks!');
if (!empty($post->post_password)) { // if there's a password if ( post_password_required() ) { ?>
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;
<p class="nocomments">This post is password protected. Enter the password to view comments.</p>
<?php
return;
}
} }
/* This variable is for alternating comment background */ /* 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; return;
} }
if ( !empty($post->post_password) ) { // if there's a password if ( post_password_required() ) {
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');
echo __('Enter your password to view comments'); return;
return;
}
} }
echo '<a href="'; echo '<a href="';

View File

@ -394,8 +394,7 @@ function html_type_rss() {
* @uses get_post_custom() To get the current post enclosure metadata. * @uses get_post_custom() To get the current post enclosure metadata.
*/ */
function rss_enclosure() { function rss_enclosure() {
global $post; if ( post_password_required() )
if ( !empty($post->post_password) && (!isset($_COOKIE['wp-postpass_'.COOKIEHASH]) || $_COOKIE['wp-postpass_'.COOKIEHASH] != $post->post_password) )
return; return;
foreach ( (array) get_post_custom() as $key => $val) { 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. * @uses get_post_custom() To get the current post enclosure metadata.
*/ */
function atom_enclosure() { function atom_enclosure() {
global $post; if ( post_password_required() )
if ( !empty($post->post_password) && ($_COOKIE['wp-postpass_'.COOKIEHASH] != $post->post_password) )
return; return;
foreach ( (array) get_post_custom() as $key => $val ) { 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 = ''; $output = '';
if ( !empty($post->post_password) ) { // if there's a password if ( post_password_required($post) ) { // If post password required and it doesn't match the cookie
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();
$output = get_the_password_form(); return $output;
return $output;
}
} }
if ( $more_file != '' ) if ( $more_file != '' )
@ -145,11 +143,9 @@ function get_the_excerpt($deprecated = '') {
global $post; global $post;
$output = ''; $output = '';
$output = $post->post_excerpt; $output = $post->post_excerpt;
if ( !empty($post->post_password) ) { // if there's a password if ( post_password_required($post) ) {
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.');
$output = __('There is no excerpt because this is a protected post.'); return $output;
return $output;
}
} }
return apply_filters('get_the_excerpt', $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); 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 * Echo "sticky" CSS class if a post is sticky
* *