Making WordPress.org

Changeset 1095


Ignore:
Timestamp:
01/07/2015 07:25:01 PM (12 years ago)
Author:
iandunn
Message:

Official WordPress Events: Make event list easier to scan.

Props mj12982 for the design

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events
Files:
1 added
2 edited

Legend:

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

    r1083 r1095  
    2727         */
    2828        public function __construct() {
     29                add_action( 'wp_enqueue_scripts',           array( $this, 'enqueue_scripts' ) );
    2930                add_shortcode( 'official_wordpress_events', array( $this, 'render_events' ) );
    3031        }
    3132
    3233        /**
     34         * Enqueue scripts and styles
     35         */
     36        public function enqueue_scripts() {
     37                global $post;
     38
     39                wp_register_style( 'official-wordpress-events', plugins_url( 'official-wordpress-events.css', __FILE__ ), array(), 1 );
     40
     41                if ( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'official_wordpress_events' ) ) {
     42                        wp_enqueue_style( 'official-wordpress-events' );
     43                }
     44
     45        }
     46
     47        /**
    3348         * Gather the events data and render the events template with it
    3449         */
    3550        public function render_events() {
    36                 $events = $this->get_all_events();
     51                $events = $this->group_events_by_date( $this->get_all_events() );
    3752               
    3853                if ( $events ) {
     
    6883                        return $a->start_timestamp > $b->start_timestamp ? 1 : -1;
    6984                }
     85        }
     86
     87        /**
     88         * Group a list of events by the date
     89         *
     90         * @param array $events
     91         *
     92         * @return array
     93         */
     94        protected function group_events_by_date( $events ) {
     95                $grouped_events = array();
     96
     97                foreach ( $events as $event ) {
     98                        $grouped_events[ date( 'Y-m-d', (int) $event->start_timestamp ) ][] = $event;
     99                }
     100
     101                return $grouped_events;
    70102        }
    71103
     
    129161                        $meetups = array();
    130162                }
    131                
     163
    132164                if ( $meetups ) {
    133165                        foreach ( $meetups as $meetup ) {
    134166                                $location        = array();
    135167                                $start_timestamp = ( $meetup->time / 1000 ) + ( $meetup->utc_offset / 1000 );    // convert to seconds
    136                                
    137                                 foreach ( array( 'city', 'state', 'country' ) as $part ) {
    138                                         if ( ! empty( $meetup->venue->$part ) ) {
    139                                                 if ( in_array( $part, array( 'state', 'country' ) ) ) {
    140                                                         $location[] = strtoupper( $meetup->venue->$part );
    141                                                 } else {
    142                                                         $location[] = $meetup->venue->$part;
     168
     169                                if ( isset( $meetup->venue ) ) {
     170                                        foreach ( array( 'city', 'state', 'country' ) as $part ) {
     171                                                if ( ! empty( $meetup->venue->$part ) ) {
     172                                                        if ( in_array( $part, array( 'state', 'country' ) ) ) {
     173                                                                $location[] = strtoupper( $meetup->venue->$part );
     174                                                        } else {
     175                                                                $location[] = $meetup->venue->$part;
     176                                                        }
    143177                                                }
    144178                                        }
     179                                        $location = implode( ', ', $location );
     180                                } else {
     181                                        $location = $this->reverse_geocode( $meetup->group->group_lat, $meetup->group->group_lon );
     182                                        $location = $this->format_reverse_geocode_address( $location->address_components );
    145183                                }
    146184                               
     
    151189                                        'start_timestamp' => $start_timestamp,
    152190                                        'end_timestamp'   => ( empty ( $meetup->duration ) ? $start_timestamp : $start_timestamp + ( $meetup->duration / 1000 ) ),      // convert to seconds
    153                                         'location'        => empty( $location ) ? '' : implode( ' ', $location )
     191                                        'location'        => $location,
    154192                                ) );
    155193                        }
     
    188226               
    189227                return $group_ids;
     228        }
     229
     230        /**
     231         * Reverse-geocodes a set of coordinates
     232         *
     233         * @param string $latitude
     234         * @param string $longitude
     235         *
     236         * @return false | object
     237         */
     238        protected function reverse_geocode( $latitude, $longitude ) {
     239                $address  = false;
     240                $response = $this->remote_get( sprintf( 'https://maps.googleapis.com/maps/api/geocode/json?latlng=%s,%s&sensor=false', $latitude, $longitude ) );
     241
     242                if ( ! is_wp_error( $response ) ) {
     243                        $body = json_decode( wp_remote_retrieve_body( $response ) );
     244
     245                        if ( isset( $body->results[0] ) ) {
     246                                $address = $body->results[0];
     247                        }
     248                }
     249
     250                return $address;
     251        }
     252
     253        /**
     254         * Formats an address returned from Google's reverse-geocode API
     255         *
     256         * @param array $address_components
     257         *
     258         * @return string
     259         */
     260        protected function format_reverse_geocode_address( $address_components ) {
     261                $address = array();
     262
     263                foreach ( $address_components as $component ) {
     264                        if ( 'locality' == $component->types[0] ) {
     265                                $address['city'] = $component->short_name;
     266                        } elseif ( 'administrative_area_level_1' == $component->types[0] ) {
     267                                $address['state'] = $component->short_name;
     268                        } elseif ( 'country' == $component->types[0] ) {
     269                                $address['country'] = $component->short_name;
     270                        }
     271                }
     272
     273                return implode( ', ', $address );
    190274        }
    191275
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/template-events.php

    r492 r1095  
    1 <div id="ofe_events">
    2         <ul>
    3                 <?php foreach ( $events as $event ) : ?>
     1<div id="ofe-events">
     2        <?php foreach ( $events as $date => $day_events ) : ?>
    43
    5                         <li>
    6                                 <a href="<?php echo esc_attr( esc_url( $event->url ) ); ?>">
    7                                         <?php echo esc_html( $event->title ); ?>
    8                                 </a><br />
     4                <h3>
     5                        <?php echo date( 'F j', strtotime( $date ) ); ?>
     6                        <span class="owe-day-of-week"><?php echo date( '(l)', strtotime( $date ) ); ?></span>
     7                </h3>
    98
    10                                 <?php echo esc_html( date( 'l, F jS | g:i a', (int) $event->start_timestamp ) ); ?><br />
     9                <ul class="ofe-event-list">
     10                        <?php foreach ( $day_events as $event ) : ?>
     11                                <li>
     12                                        <?php if ( $event->location ) : ?>
     13                                                <?php echo esc_html( $event->location ); ?>
     14                                                <span class="owe-separator"></span>
     15                                        <?php endif; ?>
    1116
    12                                 <?php echo esc_html( $event->location ); ?>
    13                         </li>
     17                                        <a href="<?php echo esc_attr( esc_url( $event->url ) ); ?>">
     18                                                <?php echo esc_html( $event->title ); ?>
     19                                        </a>
     20                                        <span class="owe-separator"></span>
    1421
    15                 <?php endforeach; ?>
    16         </ul>
    17 </div> <!-- end #ofe_events -->
     22                                        <?php echo date( 'g:i a', $event->start_timestamp ); ?>
     23                                </li>
     24                        <?php endforeach; ?>
     25                </ul>
     26
     27        <?php endforeach; ?>
     28</div> <!-- end #ofe-events -->
Note: See TracChangeset for help on using the changeset viewer.