Added Google Maps Static Key setting

This commit is contained in:
Justin Foell
2017-05-10 13:50:39 -05:00
parent ded3929a44
commit d9399b14af
2 changed files with 68 additions and 23 deletions
+42 -16
View File
@@ -35,21 +35,25 @@ class WPStrava_Settings {
* This runs after options are saved
*/
public function maybe_oauth( $value ) {
//redirect only if all the right options are in place
if ( isset( $value[0]['type'] ) && $value[0]['type'] == 'updated' ) { //make sure there were no settings errors
if ( isset( $_POST['option_page'] ) && $_POST['option_page'] == $this->option_page ) { //make sure we're on our settings page
//user is clearing to start-over, don't oauth
// Redirect only if all the right options are in place.
if ( isset( $value[0]['type'] ) && $value[0]['type'] == 'updated' ) { // Make sure there were no settings errors.
if ( isset( $_POST['option_page'] ) && $_POST['option_page'] == $this->option_page ) { // Make sure we're on our settings page.
// User is clearing to start-over, don't oauth.
if ( isset( $_POST['strava_token'] ) && empty( $_POST['strava_token'] ) )
return;
$client_id = get_option( 'strava_client_id' );
$client_secret = get_option( 'strava_client_secret' );
// Only re-auth if client ID and secret were shown.
if ( ! empty( $_POST['strava_client_id'] ) && ! empty( $_POST['strava_client_secret'] ) ) {
$client_id = get_option( 'strava_client_id' );
$client_secret = get_option( 'strava_client_secret' );
if ( $client_id && $client_secret ) {
$redirect = admin_url( "options-general.php?page={$this->page_name}" );
$url = "https://www.strava.com/oauth/authorize?client_id={$client_id}&response_type=code&redirect_uri={$redirect}&approval_prompt=force";
wp_redirect( $url );
exit();
if ( $client_id && $client_secret ) {
$redirect = admin_url( "options-general.php?page={$this->page_name}" );
$url = "https://www.strava.com/oauth/authorize?client_id={$client_id}&response_type=code&redirect_uri={$redirect}&approval_prompt=force";
wp_redirect( $url );
exit();
}
}
}
}
@@ -86,7 +90,7 @@ class WPStrava_Settings {
public function register_strava_settings() {
$this->init();
add_settings_section( 'strava_api', __( 'Strava API', 'wp-strava' ), array( $this, 'print_api_instructions' ), 'wp-strava' ); //NULL / NULL no section label needed
add_settings_section( 'strava_api', __( 'Strava API', 'wp-strava' ), array( $this, 'print_api_instructions' ), 'wp-strava' );
if ( ! $this->token ) {
register_setting( $this->option_page, 'strava_client_id', array( $this, 'sanitize_client_id' ) );
@@ -99,10 +103,14 @@ class WPStrava_Settings {
add_settings_field( 'strava_token', __( 'Strava Token', 'wp-strava' ), array( $this, 'print_token_input' ), 'wp-strava', 'strava_api' );
}
register_setting( $this->option_page, 'strava_som', array( $this, 'sanitize_som' ) );
add_settings_section( 'strava_options', __( 'Options', 'wp-strava' ), NULL, 'wp-strava' );
// Google Maps API.
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' );
// System of Measurement.
register_setting( $this->option_page, 'strava_som', array( $this, 'sanitize_som' ) );
add_settings_section( 'strava_options', __( 'Options', 'wp-strava' ), null, 'wp-strava' );
add_settings_field( 'strava_som', __( 'System of Measurement', 'wp-strava' ), array( $this, 'print_som_input' ), 'wp-strava', 'strava_options' );
}
@@ -115,7 +123,7 @@ class WPStrava_Settings {
$description = 'WP-Strava for ' . $blog_name;
printf( __( "<p>Steps:</p>
<ol>
<li>Create your API Application/Connection here: <a href='%s' target='_blank'>%s</a> using the following information:</li>
<li>Create your free API Application/Connection here: <a href='%s' target='_blank'>%s</a> using the following information:</li>
<ul>
<li>Application Name: <strong>%s</strong></li>
<li>Website: <strong>%s</strong></li>
@@ -128,6 +136,16 @@ class WPStrava_Settings {
</ol>", 'wp-strava' ), $settings_url, $settings_url, $app_name, $site_url, $description, $site_url );
}
public function print_gmaps_instructions() {
$maps_url = 'https://developers.google.com/maps/documentation/static-maps/';
printf( __( "<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='%s' target='_blank'>%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.
</ol>", 'wp-strava' ), $maps_url, $maps_url );
}
public function print_strava_options() {
?>
<div class="wrap">
@@ -202,6 +220,14 @@ class WPStrava_Settings {
}
}
public function print_gmaps_key_input() {
?><input type="text" id="strava_gmaps_key" name="strava_gmaps_key" value="<?php echo get_option( 'strava_gmaps_key' ); ?>" /><?php
}
public function sanitize_gmaps_key( $key ) {
return $key;
}
public function print_som_input() {
$strava_som = get_option( 'strava_som' );
?>
+26 -7
View File
@@ -1,18 +1,37 @@
<?php
class WPStrava_StaticMap {
/**
* 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.
*
* @static
* @access public
* @param object $ride Ride object from strava.
* @param int $height Height of map in pixels.
* @param int $width Width of map in pixels.
*/
public static function get_image_tag( $ride, $height = 320, $width = 480 ) {
$url = "http://maps.google.com/maps/api/staticmap?maptype=terrain&size={$width}x{$height}&sensor=false&path=color:0xFF0000BF|weight:2|enc:";
$key = WPStrava::get_instance()->settings->gmaps_key;
// Short circuit if missing key or ride object doesn't have the data we need.
if ( empty( $key ) || empty( $ride->map ) ) {
return '';
}
$url = "https://maps.googleapis.com/maps/api/staticmap?maptype=terrain&size={$width}x{$height}&sensor=false&key={$key}&path=color:0xFF0000BF|weight:2|enc:";
$url_len = strlen( $url );
$max_chars = 1865;
if ( $url_len + strlen( $ride->map->polyline ) < $max_chars )
if ( ! empty( $ride->map->polyline ) && ( $url_len + strlen( $ride->map->polyline ) < $max_chars ) ) {
$url .= $ride->map->polyline;
else
} else {
$url .= $ride->map->summary_polyline;
return "<img src='{$url}' />";
}
return "<img class='wp-strava-img' src='{$url}' />";
}
}
}