Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
104 lines (85 sloc) 2.28 KB
<?php
namespace Technosailor\Photogramappy\Google_Maps;
use Technosailor\Photogramappy\Object_Meta\Geo_Coordinate\Config\View;
use Technosailor\Photogramappy\Post_Types\Photographs;
use Technosailor\Photogramappy\Settings\Settings;
class Map {
const NAME = 'map';
private $params;
public function __construct( Defaults $defaults ) {
$this->params = $defaults;
}
/**
* Conditionally appends a google maps to the bottom of the_content
*
* @param string $content
*
* @filter the_content
*
* @return string
*/
public function maybe_add_map_to_post_content( string $content ): string {
if( get_post_type( get_the_ID() ) !== Photographs::NAME ) {
return $content;
}
$latitude = get_post_meta( get_the_ID(), View::FIELD_LAT, true );
if( empty( $latitude ) ) {
return $content;
}
$longitude = get_post_meta( get_the_ID(), View::FIELD_LONG, true );
if( empty( $longitude ) ) {
return $content;
}
$map_url = $this->render( get_the_ID() );
if( ! empty( $map_url ) ) {
$map = sprintf( '<img src="%s">', esc_url( $map_url ) );
$content .= $map;
}
return $content;
}
/**
* Constructs a Google Maps Static URL
*
* @param int $post_id
* @param array $args
*
* @return string
*/
public function render( int $post_id, array $args = [] ) : string {
$key = get_option( Settings::GOOGLE_APIKEY );
if ( empty( $key ) ) {
return '';
}
if( ! empty( $args ) ) {
$args = array_map( 'absint', $args );
}
foreach( $args as $key => $arg ) {
switch( $key ) {
case 'zoom' :
$this->params->setZoom( $args['zoom'] );
break;
case 'height' :
$this->params->setHeight( $args['height'] );
break;
case 'width' :
$this->params->setWidth( $args['width'] );
break;
default :
break;
}
}
$marker = sprintf( 'color:blue|label:X|%s,%s',
get_post_meta( $post_id, View::FIELD_LAT, true ),
get_post_meta( $post_id, View::FIELD_LONG, true )
);
$url = 'https://maps.googleapis.com/maps/api/staticmap';
$url = add_query_arg( [
'zoom' => $this->params->getZoom(),
'size' => $this->params->getWidth() . 'x' . $this->params->getHeight(),
'markers' => urlencode( $marker ),
'scale' => 2,
'key' => get_option( Settings::GOOGLE_APIKEY ),
], $url );
return $url;
}
}
You can’t perform that action at this time.