diff --git a/includes/WPStrava.php b/includes/WPStrava.php index c862bee..07b7a93 100644 --- a/includes/WPStrava.php +++ b/includes/WPStrava.php @@ -47,19 +47,6 @@ class WPStrava { private function __construct() { $this->settings = new WPStrava_Settings(); $this->auth = WPStrava_Auth::get_auth( 'refresh' ); - - $this->auth->hook(); - - if ( is_admin() ) { - $this->settings->hook(); - } else { - add_action( 'init', array( $this, 'register_shortcodes' ) ); - add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts' ) ); - } - - // Register widgets. - add_action( 'widgets_init', array( $this, 'register_widgets' ) ); - } /** @@ -75,6 +62,27 @@ class WPStrava { return self::$instance; } + /** + * Function to install hooks at WP runtime. + * + * @author Justin Foell + * @since 2.0.0 + */ + public function hook() { + $this->auth->hook(); + + if ( is_admin() ) { + $this->settings->hook(); + } else { + add_action( 'init', array( $this, 'register_shortcodes' ) ); + add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts' ) ); + } + + // Register widgets. + add_action( 'widgets_init', array( $this, 'register_widgets' ) ); + + } + /** * Magic method to access activity, routes, settings, etc. * diff --git a/includes/WPStrava/Auth.php b/includes/WPStrava/Auth.php index 14f6574..670906e 100644 --- a/includes/WPStrava/Auth.php +++ b/includes/WPStrava/Auth.php @@ -78,7 +78,7 @@ abstract class WPStrava_Auth { return rawurlencode( admin_url( "options-general.php?page={$page_name}" ) ); } - // was fetch_token(); + // Was fetch_token(); private function token_exchange_initial( $code ) { $settings = WPStrava::get_instance()->settings; $client_id = $settings->client_id; diff --git a/includes/WPStrava/RouteShortcode.php b/includes/WPStrava/RouteShortcode.php index a0b997a..33f8a1f 100644 --- a/includes/WPStrava/RouteShortcode.php +++ b/includes/WPStrava/RouteShortcode.php @@ -111,7 +111,6 @@ class WPStrava_RouteShortcode { private function get_table( $route_details, $som ) { $strava_som = WPStrava_SOM::get_som( $som ); - $elevation_title = '' . __( 'Elevation Gain', 'wp-strava' ) . ''; $elevation = '' . $strava_som->elevation( $route_details->elevation_gain ) . ''; $elevation_label = '' . $strava_som->get_elevation_label() . ''; diff --git a/includes/WPStrava/SOMEnglish.php b/includes/WPStrava/SOMEnglish.php index 477d9a5..73fa95c 100644 --- a/includes/WPStrava/SOMEnglish.php +++ b/includes/WPStrava/SOMEnglish.php @@ -14,17 +14,17 @@ class WPStrava_SOMEnglish extends WPStrava_SOM { * @return string Distance in miles. */ public function distance( $m ) { - return number_format( $m / 1609.344, 2 ); + return number_format_i18n( $m / 1609.344, 2 ); } /** * Change miles to meters. * - * @param float|string $dist Distance in miles. - * @return string Distance in meters. + * @param float $dist Distance in miles. + * @return float Distance in meters. */ public function distance_inverse( $dist ) { - return number_format( $dist * 1609.344, 2 ); + return (float) number_format( $dist * 1609.344, 2, '.', '' ); } /** @@ -43,7 +43,7 @@ class WPStrava_SOMEnglish extends WPStrava_SOM { * @return string Miles per hour. */ public function speed( $mps ) { - return number_format( $mps * 2.2369, 2 ); + return number_format_i18n( $mps * 2.2369, 2 ); } /** @@ -93,7 +93,7 @@ class WPStrava_SOMEnglish extends WPStrava_SOM { * @return string Elevation in feet. */ public function elevation( $m ) { - return number_format( $m / 0.3048, 2 ); + return number_format_i18n( $m / 0.3048, 2 ); } /** diff --git a/includes/WPStrava/SOMMetric.php b/includes/WPStrava/SOMMetric.php index 5a239c1..7ec59de 100644 --- a/includes/WPStrava/SOMMetric.php +++ b/includes/WPStrava/SOMMetric.php @@ -14,17 +14,17 @@ class WPStrava_SOMMetric extends WPStrava_SOM { * @return string Distance in kilometers. */ public function distance( $m ) { - return number_format( $m / 1000, 2 ); + return number_format_i18n( $m / 1000, 2 ); } /** * Change kilometers to meters. * - * @param float|string $dist Distance in kilometers. - * @return string Distance in meters. + * @param float $dist Distance in kilometers. + * @return float Distance in meters. */ public function distance_inverse( $dist ) { - return number_format( $dist * 1000, 2 ); + return (float) number_format( $dist * 1000, 2, '.', '' ); } /** @@ -43,7 +43,7 @@ class WPStrava_SOMMetric extends WPStrava_SOM { * @return string Kilometers per hour. */ public function speed( $mps ) { - return number_format( $mps * 3.6, 2 ); + return number_format_i18n( $mps * 3.6, 2 ); } /** @@ -94,7 +94,7 @@ class WPStrava_SOMMetric extends WPStrava_SOM { * @return string Elevation in meters. */ public function elevation( $m ) { - return number_format( $m, 2 ); + return number_format_i18n( $m, 2 ); } /** diff --git a/includes/WPStrava/Settings.php b/includes/WPStrava/Settings.php index 51cfcf1..6240e96 100644 --- a/includes/WPStrava/Settings.php +++ b/includes/WPStrava/Settings.php @@ -16,17 +16,6 @@ class WPStrava_Settings { private $option_page = 'wp-strava-settings-group'; private $adding_athlete = true; - - /** - * Settings constructor. - * - * @author Justin Foell - * @since 2.0 - */ - public function __construct() { - $this->ids = $this->get_ids(); - } - /** * Register actions & filters for menus and authentication. * @@ -34,6 +23,9 @@ class WPStrava_Settings { * @since 0.62 */ public function hook() { + // Load IDs for any subsequent actions. + $this->ids = $this->get_ids(); + add_action( 'admin_init', array( $this, 'register_strava_settings' ) ); add_action( 'admin_menu', array( $this, 'add_strava_menu' ) ); add_filter( 'plugin_action_links_' . WPSTRAVA_PLUGIN_NAME, array( $this, 'settings_link' ) ); @@ -260,7 +252,7 @@ class WPStrava_Settings { * * @param string $client_secret * @return string - * @author Justin Foell + * @author Justin Foell * @since 1.2.0 */ public function sanitize_client_secret( $client_secret ) { @@ -574,12 +566,12 @@ class WPStrava_Settings { } /** - * Undocumented function + * Update options with new Client ID and Info. * * @param int $id Strava API Client ID * @param string $secret Strava API Client Secret * @param stdClass $info - * @author Justin Foell + * @author Justin Foell * @since 2.0.0 */ public function save_info( $id, $secret, $info ) { @@ -595,7 +587,7 @@ class WPStrava_Settings { * * @param int $key Strava Client ID * @return boolean True if Client ID is in $this->ids, false otherwise. - * @author Justin Foell + * @author Justin Foell * @since 2.0.0 */ public function filter_by_id( $key ) { @@ -606,10 +598,9 @@ class WPStrava_Settings { } /** - * Undocumented function + * Remove the client ID and Secret (they're saved in the strava_info option). * - * @return void - * @author Justin Foell + * @author Justin Foell * @since 2.0.0 */ public function delete_id_secret() { @@ -618,11 +609,11 @@ class WPStrava_Settings { } /** - * Undocumented function + * Check to see if settings have been updated. * - * @param [type] $value + * @param array $value Data array from pre_set_transient_settings_errors filter. * @return boolean - * @author Justin Foell + * @author Justin Foell * @since 2.0.0 */ public function is_settings_updated( $value ) { @@ -630,10 +621,10 @@ class WPStrava_Settings { } /** - * Undocumented function + * Whether or not we're on the options page. * * @return boolean - * @author Justin Foell + * @author Justin Foell * @since 2.0.0 */ public function is_option_page() { @@ -641,10 +632,10 @@ class WPStrava_Settings { } /** - * Undocumented function + * Whether or not we're on the WP-Strava settings page. * * @return boolean - * @author Justin Foell + * @author Justin Foell * @since 2.0.0 */ public function is_settings_page() { @@ -652,10 +643,10 @@ class WPStrava_Settings { } /** - * Undocumented function + * Get the WP-Strava settings page name. * * @return string - * @author Justin Foell + * @author Justin Foell * @since 2.0.0 */ public function get_page_name() { @@ -663,10 +654,10 @@ class WPStrava_Settings { } /** - * Undocumented function + * Whether or not we're adding a new athlete. * * @return boolean - * @author Justin Foell + * @author Justin Foell * @since 2.0.0 */ private function is_adding_athlete() { @@ -707,7 +698,7 @@ class WPStrava_Settings { * * @param array $data Plugin data with readme additions. * @param array $response Response from wp.org. - * @author Justin Foell + * @author Justin Foell * @since 1.7.3 */ public function plugin_update_message( $data, $response ) { @@ -721,7 +712,7 @@ class WPStrava_Settings { * * @param string $file Relative path to plugin, i.e. wp-strava/wp-strava.php. * @param array $plugin Plugin data with readme additions. - * @author Justin Foell + * @author Justin Foell * @since 1.7.3 */ public function ms_plugin_update_message( $file, $plugin ) { diff --git a/tests/WPStrava/ActivityTest.php b/tests/WPStrava/ActivityTest.php new file mode 100644 index 0000000..0378568 --- /dev/null +++ b/tests/WPStrava/ActivityTest.php @@ -0,0 +1,57 @@ +activity = new WPStrava_Activity(); + + $activity0 = new stdClass(); + $activity1 = new stdClass(); + $activity2 = new stdClass(); + + $activity0->distance = 100; + $activity1->distance = 1600; + $activity2->distance = 45000; + + $this->activities = array( + $activity0, + $activity1, + $activity2, + ); + } + + public function tearDown() : void { + \WP_Mock::tearDown(); + } + + public function test_object() { + $this->assertInstanceOf( 'WPStrava_Activity', $this->activity ); + } + + /** + * Test that we only get the 45,000 meter activity when filtering greater than 10.00 kilometers. + * + * @author Justin Foell + * @since 2.0.0 + */ + public function test_get_activities_longer_than() { + \WP_Mock::userFunction( + 'get_option', + array( + 'args' => 'strava_som', + 'times' => 1, + 'return' => 'metric', + ) + ); + + $expected = array( $this->activities[2] ); + $actual = $this->activity->get_activities_longer_than( $this->activities, '10' ); + $this->assertEquals( $expected, $actual ); + } +} diff --git a/tests/WPStrava/SOMEnglishTest.php b/tests/WPStrava/SOMEnglishTest.php index 65cc3a5..277cd35 100644 --- a/tests/WPStrava/SOMEnglishTest.php +++ b/tests/WPStrava/SOMEnglishTest.php @@ -7,9 +7,14 @@ class WPStrava_SOMEnglishTest extends TestCase { private $som; public function setUp() : void { + \WP_Mock::setUp(); $this->som = new WPStrava_SOMEnglish(); } + public function tearDown() : void { + \WP_Mock::tearDown(); + } + public function test_object() { $this->assertInstanceOf( 'WPStrava_SOMEnglish', $this->som ); } @@ -26,14 +31,13 @@ class WPStrava_SOMEnglishTest extends TestCase { } /** - * Test that 6.213712 miles is 10,000.00 meters using both string and float inputs. + * Test that 6.213712 miles is 10,000.00 meters using float input. * * @author Justin Foell * @since 1.7.1 */ public function test_distance_inverse() { - $this->assertEquals( '10,000.00', $this->som->distance_inverse( '6.213712' ) ); - $this->assertEquals( '10,000.00', $this->som->distance_inverse( 6.213712 ) ); + $this->assertEquals( 10000.00, $this->som->distance_inverse( 6.213712 ) ); } /** diff --git a/tests/WPStrava/SOMMetricTest.php b/tests/WPStrava/SOMMetricTest.php index c51ae3c..18e560a 100644 --- a/tests/WPStrava/SOMMetricTest.php +++ b/tests/WPStrava/SOMMetricTest.php @@ -26,14 +26,13 @@ class WPStrava_SOMMetricTest extends TestCase { } /** - * Test that 42.195 km is 42,195.00 meters using both string and float inputs. + * Test that 42.195 km is 42,195.00 meters using float input. * * @author Justin Foell * @since 1.7.1 */ public function test_distance_inverse() { - $this->assertEquals( '42,195.00', $this->som->distance_inverse( '42.195' ) ); - $this->assertEquals( '42,195.00', $this->som->distance_inverse( 42.195 ) ); + $this->assertEquals( 42195.00, $this->som->distance_inverse( 42.195 ) ); } /** diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 24408ee..748b9cf 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -8,3 +8,10 @@ require_once dirname( __FILE__ ) . '/../includes/autoload.php'; require_once dirname( __FILE__ ) . '/../vendor/autoload.php'; WP_Mock::bootstrap(); + +// Pseudo mocks for WP functions. +if ( ! function_exists( 'number_format_i18n' ) ) : + function number_format_i18n( $number, $decimals ) { + return number_format( $number, $decimals ); + } +endif; diff --git a/wp-strava.php b/wp-strava.php index 9f9fd1c..4310339 100755 --- a/wp-strava.php +++ b/wp-strava.php @@ -44,3 +44,4 @@ function wpstrava_load_plugin_textdomain() { add_action( 'plugins_loaded', 'wpstrava_load_plugin_textdomain' ); $wpstrava = WPStrava::get_instance(); +$wpstrava->hook();