/**
  * Prepare a single comment to be inserted into the database.
  *
  * @param  WP_REST_Request $request Request object.
  * @return array|WP_Error  $prepared_comment
  */
 protected function prepare_item_for_database($request)
 {
     $prepared_comment = array();
     if (isset($request['content'])) {
         $prepared_comment['comment_content'] = $request['content'];
     }
     if (isset($request['post'])) {
         $prepared_comment['comment_post_ID'] = (int) $request['post'];
     }
     if (isset($request['parent'])) {
         $prepared_comment['comment_parent'] = $request['parent'];
     }
     if (isset($request['author'])) {
         $prepared_comment['user_id'] = $request['author'];
     }
     if (isset($request['author_name'])) {
         $prepared_comment['comment_author'] = $request['author_name'];
     }
     if (isset($request['author_email'])) {
         $prepared_comment['comment_author_email'] = $request['author_email'];
     }
     if (isset($request['author_url'])) {
         $prepared_comment['comment_author_url'] = $request['author_url'];
     }
     if (isset($request['type'])) {
         $prepared_comment['comment_type'] = $request['type'];
     }
     if (isset($request['karma'])) {
         $prepared_comment['comment_karma'] = $request['karma'];
     }
     if (!empty($request['date'])) {
         $date_data = rest_get_date_with_gmt($request['date']);
         if (!empty($date_data)) {
             list($prepared_comment['comment_date'], $prepared_comment['comment_date_gmt']) = $date_data;
         } else {
             return new WP_Error('rest_invalid_date', __('The date you provided is invalid.'), array('status' => 400));
         }
     } elseif (!empty($request['date_gmt'])) {
         $date_data = rest_get_date_with_gmt($request['date_gmt'], true);
         if (!empty($date_data)) {
             list($prepared_comment['comment_date'], $prepared_comment['comment_date_gmt']) = $date_data;
         } else {
             return new WP_Error('rest_invalid_date', __('The date you provided is invalid.'), array('status' => 400));
         }
     }
     return apply_filters('rest_preprocess_comment', $prepared_comment, $request);
 }
 /**
  * Prepare a single comment to be inserted into the database.
  *
  * @param  WP_REST_Request $request Request object.
  * @return array|WP_Error  $prepared_comment
  */
 protected function prepare_item_for_database($request)
 {
     $prepared_comment = array();
     if (isset($request['content'])) {
         $prepared_comment['comment_content'] = $request['content'];
     }
     if (isset($request['post'])) {
         $prepared_comment['comment_post_ID'] = (int) $request['post'];
     }
     if (isset($request['parent'])) {
         $prepared_comment['comment_parent'] = $request['parent'];
     }
     if (isset($request['author'])) {
         $prepared_comment['user_id'] = $request['author'];
     }
     if (isset($request['author_name'])) {
         $prepared_comment['comment_author'] = $request['author_name'];
     }
     if (isset($request['author_email'])) {
         $prepared_comment['comment_author_email'] = $request['author_email'];
     }
     if (isset($request['author_url'])) {
         $prepared_comment['comment_author_url'] = $request['author_url'];
     }
     if (isset($request['type'])) {
         $prepared_comment['comment_type'] = $request['type'];
     }
     if (isset($request['karma'])) {
         $prepared_comment['comment_karma'] = $request['karma'];
     }
     if (!empty($request['date'])) {
         $date_data = rest_get_date_with_gmt($request['date']);
         if (!empty($date_data)) {
             list($prepared_comment['comment_date'], $prepared_comment['comment_date_gmt']) = $date_data;
         }
     } elseif (!empty($request['date_gmt'])) {
         $date_data = rest_get_date_with_gmt($request['date_gmt'], true);
         if (!empty($date_data)) {
             list($prepared_comment['comment_date'], $prepared_comment['comment_date_gmt']) = $date_data;
         }
     }
     return apply_filters('rest_preprocess_comment', $prepared_comment, $request);
 }
 /**
  * Prepares a single post for create or update.
  *
  * @since 4.7.0
  * @access protected
  *
  * @param WP_REST_Request $request Request object.
  * @return stdClass|WP_Error Post object or WP_Error.
  */
 protected function prepare_item_for_database($request)
 {
     $prepared_post = new stdClass();
     // Post ID.
     if (isset($request['id'])) {
         $prepared_post->ID = absint($request['id']);
     }
     $schema = $this->get_item_schema();
     // Post title.
     if (!empty($schema['properties']['title']) && isset($request['title'])) {
         if (is_string($request['title'])) {
             $prepared_post->post_title = $request['title'];
         } elseif (!empty($request['title']['raw'])) {
             $prepared_post->post_title = $request['title']['raw'];
         }
     }
     // Post content.
     if (!empty($schema['properties']['content']) && isset($request['content'])) {
         if (is_string($request['content'])) {
             $prepared_post->post_content = $request['content'];
         } elseif (isset($request['content']['raw'])) {
             $prepared_post->post_content = $request['content']['raw'];
         }
     }
     // Post excerpt.
     if (!empty($schema['properties']['excerpt']) && isset($request['excerpt'])) {
         if (is_string($request['excerpt'])) {
             $prepared_post->post_excerpt = $request['excerpt'];
         } elseif (isset($request['excerpt']['raw'])) {
             $prepared_post->post_excerpt = $request['excerpt']['raw'];
         }
     }
     // Post type.
     if (empty($request['id'])) {
         // Creating new post, use default type for the controller.
         $prepared_post->post_type = $this->post_type;
     } else {
         // Updating a post, use previous type.
         $prepared_post->post_type = get_post_type($request['id']);
     }
     $post_type = get_post_type_object($prepared_post->post_type);
     // Post status.
     if (!empty($schema['properties']['status']) && isset($request['status'])) {
         $status = $this->handle_status_param($request['status'], $post_type);
         if (is_wp_error($status)) {
             return $status;
         }
         $prepared_post->post_status = $status;
     }
     // Post date.
     if (!empty($schema['properties']['date']) && !empty($request['date'])) {
         $date_data = rest_get_date_with_gmt($request['date']);
         if (!empty($date_data)) {
             list($prepared_post->post_date, $prepared_post->post_date_gmt) = $date_data;
         }
     } elseif (!empty($schema['properties']['date_gmt']) && !empty($request['date_gmt'])) {
         $date_data = rest_get_date_with_gmt($request['date_gmt'], true);
         if (!empty($date_data)) {
             list($prepared_post->post_date, $prepared_post->post_date_gmt) = $date_data;
         }
     }
     // Post slug.
     if (!empty($schema['properties']['slug']) && isset($request['slug'])) {
         $prepared_post->post_name = $request['slug'];
     }
     // Author.
     if (!empty($schema['properties']['author']) && !empty($request['author'])) {
         $post_author = (int) $request['author'];
         if (get_current_user_id() !== $post_author) {
             $user_obj = get_userdata($post_author);
             if (!$user_obj) {
                 return new WP_Error('rest_invalid_author', __('Invalid author ID.'), array('status' => 400));
             }
         }
         $prepared_post->post_author = $post_author;
     }
     // Post password.
     if (!empty($schema['properties']['password']) && isset($request['password'])) {
         $prepared_post->post_password = $request['password'];
         if ('' !== $request['password']) {
             if (!empty($schema['properties']['sticky']) && !empty($request['sticky'])) {
                 return new WP_Error('rest_invalid_field', __('A post can not be sticky and have a password.'), array('status' => 400));
             }
             if (!empty($prepared_post->ID) && is_sticky($prepared_post->ID)) {
                 return new WP_Error('rest_invalid_field', __('A sticky post can not be password protected.'), array('status' => 400));
             }
         }
     }
     if (!empty($schema['properties']['sticky']) && !empty($request['sticky'])) {
         if (!empty($prepared_post->ID) && post_password_required($prepared_post->ID)) {
             return new WP_Error('rest_invalid_field', __('A password protected post can not be set to sticky.'), array('status' => 400));
         }
     }
     // Parent.
     if (!empty($schema['properties']['parent']) && isset($request['parent'])) {
         if (0 === (int) $request['parent']) {
             $prepared_post->post_parent = 0;
         } else {
             $parent = get_post((int) $request['parent']);
             if (empty($parent)) {
                 return new WP_Error('rest_post_invalid_id', __('Invalid post parent ID.'), array('status' => 400));
             }
             $prepared_post->post_parent = (int) $parent->ID;
         }
     }
     // Menu order.
     if (!empty($schema['properties']['menu_order']) && isset($request['menu_order'])) {
         $prepared_post->menu_order = (int) $request['menu_order'];
     }
     // Comment status.
     if (!empty($schema['properties']['comment_status']) && !empty($request['comment_status'])) {
         $prepared_post->comment_status = $request['comment_status'];
     }
     // Ping status.
     if (!empty($schema['properties']['ping_status']) && !empty($request['ping_status'])) {
         $prepared_post->ping_status = $request['ping_status'];
     }
     /**
      * Filters a post before it is inserted via the REST API.
      *
      * The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
      *
      * @since 4.7.0
      *
      * @param stdClass        $prepared_post An object representing a single post prepared
      *                                       for inserting or updating the database.
      * @param WP_REST_Request $request       Request object.
      */
     return apply_filters("rest_pre_insert_{$this->post_type}", $prepared_post, $request);
 }
 /**
  * Prepares a single comment to be inserted into the database.
  *
  * @since 4.7.0
  * @access protected
  *
  * @param WP_REST_Request $request Request object.
  * @return array|WP_Error Prepared comment, otherwise WP_Error object.
  */
 protected function prepare_item_for_database($request)
 {
     $prepared_comment = array();
     /*
      * Allow the comment_content to be set via the 'content' or
      * the 'content.raw' properties of the Request object.
      */
     if (isset($request['content']) && is_string($request['content'])) {
         $prepared_comment['comment_content'] = $request['content'];
     } elseif (isset($request['content']['raw']) && is_string($request['content']['raw'])) {
         $prepared_comment['comment_content'] = $request['content']['raw'];
     }
     if (isset($request['post'])) {
         $prepared_comment['comment_post_ID'] = (int) $request['post'];
     }
     if (isset($request['parent'])) {
         $prepared_comment['comment_parent'] = $request['parent'];
     }
     if (isset($request['author'])) {
         $user = new WP_User($request['author']);
         if ($user->exists()) {
             $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;
         } else {
             return new WP_Error('rest_comment_author_invalid', __('Invalid comment author ID.'), array('status' => 400));
         }
     }
     if (isset($request['author_name'])) {
         $prepared_comment['comment_author'] = $request['author_name'];
     }
     if (isset($request['author_email'])) {
         $prepared_comment['comment_author_email'] = $request['author_email'];
     }
     if (isset($request['author_url'])) {
         $prepared_comment['comment_author_url'] = $request['author_url'];
     }
     if (isset($request['author_ip']) && current_user_can('moderate_comments')) {
         $prepared_comment['comment_author_IP'] = $request['author_ip'];
     } elseif (!empty($_SERVER['REMOTE_ADDR']) && rest_is_ip_address($_SERVER['REMOTE_ADDR'])) {
         $prepared_comment['comment_author_IP'] = $_SERVER['REMOTE_ADDR'];
     } else {
         $prepared_comment['comment_author_IP'] = '127.0.0.1';
     }
     if (!empty($request['author_user_agent'])) {
         $prepared_comment['comment_agent'] = $request['author_user_agent'];
     } elseif ($request->get_header('user_agent')) {
         $prepared_comment['comment_agent'] = $request->get_header('user_agent');
     }
     if (!empty($request['date'])) {
         $date_data = rest_get_date_with_gmt($request['date']);
         if (!empty($date_data)) {
             list($prepared_comment['comment_date'], $prepared_comment['comment_date_gmt']) = $date_data;
         }
     } elseif (!empty($request['date_gmt'])) {
         $date_data = rest_get_date_with_gmt($request['date_gmt'], true);
         if (!empty($date_data)) {
             list($prepared_comment['comment_date'], $prepared_comment['comment_date_gmt']) = $date_data;
         }
     }
     /**
      * Filters a comment after it is prepared for the database.
      *
      * Allows modification of the comment right after it is prepared for the database.
      *
      * @since 4.7.0
      *
      * @param array           $prepared_comment The prepared comment data for `wp_insert_comment`.
      * @param WP_REST_Request $request          The current request.
      */
     return apply_filters('rest_preprocess_comment', $prepared_comment, $request);
 }
 /**
  * Prepare a single post for create or update
  *
  * @param WP_REST_Request $request Request object
  * @return WP_Error|obj $prepared_post Post object
  */
 protected function prepare_item_for_database($request)
 {
     $prepared_post = new stdClass();
     // ID
     if (isset($request['id'])) {
         $prepared_post->ID = absint($request['id']);
     }
     $schema = $this->get_item_schema();
     // Post title
     if (!empty($schema['properties']['title']) && isset($request['title'])) {
         if (is_string($request['title'])) {
             $prepared_post->post_title = wp_filter_post_kses($request['title']);
         } elseif (!empty($request['title']['raw'])) {
             $prepared_post->post_title = wp_filter_post_kses($request['title']['raw']);
         }
     }
     // Post content
     if (!empty($schema['properties']['content']) && isset($request['content'])) {
         if (is_string($request['content'])) {
             $prepared_post->post_content = wp_filter_post_kses($request['content']);
         } elseif (isset($request['content']['raw'])) {
             $prepared_post->post_content = wp_filter_post_kses($request['content']['raw']);
         }
     }
     // Post excerpt
     if (!empty($schema['properties']['excerpt']) && isset($request['excerpt'])) {
         if (is_string($request['excerpt'])) {
             $prepared_post->post_excerpt = wp_filter_post_kses($request['excerpt']);
         } elseif (isset($request['excerpt']['raw'])) {
             $prepared_post->post_excerpt = wp_filter_post_kses($request['excerpt']['raw']);
         }
     }
     // Post type
     if (empty($request['id'])) {
         // Creating new post, use default type for the controller
         $prepared_post->post_type = $this->post_type;
     } else {
         // Updating a post, use previous type.
         $prepared_post->post_type = get_post_type($request['id']);
     }
     $post_type = get_post_type_object($prepared_post->post_type);
     // Post status
     if (isset($request['status'])) {
         $status = $this->handle_status_param($request['status'], $post_type);
         if (is_wp_error($status)) {
             return $status;
         }
         $prepared_post->post_status = $status;
     }
     // Post date
     if (!empty($request['date'])) {
         $date_data = rest_get_date_with_gmt($request['date']);
         if (!empty($date_data)) {
             list($prepared_post->post_date, $prepared_post->post_date_gmt) = $date_data;
         } else {
             return new WP_Error('rest_invalid_date', __('The date you provided is invalid.'), array('status' => 400));
         }
     } elseif (!empty($request['date_gmt'])) {
         $date_data = rest_get_date_with_gmt($request['date_gmt'], true);
         if (!empty($date_data)) {
             list($prepared_post->post_date, $prepared_post->post_date_gmt) = $date_data;
         } else {
             return new WP_Error('rest_invalid_date', __('The date you provided is invalid.'), array('status' => 400));
         }
     }
     // Post slug
     if (isset($request['slug'])) {
         $prepared_post->post_name = $request['slug'];
     }
     // Author
     if (!empty($schema['properties']['author']) && !empty($request['author'])) {
         $author = $this->handle_author_param($request['author'], $post_type);
         if (is_wp_error($author)) {
             return $author;
         }
         $prepared_post->post_author = $author;
     }
     // Post password
     if (isset($request['password'])) {
         $prepared_post->post_password = $request['password'];
         if (!empty($schema['properties']['sticky']) && !empty($request['sticky'])) {
             return new WP_Error('rest_invalid_field', __('A post can not be sticky and have a password.'), array('status' => 400));
         }
         if (!empty($prepared_post->ID) && is_sticky($prepared_post->ID)) {
             return new WP_Error('rest_invalid_field', __('A sticky post can not be password protected.'), array('status' => 400));
         }
     }
     if (!empty($request['sticky'])) {
         if (!empty($prepared_post->ID) && post_password_required($prepared_post->ID)) {
             return new WP_Error('rest_invalid_field', __('A password protected post can not be set to sticky.'), array('status' => 400));
         }
     }
     // Parent
     $post_type_obj = get_post_type_object($this->post_type);
     if (!empty($schema['properties']['parent']) && !empty($request['parent'])) {
         $parent = get_post((int) $request['parent']);
         if (empty($parent)) {
             return new WP_Error('rest_post_invalid_id', __('Invalid post parent ID.'), array('status' => 400));
         }
         $prepared_post->post_parent = (int) $parent->ID;
     }
     // Menu order
     if (!empty($schema['properties']['menu_order']) && isset($request['menu_order'])) {
         $prepared_post->menu_order = (int) $request['menu_order'];
     }
     // Comment status
     if (!empty($schema['properties']['comment_status']) && !empty($request['comment_status'])) {
         $prepared_post->comment_status = $request['comment_status'];
     }
     // Ping status
     if (!empty($schema['properties']['ping_status']) && !empty($request['ping_status'])) {
         $prepared_post->ping_status = $request['ping_status'];
     }
     return apply_filters('rest_pre_insert_' . $this->post_type, $prepared_post, $request);
 }
 /**
  * Prepares a single comment to be inserted into the database.
  *
  * @since 4.7.0
  * @access protected
  *
  * @param WP_REST_Request $request Request object.
  * @return array|WP_Error Prepared comment, otherwise WP_Error object.
  */
 protected function prepare_item_for_database($request)
 {
     $prepared_comment = array();
     /*
      * Allow the comment_content to be set via the 'content' or
      * the 'content.raw' properties of the Request object.
      */
     if (isset($request['content']) && is_string($request['content'])) {
         $prepared_comment['comment_content'] = $request['content'];
     } elseif (isset($request['content']['raw']) && is_string($request['content']['raw'])) {
         $prepared_comment['comment_content'] = $request['content']['raw'];
     }
     if (isset($request['post'])) {
         $prepared_comment['comment_post_ID'] = (int) $request['post'];
     }
     if (isset($request['parent'])) {
         $prepared_comment['comment_parent'] = $request['parent'];
     }
     if (isset($request['author'])) {
         $user = new WP_User($request['author']);
         if ($user->exists()) {
             $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;
         } else {
             return new WP_Error('rest_comment_author_invalid', __('Invalid comment author id.'), array('status' => 400));
         }
     }
     if (isset($request['author_name'])) {
         $prepared_comment['comment_author'] = $request['author_name'];
     }
     if (isset($request['author_email'])) {
         $prepared_comment['comment_author_email'] = $request['author_email'];
     }
     if (isset($request['author_url'])) {
         $prepared_comment['comment_author_url'] = $request['author_url'];
     }
     if (isset($request['author_ip'])) {
         $prepared_comment['comment_author_IP'] = $request['author_ip'];
     }
     if (isset($request['author_user_agent'])) {
         $prepared_comment['comment_agent'] = $request['author_user_agent'];
     }
     if (isset($request['type'])) {
         // Comment type "comment" needs to be created as an empty string.
         $prepared_comment['comment_type'] = 'comment' === $request['type'] ? '' : $request['type'];
     }
     if (isset($request['karma'])) {
         $prepared_comment['comment_karma'] = $request['karma'];
     }
     if (!empty($request['date'])) {
         $date_data = rest_get_date_with_gmt($request['date']);
         if (!empty($date_data)) {
             list($prepared_comment['comment_date'], $prepared_comment['comment_date_gmt']) = $date_data;
         }
     } elseif (!empty($request['date_gmt'])) {
         $date_data = rest_get_date_with_gmt($request['date_gmt'], true);
         if (!empty($date_data)) {
             list($prepared_comment['comment_date'], $prepared_comment['comment_date_gmt']) = $date_data;
         }
     }
     /**
      * Filters a comment after it is prepared for the database.
      *
      * Allows modification of the comment right after it is prepared for the database.
      *
      * @since 4.7.0
      *
      * @param array           $prepared_comment The prepared comment data for `wp_insert_comment`.
      * @param WP_REST_Request $request          The current request.
      */
     return apply_filters('rest_preprocess_comment', $prepared_comment, $request);
 }