Skip to content
This repository was archived by the owner on Sep 24, 2018. It is now read-only.

Commit e3943de

Browse files
committed
Merge pull request #1422 from WP-API/args-from-schema-comments
Power the Comments endpoint args from the schema, like everything else
2 parents fc9e949 + ea4aa75 commit e3943de

1 file changed

Lines changed: 39 additions & 91 deletions

File tree

lib/endpoints/class-wp-rest-comments-controller.php

Lines changed: 39 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -22,53 +22,7 @@ public function register_routes() {
2222
'methods' => WP_REST_Server::CREATABLE,
2323
'callback' => array( $this, 'create_item' ),
2424
'permission_callback' => array( $this, 'create_item_permissions_check' ),
25-
'args' => array(
26-
'post' => array(
27-
'required' => true,
28-
'sanitize_callback' => 'absint',
29-
),
30-
'type' => array(
31-
'default' => '',
32-
'sanitize_callback' => 'sanitize_key',
33-
),
34-
'parent' => array(
35-
'default' => 0,
36-
'sanitize_callback' => 'absint',
37-
),
38-
'content' => array(
39-
'default' => '',
40-
'sanitize_callback' => 'wp_filter_post_kses',
41-
),
42-
'author' => array(
43-
'default' => 0,
44-
'sanitize_callback' => 'absint',
45-
),
46-
'author_name' => array(
47-
'default' => '',
48-
'sanitize_callback' => 'sanitize_text_field',
49-
),
50-
'author_email' => array(
51-
'default' => '',
52-
'sanitize_callback' => 'sanitize_email',
53-
),
54-
'author_url' => array(
55-
'default' => '',
56-
'sanitize_callback' => 'esc_url_raw',
57-
),
58-
'karma' => array(
59-
'default' => 0,
60-
'sanitize_callback' => 'absint',
61-
),
62-
'status' => array(
63-
'sanitize_callback' => 'sanitize_key',
64-
),
65-
'date' => array(
66-
'default' => current_time( 'mysql' ),
67-
),
68-
'date_gmt' => array(
69-
'default' => current_time( 'mysql', true ),
70-
),
71-
),
25+
'args' => $this->get_endpoint_args_for_item_schema( true ),
7226
),
7327
) );
7428

