-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added mapbox / gmaps setting switcher
- Loading branch information
Showing
6 changed files
with
225 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,6 +95,7 @@ public function hook() { | |
|
||
if ( is_admin() ) { | ||
$this->settings->hook(); | ||
add_action( 'admin_enqueue_scripts', array( $this, 'register_admin_scripts' ) ); | ||
} else { // Front-end. | ||
add_action( 'init', array( $this, 'register_shortcodes' ) ); | ||
add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts' ) ); | ||
|
@@ -188,6 +189,18 @@ public function get_segments() { | |
return $this->segments; | ||
} | ||
|
||
/** | ||
* Register/enqueue admin javascript. | ||
* | ||
* @author Justin Foell <[email protected]> | ||
* @since next | ||
*/ | ||
public function register_admin_scripts() { | ||
if ( $this->settings->is_settings_page() ) { | ||
wp_enqueue_script( 'strava-admin-settings', WPSTRAVA_PLUGIN_URL . 'src/admin-settings.js', array(), WPSTRAVA_PLUGIN_VERSION, true ); | ||
} | ||
} | ||
|
||
/** | ||
* Register the wp-strava stylesheet. | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,9 +80,16 @@ public function register_strava_settings() { | |
} | ||
|
||
// Google Maps API. | ||
add_settings_section( 'strava_maps', __( 'Maps', 'wp-strava' ), null, 'wp-strava' ); | ||
|
||
register_setting( $this->option_page, 'strava_map_type', array( $this, 'sanitize_map_type' ) ); | ||
add_settings_field( 'strava_map_type', __( 'Map Type', 'wp-strava' ), array( $this, 'print_map_type_input' ), 'wp-strava', 'strava_maps' ); | ||
|
||
register_setting( $this->option_page, 'strava_gmaps_key', array( $this, 'sanitize_gmaps_key' ) ); | ||
add_settings_section( 'strava_gmaps', __( 'Google Maps', 'wp-strava' ), array( $this, 'print_gmaps_instructions' ), 'wp-strava' ); | ||
add_settings_field( 'strava_gmaps_key', __( 'Static Maps Key', 'wp-strava' ), array( $this, 'print_gmaps_key_input' ), 'wp-strava', 'strava_gmaps' ); | ||
add_settings_field( 'strava_gmaps_key', __( 'Google Static Maps API Key', 'wp-strava' ), array( $this, 'print_gmaps_key_input' ), 'wp-strava', 'strava_maps' ); | ||
|
||
register_setting( $this->option_page, 'strava_mapbox_token', array( $this, 'sanitize_mapbox_token' ) ); | ||
add_settings_field( 'strava_mapbox_token', __( 'Mapbox Public Token', 'wp-strava' ), array( $this, 'print_mapbox_token_input' ), 'wp-strava', 'strava_maps' ); | ||
|
||
// System of Measurement. | ||
register_setting( $this->option_page, 'strava_som', array( $this, 'sanitize_som' ) ); | ||
|
@@ -160,27 +167,80 @@ public function print_api_instructions() { | |
} | ||
|
||
/** | ||
* Print the google maps instructions. | ||
* Print the map type selection. | ||
* | ||
* @author Justin Foell <[email protected]> | ||
* @since 1.1 | ||
* @since next | ||
*/ | ||
public function print_gmaps_instructions() { | ||
$maps_url = 'https://developers.google.com/maps/documentation/static-maps/'; | ||
echo wp_kses_post( | ||
sprintf( | ||
__( | ||
"<p>Steps:</p> | ||
<ol> | ||
<li>To use Google map images, you must create a Static Maps API Key. Create a free key by going here: <a href='%1\$s'>%2\$s</a> and clicking <strong>Get a Key</strong></li> | ||
<li>Once you've created your Google Static Maps API Key, enter the key below.</li> | ||
</ol>", | ||
'wp-strava' | ||
), | ||
$maps_url, | ||
$maps_url | ||
) | ||
); | ||
public function print_map_type_input() { | ||
$gmaps_url = 'https://developers.google.com/maps/documentation/static-maps/'; | ||
$mapbox_url = 'https://www.mapbox.com/static-maps'; | ||
$selected = 'mapbox' === $this->map_type ? 'mapbox' : 'gmaps'; | ||
$gmaps_class = 'gmaps' !== $selected ? 'hidden' : ''; | ||
$mapbox_class = 'mapbox' !== $selected ? 'hidden' : ''; | ||
|
||
?> | ||
<select id="strava_map_type" name="strava_map_type"> | ||
<option value="gmaps" <?php selected( $this->map_type, 'gmaps' ); ?>><?php esc_html_e( 'Google Maps', 'wp-strava' ); ?></option> | ||
<option value="mapbox" <?php selected( $this->map_type, 'mapbox' ); ?>><?php esc_html_e( 'Mapbox', 'wp-strava' ); ?></option> | ||
</select> | ||
<p class="description"> | ||
<br/> | ||
<div class="strava-maps"> | ||
<div id="strava-maps-gmaps-instructions" class="<?php echo esc_attr( $gmaps_class ); ?>"> | ||
<?php | ||
echo wp_kses_post( | ||
sprintf( | ||
__( | ||
"<p>Steps:</p> | ||
<ol> | ||
<li>To use Google map images, you must create a Static Maps API Key. Google now requires billing information for overages, but effectively gives you 100k map loads for free each month. Create your Google Static Maps API key by going here: <a href='%1\$s'>%2\$s</a></li> | ||
<li>Once you've created your Google Static Maps API Key, enter the key below.</li> | ||
</ol>", | ||
'wp-strava' | ||
), | ||
$gmaps_url, | ||
$gmaps_url | ||
) | ||
); | ||
?> | ||
</div> | ||
<div id="strava-maps-mapbox-instructions" class="<?php echo esc_attr( $mapbox_class ); ?>"> | ||
<?php | ||
echo wp_kses_post( | ||
sprintf( | ||
__( | ||
"<p>Steps:</p> | ||
<ol> | ||
<li>To use Mapbox map images, create a free Mapbox account here: <a href='%1\$s'>%2\$s</a></li> | ||
<li>All Mapbox accounts are issued a default public access token, enter the token below.</li> | ||
</ol>", | ||
'wp-strava' | ||
), | ||
$mapbox_url, | ||
$mapbox_url | ||
) | ||
); | ||
?> | ||
</div> | ||
</div> | ||
</p> | ||
<?php | ||
} | ||
|
||
/** | ||
* Sanitize map type select input. | ||
* | ||
* @param string $map_type | ||
* @return string | ||
* @author Justin Foell <[email protected]> | ||
* @since next | ||
*/ | ||
public function sanitize_map_type_input( $map_type ) { | ||
if ( in_array( $map_type, array( 'gmaps', 'mapbox' ), true ) ) { | ||
return $map_type; | ||
} | ||
return ''; | ||
} | ||
|
||
/** | ||
|
@@ -389,7 +449,7 @@ private function maybe_clean_info( $ids ) { | |
$infos = $this->info; | ||
|
||
foreach ( $infos as $id => $info ) { | ||
if ( ! in_array( $id, $ids ) ) { | ||
if ( ! in_array( $id, $ids ) ) { // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict -- loose OK. | ||
$update = true; | ||
unset( $infos[ $id ] ); | ||
} | ||
|
@@ -425,6 +485,30 @@ public function sanitize_gmaps_key( $key ) { | |
return $key; | ||
} | ||
|
||
/** | ||
* Print the Mapbox token input. | ||
* | ||
* @author Justin Foell <[email protected]> | ||
* @since next | ||
*/ | ||
public function print_mapbox_token_input() { | ||
?> | ||
<input type="text" id="strava_mapbox_token" name="strava_mapbox_token" value="<?php echo esc_attr( $this->mapbox_token ); ?>" /> | ||
<?php | ||
} | ||
|
||
/** | ||
* Sanitize Mapbox token input. | ||
* | ||
* @param string $token | ||
* @return string | ||
* @author Justin Foell <[email protected]> | ||
* @since next | ||
*/ | ||
public function sanitize_mapbox_token( $token ) { | ||
return $token; | ||
} | ||
|
||
/** | ||
* Print System of Measure option. | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,12 +5,8 @@ abstract class WPStrava_StaticMap { | |
protected static $max_chars = 1865; | ||
|
||
/** | ||
* Get an image tag to a static google map. Will render with | ||
* detailed polyline if not greater than 1865 chars, otherwise | ||
* rendering will use summary polyline. | ||
* Get an image tag to a static map. | ||
* | ||
* @static | ||
* @access public | ||
* @param object $activity Activity object to get image tag for. | ||
* @param int $height Height of map in pixels. | ||
* @param int $width Width of map in pixels. | ||
|
@@ -21,31 +17,24 @@ abstract class WPStrava_StaticMap { | |
abstract public function get_image_tag( $activity, $height = 320, $width = 480, $markers = false, $title = '' ); | ||
|
||
/** | ||
* Factory method to get the correct StaticMap class based on specified string | ||
* or by the options setting. | ||
* Factory method to get the correct StaticMap class based on options setting. | ||
* | ||
* @param string $auth 'google' or 'mapbox' (default 'google'). | ||
* @return WPStrava_StaticMap Instance of StaticMap | ||
* @author Justin Foell <[email protected]> | ||
* @since NEXT | ||
*/ | ||
public static function get_map( $type = 'google' ) { | ||
/* | ||
if ( 'google' === $type ) { | ||
return new WPStrava_StaticGMap(); | ||
public static function get_map() { | ||
if ( 'mapbox' === WPStrava::get_instance()->settings->map_type ) { | ||
return new WPStrava_StaticMapbox(); | ||
} | ||
*/ | ||
// Default to refresh. | ||
return new WPStrava_StaticMapbox(); | ||
return new WPStrava_StaticGMap(); | ||
} | ||
|
||
/** | ||
* From an encoded polyline, get the start and finish points for | ||
* the purposes of displaying start and finish markers. | ||
* | ||
* @static | ||
* @see https://developers.google.com/maps/documentation/utilities/polylinealgorithm | ||
* @access private | ||
* @param string $enc Encoded polyline. | ||
* @return array { | ||
* Indexes of start & finish containing lat/lon for each. | ||
|
@@ -74,11 +63,11 @@ protected function decode_start_finish( $enc ) { | |
* From a (large) encoded polyline, reduce the number of points | ||
* until it is small enough for a GET URL. | ||
* | ||
* @param int $base_url_len Length of map URL. | ||
* @param string $enc Encoded polyline. | ||
* @return string Smaller encoded polyline. | ||
* @param int $base_url_len Length of map URL. | ||
* @param string $enc Encoded polyline. | ||
* @return string Smaller encoded polyline. | ||
* @author Justin Foell <[email protected]> | ||
* @since 2.10.0 | ||
* @since 2.10 | ||
*/ | ||
protected function reduce_polyline( $base_url_len, $enc ) { | ||
require_once WPSTRAVA_PLUGIN_DIR . 'src/Polyline.php'; | ||
|
@@ -100,6 +89,14 @@ protected function reduce_polyline( $base_url_len, $enc ) { | |
return $polyline; | ||
} | ||
|
||
/** | ||
* Get the length of a polyline. | ||
* | ||
* @param mixed $polyline Polyline string. | ||
* @return int Polyline string length. | ||
* @author Justin Foell <[email protected]> | ||
* @since next | ||
*/ | ||
protected function polyline_length( $polyline ) { | ||
return strlen( $polyline ); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,18 @@ | |
|
||
class WPStrava_StaticMapbox extends WPStrava_StaticMap { | ||
|
||
/** | ||
* Get an image tag to a static mapbox map. Will render with | ||
* detailed polyline if not greater than 1865 chars, otherwise | ||
* rendering will use summary polyline. | ||
* | ||
* @param object $activity Activity object to get image tag for. | ||
* @param int $height Height of map in pixels. | ||
* @param int $width Width of map in pixels. | ||
* @param bool $markers Display start and finish markers. | ||
* @param string $title Title attribute to accompany image (default empty). | ||
* @return string HTML img tag with static map image. | ||
*/ | ||
public function get_image_tag( $activity, $height = 320, $width = 480, $markers = false, $title = '' ) { | ||
|
||
$polyline = ''; | ||
|
@@ -17,6 +29,11 @@ public function get_image_tag( $activity, $height = 320, $width = 480, $markers | |
return ''; | ||
} | ||
|
||
if ( ! $height || ! $width ) { | ||
$height = 320; | ||
$width = 480; | ||
} | ||
|
||
$url = $this->build_url( $polyline, $height, $width, $markers ); | ||
|
||
$url_len = strlen( $url ); | ||
|
@@ -30,11 +47,22 @@ public function get_image_tag( $activity, $height = 320, $width = 480, $markers | |
return "<img class='wp-strava-img' src='{$url}'{$title_attr} />"; | ||
} | ||
|
||
/** | ||
* Build a Mapbox Static Map URL. | ||
* | ||
* @param string $polyline Polyline string to overlay. | ||
* @param int $height Height of map in pixels. | ||
* @param int $width Width of map in pixels. | ||
* @param bool $markers Display start and finish markers. | ||
* @return string Image URL. | ||
* @author Justin Foell <[email protected]> | ||
* @since next | ||
*/ | ||
private function build_url( $polyline, $height = 320, $width = 480, $markers = false ) { | ||
|
||
$url = 'https://api.mapbox.com/styles/v1/mapbox/outdoors-v11/static/'; | ||
$size = "auto/{$width}x{$height}@2x"; | ||
$token = 'access_token=pk.eyJ1IjoianJmb2VsbCIsImEiOiJ4NkNwU2RjIn0.MHjY7k0Okawa3bdV9HtSXg'; | ||
$token = WPStrava::get_instance()->settings->mapbox_token; | ||
|
||
$path = array(); | ||
|
||
|
@@ -47,11 +75,19 @@ private function build_url( $polyline, $height = 320, $width = 480, $markers = f | |
// polyline must be URL encoded https://stackoverflow.com/a/65523379/2146022 | ||
$url_polyline = rawurlencode( $polyline ); | ||
$path[] = "path-2+ff0000({$url_polyline})"; | ||
$url .= implode( ',', $path ) . "/{$size}?{$token}"; | ||
$url .= implode( ',', $path ) . "/{$size}?access_token={$token}"; | ||
|
||
return $url; | ||
} | ||
|
||
/** | ||
* Get the length of a polyline after encoding. | ||
* | ||
* @param mixed $polyline Polyline string. | ||
* @return int Encoded polyline string length. | ||
* @author Justin Foell <[email protected]> | ||
* @since next | ||
*/ | ||
protected function polyline_length( $polyline ) { | ||
return strlen( rawurlencode( $polyline ) ); | ||
} | ||
|
Oops, something went wrong.