Making WordPress.org


Ignore:
Timestamp:
04/03/2018 08:46:45 PM (8 years ago)
Author:
iandunn
Message:

WP15: Use wp_localize_script() to pass all data to JS.

This will be more organized as we add more data in future commits, like localized strings. It also takes some logic out of the view file.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wp15.wordpress.net/public_html/content/plugins/wp15-meetup-events/wp15-meetup-events.php

    r7009 r7011  
    177177        }
    178178
     179        wp_enqueue_style(
     180                'wp15-meetup-events',
     181                plugins_url( 'wp15-meetup-events.css', __FILE__ ),
     182                array(),
     183                filemtime( __DIR__ . '/wp15-meetup-events.css' )
     184        );
     185
    179186        wp_register_script(
    180187                'google-maps',
     
    201208        );
    202209
    203         wp_enqueue_style(
     210        wp_localize_script(
    204211                'wp15-meetup-events',
    205                 plugins_url( 'wp15-meetup-events.css', __FILE__ ),
    206                 array(),
    207                 filemtime( __DIR__ . '/wp15-meetup-events.css' )
    208         );
    209 }
    210 
    211 /**
    212  * Render the WP15 events shortcode.
    213  */
    214 function render_events_shortcode() {
    215         $events = get_option( 'wp15_events' );
    216 
    217         // This needs to be done on the fly, in order to use the date format for the visitor's locale.
    218         foreach ( $events as & $event ) {
    219                 $event['time'] = get_local_formatted_date( $event['time'], $event['timezone'] );
    220         }
    221         unset( $event ); // Destroy the reference to restore expected behavior; see https://stackoverflow.com/a/4969286/450127.
    222 
    223         usort( $events, __NAMESPACE__ . '\sort_events' );
    224 
    225         $map_options = array(
     212                'wp15MeetupEventsData',
     213                array(
     214                        'map_options' => get_map_options(),
     215                        'events'      => get_formatted_events(),
     216                )
     217        );
     218}
     219
     220
     221/**
     222 * Get the configuration for the Google Map of events.
     223 *
     224 * @return array
     225 */
     226function get_map_options() {
     227        return array(
    226228                'mapContainer'            => 'wp15-events-map',
    227                 'mapMarkers'              => $events,
    228229                'markerIconBaseURL'       => plugins_url( '/images/', __FILE__ ),
    229230                'markerIcon'              => 'map-marker.svg',
     
    235236                'clusterIconHeight'       => 52,
    236237        );
     238}
     239
     240/**
     241 * Format the WP15 events for presentation.
     242 *
     243 * @return array
     244 */
     245function get_formatted_events() {
     246        $events = get_option( 'wp15_events' );
     247
     248        // This needs to be done on the fly, in order to use the date format for the visitor's locale.
     249        foreach ( $events as & $event ) {
     250                $event['time'] = get_local_formatted_date( $event['time'], $event['timezone'] );
     251        }
     252        unset( $event ); // Destroy the reference to restore expected behavior; see https://stackoverflow.com/a/4969286/450127.
     253
     254        usort( $events, __NAMESPACE__ . '\sort_events' );
     255
     256        return $events;
     257}
     258
     259/**
     260 * Sort events by their timestamp.
     261 *
     262 * @param array $a
     263 * @param array $b
     264 *
     265 * @return int
     266 */
     267function sort_events( $a, $b ) {
     268        if ( $a['time'] === $b['time'] ) {
     269                return 0;
     270        }
     271
     272        return $a['time'] > $b['time'];
     273}
     274
     275/**
     276 * Render the WP15 events shortcode.
     277 */
     278function render_events_shortcode() {
     279        $events = get_formatted_events();
    237280
    238281        ob_start();
     
    240283        require_once( __DIR__ . '/views/events-list.php' );
    241284        return ob_get_clean();
    242 }
    243 
    244 /**
    245  * Sort events by their timestamp.
    246  *
    247  * @param array $a
    248  * @param array $b
    249  *
    250  * @return int
    251  */
    252 function sort_events( $a, $b ) {
    253         if ( $a['time'] === $b['time'] ) {
    254                 return 0;
    255         }
    256 
    257         return $a['time'] > $b['time'];
    258285}
    259286
Note: See TracChangeset for help on using the changeset viewer.