From dc133c3f2d96e1cc809389b79b325149fc79eb54 Mon Sep 17 00:00:00 2001 From: Ryan McCue Date: Tue, 17 Jan 2017 05:18:41 +0000 Subject: [PATCH] REST API: Allow shortcircuiting rest_pre_insert_comment rest_pre_insert_{post_type} allows returning a WP_Error from the filter to shortcircuit actually creating the object, so it makes sense to do so for comments too. Props dspilka. Fixes #39578. Built from https://develop.svn.wordpress.org/trunk@39922 git-svn-id: http://core.svn.wordpress.org/trunk@39859 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../endpoints/class-wp-rest-comments-controller.php | 8 +++++++- wp-includes/version.php | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index fbe5259334..d7a34f3301 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -552,13 +552,19 @@ class WP_REST_Comments_Controller extends WP_REST_Controller { * Filters a comment before it is inserted via the REST API. * * Allows modification of the comment right before it is inserted via wp_insert_comment(). + * Returning a WP_Error value from the filter will shortcircuit insertion and allow + * skipping further processing. * * @since 4.7.0 + * @since 4.8.0 $prepared_comment can now be a WP_Error to shortcircuit insertion. * - * @param array $prepared_comment The prepared comment data for wp_insert_comment(). + * @param array|WP_Error $prepared_comment The prepared comment data for wp_insert_comment(). * @param WP_REST_Request $request Request used to insert the comment. */ $prepared_comment = apply_filters( 'rest_pre_insert_comment', $prepared_comment, $request ); + if ( is_wp_error( $prepared_comment ) ) { + return $prepared_comment; + } $comment_id = wp_insert_comment( wp_filter_comment( wp_slash( (array) $prepared_comment ) ) ); diff --git a/wp-includes/version.php b/wp-includes/version.php index 7b63b79fd4..6a0d0539ef 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.8-alpha-39921'; +$wp_version = '4.8-alpha-39922'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.