Make WordPress Core

Ticket #41112: 41112.2.diff

File 41112.2.diff, 6.2 KB (added by iandunn, 6 years ago)

Pin to bottom, add test case

  • src/wp-admin/includes/class-wp-community-events.php

    diff --git src/wp-admin/includes/class-wp-community-events.php src/wp-admin/includes/class-wp-community-events.php
    index 410f9b6237..0258b37d48 100644
    class WP_Community_Events { 
    416416        }
    417417
    418418        /**
    419          * Discards expired events, and reduces the remaining list.
     419         * Prepares the event list for presentation.
    420420         *
     421         * Discards expired events, and sticks a WordCamp to the final list if it
     422         * doesn't naturally appear.
     423         *
     424         * @access protected
    421425         * @since 4.8.0
     426         * @since 5.0.0 Stick a WordCamp to the final list.
    422427         *
    423428         * @param  array $response_body The response body which contains the events.
    424429         * @return array The response body with events trimmed.
    425430         */
    426431        protected function trim_events( $response_body ) {
    427432                if ( isset( $response_body['events'] ) ) {
     433                        $wordcamps = array();
    428434                        $current_timestamp = current_time( 'timestamp' );
    429435
    430436                        foreach ( $response_body['events'] as $key => $event ) {
    431                                 // Skip WordCamps, because they might be multi-day events.
    432                                 if ( 'meetup' !== $event['type'] ) {
    433                                         continue;
    434                                 }
     437                                if ( 'wordcamp' === $event['type'] ) {
     438                                        // Store WordCamps so that one can be pinned later.
     439                                        $wordcamps[] = $event;
    435440
    436                                 $event_timestamp = strtotime( $event['date'] );
     441                                        // Don't unset WordCamps that are past their start date, because they might be multi-day events.
     442                                } else {
     443                                        $event_timestamp = strtotime( $event['date'] );
    437444
    438                                 if ( $current_timestamp > $event_timestamp && ( $current_timestamp - $event_timestamp ) > DAY_IN_SECONDS ) {
    439                                         unset( $response_body['events'][ $key ] );
     445                                        if ( $current_timestamp > $event_timestamp && ( $current_timestamp - $event_timestamp ) > DAY_IN_SECONDS ) {
     446                                                unset( $response_body['events'][ $key ] );
     447                                        }
    440448                                }
    441449                        }
    442450
    443451                        $response_body['events'] = array_slice( $response_body['events'], 0, 3 );
     452                        $trimmed_event_types     = array_column( $response_body['events'], 'type' );
     453
     454                        // Make sure the soonest upcoming WordCamps is pinned in the list.
     455                        if ( ! in_array( 'wordcamp', $trimmed_event_types ) && $wordcamps ) {
     456                                array_pop( $response_body['events'] );
     457                                array_push( $response_body['events'], $wordcamps[0] );
     458                        }
    444459                }
    445460
    446461                return $response_body;
  • tests/phpunit/tests/admin/includesCommunityEvents.php

    diff --git tests/phpunit/tests/admin/includesCommunityEvents.php tests/phpunit/tests/admin/includesCommunityEvents.php
    index e2f8528c85..31da93b5c0 100644
    class Test_WP_Community_Events extends WP_UnitTestCase { 
    258258                );
    259259        }
    260260
     261
     262        /**
     263         * Test: get_events() should return the events with the WordCamp pinned in the prepared list.
     264         *
     265         * @since 5.0.0
     266         */
     267        public function test_get_events_pin_wordcamp() {
     268                add_filter( 'pre_http_request', array( $this, '_http_request_valid_response_unpinned_wordcamp' ) );
     269
     270                $response_body = $this->instance->get_events();
     271
     272                /*
     273                 * San Diego was at position 3 in the mock API response, but pinning puts it at position 2,
     274                 * so that it remains in the list.
     275                 */
     276                $this->assertEquals( $response_body['events'][2]['title'], 'WordCamp San Diego' );
     277
     278                remove_filter( 'pre_http_request', array( $this, '_http_request_valid_response_unpinned_wordcamp' ) );
     279        }
     280
     281        /**
     282         * Simulates a valid HTTP response where a WordCamp needs to be pinned higher than it's default position.
     283         *
     284         * @since 5.0.0
     285         *
     286         * @return array A mock HTTP response.
     287         */
     288        public function _http_request_valid_response_unpinned_wordcamp() {
     289                return array(
     290                        'headers'  => '',
     291                        'response' => array( 'code' => 200 ),
     292                        'cookies'  => '',
     293                        'filename' => '',
     294                        'body'     => wp_json_encode(
     295                                array(
     296                                        'location' => $this->get_user_location(),
     297                                        'events'   => array(
     298                                                array(
     299                                                        'type'       => 'meetup',
     300                                                        'title'      => 'Flexbox + CSS Grid: Magic for Responsive Layouts',
     301                                                        'url'        => 'https://www.meetup.com/Eastbay-WordPress-Meetup/events/236031233/',
     302                                                        'meetup'     => 'The East Bay WordPress Meetup Group',
     303                                                        'meetup_url' => 'https://www.meetup.com/Eastbay-WordPress-Meetup/',
     304                                                        'date'       => date( 'Y-m-d H:i:s', strtotime( 'next Sunday 1pm' ) ),
     305                                                        'location'   => array(
     306                                                                'location'  => 'Oakland, CA, USA',
     307                                                                'country'   => 'us',
     308                                                                'latitude'  => 37.808453,
     309                                                                'longitude' => -122.26593,
     310                                                        ),
     311                                                ),
     312                                                array(
     313                                                        'type'       => 'meetup',
     314                                                        'title'      => 'Part 3- Site Maintenance - Tools to Make It Easy',
     315                                                        'url'        => 'https://www.meetup.com/Wordpress-Bay-Area-CA-Foothills/events/237706839/',
     316                                                        'meetup'     => 'WordPress Bay Area Foothills Group',
     317                                                        'meetup_url' => 'https://www.meetup.com/Wordpress-Bay-Area-CA-Foothills/',
     318                                                        'date'       => date( 'Y-m-d H:i:s', strtotime( 'next Wednesday 1:30pm' ) ),
     319                                                        'location'   => array(
     320                                                                'location'  => 'Milpitas, CA, USA',
     321                                                                'country'   => 'us',
     322                                                                'latitude'  => 37.432813,
     323                                                                'longitude' => -121.907095,
     324                                                        ),
     325                                                ),
     326                                                array(
     327                                                        'type'       => 'meetup',
     328                                                        'title'      => 'WordPress Q&A',
     329                                                        'url'        => 'https://www.meetup.com/sanjosewp/events/245419844/',
     330                                                        'meetup'     => 'The San Jose WordPress Meetup',
     331                                                        'meetup_url' => 'https://www.meetup.com/sanjosewp/',
     332                                                        'date'       => date( 'Y-m-d H:i:s', strtotime( 'next Thursday 5:30pm' ) ),
     333                                                        'location'   => array(
     334                                                                'location'  => 'Milpitas, CA, USA',
     335                                                                'country'   => 'us',
     336                                                                'latitude'  => 37.244194,
     337                                                                'longitude' => -121.889313,
     338                                                        ),
     339                                                ),
     340                                                array(
     341                                                        'type'       => 'wordcamp',
     342                                                        'title'      => 'WordCamp San Diego',
     343                                                        'url'        => 'https://2018.sandiego.wordcamp.org',
     344                                                        'meetup'     => null,
     345                                                        'meetup_url' => null,
     346                                                        'date'       => date( 'Y-m-d H:i:s', strtotime( 'next Saturday' ) ),
     347                                                        'location'   => array(
     348                                                                'location'  => 'San Diego, CA',
     349                                                                'country'   => 'US',
     350                                                                'latitude'  =>  32.7220419,
     351                                                                'longitude' => -117.1534513,
     352                                                        ),
     353                                                ),
     354                                        ),
     355                                )
     356                        ),
     357                );
     358        }
     359
    261360        /**
    262361         * Test that get_unsafe_client_ip() properly anonymizes all possible address formats
    263362         *