Make WordPress Core

Changeset 43357


Ignore:
Timestamp:
06/16/2018 10:42:16 AM (8 years ago)
Author:
SergeyBiryukov
Message:

Community Events Dashboard: Always show a WordCamp if one is coming up.

WordCamps are celebrations of the local WordPress Community and once a local one is scheduled, people in that community should know it is coming. This adjusts the WordPress Events in the dashboard widgets to always display a WordCamp, even if there are multiple Meetups happening first.

Props iandunn, metalandcoffee, warmlaundry, alejandroxlopez, jorbin.
Merges [42726], [42728], and [43356] to the 4.9 branch.
Fixes #41112.

Location:
branches/4.9
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/4.9

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

    r43082 r43357  
    386386
    387387        /**
    388          * Discards expired events, and reduces the remaining list.
    389          *
    390          * @since 4.8.0
     388         * Prepares the event list for presentation.
     389         *
     390         * Discards expired events, and makes WordCamps "sticky." Attendees need more
     391         * advanced notice about WordCamps than they do for meetups, so camps should
     392         * appear in the list sooner. If a WordCamp is coming up, the API will "stick"
     393         * it in the response, even if it wouldn't otherwise appear. When that happens,
     394         * the event will be at the end of the list, and will need to be moved into a
     395         * higher position, so that it doesn't get trimmed off.
     396         *
     397         * @since 4.8.0
     398         * @since 4.9.7 Stick a WordCamp to the final list.
    391399         *
    392400         * @param  array $response_body The response body which contains the events.
    … …  
    395403        protected function trim_events( $response_body ) {
    396404                if ( isset( $response_body['events'] ) ) {
     405                        $wordcamps         = array();
    397406                        $current_timestamp = current_time( 'timestamp' );
    398407
    399408                        foreach ( $response_body['events'] as $key => $event ) {
    400                                 // Skip WordCamps, because they might be multi-day events.
    401                                 if ( 'meetup' !== $event['type'] ) {
     409                                /*
     410                                 * Skip WordCamps, because they might be multi-day events.
     411                                 * Save a copy so they can be pinned later.
     412                                 */
     413                                if ( 'wordcamp' === $event['type'] ) {
     414                                        $wordcamps[] = $event;
    402415                                        continue;
    403416                                }
    … …  
    411424
    412425                        $response_body['events'] = array_slice( $response_body['events'], 0, 3 );
     426                        $trimmed_event_types     = wp_list_pluck( $response_body['events'], 'type' );
     427
     428                        // Make sure the soonest upcoming WordCamps is pinned in the list.
     429                        if ( ! in_array( 'wordcamp', $trimmed_event_types ) && $wordcamps ) {
     430                                array_pop( $response_body['events'] );
     431                                array_push( $response_body['events'], $wordcamps[0] );
     432                        }
    413433                }
    414434
  • branches/4.9/tests/phpunit/tests/admin/includesCommunityEvents.php

    r43082 r43357  
    258258
    259259        /**
     260         * Test: get_events() should return the events with the WordCamp pinned in the prepared list.
     261         *
     262         * @since 4.9.7
     263         */
     264        public function test_get_events_pin_wordcamp() {
     265                add_filter( 'pre_http_request', array( $this, '_http_request_valid_response_unpinned_wordcamp' ) );
     266
     267                $response_body = $this->instance->get_events();
     268
     269                /*
     270                 * San Diego was at position 3 in the mock API response, but pinning puts it at position 2,
     271                 * so that it remains in the list. The other events should remain unchanged.
     272                 */
     273                $this->assertCount( 3, $response_body['events'] );
     274                $this->assertEquals( $response_body['events'][0]['title'], 'Flexbox + CSS Grid: Magic for Responsive Layouts' );
     275                $this->assertEquals( $response_body['events'][1]['title'], 'Part 3- Site Maintenance - Tools to Make It Easy' );
     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 4.9.7
     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 Monday 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 Tuesday 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 Wednesday 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 Thursday 9am' ) ),
     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
     360        /**
     361         * Test: get_events() shouldn't stick an extra WordCamp when there's already one that naturally
     362         * falls into the list.
     363         *
     364         * @since 4.9.7
     365         */
     366        public function test_get_events_dont_pin_multiple_wordcamps() {
     367                add_filter( 'pre_http_request', array( $this, '_http_request_valid_response_multiple_wordcamps' ) );
     368
     369                $response_body = $this->instance->get_events();
     370
     371                /*
     372                 * The first meetup should be removed because it's expired, while the next 3 events are selected.
     373                 * WordCamp LA should not be stuck to the list, because San Diego already appears naturally.
     374                 */
     375                $this->assertCount( 3, $response_body['events'] );
     376                $this->assertEquals( $response_body['events'][0]['title'], 'WordCamp San Diego' );
     377                $this->assertEquals( $response_body['events'][1]['title'], 'Part 3- Site Maintenance - Tools to Make It Easy' );
     378                $this->assertEquals( $response_body['events'][2]['title'], 'WordPress Q&A' );
     379
     380                remove_filter( 'pre_http_request', array( $this, '_http_request_valid_response_multiple_wordcamps' ) );
     381        }
     382
     383        /**
     384         * Simulates a valid HTTP response where a WordCamp needs to be pinned higher than it's default position.
     385         * no need to pin extra camp b/c one already exists in response
     386         *
     387         * @since 4.9.7
     388         *
     389         * @return array A mock HTTP response.
     390         */
     391        public function _http_request_valid_response_multiple_wordcamps() {
     392                return array(
     393                        'headers'  => '',
     394                        'response' => array( 'code' => 200 ),
     395                        'cookies'  => '',
     396                        'filename' => '',
     397                        'body'     => wp_json_encode(
     398                                array(
     399                                        'location' => $this->get_user_location(),
     400                                        'events'   => array(
     401                                                array(
     402                                                        'type'       => 'meetup',
     403                                                        'title'      => 'Flexbox + CSS Grid: Magic for Responsive Layouts',
     404                                                        'url'        => 'https://www.meetup.com/Eastbay-WordPress-Meetup/events/236031233/',
     405                                                        'meetup'     => 'The East Bay WordPress Meetup Group',
     406                                                        'meetup_url' => 'https://www.meetup.com/Eastbay-WordPress-Meetup/',
     407                                                        'date'       => date( 'Y-m-d H:i:s', strtotime( '2 days ago' ) ),
     408                                                        'location'   => array(
     409                                                                'location'  => 'Oakland, CA, USA',
     410                                                                'country'   => 'us',
     411                                                                'latitude'  => 37.808453,
     412                                                                'longitude' => -122.26593,
     413                                                        ),
     414                                                ),
     415                                                array(
     416                                                        'type'       => 'wordcamp',
     417                                                        'title'      => 'WordCamp San Diego',
     418                                                        'url'        => 'https://2018.sandiego.wordcamp.org',
     419                                                        'meetup'     => null,
     420                                                        'meetup_url' => null,
     421                                                        'date'       => date( 'Y-m-d H:i:s', strtotime( 'next Tuesday 9am' ) ),
     422                                                        'location'   => array(
     423                                                                'location'  => 'San Diego, CA',
     424                                                                'country'   => 'US',
     425                                                                'latitude'  => 32.7220419,
     426                                                                'longitude' => -117.1534513,
     427                                                        ),
     428                                                ),
     429                                                array(
     430                                                        'type'       => 'meetup',
     431                                                        'title'      => 'Part 3- Site Maintenance - Tools to Make It Easy',
     432                                                        'url'        => 'https://www.meetup.com/Wordpress-Bay-Area-CA-Foothills/events/237706839/',
     433                                                        'meetup'     => 'WordPress Bay Area Foothills Group',
     434                                                        'meetup_url' => 'https://www.meetup.com/Wordpress-Bay-Area-CA-Foothills/',
     435                                                        'date'       => date( 'Y-m-d H:i:s', strtotime( 'next Wednesday 1:30pm' ) ),
     436                                                        'location'   => array(
     437                                                                'location'  => 'Milpitas, CA, USA',
     438                                                                'country'   => 'us',
     439                                                                'latitude'  => 37.432813,
     440                                                                'longitude' => -121.907095,
     441                                                        ),
     442                                                ),
     443                                                array(
     444                                                        'type'       => 'meetup',
     445                                                        'title'      => 'WordPress Q&A',
     446                                                        'url'        => 'https://www.meetup.com/sanjosewp/events/245419844/',
     447                                                        'meetup'     => 'The San Jose WordPress Meetup',
     448                                                        'meetup_url' => 'https://www.meetup.com/sanjosewp/',
     449                                                        'date'       => date( 'Y-m-d H:i:s', strtotime( 'next Thursday 5:30pm' ) ),
     450                                                        'location'   => array(
     451                                                                'location'  => 'Milpitas, CA, USA',
     452                                                                'country'   => 'us',
     453                                                                'latitude'  => 37.244194,
     454                                                                'longitude' => -121.889313,
     455                                                        ),
     456                                                ),
     457                                                array(
     458                                                        'type'       => 'wordcamp',
     459                                                        'title'      => 'WordCamp Los Angeles',
     460                                                        'url'        => 'https://2018.la.wordcamp.org',
     461                                                        'meetup'     => null,
     462                                                        'meetup_url' => null,
     463                                                        'date'       => date( 'Y-m-d H:i:s', strtotime( 'next Friday 9am' ) ),
     464                                                        'location'   => array(
     465                                                                'location'  => 'Los Angeles, CA',
     466                                                                'country'   => 'US',
     467                                                                'latitude'  => 34.050888,
     468                                                                'longitude' => -118.285426,
     469                                                        ),
     470                                                ),
     471                                        ),
     472                                )
     473                        ),
     474                );
     475        }
     476
     477        /**
    260478         * Test that get_unsafe_client_ip() properly anonymizes all possible address formats
    261479         *
Note: See TracChangeset for help on using the changeset viewer.