|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * A custom REST server for Gutenberg. |
| 4 | + * |
| 5 | + * @package gutenberg |
| 6 | + * @since 6.8.0 |
| 7 | + */ |
| 8 | + |
| 9 | +// Create a new class that extends WP_REST_Comments_Controller |
| 10 | +class Gutenberg_REST_Comment_Controller_6_8 extends WP_REST_Comments_Controller { |
| 11 | + |
| 12 | + public function create_item_permissions_check( $request ) { |
| 13 | + if ( ! is_user_logged_in() ) { |
| 14 | + if ( get_option( 'comment_registration' ) ) { |
| 15 | + return new WP_Error( |
| 16 | + 'rest_comment_login_required', |
| 17 | + __( 'Sorry, you must be logged in to comment.' ), |
| 18 | + array( 'status' => 401 ) |
| 19 | + ); |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Filters whether comments can be created via the REST API without authentication. |
| 24 | + * |
| 25 | + * Enables creating comments for anonymous users. |
| 26 | + * |
| 27 | + * @since 4.7.0 |
| 28 | + * |
| 29 | + * @param bool $allow_anonymous Whether to allow anonymous comments to |
| 30 | + * be created. Default `false`. |
| 31 | + * @param WP_REST_Request $request Request used to generate the |
| 32 | + * response. |
| 33 | + */ |
| 34 | + $allow_anonymous = apply_filters( 'rest_allow_anonymous_comments', false, $request ); |
| 35 | + |
| 36 | + if ( ! $allow_anonymous ) { |
| 37 | + return new WP_Error( |
| 38 | + 'rest_comment_login_required', |
| 39 | + __( 'Sorry, you must be logged in to comment.' ), |
| 40 | + array( 'status' => 401 ) |
| 41 | + ); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + // Limit who can set comment `author`, `author_ip` or `status` to anything other than the default. |
| 46 | + if ( isset( $request['author'] ) && get_current_user_id() !== $request['author'] && ! current_user_can( 'moderate_comments' ) ) { |
| 47 | + return new WP_Error( |
| 48 | + 'rest_comment_invalid_author', |
| 49 | + /* translators: %s: Request parameter. */ |
| 50 | + sprintf( __( "Sorry, you are not allowed to edit '%s' for comments." ), 'author' ), |
| 51 | + array( 'status' => rest_authorization_required_code() ) |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + if ( isset( $request['author_ip'] ) && ! current_user_can( 'moderate_comments' ) ) { |
| 56 | + if ( empty( $_SERVER['REMOTE_ADDR'] ) || $request['author_ip'] !== $_SERVER['REMOTE_ADDR'] ) { |
| 57 | + return new WP_Error( |
| 58 | + 'rest_comment_invalid_author_ip', |
| 59 | + /* translators: %s: Request parameter. */ |
| 60 | + sprintf( __( "Sorry, you are not allowed to edit '%s' for comments." ), 'author_ip' ), |
| 61 | + array( 'status' => rest_authorization_required_code() ) |
| 62 | + ); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if ( isset( $request['status'] ) && ! current_user_can( 'moderate_comments' ) ) { |
| 67 | + return new WP_Error( |
| 68 | + 'rest_comment_invalid_status', |
| 69 | + /* translators: %s: Request parameter. */ |
| 70 | + sprintf( __( "Sorry, you are not allowed to edit '%s' for comments." ), 'status' ), |
| 71 | + array( 'status' => rest_authorization_required_code() ) |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + if ( empty( $request['post'] ) ) { |
| 76 | + return new WP_Error( |
| 77 | + 'rest_comment_invalid_post_id', |
| 78 | + __( 'Sorry, you are not allowed to create this comment without a post.' ), |
| 79 | + array( 'status' => 403 ) |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + $post = get_post( (int) $request['post'] ); |
| 84 | + |
| 85 | + if ( ! $post ) { |
| 86 | + return new WP_Error( |
| 87 | + 'rest_comment_invalid_post_id', |
| 88 | + __( 'Sorry, you are not allowed to create this comment without a post.' ), |
| 89 | + array( 'status' => 403 ) |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + if ( 'draft' === $post->post_status && 'comment' === $request['comment_type'] ) { |
| 94 | + return new WP_Error( |
| 95 | + 'rest_comment_draft_post', |
| 96 | + __( 'Sorry, you are not allowed to create a comment on this post.' ), |
| 97 | + array( 'status' => 403 ) |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + if ( 'trash' === $post->post_status ) { |
| 102 | + return new WP_Error( |
| 103 | + 'rest_comment_trash_post', |
| 104 | + __( 'Sorry, you are not allowed to create a comment on this post.' ), |
| 105 | + array( 'status' => 403 ) |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + if ( ! $this->check_read_post_permission( $post, $request ) ) { |
| 110 | + return new WP_Error( |
| 111 | + 'rest_cannot_read_post', |
| 112 | + __( 'Sorry, you are not allowed to read the post for this comment.' ), |
| 113 | + array( 'status' => rest_authorization_required_code() ) |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + if ( ! comments_open( $post->ID ) && 'comment' === $request['comment_type'] ) { |
| 118 | + return new WP_Error( |
| 119 | + 'rest_comment_closed', |
| 120 | + __( 'Sorry, comments are closed for this item.' ), |
| 121 | + array( 'status' => 403 ) |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + return true; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +add_action( |
| 130 | + 'rest_api_init', |
| 131 | + function () { |
| 132 | + $controller = new Gutenberg_REST_Comment_Controller_6_8(); |
| 133 | + $controller->register_routes(); |
| 134 | + } |
| 135 | +); |
0 commit comments