Make WordPress Core


Ignore:
Timestamp:
03/04/2018 04:40:43 PM (7 years ago)
Author:
azaozz
Message:

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

Props lakenh, xkon, birgire, azaozz.
See #43436.

File:
1 edited

Legend:

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

    r42678 r42772  
    543543 * @param WP_Comment $comment Comment object.
    544544 * @param object     $user    Comment author's object.
     545 * @param boolean    $cookies_consent Optional. Comment author's consent to store cookies. Default true.
    545546 *
    546547 * @since 3.4.0
    547548 */
    548 function wp_set_comment_cookies( $comment, $user ) {
     549function wp_set_comment_cookies( $comment, $user, $cookies_consent = true ) {
     550    // If the user already exists, or the user opted out of cookies, don't set cookies.
    549551    if ( $user->exists() ) {
    550552        return;
    551553    }
    552554
     555    if ( false === $cookies_consent ) {
     556        // Remove any existing cookies.
     557        $past = time() - YEAR_IN_SECONDS;
     558        setcookie( 'comment_author_' . COOKIEHASH, ' ', $past, COOKIEPATH, COOKIE_DOMAIN );
     559        setcookie( 'comment_author_email_' . COOKIEHASH, ' ', $past, COOKIEPATH, COOKIE_DOMAIN );
     560        setcookie( 'comment_author_url_' . COOKIEHASH, ' ', $past, COOKIEPATH, COOKIE_DOMAIN );
     561
     562        return;
     563    }
     564
    553565    /**
    554566     * Filters the lifetime of the comment cookie in seconds.
     
    558570     * @param int $seconds Comment cookie lifetime. Default 30000000.
    559571     */
    560     $comment_cookie_lifetime = apply_filters( 'comment_cookie_lifetime', 30000000 );
     572    $comment_cookie_lifetime = time() + apply_filters( 'comment_cookie_lifetime', 30000000 );
    561573    $secure                  = ( 'https' === parse_url( home_url(), PHP_URL_SCHEME ) );
    562     setcookie( 'comment_author_' . COOKIEHASH, $comment->comment_author, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
    563     setcookie( 'comment_author_email_' . COOKIEHASH, $comment->comment_author_email, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
    564     setcookie( 'comment_author_url_' . COOKIEHASH, esc_url( $comment->comment_author_url ), time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
     574    setcookie( 'comment_author_' . COOKIEHASH, $comment->comment_author, $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
     575    setcookie( 'comment_author_email_' . COOKIEHASH, $comment->comment_author_email, $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
     576    setcookie( 'comment_author_url_' . COOKIEHASH, esc_url( $comment->comment_author_url ), $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
    565577}
    566578
Note: See TracChangeset for help on using the changeset viewer.