Inventory_Presser_Map_Widget::widget( array $args, array $instance )
Outputs the widget front-end HTML
On This Page
Parameters Parameters
- $args
-
(array) (Required)
- $instance
-
(array) (Required)
Return Return
(void)
Source Source
File: includes/widget/class-widget-map.php
public function widget( $args, $instance ) { // abort if we don't have an address to show. if ( empty( $instance['location_slug'] ) ) { return; } // abort if we do not have a mapbox.com API token. $settings = INVP::settings(); if ( empty( $settings['mapbox_public_token'] ) ) { return; } $location_terms = get_terms( array( 'hide_empty' => false, 'slug' => $instance['location_slug'], 'taxonomy' => 'location', ) ); if ( ! $location_terms ) { // there are no dealership addresses stored in this site, abort. return; } if ( empty( $args['widget_id'] ) ) { // Sometimes the widget ID is unavailable in stuff like Elementor. $args['widget_id'] = '9999'; } /** * Create an array that contains the data needed to create the markers * and popups: location names, addresses, and lat lon coords */ $popups = array(); $location_count = count( $location_terms ); for ( $t = 0; $t < $location_count; $t++ ) { $popup = new stdClass(); /** * Store the widget ID in case there are two instances of this * widget on the same page. */ $popup->widget_id = $args['widget_id']; // Location title/dealership name. $popup->name = $location_terms[ $t ]->name; // Address. $popup->address = str_replace( "\r", '', str_replace( PHP_EOL, '<br />', $location_terms[ $t ]->description ) ); // Get the latitude and longitude coordinates for this address. $location = INVP::fetch_latitude_and_longitude( $location_terms[ $t ]->term_id ); if ( false !== $location ) { $popup->coords = new stdClass(); $popup->coords->lat = $location->lat; $popup->coords->lon = $location->lon; $popups[] = $popup; } } if ( empty( $popups ) ) { /** * We didn't find any latitude & longitude coordinates using the * addresses on openstreetmap.org. It is likely that the addresses * need to be added to the buildings for this dealer's locations. */ return; } // Enqueue leaflet.js scripts and styles. if ( ! wp_script_is( self::SCRIPT_HANDLE_LEAFLET ) ) { wp_enqueue_script( self::SCRIPT_HANDLE_LEAFLET ); wp_enqueue_style( self::SCRIPT_HANDLE_LEAFLET ); wp_enqueue_style( self::ID_BASE ); } // Include the JavaScript file that powers the map. $handle = 'invp-maps'; /** * If there are two Map widgets on the same page, we need to avoid the * second one redefining the same invp_maps constant because that will * produce a JavaScript error. */ if ( ! wp_script_is( $handle ) ) { // First instance of this widget on the page. wp_enqueue_script( $handle ); // Localize an API key and the popups array for JavaScript. wp_add_inline_script( $handle, 'const invp_maps = ' . wp_json_encode( array( 'mapbox_public_token' => $settings['mapbox_public_token'], 'popups' => $popups, ) ), 'before' ); } else { // There is another Map widget on this page already. foreach ( $popups as $popup ) { wp_add_inline_script( $handle, 'invp_maps.popups.push( ' . wp_json_encode( $popup ) . ' );', 'before' ); } } // before and after widget arguments are defined by themes. echo $args['before_widget']; $title = apply_filters( 'widget_title', $instance['title'] ); if ( ! empty( $title ) ) { echo $args['before_title'] . $title . $args['after_title']; } echo sprintf( '<div class="invp-map %1$s" id="%1$s-inner"></div>', esc_attr( $args['widget_id'] ) ) . $args['after_widget']; }
Expand full source codeCollapse full source codeView on Github