From 76306269c9830263551166c90d16c4d7d7988414 Mon Sep 17 00:00:00 2001 From: Justin Foell <630830+jrfoell@users.noreply.github.com> Date: Tue, 26 Oct 2021 10:15:33 -0500 Subject: [PATCH 1/6] Reworked composer scripts for maximum compatibility --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 5819cc5..f807b32 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,6 @@ "dealerdirect/phpcodesniffer-composer-installer": "^0.7.1" }, "scripts": { - "lint": "@php ./vendor/bin/phpcs -s" + "lint": "phpcs -s" } } From f4211ca880193f82d67116143364c85e1a6fd6fc Mon Sep 17 00:00:00 2001 From: Justin Foell <630830+jrfoell@users.noreply.github.com> Date: Fri, 26 Nov 2021 13:06:21 -0600 Subject: [PATCH 2/6] Added some transformations --- src/blocks/activity/index.js | 46 ++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/src/blocks/activity/index.js b/src/blocks/activity/index.js index 333c154..cf2cb16 100644 --- a/src/blocks/activity/index.js +++ b/src/blocks/activity/index.js @@ -9,19 +9,51 @@ metadata.save = () => null; metadata.transforms = { from: [ { - type: "raw", - priority: 10, - isMatch: ( node ) => - node.nodeName === "P" && - node.innerText.startsWith( "https://www.strava.com/activities/" ), - + type: 'raw', + isMatch: ( node ) => { + return ( + node.nodeName === 'P' && + node.innerText.startsWith( 'https://www.strava.com/activities/' ) + ); + }, transform: function( node ) { return createBlock( metadata.name, { url: node.innerText, } ); } + }, + { + type: 'block', + blocks: [ 'core/paragraph' ], + isMatch: ( node ) => { + return node.content.startsWith( 'https://www.strava.com/activities/' ); + }, + transform: function( node ) { + return createBlock( metadata.name, { url: node.content } ); + } + }, + { + type: 'shortcode', + tag: [ 'activity', 'ride' ], + attributes: { + url: { + type: 'string', + shortcode: ( { named: atts } ) => { + return 'https://www.strava.com/activities/' + atts.id; + }, + }, + }, } - ] + ], + to: [ + { + type: 'block', + blocks: [ 'core/paragraph' ], + transform: ( attributes ) => { + return createBlock( 'core/paragraph', { content: attributes.url } ); + } + }, + ], }; registerBlockType( metadata.name, metadata ); From 5cc106aa48329fea3774eba22330b5115f7b8f3e Mon Sep 17 00:00:00 2001 From: Justin Foell <630830+jrfoell@users.noreply.github.com> Date: Fri, 26 Nov 2021 13:09:20 -0600 Subject: [PATCH 3/6] Bumped version --- readme.txt | 6 +++++- wp-strava.php | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/readme.txt b/readme.txt index 70ce305..54bd778 100755 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Contributors: cmanon, jrfoell, lancewillett, dlintott, sebastianerb Tags: strava, activity, bicycle, cycling, biking, running, run, swimming, swim, paddle, kayak, gps, shortcode, widget, plugin, block, blocks Requires at least: 4.6 Tested up to: 5.8 -Stable tag: 2.10.1 +Stable tag: 2.11.0 Requires PHP: 5.3 License: GPLv2 or later @@ -134,6 +134,10 @@ On the WP-Strava settings page you cannot currently remove and add another athle == Changelog == += 2.11.0 = +Added additional block transformations for "Activity" (from: Paragraph, Classic Shortcode; to: Paragraph) + + = 2.10.1 = Reworked settings save with multiple athletes, related to https://wordpress.org/support/topic/wp-strava-error-401-unauthorized/ diff --git a/wp-strava.php b/wp-strava.php index c732c04..416c721 100755 --- a/wp-strava.php +++ b/wp-strava.php @@ -3,7 +3,7 @@ * Plugin Name: WP Strava * Plugin URI: https://wordpress.org/plugins/wp-strava/ * Description: Show your strava.com activity on your WordPress site. Some Icons are Copyright © Yusuke Kamiyamane. All rights reserved. Licensed under a Creative Commons Attribution 3.0 license. - * Version: 2.10.1 + * Version: 2.11.0 * Author: Carlos Santa Cruz, Justin Foell, Lance Willett, Daniel Lintott, Sebastian Erb * License: GPL2 * Text Domain: wp-strava @@ -27,7 +27,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -define( 'WPSTRAVA_PLUGIN_VERSION', '2.10.1' ); +define( 'WPSTRAVA_PLUGIN_VERSION', '2.11.0' ); define( 'WPSTRAVA_PLUGIN_FILE', __FILE__ ); define( 'WPSTRAVA_PLUGIN_DIR', trailingslashit( dirname( __FILE__ ) ) ); define( 'WPSTRAVA_PLUGIN_URL', plugins_url( '/', __FILE__ ) ); From e1814ccfb6bb5896db7e4060c5ba9d8cee78c529 Mon Sep 17 00:00:00 2001 From: Justin Foell <630830+jrfoell@users.noreply.github.com> Date: Fri, 26 Nov 2021 16:36:39 -0600 Subject: [PATCH 4/6] Initial pass at static Mapbox maps --- src/WPStrava/ActivityRenderer.php | 2 +- src/WPStrava/LatestMap.php | 2 +- src/WPStrava/RouteRenderer.php | 2 +- src/WPStrava/SegmentsRenderer.php | 2 +- src/WPStrava/StaticGMap.php | 56 ++++++++++++++++++++++++ src/WPStrava/StaticMap.php | 71 +++++++++++++------------------ src/WPStrava/StaticMapbox.php | 58 +++++++++++++++++++++++++ 7 files changed, 147 insertions(+), 46 deletions(-) create mode 100644 src/WPStrava/StaticGMap.php create mode 100644 src/WPStrava/StaticMapbox.php diff --git a/src/WPStrava/ActivityRenderer.php b/src/WPStrava/ActivityRenderer.php index 26ecef6..cb0edf3 100644 --- a/src/WPStrava/ActivityRenderer.php +++ b/src/WPStrava/ActivityRenderer.php @@ -55,7 +55,7 @@ class WPStrava_ActivityRenderer { $activity_output .= $activity->get_activity_link( $activity_details->id, - WPStrava_StaticMap::get_image_tag( $activity_details, $map_height, $map_width, $atts['markers'], $activity_details->name ), + WPStrava_StaticMap::get_map()->get_image_tag( $activity_details, $map_height, $map_width, $atts['markers'], $activity_details->name ), $activity_details->name ); diff --git a/src/WPStrava/LatestMap.php b/src/WPStrava/LatestMap.php index a28815d..e62a4d9 100644 --- a/src/WPStrava/LatestMap.php +++ b/src/WPStrava/LatestMap.php @@ -54,7 +54,7 @@ class WPStrava_LatestMap { // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped -- Image OK. $strava_activity->get_activity_link( $activity->id, - WPStrava_StaticMap::get_image_tag( $activity, null, null, false, $activity->name ), + WPStrava_StaticMap::get_map()->get_image_tag( $activity, null, null, false, $activity->name ), $activity->name ); // phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped diff --git a/src/WPStrava/RouteRenderer.php b/src/WPStrava/RouteRenderer.php index 524e8ff..f507635 100644 --- a/src/WPStrava/RouteRenderer.php +++ b/src/WPStrava/RouteRenderer.php @@ -60,7 +60,7 @@ class WPStrava_RouteRenderer { $route_output .= $route->get_route_link( $route_details->id, - WPStrava_StaticMap::get_image_tag( $route_details, $map_height, $map_width, $atts['markers'], $route_details->name ), + WPStrava_StaticMap::get_map()->get_image_tag( $route_details, $map_height, $map_width, $atts['markers'], $route_details->name ), $route_details->name ); diff --git a/src/WPStrava/SegmentsRenderer.php b/src/WPStrava/SegmentsRenderer.php index c034611..c7a9631 100644 --- a/src/WPStrava/SegmentsRenderer.php +++ b/src/WPStrava/SegmentsRenderer.php @@ -63,7 +63,7 @@ class WPStrava_SegmentsRenderer { $segments_output .= $segments->get_segments_link( $segment_details->id, - WPStrava_StaticMap::get_image_tag( $segment_details, $map_height, $map_width, $atts['markers'], $segment_details->name ), + WPStrava_StaticMap::get_map()->get_image_tag( $segment_details, $map_height, $map_width, $atts['markers'], $segment_details->name ), $segment_details->name ); diff --git a/src/WPStrava/StaticGMap.php b/src/WPStrava/StaticGMap.php new file mode 100644 index 0000000..de5cedf --- /dev/null +++ b/src/WPStrava/StaticGMap.php @@ -0,0 +1,56 @@ +settings->gmaps_key; + + // Short circuit if missing key or activity object doesn't have the data we need. + if ( empty( $key ) || empty( $activity->map ) ) { + return ''; + } + + if ( ! $height || ! $width ) { + $height = 320; + $width = 480; + } + + $url = "https://maps.googleapis.com/maps/api/staticmap?maptype=terrain&size={$width}x{$height}&scale=2&sensor=false&key={$key}&path=color:0xFF0000BF|weight:2|enc:"; + $url_len = strlen( $url ); + + $polyline = ''; + if ( ! empty( $activity->map->polyline ) && ( $url_len + strlen( $activity->map->polyline ) < self::$max_chars ) ) { + $polyline = $activity->map->polyline; + } elseif ( ! empty( $activity->map->summary_polyline ) ) { + $polyline = $activity->map->summary_polyline; + } elseif ( ! empty( $activity->map->polyline ) ) { + // Need to reduce the polyline b/c it's too big and no summary was provided. + $polyline = $this->reduce_polyline( $url_len, $activity->map->polyline ); + } + $url .= $polyline; + + if ( $markers ) { + $points = $this->decode_start_finish( $polyline ); + $markers = '&markers=color:green|' . $points['start'][0] . ',' . $points['start'][1] . + '&markers=color:red|' . $points['finish'][0] . ',' . $points['finish'][1]; + $url .= $markers; + } + + $title_attr = $title ? " title='" . esc_attr( $title ) . "'" : ''; + return ""; + } +} diff --git a/src/WPStrava/StaticMap.php b/src/WPStrava/StaticMap.php index 1db9475..8d469f1 100644 --- a/src/WPStrava/StaticMap.php +++ b/src/WPStrava/StaticMap.php @@ -1,8 +1,8 @@ settings->gmaps_key; + abstract public function get_image_tag( $activity, $height = 320, $width = 480, $markers = false, $title = '' ); - // Short circuit if missing key or activity object doesn't have the data we need. - if ( empty( $key ) || empty( $activity->map ) ) { - return ''; + /** + * Factory method to get the correct StaticMap class based on specified string + * or by the options setting. + * + * @param string $auth 'google' or 'mapbox' (default 'google'). + * @return WPStrava_StaticMap Instance of StaticMap + * @author Justin Foell + * @since NEXT + */ + public static function get_map( $type = 'google' ) { + /* + if ( 'google' === $type ) { + return new WPStrava_StaticGMap(); } - - if ( ! $height || ! $width ) { - $height = 320; - $width = 480; - } - - $url = "https://maps.googleapis.com/maps/api/staticmap?maptype=terrain&size={$width}x{$height}&scale=2&sensor=false&key={$key}&path=color:0xFF0000BF|weight:2|enc:"; - $url_len = strlen( $url ); - - $polyline = ''; - if ( ! empty( $activity->map->polyline ) && ( $url_len + strlen( $activity->map->polyline ) < self::$max_chars ) ) { - $polyline = $activity->map->polyline; - } elseif ( ! empty( $activity->map->summary_polyline ) ) { - $polyline = $activity->map->summary_polyline; - } elseif ( ! empty( $activity->map->polyline ) ) { - // Need to reduce the polyline b/c it's too big and no summary was provided. - $polyline = self::reduce_polyline( $url_len, $activity->map->polyline ); - } - $url .= $polyline; - - if ( $markers ) { - $points = self::decode_start_finish( $polyline ); - $markers = '&markers=color:green|' . $points['start'][0] . ',' . $points['start'][1] . - '&markers=color:red|' . $points['finish'][0] . ',' . $points['finish'][1]; - $url .= $markers; - } - - $title_attr = $title ? " title='" . esc_attr( $title ) . "'" : ''; - return ""; + */ + // Default to refresh. + return new WPStrava_StaticMapbox(); } /** @@ -76,7 +59,7 @@ class WPStrava_StaticMap { * } * } */ - private static function decode_start_finish( $enc ) { + protected function decode_start_finish( $enc ) { require_once WPSTRAVA_PLUGIN_DIR . 'src/Polyline.php'; $points = Polyline::decode( $enc ); $points = Polyline::pair( $points ); @@ -91,13 +74,13 @@ class WPStrava_StaticMap { * From a (large) encoded polyline, reduce the number of points * until it is small enough for a GET URL. * - * @param int $url_len Length of map URL. - * @param string $enc Encoded polyline. + * @param int $base_url_len Length of map URL. + * @param string $enc Encoded polyline. * @return string Smaller encoded polyline. * @author Justin Foell * @since 2.10.0 */ - private static function reduce_polyline( $url_len, $enc ) { + protected function reduce_polyline( $base_url_len, $enc ) { require_once WPSTRAVA_PLUGIN_DIR . 'src/Polyline.php'; $points = Polyline::decode( $enc ); $points = Polyline::pair( $points ); @@ -109,11 +92,15 @@ class WPStrava_StaticMap { $points = Polyline::flatten( $points ); $polyline = Polyline::encode( $points ); - if ( $url_len + strlen( $polyline ) >= self::$max_chars ) { + if ( $base_url_len + $this->polyline_length( $polyline ) >= self::$max_chars ) { // Reduce again. - $polyline = self::reduce_polyline( $url_len, $polyline ); + $polyline = $this->reduce_polyline( $base_url_len, $polyline ); } return $polyline; } + + protected function polyline_length( $polyline ) { + return strlen( $polyline ); + } } diff --git a/src/WPStrava/StaticMapbox.php b/src/WPStrava/StaticMapbox.php new file mode 100644 index 0000000..40a4854 --- /dev/null +++ b/src/WPStrava/StaticMapbox.php @@ -0,0 +1,58 @@ +map->polyline ) ) { + $polyline = $activity->map->polyline; + } elseif ( ! empty( $activity->map->summary_polyline ) ) { + $polyline = $activity->map->summary_polyline; + } + + if ( empty( $polyline ) ) { + // No polyline provided. + return ''; + } + + $url = $this->build_url( $polyline, $height, $width, $markers ); + + $url_len = strlen( $url ); + if ( $url_len > self::$max_chars ) { + // Need to reduce the polyline b/c it's too big. + $polyline = $this->reduce_polyline( $url_len - $this->polyline_length( $polyline ), $polyline ); + $url = $this->build_url( $polyline, $height, $width, $markers ); + } + + $title_attr = $title ? " title='" . esc_attr( $title ) . "'" : ''; + return ""; + } + + 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'; + + $path = array(); + + if ( $markers ) { + $points = $this->decode_start_finish( $polyline ); + $path[] = "pin-s+008000({$points['start'][0]},{$points['start'][1]})"; + $path[] = "pin-s+ff0000({$points['finish'][0]},{$points['finish'][1]})"; + } + + // 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}"; + + return $url; + } + + protected function polyline_length( $polyline ) { + return strlen( rawurlencode( $polyline ) ); + } +} From 51cc4b7abee7d3373f1e21aaf0e09a39f142fbb7 Mon Sep 17 00:00:00 2001 From: Justin Foell <630830+jrfoell@users.noreply.github.com> Date: Thu, 30 Dec 2021 14:06:30 -0600 Subject: [PATCH 5/6] Added mapbox / gmaps setting switcher --- src/WPStrava.php | 13 ++++ src/WPStrava/Settings.php | 126 ++++++++++++++++++++++++++++------ src/WPStrava/StaticGMap.php | 4 +- src/WPStrava/StaticMap.php | 39 +++++------ src/WPStrava/StaticMapbox.php | 40 ++++++++++- src/admin-settings.js | 50 ++++++++++++++ 6 files changed, 225 insertions(+), 47 deletions(-) create mode 100644 src/admin-settings.js diff --git a/src/WPStrava.php b/src/WPStrava.php index 27ce19d..5abe531 100644 --- a/src/WPStrava.php +++ b/src/WPStrava.php @@ -95,6 +95,7 @@ class WPStrava { 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 @@ class WPStrava { return $this->segments; } + /** + * Register/enqueue admin javascript. + * + * @author Justin Foell + * @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. */ diff --git a/src/WPStrava/Settings.php b/src/WPStrava/Settings.php index aaa50b2..40ba711 100644 --- a/src/WPStrava/Settings.php +++ b/src/WPStrava/Settings.php @@ -80,9 +80,16 @@ class WPStrava_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 @@ class WPStrava_Settings { } /** - * Print the google maps instructions. + * Print the map type selection. * * @author Justin Foell - * @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( - __( - "

