Making WordPress.org


Ignore:
Timestamp:
05/12/2015 10:41:20 PM (10 years ago)
Author:
iandunn
Message:

Central Theme: Add the [wcc_map] shortcode to show a Google Map of WordCamps.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/themes/wordcamp-central-2012/functions.php

    r1545 r1566  
    4444        add_filter( 'wp_nav_menu_items', array( __CLASS__, 'add_rss_links_to_footer_menu' ), 10, 2 );
    4545
     46        add_shortcode( 'wcc_map',         array( __CLASS__, 'shortcode_map'         ) );
    4647        add_shortcode( 'wcc_about_stats', array( __CLASS__, 'shortcode_about_stats' ) );
    4748    }
     
    184185     */
    185186    static function enqueue_scripts() {
    186         wp_enqueue_style( 'central', get_stylesheet_uri(), array(), 5 );
    187         wp_enqueue_script( 'wordcamp-central', get_stylesheet_directory_uri() . '/js/central.js', array( 'jquery', 'underscore' ), 1, true );
    188 
    189         wp_localize_script( 'wordcamp-central', 'wordcampCentralOptions', array( 'ajaxURL' => admin_url( 'admin-ajax.php' ) ) );
     187        wp_enqueue_style( 'central', get_stylesheet_uri(), array(), 6 );
     188        wp_enqueue_script( 'wordcamp-central', get_stylesheet_directory_uri() . '/js/central.js', array( 'jquery', 'underscore' ), 2, true );
     189
     190        wp_localize_script( 'wordcamp-central', 'wordcampCentralOptions', self::get_javascript_options() );
    190191
    191192        /* We add some JavaScript to pages with the comment form
     
    200201        }
    201202
     203        if ( is_page( 'about' ) || is_page( 'schedule' ) ) {
     204            wp_enqueue_script( 'google-maps', 'https://maps.googleapis.com/maps/api/js', array(), false, true );
     205        }
     206    }
     207
     208    /**
     209     * Build the array of options to pass to the client side
     210     *
     211     * @return array
     212     */
     213    protected static function get_javascript_options() {
     214        global $post;
     215
     216        $options = array( 'ajaxURL' => admin_url( 'admin-ajax.php' ) );
     217
     218        if ( $map_id = self::get_map_id( $post->post_content ) ) {
     219            $options['mapContainer']            = "wcc-map-$map_id";
     220            $options['markerIconBaseURL']       = get_stylesheet_directory_uri() . '/images/';
     221            $options['markerClusterIcon']       = 'icon-marker-clustered.png';
     222            $options['markerIconAnchorXOffset'] = 24;
     223            $options['markerIconHeight']        = 94;
     224            $options['markerIconWidth']         = 122;
     225
     226            if ( $map_markers = self::get_map_markers( $map_id ) ) {
     227                $options['mapMarkers'] = $map_markers;
     228            }
     229        }
     230
     231        return $options;
     232    }
     233
     234    /**
     235     * Get the ID of the map called in the given page
     236     *
     237     * @param string $post_content
     238     *
     239     * @return mixed A string of the map name on success, or false on failure
     240     */
     241    protected static function get_map_id( $post_content ) {
     242        $map_id = false;
     243
     244        if ( has_shortcode( $post_content, 'wcc_map' ) ) {
     245            preg_match_all( '/' . get_shortcode_regex() . '/s', $post_content, $shortcodes, PREG_SET_ORDER );
     246
     247            foreach ( $shortcodes as $shortcode ) {
     248                if ( 'wcc_map' == $shortcode[2] ) {
     249                    $attributes = shortcode_parse_atts( $shortcode[3] );
     250                    $map_id     = sanitize_text_field( $attributes['id'] );
     251                    break;
     252                }
     253            }
     254        }
     255
     256        return $map_id;
     257    }
     258
     259    /**
     260     * Get the markers assigned to the given map
     261     *
     262     * @param string $map_id
     263     *
     264     * @return array
     265     */
     266    protected static function get_map_markers( $map_id ) {
     267        $transient_key = "wcc_map_markers_$map_id";
     268
     269        if ( $markers = get_transient( $transient_key ) ) {
     270            return $markers;
     271        } else {
     272            $markers = array();
     273        }
     274
     275        // Get the raw marker posts for the given map
     276        $parameters = array(
     277            'post_type'      => 'wordcamp',
     278            'posts_per_page' => -1,
     279        );
     280
     281        switch( $map_id ) {
     282            case 'schedule':
     283                $parameters['post_status'][] = array( 'publish', 'pending' );
     284                $parameters['meta_query'][] = array(
     285                    'key'     => 'Start Date (YYYY-mm-dd)',
     286                    'value'   => strtotime( '-2 days' ),
     287                    'compare' => '>',
     288                );
     289                break;
     290        }
     291
     292        $raw_markers = get_posts( $parameters );
     293
     294        // Convert the raw markers into prepared objects that are ready to be used on the JavaScript side
     295        foreach ( $raw_markers as $marker ) {
     296            if ( 'schedule' == $map_id ) {
     297                $marker_type = 'upcoming';
     298            } else {
     299                $marker_type = get_post_meta( $marker->ID, 'Start Date (YYYY-mm-dd)', true ) > strtotime( '-2 days' ) ? 'upcoming' : 'past';
     300            }
     301
     302            if ( ! $coordinates = get_post_meta( $marker->ID, '_venue_coordinates', true ) ) {
     303                continue;
     304            }
     305
     306            $markers[ $marker->ID ] = array(
     307                'id'          => $marker->ID,
     308                'name'        => wcpt_get_wordcamp_title( $marker->ID ),
     309                'dates'       => wcpt_get_wordcamp_start_date( $marker->ID ),
     310                'location'    => get_post_meta( $marker->ID, 'Location', true ),
     311                'venueName'   => get_post_meta( $marker->ID, 'Venue Name', true ),
     312                'url'         => self::get_best_wordcamp_url( $marker->ID ),
     313                'latitude'    => $coordinates['latitude'],
     314                'longitude'   => $coordinates['longitude'],
     315                'iconURL'     => "icon-marker-{$marker_type}-2x.png",
     316            );
     317        }
     318
     319        $markers = apply_filters( 'wcc_get_map_markers', $markers );
     320
     321        set_transient( $transient_key, $markers, WEEK_IN_SECONDS );
     322
     323        return $markers;
    202324    }
    203325
     
    649771
    650772    /**
     773     * Render the [wcc_map] shortcode
     774     *
     775     * @param array $attributes
     776     *
     777     * @return string
     778     */
     779    public static function shortcode_map( $attributes ) {
     780        $attributes = shortcode_atts( array( 'id' => '' ), $attributes );
     781
     782        ob_start();
     783        require( __DIR__ . '/shortcode-about-map.php' );
     784        return ob_get_clean();
     785    }
     786
     787    /**
    651788     * Render the [wcc_about_stats] shortcode
    652789     *
Note: See TracChangeset for help on using the changeset viewer.