Modified formatting

Made Polyline PHP 5.2 compatible
This commit is contained in:
Justin Foell
2017-12-26 13:22:22 -06:00
parent abbdf7af95
commit 04cfda401a
5 changed files with 171 additions and 156 deletions
+23 -22
View File
@@ -3,7 +3,7 @@
/** /**
* Polyline * Polyline
* *
* PHP Version 5.3 * PHP Version 5.2 (forked)
* *
* A simple class to handle polyline-encoding for Google Maps * A simple class to handle polyline-encoding for Google Maps
* *
@@ -41,8 +41,8 @@
* @license http://www.gnu.org/licenses/lgpl.html LGPL v3 * @license http://www.gnu.org/licenses/lgpl.html LGPL v3
* @link https://github.com/emcconville/google-map-polyline-encoding-tool * @link https://github.com/emcconville/google-map-polyline-encoding-tool
*/ */
class Polyline class Polyline {
{
/** /**
* Default precision level of 1e-5. * Default precision level of 1e-5.
* *
@@ -57,6 +57,9 @@ class Polyline
*/ */
protected static $precision = 5; protected static $precision = 5;
// To remove PHP 5.3 requirement.
protected static $flatten = array();
/** /**
* Apply Google Polyline algorithm to list of points. * Apply Google Polyline algorithm to list of points.
* *
@@ -65,17 +68,18 @@ class Polyline
* *
* @return string encoded string * @return string encoded string
*/ */
final public static function encode( $points ) final public static function encode( $points ) {
{
$points = self::flatten( $points ); $points = self::flatten( $points );
$encodedString = ''; $encoded_string = '';
$index = 0; $index = 0;
$previous = array( 0, 0 ); $previous = array( 0, 0 );
foreach ( $points as $number ) { foreach ( $points as $number ) {
$number = (float)($number); $number = (float) $number;
$number = (int) round( $number * pow( 10, static::$precision ) ); $number = (int) round( $number * pow( 10, static::$precision ) );
$diff = $number - $previous[ $index % 2 ]; $diff = $number - $previous[ $index % 2 ];
$previous[ $index % 2 ] = $number; $previous[ $index % 2 ] = $number;
$number = $diff; $number = $diff;
$index++; $index++;
$number = ( $number < 0 ) ? ~( $number << 1 ) : ( $number << 1 ); $number = ( $number < 0 ) ? ~( $number << 1 ) : ( $number << 1 );
@@ -85,9 +89,9 @@ class Polyline
$number >>= 5; $number >>= 5;
} }
$chunk .= chr( $number + 63 ); $chunk .= chr( $number + 63 );
$encodedString .= $chunk; $encoded_string .= $chunk;
} }
return $encodedString; return $encoded_string;
} }
/** /**
@@ -97,8 +101,7 @@ class Polyline
* *
* @return array points * @return array points
*/ */
final public static function decode( $string ) final public static function decode( $string ) {
{
$points = array(); $points = array();
$index = $i = 0; $index = $i = 0;
$previous = array( 0, 0 ); $previous = array( 0, 0 );
@@ -112,6 +115,7 @@ class Polyline
$diff = ( $result & 1 ) ? ~( $result >> 1 ) : ( $result >> 1 ); $diff = ( $result & 1 ) ? ~( $result >> 1 ) : ( $result >> 1 );
$number = $previous[ $index % 2 ] + $diff; $number = $previous[ $index % 2 ] + $diff;
$previous[ $index % 2 ] = $number; $previous[ $index % 2 ] = $number;
$index++; $index++;
$points[] = $number * 1 / pow( 10, static::$precision ); $points[] = $number * 1 / pow( 10, static::$precision );
@@ -126,16 +130,14 @@ class Polyline
* *
* @return array flattened * @return array flattened
*/ */
final public static function flatten( $array ) final public static function flatten( $array ) {
{ self::$flatten = array();
$flatten = array(); array_walk_recursive( $array, array( 'Polyline', 'flatten_callback' ) );
array_walk_recursive( return self::$flatten;
$array, // @codeCoverageIgnore
function ($current) use (&$flatten) {
$flatten[] = $current;
} }
);
return $flatten; final public static function flatten_callback( $value ) {
self::$flatten[] = $value;
} }
/** /**
@@ -145,8 +147,7 @@ class Polyline
* *
* @return array pairs * @return array pairs
*/ */
final public static function pair( $list ) final public static function pair( $list ) {
{
return is_array( $list ) ? array_chunk( $list, 2 ) : array(); return is_array( $list ) ? array_chunk( $list, 2 ) : array();
} }
} }
+1 -4
View File
@@ -26,7 +26,7 @@ class WPStrava_RouteShortcode {
$strava_som = WPStrava_SOM::get_som( $som ); $strava_som = WPStrava_SOM::get_som( $som );
$route = WPStrava::get_instance()->routes; $route = WPStrava::get_instance()->routes;
$route_details = $route->getRoute( $id ); $route_details = $route->get_route( $id );
//sanitize width & height //sanitize width & height
$map_width = str_replace( '%', '', $map_width ); $map_width = str_replace( '%', '', $map_width );
@@ -66,9 +66,6 @@ class WPStrava_RouteShortcode {
public static function print_scripts() { public static function print_scripts() {
if ( self::$add_script ) { if ( self::$add_script ) {
wp_enqueue_style( 'wp-strava-style' ); wp_enqueue_style( 'wp-strava-style' );
//wp_print_scripts('google-maps');
//wp_print_scripts('wp-strava-script');
} }
} }
} }
+3 -4
View File
@@ -3,8 +3,7 @@
* Routes is a class wrapper for the Strava REST API functions. * Routes is a class wrapper for the Strava REST API functions.
*/ */
class WPStrava_Routes class WPStrava_Routes {
{
const ROUTES_URL = 'http://app.strava.com/routes/'; const ROUTES_URL = 'http://app.strava.com/routes/';
/** /**
@@ -14,7 +13,7 @@ class WPStrava_Routes
* @return object stdClass representing this route. * @return object stdClass representing this route.
* @author Daniel Lintott * @author Daniel Lintott
*/ */
public function getRoute($route_id ) { public function get_route( $route_id ) {
return WPStrava::get_instance()->get_api()->get( "routes/{$route_id}" ); return WPStrava::get_instance()->get_api()->get( "routes/{$route_id}" );
} // getRouteDetails }
} }
+22 -8
View File
@@ -1,7 +1,5 @@
<?php <?php
require_once WPSTRAVA_PLUGIN_DIR . 'lib/Polyline.php';
class WPStrava_StaticMap { class WPStrava_StaticMap {
/** /**
@@ -15,6 +13,7 @@ class WPStrava_StaticMap {
* @param int $height Height of map in pixels. * @param int $height Height of map in pixels.
* @param int $width Width of map in pixels. * @param int $width Width of map in pixels.
* @param bool $markers Display start and finish markers. * @param bool $markers Display start and finish markers.
* @return string HTML img tag with static map image.
*/ */
public static function get_image_tag( $ride, $height = 320, $width = 480, $markers = false ) { public static function get_image_tag( $ride, $height = 320, $width = 480, $markers = false ) {
$key = WPStrava::get_instance()->settings->gmaps_key; $key = WPStrava::get_instance()->settings->gmaps_key;
@@ -28,15 +27,16 @@ class WPStrava_StaticMap {
$url_len = strlen( $url ); $url_len = strlen( $url );
$max_chars = 1865; $max_chars = 1865;
$polyline = '';
if ( ! empty( $ride->map->polyline ) && ( $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; $polyline = $ride->map->polyline;
$points = self::decode_polyline($ride->map->polyline);
} elseif ( ! empty( $ride->map->summary_polyline ) ) { } elseif ( ! empty( $ride->map->summary_polyline ) ) {
$url .= $ride->map->summary_polyline; $polyline = $ride->map->summary_polyline;
$points = self::decode_polyline($ride->map->summary_polyline);
} }
$url .= $polyline;
if ( $markers ) { if ( $markers ) {
$points = self::decode_start_finish( $polyline );
$markers = '&markers=color:green|' . $points['start'][0] . ',' . $points['start'][1] . $markers = '&markers=color:green|' . $points['start'][0] . ',' . $points['start'][1] .
'&markers=color:red|' . $points['finish'][0] . ',' . $points['finish'][1]; '&markers=color:red|' . $points['finish'][0] . ',' . $points['finish'][1];
$url .= $markers; $url .= $markers;
@@ -45,13 +45,27 @@ class WPStrava_StaticMap {
return "<img class='wp-strava-img' src='{$url}' />"; return "<img class='wp-strava-img' src='{$url}' />";
} }
private static function decode_polyline($enc) { /**
* 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 with indexes of start & finish containing lat/lon for each.
*/
private static function decode_start_finish( $enc ) {
require_once WPSTRAVA_PLUGIN_DIR . 'lib/Polyline.php';
$points = Polyline::decode( $enc ); $points = Polyline::decode( $enc );
$points = Polyline::pair( $points ); $points = Polyline::pair( $points );
$start = $points[0]; $start = $points[0];
$finish = $points[ count( $points ) - 1 ]; $finish = $points[ count( $points ) - 1 ];
return array('start' => $start, 'finish' => $finish); return array(
'start' => $start,
'finish' => $finish,
);
} }
} }
+11 -7
View File
@@ -38,12 +38,16 @@ Strava Latest Map - shows map of latest activity with option to limit latest map
== Changelog == == Changelog ==
= 1.3.0 =
Added [route] shortcode and start/finish https://github.com/cmanon/wp-strava/pull/10/
Fixed error with /rides link (should be /activities). https://wordpress.org/support/topic/problem-with-link-4/
= 1.2.0 = = 1.2.0 =
Added multi-athlete configuration. Added multi-athlete configuration. https://wordpress.org/support/topic/multi-strava-user/
Additional transitions from Ride -> Activity. Additional transitions from Ride -> Activity.
Updated setup instructions to reflect latest Strava API set up process. Updated setup instructions to reflect latest Strava API set up process.
Backwards Compatibility - removed PHP 5.3+ specific operator (should work with PHP 5.2 now - versions 1.1 and 1.1.1 don't). Backwards Compatibility - removed PHP 5.3+ specific operator (should work with PHP 5.2 now - versions 1.1 and 1.1.1 don't). https://wordpress.org/support/topic/version-1-1-broken/
Reworked error reporting and formatting. Reworked error reporting and formatting. https://wordpress.org/support/topic/updating-settings-failure/#post-9764942
= 1.1.1 = = 1.1.1 =
Changes to better support translations through https://translate.wordpress.org. Changes to better support translations through https://translate.wordpress.org.
@@ -52,15 +56,15 @@ Cleaned up formatting.
= 1.1 = = 1.1 =
Added [activity] shortcode to deprecate [ride] in the future. Added [activity] shortcode to deprecate [ride] in the future.
Fixed static method call error in shortcode. Fixed static method call error in shortcode.
Added title to Strava Latest Map Widget. Added title to Strava Latest Map Widget. https://wordpress.org/support/topic/change-widget-title-from-latest-ride-to-latest-run-or-something-else/
Added Lance Willett to contributors. Added Lance Willett to contributors.
Added target="_blank" to widget hrefs. Added target="_blank" to widget hrefs.
Added Google Maps Key to settings (required for map images). Added Google Maps Key to settings (required for map images). https://wordpress.org/support/topic/the-google-maps-api-server-rejected-your-request-3/
Added cache clear option to remove transient & image data. Added cache clear option to remove transient & image data.
Cleaned up formatting. Cleaned up formatting.
= 1.0 = = 1.0 =
Change to Strava API V3. Change to Strava API V3. https://wordpress.org/support/topic/does-not-work-354/
Switch ride shortcode to use static map. Switch ride shortcode to use static map.
= 0.70 = = 0.70 =
@@ -73,7 +77,7 @@ Fixed several bugs.
Added feature to show athlete name/link to the widget if the search option is by club. Added feature to show athlete name/link to the widget if the search option is by club.
= 0.61 = = 0.61 =
Added option to select unit of measurements on the widget. Added option to select unit of measurements on the widget. https://wordpress.org/support/topic/feature-request-runs-in-minkm/
= 0.6 = = 0.6 =
Initial version. Initial version.