Make WordPress Core

Ticket #38855: 38855.diff

File 38855.diff, 1.8 KB (added by rachelbaker, 9 years ago)

New rest_allow_anonymous_comments filter

  • src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

     
    366366         * @return WP_Error|bool True if the request has access to create items, error object otherwise.
    367367         */
    368368        public function create_item_permissions_check( $request ) {
    369 
    370369                if ( ! is_user_logged_in() && get_option( 'comment_registration' ) ) {
    371370                        return new WP_Error( 'rest_comment_login_required', __( 'Sorry, you must be logged in to comment.' ), array( 'status' => 401 ) );
    372371                }
    373372
     373                /**
     374                 * Filters whether comments can be created without authentication.
     375                 *
     376                 * Enables creating comments for anonymous users.
     377                 *
     378                 * @since 4.7.0
     379                 *
     380                 * @param bool            $allow_anonymous Whether to allow anonymous
     381                 *                                         comments to be created.
     382                 *                                         Default `false`.
     383                 * @param WP_REST_Request $request         Request used to generate the
     384                 *                                         response.
     385                 */
     386                $allow_anonymous = apply_filters( 'rest_allow_anonymous_comments', false, $request );
     387                if ( ! is_user_logged_in() && false === $allow_anonymous ) {
     388                        return new WP_Error( 'rest_comment_login_required', __( 'Sorry, you must be logged in to comment.' ), array( 'status' => 401 ) );
     389                }
     390
    374391                // Limit who can set comment `author`, `author_ip` or `status` to anything other than the default.
    375392                if ( isset( $request['author'] ) && get_current_user_id() !== $request['author'] && ! current_user_can( 'moderate_comments' ) ) {
    376393                        /* translators: %s: request parameter */