Making WordPress.org

Ticket #2994: 2994.diff

File 2994.diff, 4.8 KB (added by metalandcoffee, 6 years ago)
  • api.wordpress.org/public_html/events/1.0/index.php

    diff --git a/api.wordpress.org/public_html/events/1.0/index.php b/api.wordpress.org/public_html/events/1.0/index.php
    index b28402f0..4c8fcaca 100644
    a b function get_events( $args = array() ) { 
    696696        }
    697697
    698698        $wheres = array();
    699         if ( !empty( $args['type'] ) && in_array( $args['type'], array( 'meetup', 'wordcamp' ) ) ) {
     699        if ( ! empty( $args['type'] ) && in_array( $args['type'], array( 'meetup', 'wordcamp' ) ) ) {
    700700                $wheres[] = '`type` = %s';
    701701                $sql_values[] = $args['type'];
    702702        }
    703703
    704704        // If we want nearby events, create a WHERE based on a bounded box of lat/long co-ordinates.
    705         if ( !empty( $args['nearby'] ) ) {
     705        if ( ! empty( $args['nearby'] ) ) {
    706706                // Distances in kilometers
    707707                $event_distances = array(
    708708                        'meetup' => 100,
    function get_events( $args = array() ) { 
    711711                $nearby_where = array();
    712712
    713713                foreach ( $event_distances as $type => $distance ) {
    714                         if ( !empty( $args['type'] ) && $type != $args['type'] ) {
     714                        if ( ! empty( $args['type'] ) && $type != $args['type'] ) {
    715715                                continue;
    716716                        }
    717717                        $bounded_box = get_bounded_coordinates( $args['nearby']['latitude'], $args['nearby']['longitude'], $distance );
    function get_events( $args = array() ) { 
    721721                        $sql_values[] = $bounded_box['latitude']['max'];
    722722                        $sql_values[] = $bounded_box['longitude']['min'];
    723723                        $sql_values[] = $bounded_box['longitude']['max'];
     724
     725                        // Store SQL for local WordCamp query later
     726                        if ( 'wordcamp' === $type ) {
     727                                $wc_nearby_where[] = '( `type` = %s AND `latitude` BETWEEN %f AND %f AND `longitude` BETWEEN %f AND %f )';
     728                                $wc_sql_values[] = $type;
     729                                $wc_sql_values[] = $bounded_box['latitude']['min'];
     730                                $wc_sql_values[] = $bounded_box['latitude']['max'];
     731                                $wc_sql_values[] = $bounded_box['longitude']['min'];
     732                                $wc_sql_values[] = $bounded_box['longitude']['max'];
     733                        }
    724734                }
    725735                // Build the nearby where as a OR as different event types have different distances.
    726736                $wheres[] = '(' . implode( ' OR ', $nearby_where ) . ')';
    727737        }
    728738
    729739        // Allow queries for limiting to specific countries.
    730         if ( !empty( $args['country'] ) && preg_match( '![a-z]{2}!i', $args['country'] ) ) {
     740        if ( ! empty( $args['country'] ) && preg_match( '![a-z]{2}!i', $args['country'] ) ) {
    731741                $wheres[] = '`country` = %s';
     742                $wc_nearby_where[] = '`country` = %s';
    732743                $sql_values[] = $args['country'];
     744                $wc_sql_values[] = $args['country'];
    733745        }
    734746
    735747        // Just show upcoming events
    736748        $wheres[] = '`date_utc` >= %s';
     749        $wc_nearby_where[] = '`date_utc` >= %s';
    737750        // Dates are in local-time not UTC, so the API output will contain events that have already happened in some parts of the world.
    738751        // TODO update this when the UTC dates are stored.
    739         $sql_values[] = gmdate( 'Y-m-d', time() - ( 24 * 60 * 60 ) );
     752        $day_in_seconds = 24 * 60 * 60;
     753        $sql_values[] = gmdate( 'Y-m-d', time() - ( $day_in_seconds ) );
     754        $wc_sql_values[] = gmdate( 'Y-m-d', time() - ( $day_in_seconds ) );
    740755
    741         // Limit 
     756        // Limit
    742757        if ( isset( $args['number'] ) ) {
    743758                $sql_limits = 'LIMIT %d';
    744759                $sql_values[] = $args['number'];
    function get_events( $args = array() ) { 
    780795                );
    781796        }
    782797
     798        // Run query to find if local WordCamp is occuring within the
     799        // next 6 weeks and add it to the events array
     800        $wc_nearby_where[] = '`date_utc` <= %s';
     801        $wc_sql_values[] = gmdate( 'Y-m-d', time() + ( $day_in_seconds * 42 ) );
     802        $wc_sql_where = 'WHERE ' . implode( ' AND ', $wc_nearby_where );
     803        $raw_wordcamps = $wpdb->get_results( $wpdb->prepare(
     804                "SELECT
     805                        `type`, `title`, `url`,
     806                        `meetup`, `meetup_url`,
     807                        `date_utc`, `date_utc_offset`,
     808                        `location`, `country`, `latitude`, `longitude`
     809                FROM `wporg_events`
     810                $wc_sql_where
     811                ORDER BY `date_utc` ASC",
     812                $wc_sql_values
     813        ) );
     814        $wordcamps = array();
     815        foreach ( $raw_wordcamps as $wordcamp ) {
     816                $events[] = array(
     817                        'type'  => $wordcamp->type,
     818                        'title' => $wordcamp->title,
     819                        'url'   => $wordcamp->url,
     820                        'meetup' => $wordcamp->meetup,
     821                        'meetup_url' => $wordcamp->meetup_url,
     822                        'date' => $wordcamp->date_utc, // TODO: DB stores a local date, not UTC.
     823                        'location' => array(
     824                                'location' => $wordcamp->location,
     825                                'country' => $wordcamp->country,
     826                                'latitude' => (float) $wordcamp->latitude,
     827                                'longitude' => (float) $wordcamp->longitude,
     828                        ),
     829                );
     830        }
     831        // Remove duplicates
     832        $events = array_unique( $events, SORT_REGULAR );
     833        // If events array is greater than limit, knock off
     834        // meetup(s) with later dates.
     835        $events = array_reverse( $events );
     836        foreach ( $events as $key => $event ) {
     837                if ( count( $events ) > $args['number'] ) {
     838                        if ( 'meetup' === $event['type'] ) {
     839                                unset( $events[ $key ] );
     840                        }
     841                } else {
     842                        break;
     843                }
     844        }
     845        $events = array_reverse( $events );
     846
    783847        wp_cache_set( $cache_key, $events, $cache_group, $cache_life );
    784         return $events; 
     848        return $events;
    785849}
    786850
    787851/**