Steps:

-
    -
  1. To use Google map images, you must create a Static Maps API Key. Create a free key by going here: %2\$s and clicking Get a Key
  2. -
  3. Once you've created your Google Static Maps API Key, enter the key below.
  4. -
", - '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' : ''; + + ?> + +

+
+

+
+ Steps:

+
    +
  1. 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: %2\$s
  2. +
  3. Once you've created your Google Static Maps API Key, enter the key below.
  4. +
", + 'wp-strava' + ), + $gmaps_url, + $gmaps_url + ) + ); + ?> +
+
+ Steps:

+
    +
  1. To use Mapbox map images, create a free Mapbox account here: %2\$s
  2. +
  3. All Mapbox accounts are issued a default public access token, enter the token below.
  4. +
", + 'wp-strava' + ), + $mapbox_url, + $mapbox_url + ) + ); + ?> +
+
+

+ + * @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 @@ class WPStrava_Settings { $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 @@ class WPStrava_Settings { return $key; } + /** + * Print the Mapbox token input. + * + * @author Justin Foell + * @since next + */ + public function print_mapbox_token_input() { + ?> + + + * @since next + */ + public function sanitize_mapbox_token( $token ) { + return $token; + } + /** * Print System of Measure option. * diff --git a/src/WPStrava/StaticGMap.php b/src/WPStrava/StaticGMap.php index de5cedf..b7f99b8 100644 --- a/src/WPStrava/StaticGMap.php +++ b/src/WPStrava/StaticGMap.php @@ -1,14 +1,12 @@ * @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 @@ abstract class WPStrava_StaticMap { * 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 - * @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 @@ abstract class WPStrava_StaticMap { return $polyline; } + /** + * Get the length of a polyline. + * + * @param mixed $polyline Polyline string. + * @return int Polyline string length. + * @author Justin Foell + * @since next + */ protected function polyline_length( $polyline ) { return strlen( $polyline ); } diff --git a/src/WPStrava/StaticMapbox.php b/src/WPStrava/StaticMapbox.php index 40a4854..8711a4a 100644 --- a/src/WPStrava/StaticMapbox.php +++ b/src/WPStrava/StaticMapbox.php @@ -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 @@ class WPStrava_StaticMapbox extends WPStrava_StaticMap { return ''; } + if ( ! $height || ! $width ) { + $height = 320; + $width = 480; + } + $url = $this->build_url( $polyline, $height, $width, $markers ); $url_len = strlen( $url ); @@ -30,11 +47,22 @@ class WPStrava_StaticMapbox extends WPStrava_StaticMap { return ""; } + /** + * 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 + * @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 @@ class WPStrava_StaticMapbox extends WPStrava_StaticMap { // 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 + * @since next + */ protected function polyline_length( $polyline ) { return strlen( rawurlencode( $polyline ) ); } diff --git a/src/admin-settings.js b/src/admin-settings.js new file mode 100644 index 0000000..abea924 --- /dev/null +++ b/src/admin-settings.js @@ -0,0 +1,50 @@ +window.addEventListener( 'load', function() { + strava_toggle_map_type(); // Initial load toggle. + document.getElementById( 'strava_map_type' ).addEventListener( 'input', function( e ) { + if ( 'SELECT' === e.target.tagName ) { + strava_toggle_map_type(); // On change toggle. + } + }, false ); +} ); + +/** + * Toggle Mapbox / GMaps fields & instructions. + */ +function strava_toggle_map_type() { + var select = document.getElementById( 'strava_map_type' ), + map_type = select.options[select.selectedIndex].value + mapbox_id = 'strava_mapbox_token', + gmaps_id = 'strava_gmaps_key', + mapbox_ins = 'strava-maps-mapbox-instructions', + gmaps_ins = 'strava-maps-gmaps-instructions'; + + // Toggle Instructions. + var mapbox_text = document.getElementById( mapbox_ins ); + if ( 'mapbox' === map_type ) { + if ( mapbox_text.classList.contains( 'hidden' ) ) { + mapbox_text.classList.remove( 'hidden' ); + } + } else { + mapbox_text.classList.add( 'hidden' ); + } + + var gmaps_text = document.getElementById( gmaps_ins ); + if ( 'mapbox' !== map_type ) { + if ( gmaps_text.classList.contains( 'hidden' ) ) { + gmaps_text.classList.remove( 'hidden' ); + } + } else { + gmaps_text.classList.add( 'hidden' ); + } + + // Toggle Inputs. + var mapbox_row = document.getElementById( mapbox_id ).closest( 'tr' ); + if ( mapbox_row ) { + mapbox_row.style.display = ( 'mapbox' === map_type ) ? '' : 'none'; + } + + var gmaps_row = document.getElementById( gmaps_id ).closest( 'tr' ); + if ( gmaps_row ) { + gmaps_row.style.display = ( 'mapbox' !== map_type ) ? '' : 'none'; + } +} From 9abd2e9ac2efd0ecad8aefcc1b8a1c5071261c52 Mon Sep 17 00:00:00 2001 From: Justin Foell <630830+jrfoell@users.noreply.github.com> Date: Thu, 30 Dec 2021 14:14:07 -0600 Subject: [PATCH 6/6] Updated readme and versions --- readme.txt | 3 ++- src/WPStrava.php | 2 +- src/WPStrava/Settings.php | 8 ++++---- src/WPStrava/StaticMap.php | 4 ++-- src/WPStrava/StaticMapbox.php | 4 ++-- 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/readme.txt b/readme.txt index 54bd778..3ff3fca 100755 --- a/readme.txt +++ b/readme.txt @@ -135,7 +135,8 @@ On the WP-Strava settings page you cannot currently remove and add another athle == Changelog == = 2.11.0 = -Added additional block transformations for "Activity" (from: Paragraph, Classic Shortcode; to: Paragraph) +Add additional block transformations for "Activity" (from: Paragraph, Classic Shortcode; to: Paragraph) +Add Mapbox Static Map support https://github.com/cmanon/wp-strava/issues/26 = 2.10.1 = diff --git a/src/WPStrava.php b/src/WPStrava.php index 5abe531..212b100 100644 --- a/src/WPStrava.php +++ b/src/WPStrava.php @@ -193,7 +193,7 @@ class WPStrava { * Register/enqueue admin javascript. * * @author Justin Foell - * @since next + * @since 2.11 */ public function register_admin_scripts() { if ( $this->settings->is_settings_page() ) { diff --git a/src/WPStrava/Settings.php b/src/WPStrava/Settings.php index 40ba711..d4b8401 100644 --- a/src/WPStrava/Settings.php +++ b/src/WPStrava/Settings.php @@ -170,7 +170,7 @@ class WPStrava_Settings { * Print the map type selection. * * @author Justin Foell - * @since next + * @since 2.11 */ public function print_map_type_input() { $gmaps_url = 'https://developers.google.com/maps/documentation/static-maps/'; @@ -234,7 +234,7 @@ class WPStrava_Settings { * @param string $map_type * @return string * @author Justin Foell - * @since next + * @since 2.11 */ public function sanitize_map_type_input( $map_type ) { if ( in_array( $map_type, array( 'gmaps', 'mapbox' ), true ) ) { @@ -489,7 +489,7 @@ class WPStrava_Settings { * Print the Mapbox token input. * * @author Justin Foell - * @since next + * @since 2.11 */ public function print_mapbox_token_input() { ?> @@ -503,7 +503,7 @@ class WPStrava_Settings { * @param string $token * @return string * @author Justin Foell - * @since next + * @since 2.11 */ public function sanitize_mapbox_token( $token ) { return $token; diff --git a/src/WPStrava/StaticMap.php b/src/WPStrava/StaticMap.php index 7818dc2..d52348c 100644 --- a/src/WPStrava/StaticMap.php +++ b/src/WPStrava/StaticMap.php @@ -21,7 +21,7 @@ abstract class WPStrava_StaticMap { * * @return WPStrava_StaticMap Instance of StaticMap * @author Justin Foell - * @since NEXT + * @since 2.11 */ public static function get_map() { if ( 'mapbox' === WPStrava::get_instance()->settings->map_type ) { @@ -95,7 +95,7 @@ abstract class WPStrava_StaticMap { * @param mixed $polyline Polyline string. * @return int Polyline string length. * @author Justin Foell - * @since next + * @since 2.11 */ protected function polyline_length( $polyline ) { return strlen( $polyline ); diff --git a/src/WPStrava/StaticMapbox.php b/src/WPStrava/StaticMapbox.php index 8711a4a..ffe28d3 100644 --- a/src/WPStrava/StaticMapbox.php +++ b/src/WPStrava/StaticMapbox.php @@ -56,7 +56,7 @@ class WPStrava_StaticMapbox extends WPStrava_StaticMap { * @param bool $markers Display start and finish markers. * @return string Image URL. * @author Justin Foell - * @since next + * @since 2.11 */ private function build_url( $polyline, $height = 320, $width = 480, $markers = false ) { @@ -86,7 +86,7 @@ class WPStrava_StaticMapbox extends WPStrava_StaticMap { * @param mixed $polyline Polyline string. * @return int Encoded polyline string length. * @author Justin Foell - * @since next + * @since 2.11 */ protected function polyline_length( $polyline ) { return strlen( rawurlencode( $polyline ) );