@@ -87,40 +41,7 @@ public function register_routes() {
8741
'methods' => WP_REST_Server::EDITABLE,
8842
'callback' => array( $this, 'update_item' ),
8943
'permission_callback' => array( $this, 'update_item_permissions_check' ),
90-
'args' => array(
91-
'post' => array(
92-
'sanitize_callback' => 'absint',
93-
),
94-
'type' => array(
95-
'sanitize_callback' => 'sanitize_key',
96-
),
97-
'parent' => array(
98-
'sanitize_callback' => 'absint',
99-
),
100-
'content' => array(
101-
'sanitize_callback' => 'wp_filter_post_kses',
102-
),
103-
'author' => array(
104-
'sanitize_callback' => 'absint',
105-
),
106-
'author_name' => array(
107-
'sanitize_callback' => 'sanitize_text_field',
108-
),
109-
'author_email' => array(
110-
'sanitize_callback' => 'sanitize_email',
111-
),
112-
'author_url' => array(
113-
'sanitize_callback' => 'esc_url_raw',
114-
),
115-
'karma' => array(
116-
'sanitize_callback' => 'absint',
117-
),
118-
'status' => array(
119-
'sanitize_callback' => 'sanitize_key',
120-
),
121-
'date' => array(),
122-
'date_gmt' => array(),
123-
),
44+
'args' => $this->get_endpoint_args_for_item_schema( false ),
12445
),
12546
array(
12647
'methods' => WP_REST_Server::DELETABLE,
@@ -232,8 +153,18 @@ public function create_item( $request ) {
232153
}
233154

234155
$prepared_comment = $this->prepare_item_for_database( $request );
156+
235157
// Setting remaining values before wp_insert_comment so we can
236158
// use wp_allow_comment().
159+
if ( ! isset( $prepared_comment['comment_date_gmt'] ) ) {
160+
$prepared_comment['comment_date_gmt'] = current_time( 'mysql', true );
161+
}
162+
if ( ! isset( $prepared_comment['comment_author_email'] ) ) {
163+
$prepared_comment['comment_author_email'] = '';
164+
}
165+
if ( ! isset( $prepared_comment['comment_author_url'] ) ) {
166+
$prepared_comment['comment_author_url'] = '';
167+
}
237168
$prepared_comment['comment_author_IP'] = '127.0.0.1';
238169
$prepared_comment['comment_agent'] = '';
239170
$prepared_comment['comment_approved'] = wp_allow_comment( $prepared_comment );
@@ -776,48 +707,52 @@ public function get_item_schema() {
776707
'type' => 'integer',
777708
'context' => array( 'view', 'edit', 'embed' ),
778709
'readonly' => true,
779-
),
710+
),
780711
'author' => array(
781712
'description' => 'The ID of the user object, if author was a user.',
782713
'type' => 'integer',
783714
'context' => array( 'view', 'edit', 'embed' ),
784-
),
715+
),
785716
'author_avatar_urls' => array(
786717
'description' => 'Avatar URLs for the object author.',
787718
'type' => 'object',
788719
'context' => array( 'view', 'edit', 'embed' ),
789720
'readonly' => true,
790721
'properties' => $avatar_properties,
791-
),
722+
),
792723
'author_email' => array(
793724
'description' => 'Email address for the object author.',
794725
'type' => 'string',
795726
'format' => 'email',
796727
'context' => array( 'edit' ),
797-
),
728+
),
798729
'author_ip' => array(
799730
'description' => 'IP address for the object author.',
800731
'type' => 'string',
801732
'context' => array( 'edit' ),
802733
'readonly' => true,
803-
),
734+
),
804735
'author_name' => array(
805736
'description' => 'Display name for the object author.',
806737
'type' => 'string',
807738
'context' => array( 'view', 'edit', 'embed' ),
739+
'arg_options' => array(
740+
'sanitize_callback' => 'sanitize_text_field',
741+
'default' => '',
808742
),
743+
),
809744
'author_url' => array(
810745
'description' => 'URL for the object author.',
811746
'type' => 'string',
812747
'format' => 'uri',
813748
'context' => array( 'view', 'edit', 'embed' ),
814-
),
749+
),
815750
'author_user_agent' => array(
816751
'description' => 'User agent for the object author.',
817752
'type' => 'string',
818753
'context' => array( 'edit' ),
819754
'readonly' => true,
820-
),
755+
),
821756
'content' => array(
822757
'description' => 'The content for the object.',
823758
'type' => 'object',
@@ -827,14 +762,18 @@ public function get_item_schema() {
827762
'description' => 'Content for the object, as it exists in the database.',
828763
'type' => 'string',
829764
'context' => array( 'edit' ),
830-
),
765+
),
831766
'rendered' => array(
832767
'description' => 'Content for the object, transformed for display.',
833768
'type' => 'string',
834769
'context' => array( 'view', 'edit', 'embed' ),
835-
),
836770
),
837771
),
772+
'arg_options' => array(
773+
'sanitize_callback' => 'wp_filter_post_kses',
774+
'default' => '',
775+
),
776+
),
838777
'date' => array(
839778
'description' => 'The date the object was published.',
840779
'type' => 'string',
@@ -851,7 +790,6 @@ public function get_item_schema() {
851790
'description' => 'Karma for the object.',
852791
'type' => 'integer',
853792
'context' => array( 'edit' ),
854-
'readonly' => true,
855793
),
856794
'link' => array(
857795
'description' => 'URL to the object.',
@@ -864,6 +802,9 @@ public function get_item_schema() {
864802
'description' => 'The ID for the parent of the object.',
865803
'type' => 'integer',
866804
'context' => array( 'view', 'edit', 'embed' ),
805+
'arg_options' => array(
806+
'default' => 0,
807+
),
867808
),
868809
'post' => array(
869810
'description' => 'The ID of the associated post object.',
@@ -874,11 +815,18 @@ public function get_item_schema() {
874815
'description' => 'State of the object.',
875816
'type' => 'string',
876817
'context' => array( 'view', 'edit' ),
818+
'arg_options' => array(
819+
'sanitize_callback' => 'sanitize_key',
820+
),
877821
),
878822
'type' => array(
879823
'description' => 'Type of Comment for the object.',
880824
'type' => 'string',
881825
'context' => array( 'view', 'edit', 'embed' ),
826+
'arg_options' => array(
827+
'sanitize_callback' => 'sanitize_key',
828+
'default' => '',
829+
),
882830
),
883831
),
884832
);

0 commit comments

Comments
 (0)