Changeset 42726
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/includes/class-wp-community-events.php
r42343 r42726 417 417 418 418 /** 419 * Discards expired events, and reduces the remaining list. 420 * 421 * @since 4.8.0 419 * Prepares the event list for presentation. 420 * 421 * Discards expired events, and makes WordCamps "sticky." Attendees need more 422 * advanced notice about WordCamps than they do for meetups, so camps should 423 * appear in the list sooner. If a WordCamp is coming up, the API will "stick" 424 * it in the response, even if it wouldn't otherwise appear. When that happens, 425 * the event will be at the end of the list, and will need to be moved into a 426 * higher position, so that it doesn't get trimmed off. 427 * 428 * @since 4.8.0 429 * @since 5.0.0 Stick a WordCamp to the final list. 422 430 * 423 431 * @param array $response_body The response body which contains the events. … … 426 434 protected function trim_events( $response_body ) { 427 435 if ( isset( $response_body['events'] ) ) { 436 $wordcamps = array(); 428 437 $current_timestamp = current_time( 'timestamp' ); 429 438 430 439 foreach ( $response_body['events'] as $key => $event ) { 431 // Skip WordCamps, because they might be multi-day events. 432 if ( 'meetup' !== $event['type'] ) { 440 /* 441 * Skip WordCamps, because they might be multi-day events. 442 * Save a copy so they can be pinned later. 443 */ 444 if ( 'wordcamp' === $event['type'] ) { 445 $wordcamps[] = $event; 433 446 continue; 434 447 } … … 442 455 443 456 $response_body['events'] = array_slice( $response_body['events'], 0, 3 ); 457 $trimmed_event_types = array_column( $response_body['events'], 'type' ); 458 459 // Make sure the soonest upcoming WordCamps is pinned in the list. 460 if ( ! in_array( 'wordcamp', $trimmed_event_types ) && $wordcamps ) { 461 array_pop( $response_body['events'] ); 462 array_push( $response_body['events'], $wordcamps[0] ); 463 } 444 464 } 445 465 -
trunk/tests/phpunit/tests/admin/includesCommunityEvents.php
r42343 r42726 260 260 261 261 /** 262 * Test: get_events() should return the events with the WordCamp pinned in the prepared list. 263 * 264 * @since 5.0.0 265 */ 266 public function test_get_events_pin_wordcamp() { 267 add_filter( 'pre_http_request', array( $this, '_http_request_valid_response_unpinned_wordcamp' ) ); 268 269 $response_body = $this->instance->get_events(); 270 271 /* 272 * San Diego was at position 3 in the mock API response, but pinning puts it at position 2, 273 * so that it remains in the list. The other events should remain unchanged. 274 */ 275 $this->assertCount( 3, $response_body['events'] ); 276 $this->assertEquals( $response_body['events'][0]['title'], 'Flexbox + CSS Grid: Magic for Responsive Layouts' ); 277 $this->assertEquals( $response_body['events'][1]['title'], 'Part 3- Site Maintenance - Tools to Make It Easy' ); 278 $this->assertEquals( $response_body['events'][2]['title'], 'WordCamp San Diego' ); 279 280 remove_filter( 'pre_http_request', array( $this, '_http_request_valid_response_unpinned_wordcamp' ) ); 281 } 282 283 /** 284 * Simulates a valid HTTP response where a WordCamp needs to be pinned higher than it's default position. 285 * 286 * @since 5.0.0 287 * 288 * @return array A mock HTTP response. 289 */ 290 public function _http_request_valid_response_unpinned_wordcamp() { 291 return array( 292 'headers' => '', 293 'response' => array( 'code' => 200 ), 294 'cookies' => '', 295 'filename' => '', 296 'body' => wp_json_encode( 297 array( 298 'location' => $this->get_user_location(), 299 'events' => array( 300 array( 301 'type' => 'meetup', 302 'title' => 'Flexbox + CSS Grid: Magic for Responsive Layouts', 303 'url' => 'https://www.meetup.com/Eastbay-WordPress-Meetup/events/236031233/', 304 'meetup' => 'The East Bay WordPress Meetup Group', 305 'meetup_url' => 'https://www.meetup.com/Eastbay-WordPress-Meetup/', 306 'date' => date( 'Y-m-d H:i:s', strtotime( 'next Monday 1pm' ) ), 307 'location' => array( 308 'location' => 'Oakland, CA, USA', 309 'country' => 'us', 310 'latitude' => 37.808453, 311 'longitude' => -122.26593, 312 ), 313 ), 314 array( 315 'type' => 'meetup', 316 'title' => 'Part 3- Site Maintenance - Tools to Make It Easy', 317 'url' => 'https://www.meetup.com/Wordpress-Bay-Area-CA-Foothills/events/237706839/', 318 'meetup' => 'WordPress Bay Area Foothills Group', 319 'meetup_url' => 'https://www.meetup.com/Wordpress-Bay-Area-CA-Foothills/', 320 'date' => date( 'Y-m-d H:i:s', strtotime( 'next Tuesday 1:30pm' ) ), 321 'location' => array( 322 'location' => 'Milpitas, CA, USA', 323 'country' => 'us', 324 'latitude' => 37.432813, 325 'longitude' => -121.907095, 326 ), 327 ), 328 array( 329 'type' => 'meetup', 330 'title' => 'WordPress Q&A', 331 'url' => 'https://www.meetup.com/sanjosewp/events/245419844/', 332 'meetup' => 'The San Jose WordPress Meetup', 333 'meetup_url' => 'https://www.meetup.com/sanjosewp/', 334 'date' => date( 'Y-m-d H:i:s', strtotime( 'next Wednesday 5:30pm' ) ), 335 'location' => array( 336 'location' => 'Milpitas, CA, USA', 337 'country' => 'us', 338 'latitude' => 37.244194, 339 'longitude' => -121.889313, 340 ), 341 ), 342 array( 343 'type' => 'wordcamp', 344 'title' => 'WordCamp San Diego', 345 'url' => 'https://2018.sandiego.wordcamp.org', 346 'meetup' => null, 347 'meetup_url' => null, 348 'date' => date( 'Y-m-d H:i:s', strtotime( 'next Thursday 9am' ) ), 349 'location' => array( 350 'location' => 'San Diego, CA', 351 'country' => 'US', 352 'latitude' => 32.7220419, 353 'longitude' => -117.1534513, 354 ), 355 ), 356 ), 357 ) 358 ), 359 ); 360 } 361 362 /** 363 * Test: get_events() shouldn't stick an extra WordCamp when there's already one that naturally 364 * falls into the list. 365 * 366 * @since 5.0.0 367 */ 368 public function test_get_events_dont_pin_multiple_wordcamps() { 369 add_filter( 'pre_http_request', array( $this, '_http_request_valid_response_multiple_wordcamps' ) ); 370 371 $response_body = $this->instance->get_events(); 372 373 /* 374 * The first meetup should be removed because it's expired, while the next 3 events are selected. 375 * WordCamp LA should not be stuck to the list, because San Diego already appears naturally. 376 */ 377 $this->assertCount( 3, $response_body['events'] ); 378 $this->assertEquals( $response_body['events'][0]['title'], 'WordCamp San Diego' ); 379 $this->assertEquals( $response_body['events'][1]['title'], 'Part 3- Site Maintenance - Tools to Make It Easy' ); 380 $this->assertEquals( $response_body['events'][2]['title'], 'WordPress Q&A' ); 381 382 remove_filter( 'pre_http_request', array( $this, '_http_request_valid_response_multiple_wordcamps' ) ); 383 } 384 385 /** 386 * Simulates a valid HTTP response where a WordCamp needs to be pinned higher than it's default position. 387 * no need to pin extra camp b/c one already exists in response 388 * 389 * @since 5.0.0 390 * 391 * @return array A mock HTTP response. 392 */ 393 public function _http_request_valid_response_multiple_wordcamps() { 394 return array( 395 'headers' => '', 396 'response' => array( 'code' => 200 ), 397 'cookies' => '', 398 'filename' => '', 399 'body' => wp_json_encode( 400 array( 401 'location' => $this->get_user_location(), 402 'events' => array( 403 array( 404 'type' => 'meetup', 405 'title' => 'Flexbox + CSS Grid: Magic for Responsive Layouts', 406 'url' => 'https://www.meetup.com/Eastbay-WordPress-Meetup/events/236031233/', 407 'meetup' => 'The East Bay WordPress Meetup Group', 408 'meetup_url' => 'https://www.meetup.com/Eastbay-WordPress-Meetup/', 409 'date' => date( 'Y-m-d H:i:s', strtotime( '2 days ago' ) ), 410 'location' => array( 411 'location' => 'Oakland, CA, USA', 412 'country' => 'us', 413 'latitude' => 37.808453, 414 'longitude' => -122.26593, 415 ), 416 ), 417 array( 418 'type' => 'wordcamp', 419 'title' => 'WordCamp San Diego', 420 'url' => 'https://2018.sandiego.wordcamp.org', 421 'meetup' => null, 422 'meetup_url' => null, 423 'date' => date( 'Y-m-d H:i:s', strtotime( 'next Tuesday 9am' ) ), 424 'location' => array( 425 'location' => 'San Diego, CA', 426 'country' => 'US', 427 'latitude' => 32.7220419, 428 'longitude' => -117.1534513, 429 ), 430 ), 431 array( 432 'type' => 'meetup', 433 'title' => 'Part 3- Site Maintenance - Tools to Make It Easy', 434 'url' => 'https://www.meetup.com/Wordpress-Bay-Area-CA-Foothills/events/237706839/', 435 'meetup' => 'WordPress Bay Area Foothills Group', 436 'meetup_url' => 'https://www.meetup.com/Wordpress-Bay-Area-CA-Foothills/', 437 'date' => date( 'Y-m-d H:i:s', strtotime( 'next Wednesday 1:30pm' ) ), 438 'location' => array( 439 'location' => 'Milpitas, CA, USA', 440 'country' => 'us', 441 'latitude' => 37.432813, 442 'longitude' => -121.907095, 443 ), 444 ), 445 array( 446 'type' => 'meetup', 447 'title' => 'WordPress Q&A', 448 'url' => 'https://www.meetup.com/sanjosewp/events/245419844/', 449 'meetup' => 'The San Jose WordPress Meetup', 450 'meetup_url' => 'https://www.meetup.com/sanjosewp/', 451 'date' => date( 'Y-m-d H:i:s', strtotime( 'next Thursday 5:30pm' ) ), 452 'location' => array( 453 'location' => 'Milpitas, CA, USA', 454 'country' => 'us', 455 'latitude' => 37.244194, 456 'longitude' => -121.889313, 457 ), 458 ), 459 array( 460 'type' => 'wordcamp', 461 'title' => 'WordCamp Los Angeles', 462 'url' => 'https://2018.la.wordcamp.org', 463 'meetup' => null, 464 'meetup_url' => null, 465 'date' => date( 'Y-m-d H:i:s', strtotime( 'next Friday 9am' ) ), 466 'location' => array( 467 'location' => 'Los Angeles, CA', 468 'country' => 'US', 469 'latitude' => 34.050888, 470 'longitude' => -118.285426, 471 ), 472 ), 473 ), 474 ) 475 ), 476 ); 477 } 478 479 /** 262 480 * Test that get_unsafe_client_ip() properly anonymizes all possible address formats 263 481 *
Note: See TracChangeset
for help on using the changeset viewer.