Make WordPress Core


Ignore:
Timestamp:
05/02/2018 10:10:30 PM (6 years ago)
Author:
SergeyBiryukov
Message:

Add a checkbox to the comment form so logged out users can opt-out of commenter cookies.

Props lakenh, xkon, birgire, azaozz, johnbillion.
Merges [42772] and [43042] to the 4.9 branch.
See #43436.

Location:
branches/4.9
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.9

  • branches/4.9/src/wp-includes/comment.php

    r43110 r43127  
    523523 * to recall previous comments by this commentator that are still held in moderation.
    524524 *
    525  * @param WP_Comment $comment Comment object.
    526  * @param object     $user    Comment author's object.
    527  *
    528525 * @since 3.4.0
    529  */
    530 function wp_set_comment_cookies($comment, $user) {
    531     if ( $user->exists() )
     526 * @since 4.9.6 The `$cookies_consent` parameter was added.
     527 *
     528 * @param WP_Comment $comment         Comment object.
     529 * @param WP_User    $user            Comment author's user object. The user may not exist.
     530 * @param boolean    $cookies_consent Optional. Comment author's consent to store cookies. Default true.
     531 */
     532function wp_set_comment_cookies( $comment, $user, $cookies_consent = true ) {
     533    // If the user already exists, or the user opted out of cookies, don't set cookies.
     534    if ( $user->exists() ) {
    532535        return;
     536    }
     537
     538    if ( false === $cookies_consent ) {
     539        // Remove any existing cookies.
     540        $past = time() - YEAR_IN_SECONDS;
     541        setcookie( 'comment_author_' . COOKIEHASH, ' ', $past, COOKIEPATH, COOKIE_DOMAIN );
     542        setcookie( 'comment_author_email_' . COOKIEHASH, ' ', $past, COOKIEPATH, COOKIE_DOMAIN );
     543        setcookie( 'comment_author_url_' . COOKIEHASH, ' ', $past, COOKIEPATH, COOKIE_DOMAIN );
     544
     545        return;
     546    }
    533547
    534548    /**
     
    539553     * @param int $seconds Comment cookie lifetime. Default 30000000.
    540554     */
    541     $comment_cookie_lifetime = apply_filters( 'comment_cookie_lifetime', 30000000 );
     555    $comment_cookie_lifetime = time() + apply_filters( 'comment_cookie_lifetime', 30000000 );
    542556    $secure = ( 'https' === parse_url( home_url(), PHP_URL_SCHEME ) );
    543     setcookie( 'comment_author_' . COOKIEHASH, $comment->comment_author, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
    544     setcookie( 'comment_author_email_' . COOKIEHASH, $comment->comment_author_email, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
    545     setcookie( 'comment_author_url_' . COOKIEHASH, esc_url($comment->comment_author_url), time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
     557    setcookie( 'comment_author_' . COOKIEHASH, $comment->comment_author, $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
     558    setcookie( 'comment_author_email_' . COOKIEHASH, $comment->comment_author_email, $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
     559    setcookie( 'comment_author_url_' . COOKIEHASH, esc_url( $comment->comment_author_url ), $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
    546560}
    547561
Note: See TracChangeset for help on using the changeset viewer.