Changeset 33021
- Timestamp:
- 07/01/2015 12:07:28 PM (9 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/comment.php
r32964 r33021 2272 2272 * 2273 2273 * @since 1.5.0 2274 * @since 4.3.0 'comment_agent' and 'comment_author_IP' can be set via `$commentdata`. 2274 2275 * 2275 2276 * @see wp_insert_comment() … … 2310 2311 $commentdata['comment_parent'] = ( 'approved' == $parent_status || 'unapproved' == $parent_status ) ? $commentdata['comment_parent'] : 0; 2311 2312 2312 $commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR'] ); 2313 $commentdata['comment_agent'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? substr( $_SERVER['HTTP_USER_AGENT'], 0, 254 ) : ''; 2313 if ( ! isset( $commentdata['comment_author_IP'] ) ) { 2314 $commentdata['comment_author_IP'] = $_SERVER['REMOTE_ADDR']; 2315 } 2316 $commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '', $commentdata['comment_author_IP'] ); 2317 2318 if ( ! isset( $commentdata['comment_agent'] ) ) { 2319 $commentdata['comment_agent'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT']: ''; 2320 } 2321 $commentdata['comment_agent'] = substr( $commentdata['comment_agent'], 0, 254 ); 2314 2322 2315 2323 if ( empty( $commentdata['comment_date'] ) ) { -
trunk/tests/phpunit/tests/comment.php
r32692 r33021 114 114 } 115 115 116 /** 117 * @ticket 14601 118 */ 119 public function test_wp_new_comment_respects_author_ip() { 120 $u = $this->factory->user->create(); 121 $post_id = $this->factory->post->create( array( 'post_author' => $u ) ); 122 123 $data = array( 124 'comment_post_ID' => $post_id, 125 'comment_author' => rand_str(), 126 'comment_author_IP' => '192.168.1.1', 127 'comment_author_url' => '', 128 'comment_author_email' => '', 129 'comment_type' => '', 130 'comment_content' => rand_str(), 131 ); 132 133 $id = wp_new_comment( $data ); 134 135 $comment = get_comment( $id ); 136 137 $this->assertEquals( $data['comment_author_IP'], $comment->comment_author_IP ); 138 } 139 140 /** 141 * @ticket 14601 142 */ 143 public function test_wp_new_comment_respects_author_ip_empty_string() { 144 $u = $this->factory->user->create(); 145 $post_id = $this->factory->post->create( array( 'post_author' => $u ) ); 146 147 $data = array( 148 'comment_post_ID' => $post_id, 149 'comment_author' => rand_str(), 150 'comment_author_IP' => '', 151 'comment_author_url' => '', 152 'comment_author_email' => '', 153 'comment_type' => '', 154 'comment_content' => rand_str(), 155 ); 156 157 $id = wp_new_comment( $data ); 158 159 $comment = get_comment( $id ); 160 161 $this->assertEquals( $data['comment_author_IP'], $comment->comment_author_IP ); 162 } 163 164 /** 165 * @ticket 14601 166 */ 167 public function test_wp_new_comment_respects_comment_agent() { 168 $u = $this->factory->user->create(); 169 $post_id = $this->factory->post->create( array( 'post_author' => $u ) ); 170 171 $data = array( 172 'comment_post_ID' => $post_id, 173 'comment_author' => rand_str(), 174 'comment_author_IP' => '', 175 'comment_author_url' => '', 176 'comment_author_email' => '', 177 'comment_agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53', 178 'comment_type' => '', 179 'comment_content' => rand_str(), 180 ); 181 182 $id = wp_new_comment( $data ); 183 184 $comment = get_comment( $id ); 185 186 $this->assertEquals( $data['comment_agent'], $comment->comment_agent ); 187 } 188 189 /** 190 * @ticket 14601 191 */ 192 public function test_wp_new_comment_should_trim_provided_comment_agent_to_254_chars() { 193 $u = $this->factory->user->create(); 194 $post_id = $this->factory->post->create( array( 'post_author' => $u ) ); 195 196 $data = array( 197 'comment_post_ID' => $post_id, 198 'comment_author' => rand_str(), 199 'comment_author_IP' => '', 200 'comment_author_url' => '', 201 'comment_author_email' => '', 202 'comment_agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53 Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16 Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en; rv:1.8.1.4pre) Gecko/20070511 Camino/1.6pre', 203 'comment_type' => '', 204 'comment_content' => rand_str(), 205 ); 206 207 $id = wp_new_comment( $data ); 208 209 $comment = get_comment( $id ); 210 211 $this->assertEquals( 'Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53 Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16 Mozilla/5.0 (Macintosh; U; PPC Mac OS ', $comment->comment_agent ); 212 } 213 214 /** 215 * @ticket 14601 216 */ 217 public function test_wp_new_comment_respects_comment_agent_empty_string() { 218 $u = $this->factory->user->create(); 219 $post_id = $this->factory->post->create( array( 'post_author' => $u ) ); 220 221 $data = array( 222 'comment_post_ID' => $post_id, 223 'comment_author' => rand_str(), 224 'comment_author_IP' => '', 225 'comment_author_url' => '', 226 'comment_author_email' => '', 227 'comment_agent' => '', 228 'comment_type' => '', 229 'comment_content' => rand_str(), 230 ); 231 232 $id = wp_new_comment( $data ); 233 234 $comment = get_comment( $id ); 235 236 $this->assertEquals( $data['comment_agent'], $comment->comment_agent ); 237 } 238 239 116 240 public function test_comment_field_lengths() { 117 241 // `wp_new_comment()` checks REMOTE_ADDR, so we fake it to avoid PHP notices.
Note: See TracChangeset
for help on using the changeset viewer.