Making WordPress.org

Changeset 6824


Ignore:
Timestamp:
03/02/2018 08:48:41 PM (7 years ago)
Author:
iandunn
Message:

Official WordPress Events: Mark deleted Meetup events as deleted.

Fixes #3491

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/official-wordpress-events.php

    r6823 r6824  
    3838        add_action( 'wp_enqueue_scripts',           array( $this, 'enqueue_scripts'    ) );
    3939        add_action( 'owpe_prime_events_cache',      array( $this, 'prime_events_cache' ) );
     40        add_action( 'owpe_mark_deleted_meetups',    array( $this, 'mark_deleted_meetups' ) );
    4041        add_shortcode( 'official_wordpress_events', array( $this, 'render_events'      ) );
    4142
    4243        if ( ! wp_next_scheduled( 'owpe_prime_events_cache' ) ) {
    4344            wp_schedule_event( time(), 'hourly', 'owpe_prime_events_cache' );
     45        }
     46
     47        if ( ! wp_next_scheduled( 'owpe_mark_deleted_meetups' ) ) {
     48            wp_schedule_event( time(), 'hourly', 'owpe_mark_deleted_meetups' );
    4449        }
    4550    }
     
    444449     * @param string $longitude
    445450     *
    446      * @return false | object
     451     * @return false | array
    447452     */
    448453    protected function reverse_geocode( $latitude, $longitude ) {
     
    532537
    533538        return implode( ', ', $location );
     539    }
     540
     541    /**
     542     * Mark Meetup events as deleted in our database when they're deleted from Meetup.com.
     543     *
     544     * Meetup.com allows organizers to either cancel or delete events. If the event is cancelled, then the status
     545     * in our database will be updated the next time `prime_events_cache` runs. If the event is deleted, though,
     546     * it is removed from their API results, so `prime_events_cache` won't see it, and the status will remain
     547     * `scheduled`.
     548     *
     549     * This checks all the upcoming Meetup.com events to see if any of them are missing. If they are, it assumes
     550     * that they were deleted, and updates their status.
     551     */
     552    public function mark_deleted_meetups() {
     553        global $wpdb;
     554
     555        $chunked_db_events = array();
     556
     557        // Don't include anything before tomorrow, because time zone differences could result in past events being flagged.
     558        $raw_events = $wpdb->get_results( "
     559            SELECT id, source_id, meetup_url
     560            FROM `". self::EVENTS_TABLE ."`
     561            WHERE
     562                type      = 'meetup'    AND
     563                status    = 'scheduled' AND
     564                date_utc >= DATE_ADD( NOW(), INTERVAL 24 HOUR )
     565            LIMIT 5000
     566        " );
     567
     568        foreach ( $raw_events as $event ) {
     569            $chunked_db_events[ $event->meetup_url ][] = $event;
     570        }
     571
     572        foreach ( $chunked_db_events as $group_url => $db_events ) {
     573            $url_name = trim( wp_parse_url( $group_url, PHP_URL_PATH ), '/' );
     574
     575            $request_url = sprintf(
     576                '%s%s/events?page=500&status=upcoming,cancelled&key=%s',
     577                self::MEETUP_API_BASE_URL,
     578                $url_name,
     579                MEETUP_API_KEY
     580            );
     581
     582            $response   = $this->remote_get( $request_url );
     583            $body       = json_decode( wp_remote_retrieve_body( $response ) );
     584            $api_events = wp_list_pluck( $body, 'id' );
     585
     586            // Make sure we have a valid API response, to avoid marking events as deleted just because the request failed.
     587            if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
     588                continue;
     589            }
     590
     591            if ( empty( $body[0]->status ) || empty( $api_events[0] ) ) {
     592                continue;
     593            }
     594
     595            foreach ( $db_events as $db_event ) {
     596                // If the event is still appearing in the Meetup.com API results, it hasn't been deleted.
     597                if ( in_array( $db_event->source_id, $api_events, true ) ) {
     598                    continue;
     599                }
     600
     601                // The event is missing from a valid response, so assume that it's been deleted.
     602                $wpdb->update( self::EVENTS_TABLE, array( 'status' => 'deleted' ), array( 'id' => $db_event->id ) );
     603
     604                if ( 'cli' === php_sapi_name() ) {
     605                    echo "\nMarked {$db_event->source_id} as deleted.";
     606                }
     607            }
     608        }
    534609    }
    535610
Note: See TracChangeset for help on using the changeset viewer.