• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WordPress core a2z

WordPress core a2z

WordPress core only

  • Home
  • Plugins
  • Blocks
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / WP_REST_Comments_Controller::create_item() – Creates a comment.

You appear to be a bot. Output may be restricted

Description

Creates a comment.

Usage

$WP_REST_Response|WP_Error = WP_REST_Comments_Controller::create_item( $request );

Parameters

$request
( WP_REST_Request ) required – Full details about the request.

Returns

WP_REST_Response|WP_Error Response object on success, or error object on failure.

Source

File name: wordpress/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php


Lines:

1 to 100 of 205
  public function create_item( $request ) {
    if ( ! empty( $request['id'] ) ) {
      return new WP_Error(
        'rest_comment_exists',
        __( 'Cannot create existing comment.' ),
        array( 'status' => 400 )
      );
    }

    // Do not allow comments to be created with a non-default type.
    if ( ! empty( $request['type'] ) && 'comment' !== $request['type'] ) {
      return new WP_Error(
        'rest_invalid_comment_type',
        __( 'Cannot create a comment with that type.' ),
        array( 'status' => 400 )
      );
    }

    $prepared_comment = $this->WP_REST_Comments_Controller::prepare_item_for_database( $request );
    if ( is_wp_error( $prepared_comment ) ) {
      return $prepared_comment;
    }

    $prepared_comment['comment_type'] = 'comment';

    if ( ! isset( $prepared_comment['comment_content'] ) ) {
      $prepared_comment['comment_content'] = '';
    }

    if ( ! $this->check_is_comment_content_allowed( $prepared_comment ) ) {
      return new WP_Error(
        'rest_comment_content_invalid',
        __( 'Invalid comment content.' ),
        array( 'status' => 400 )
      );
    }

    // Setting remaining values before wp_insert_comment so we can use wp_allow_comment().
    if ( ! isset( $prepared_comment['comment_date_gmt'] ) ) {
      $prepared_comment['comment_date_gmt'] = current_time( 'mysql', true );
    }

    // Set author data if the user's logged in.
    $missing_author = empty( $prepared_comment['user_id'] )
      && empty( $prepared_comment['comment_author'] )
      && empty( $prepared_comment['comment_author_email'] )
      && empty( $prepared_comment['comment_author_url'] );

    if ( is_user_logged_in() && $missing_author ) {
      $user = wp_get_current_user();

      $prepared_comment['user_id']              = $user->ID;
      $prepared_comment['comment_author']       = $user->display_name;
      $prepared_comment['comment_author_email'] = $user->user_email;
      $prepared_comment['comment_author_url']   = $user->user_url;
    }

    // Honor the discussion setting that requires a name and email address of the comment author.
    if ( get_option( 'require_name_email' ) ) {
      if ( empty( $prepared_comment['comment_author'] ) || empty( $prepared_comment['comment_author_email'] ) ) {
        return new WP_Error(
          'rest_comment_author_data_required',
          __( 'Creating a comment requires valid author name and email values.' ),
          array( 'status' => 400 )
        );
      }
    }

    if ( ! isset( $prepared_comment['comment_author_email'] ) ) {
      $prepared_comment['comment_author_email'] = '';
    }

    if ( ! isset( $prepared_comment['comment_author_url'] ) ) {
      $prepared_comment['comment_author_url'] = '';
    }

    if ( ! isset( $prepared_comment['comment_agent'] ) ) {
      $prepared_comment['comment_agent'] = '';
    }

    $check_comment_lengths = wp_check_comment_data_max_lengths( $prepared_comment );

    if ( is_wp_error( $check_comment_lengths ) ) {
      $error_code = $check_comment_lengths->get_error_code();
      return new WP_Error(
        $error_code,
        __( 'Comment field exceeds maximum length allowed.' ),
        array( 'status' => 400 )
      );
    }

    $prepared_comment['comment_approved'] = wp_allow_comment( $prepared_comment, true );

    if ( is_wp_error( $prepared_comment['comment_approved'] ) ) {
      $error_code    = $prepared_comment['comment_approved']->get_error_code();
      $error_message = $prepared_comment['comment_approved']->get_error_message();

      if ( 'comment_duplicate' === $error_code ) {
        return new WP_Error(
          $error_code,
[1] [2] [3] Next »

 View on GitHub View on Trac

Published: 25th November 2019 | Last updated: 9th December 2020

Primary Sidebar

Information

Function name: WP_REST_Comments_Controller::create_item
Class ref: WP_REST_Comments_Controller
Plugin ref: WordPress
Version: 5.6
Sourcefile: wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
File ref: wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
Deprecated?: No
API Letters: C,R,W

Footer

WP-a2z
WordPress core a2z
WordPress core only
WordPress 5.6
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  core.wp-a2z.org
© Copyright WP-a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites