Changeset 63708 for branches/5.4/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
- Timestamp:
- 09/17/2026 09:05:55 PM (6 hours ago)
- Location:
- branches/5.4
- Files:
-
- 2 edited
-
. (modified) (1 prop)
-
src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/5.4
- Property svn:mergeinfo changed
/trunk merged: 62658,63656-63657,63659-63660,63665,63669,63672,63675
- Property svn:mergeinfo changed
-
branches/5.4/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
r47391 r63708 769 769 * 770 770 * @since 4.7.0 771 * @since 7.1.0 Target post permissions are checked when a comment's parent post is changed. 771 772 * 772 773 * @param WP_REST_Request $request Full details about the request. … … 785 786 array( 'status' => rest_authorization_required_code() ) 786 787 ); 788 } 789 790 /* 791 * check_edit_permission() above only establishes that the comment may be 792 * edited where it currently sits, because 'edit_comment' maps to 'edit_post' 793 * on the comment's current parent. When the parent is being changed, the new 794 * parent has to be authorized as well. Without this, a user holding 795 * edit_comment on their own comment could reparent it onto any post, 796 * including posts they can neither read nor edit. 797 */ 798 if ( isset( $request['post'] ) && (int) $request['post'] !== (int) $comment->comment_post_ID ) { 799 $target_check = $this->check_target_post_permission( (int) $request['post'] ); 800 801 if ( is_wp_error( $target_check ) ) { 802 return $target_check; 803 } 787 804 } 788 805 … … 1869 1886 return $email; 1870 1887 } 1888 1889 /** 1890 * Checks that a post can receive a comment from the current user. 1891 * 1892 * Used when changing the parent post of an existing comment, so that 1893 * attaching a comment to a post is authorized the same way whichever 1894 * path it arrives by. 1895 * 1896 * @since 7.1.0 1897 * 1898 * @param int $post_id Target post ID. 1899 * @return true|WP_Error True if the post can receive the comment, error object otherwise. 1900 */ 1901 protected function check_target_post_permission( $post_id ) { 1902 if ( ! $post_id ) { 1903 return new WP_Error( 1904 'rest_comment_invalid_post_id', 1905 __( 'Sorry, you are not allowed to create this comment without a post.' ), 1906 array( 'status' => 403 ) 1907 ); 1908 } 1909 1910 /* 1911 * A comment needs either comment moderation rights or edit access to the 1912 * post, which is what check_edit_permission() grants on the post a comment 1913 * is moving away from. Requiring the same at the destination means both 1914 * ends of a move are authorized alike. 1915 */ 1916 if ( ! current_user_can( 'moderate_comments' ) && ! current_user_can( 'edit_post', $post_id ) ) { 1917 return new WP_Error( 1918 'rest_cannot_edit', 1919 __( 'Sorry, you are not allowed to edit this comment.' ), 1920 array( 'status' => rest_authorization_required_code() ) 1921 ); 1922 } 1923 1924 $post = get_post( $post_id ); 1925 1926 if ( ! $post ) { 1927 return new WP_Error( 1928 'rest_comment_invalid_post_id', 1929 __( 'Sorry, you are not allowed to create this comment without a post.' ), 1930 array( 'status' => 403 ) 1931 ); 1932 } 1933 1934 /* 1935 * The create-time draft and comments-open rules are deliberately not applied 1936 * here, because moderators move comments onto posts whose discussion has 1937 * closed and onto drafts today. Enforcing them would break that without 1938 * blocking anything the capability check above already permits. 1939 */ 1940 return true; 1941 } 1871 1942 }
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)