som = WPStrava_SOM::get_som(); parent::__construct( false, __( 'Strava Latest Map', 'wp-strava' ), // Name array( 'description' => __( 'Strava latest activity using static google map image', 'wp-strava' ) ) // Args. ); } public function form( $instance ) { // outputs the options form on admin $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : __( 'Latest Activity Map', 'wp-strava' ); $all_tokens = WPStrava::get_instance()->settings->get_all_tokens(); $athlete_token = isset( $instance['athlete_token'] ) ? esc_attr( $instance['athlete_token'] ) : WPStrava::get_instance()->settings->get_default_token(); $distance_min = isset( $instance['distance_min'] ) ? esc_attr( $instance['distance_min'] ) : ''; $strava_club_id = isset( $instance['strava_club_id'] ) ? esc_attr( $instance['strava_club_id'] ) : ''; ?>
settings->get_default_token(); $distance_min = empty( $instance['distance_min'] ) ? 0 : absint( $instance['distance_min'] ); $strava_club_id = empty( $instance['strava_club_id'] ) ? null : $instance['strava_club_id']; $build_new = false; // Try our transient first. $ride_transient = get_transient( 'strava_latest_map_activity_' . $athlete_token ); $ride_option = get_option( 'strava_latest_map_activity_' . $athlete_token ); $ride = $ride_transient ? $ride_transient : null; if ( ! $ride ) { $strava_rides = WPStrava::get_instance()->rides; $rides = $strava_rides->getRides( $athlete_token, $strava_club_id ); if ( is_wp_error( $rides ) ) { echo $before_widget; if ( $title ) { echo $before_title . $title . $after_title; } if ( WPSTRAVA_DEBUG ) { echo '
'; print_r( $rides ); // @codingStandardsIgnoreLine echo ''; } else { echo $rides->get_error_message(); } echo $after_widget; return; } if ( ! empty( $rides ) ) { if ( ! empty( $distance_min ) ) { $rides = $strava_rides->getRidesLongerThan( $rides, $distance_min ); } $ride = current( $rides ); // Compare transient (temporary storage) to option (more permenant). // If the option isn't set or the transient is different, update the option. if ( empty( $ride_option->id ) || $ride->id != $ride_option->id ) { $build_new = true; $this->update_activity( $athlete_token, $ride ); } // Update the transient if it needs updating. if ( empty( $ride_transient->id ) || $ride->id != $ride_transient->id ) { $this->update_activity_transient( $athlete_token, $ride ); } } } if ( $ride ) { echo $before_widget; if ( $title ) { echo $before_title . $title . $after_title; } echo ""; echo $this->getStaticImage( $athlete_token, $ride, $build_new ); echo ''; echo $after_widget; } } /** * Get image for specific ride using Static Maps class. * * @author Justin Foell * * @param string $athlete_token Token for athelete. * @param int $ride_id Club ID (optional). * @param boolean $build_new Whether to refresh the image from cache. * @return string Image tag. */ private function getStaticImage( $athlete_token, $ride, $build_new ) { $img = get_option( 'strava_latest_map_' . $athlete_token ); if ( $build_new || ! $img ) { $img = WPStrava_StaticMap::get_image_tag( $ride ); $this->update_map( $athlete_token, $img ); } return $img; } /** * Update map in option to cache. * * @author Justin Foell * @since 1.2.0 * * @param string $athlete_token Token for athelete. * @param string $img Image tag. */ private function update_map( $athlete_token, $img ) { // Remove old (pre 1.2.0) cached maps. if ( get_option( 'strava_latest_map' ) ) { delete_option( 'strava_latest_map' ); } update_option( 'strava_latest_map_' . $athlete_token, $img ); } /** * Update activity in option to cache. * * @author Justin Foell * @since 1.2.0 * * @param string $athlete_token Token for athelete. * @param object $activity stdClass Strava activity object. */ private function update_activity( $athlete_token, $activity ) { // Remove old (pre 1.2.0) option. if ( get_option( 'strava_latest_map_ride' ) ) { delete_option( 'strava_latest_map_ride' ); } update_option( 'strava_latest_map_activity_' . $athlete_token, $activity ); } /** * Update activity in transient to cache. * * @author Justin Foell * @since 1.2.0 * * @param string $athlete_token Token for athelete. * @param object $activity stdClass Strava activity object. */ private function update_activity_transient( $athlete_token, $activity ) { // Remove old (pre 1.2.0) transient. if ( get_transient( 'strava_latest_map_ride' ) ) { delete_transient( 'strava_latest_map_ride' ); } set_transient( 'strava_latest_map_activity_' . $athlete_token, $activity, HOUR_IN_SECONDS ); } }