Inventory_Presser_Map_Widget::widget( array $args, array $instance )

widget


Description Description

Outputs the widget front-end HTML


Top ↑

Parameters Parameters

$args

(array) (Required)

$instance

(array) (Required)


Top ↑

Return Return

(void)


Top ↑

Source Source

File: includes/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;
		}

		/**
		 * Create an array that contains the data needed to create the markers 
		 * and popups: location names, addresses, and lat lon coords
		 */
		$popups = array();
		for( $t=0; $t<sizeof( $location_terms ); $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 = $this->escape_single_quotes( $location_terms[$t]->name );
			//Address
			$popup->address = str_replace( "\r", '', str_replace( PHP_EOL, '<br />', $this->escape_single_quotes( $location_terms[$t]->description ) ) );
			//Get the latitude and longitude coordinates for this address
			$location = $this->get_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, but only once
		$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, plugins_url( 'js/widget-map.min.js', INVP_PLUGIN_FILE_PATH ) );
			//Localize an API key and the popups array we built data for JavaScript
			wp_add_inline_script( $handle, 'const invp_maps = ' . 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( ' . 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"></div>', $args['widget_id'] ) . $args['after_widget'];
	}