Ticket #2994: 2994.diff
File 2994.diff, 4.8 KB (added by , 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() ) { 696 696 } 697 697 698 698 $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' ) ) ) { 700 700 $wheres[] = '`type` = %s'; 701 701 $sql_values[] = $args['type']; 702 702 } 703 703 704 704 // 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'] ) ) { 706 706 // Distances in kilometers 707 707 $event_distances = array( 708 708 'meetup' => 100, … … function get_events( $args = array() ) { 711 711 $nearby_where = array(); 712 712 713 713 foreach ( $event_distances as $type => $distance ) { 714 if ( ! empty( $args['type'] ) && $type != $args['type'] ) {714 if ( ! empty( $args['type'] ) && $type != $args['type'] ) { 715 715 continue; 716 716 } 717 717 $bounded_box = get_bounded_coordinates( $args['nearby']['latitude'], $args['nearby']['longitude'], $distance ); … … function get_events( $args = array() ) { 721 721 $sql_values[] = $bounded_box['latitude']['max']; 722 722 $sql_values[] = $bounded_box['longitude']['min']; 723 723 $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 } 724 734 } 725 735 // Build the nearby where as a OR as different event types have different distances. 726 736 $wheres[] = '(' . implode( ' OR ', $nearby_where ) . ')'; 727 737 } 728 738 729 739 // 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'] ) ) { 731 741 $wheres[] = '`country` = %s'; 742 $wc_nearby_where[] = '`country` = %s'; 732 743 $sql_values[] = $args['country']; 744 $wc_sql_values[] = $args['country']; 733 745 } 734 746 735 747 // Just show upcoming events 736 748 $wheres[] = '`date_utc` >= %s'; 749 $wc_nearby_where[] = '`date_utc` >= %s'; 737 750 // Dates are in local-time not UTC, so the API output will contain events that have already happened in some parts of the world. 738 751 // 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 ) ); 740 755 741 // Limit 756 // Limit 742 757 if ( isset( $args['number'] ) ) { 743 758 $sql_limits = 'LIMIT %d'; 744 759 $sql_values[] = $args['number']; … … function get_events( $args = array() ) { 780 795 ); 781 796 } 782 797 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 783 847 wp_cache_set( $cache_key, $events, $cache_group, $cache_life ); 784 return $events; 848 return $events; 785 849 } 786 850 787 851 /**