diff --git a/css/wp-strava.css b/css/wp-strava.css index 7c1aea6..0b58c0d 100755 --- a/css/wp-strava.css +++ b/css/wp-strava.css @@ -32,3 +32,44 @@ .activity-details-table-units { font-size: 0.8em; } + +/* Responsive Tables */ +@media +only screen and (max-width: 760px), +(min-device-width: 768px) and (max-device-width: 1024px) { + + /* Force table to not be like tables anymore */ + table, thead, tbody, th, td, tr { + display: block; + } + + /* Hide table headers (but not display: none;, for accessibility) */ + thead tr { + position: absolute; + top: -9999px; + left: -9999px; + } + + td { + /* Behave like a "row" */ + border: none; + border-bottom: 1px solid #eee; + position: relative; + padding-left: 50%; + } + + td:before { + /* Now like a table header */ + position: absolute; + /* Top/left values mimic padding */ + top: 5px; + left: 5px; + width: 45%; + padding-right: 10px; + white-space: nowrap; + } + + .activity-details-table-info, .activity-details-table-units { + display: inline; + } +} diff --git a/src/WPStrava.php b/src/WPStrava.php index c15be54..e3ceb17 100644 --- a/src/WPStrava.php +++ b/src/WPStrava.php @@ -85,8 +85,6 @@ class WPStrava { if ( is_admin() ) { $this->settings->hook(); } else { // Front-end. - $this->style_translations = new WPStrava_StyleTranslations(); - $this->style_translations->hook(); add_action( 'init', array( $this, 'register_shortcodes' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts' ) ); } diff --git a/src/WPStrava/ActivityRenderer.php b/src/WPStrava/ActivityRenderer.php index 6a94040..5322af6 100644 --- a/src/WPStrava/ActivityRenderer.php +++ b/src/WPStrava/ActivityRenderer.php @@ -2,7 +2,7 @@ /* * ActivityRenderer has all the markup for the Activity Block & Shortcode. */ -class WPStrava_ActivityRenderer { +class WPStrava_ActivityRenderer extends WPStrava_StyleTranslationRenderer { /** * Get the HTML for a single activity. @@ -64,10 +64,19 @@ class WPStrava_ActivityRenderer { return $activity_output; } + public static function load_style_translations() { + self::add_style_translation( 'wp-strava-td-avg-speed', __( 'Average Speed', 'wp-strava' ) ); + self::add_style_translation( 'wp-strava-td-max-speed', __( 'Max Speed', 'wp-strava' ) ); + self::add_style_translation( 'wp-strava-td-elevation', __( 'Elevation Gain', 'wp-strava' ) ); + self::add_style_translation( 'wp-strava-td-elapsed-time', __( 'Elapsed Time', 'wp-strava' ) ); + self::add_style_translation( 'wp-strava-td-moving-time', __( 'Moving Time', 'wp-strava' ) ); + self::add_style_translation( 'wp-strava-td-distance', __( 'Distance', 'wp-strava' ) ); + } + /** * The the activity details in in HTML table. * - * @param string $activity_details Activity details from the activity class. + * @param stdClass $activity_details Activity details from the activity class. * @param string $som System of measure (english/metric). * @return string HTML Table of activity details. * @author Justin Foell @@ -80,27 +89,42 @@ class WPStrava_ActivityRenderer { $avg_title = '' . __( 'Average Speed', 'wp-strava' ) . ''; $max_title = '' . __( 'Max Speed', 'wp-strava' ) . ''; $elevation_title = '' . __( 'Elevation Gain', 'wp-strava' ) . ''; + $elevation = ' +
' . $strava_som->elevation( $activity_details->total_elevation_gain ) . '
+
' . $strava_som->get_elevation_label() . '
+ '; $avg_speed = ''; $max_speed = ''; - $elevation = '' . $strava_som->elevation( $activity_details->total_elevation_gain ) . ''; - $speed_label = ''; - $elevation_label = '' . $strava_som->get_elevation_label() . ''; switch ( $strava_activitytype ) { case WPStrava_ActivityType::TYPE_GROUP_PACE: - $avg_speed = '' . $strava_som->pace( $activity_details->average_speed ) . ''; - $max_speed = '' . $strava_som->pace( $activity_details->max_speed ) . ''; - $speed_label = '' . $strava_som->get_pace_label() . ''; + $avg_speed = ' +
' . $strava_som->pace( $activity_details->average_speed ) . '
+
' . $strava_som->get_pace_label() . '
+ '; + $max_speed = ' +
' . $strava_som->pace( $activity_details->max_speed ) . '
+ '; break; case WPStrava_ActivityType::TYPE_GROUP_SPEED: - $avg_speed = '' . $strava_som->speed( $activity_details->average_speed ) . ''; - $max_speed = '' . $strava_som->speed( $activity_details->max_speed ) . ''; - $speed_label = '' . $strava_som->get_speed_label() . ''; + $avg_speed = ' +
' . $strava_som->speed( $activity_details->average_speed ) . '
+
' . $strava_som->get_speed_label() . '
+ '; + $max_speed = ' +
' . $strava_som->speed( $activity_details->max_speed ) . '
+
' . $strava_som->get_speed_label() . '
+ '; break; case WPStrava_ActivityType::TYPE_GROUP_PACE: - $avg_speed = '' . $strava_som->swimpace( $activity_details->average_speed ) . ''; - $max_speed = '' . $strava_som->swimpace( $activity_details->max_speed ) . ''; - $speed_label = '' . $strava_som->get_swimpace_label() . ''; + $avg_speed = ' +
' . $strava_som->swimpace( $activity_details->average_speed ) . '
+
' . $strava_som->get_swimpace_label() . '
+ '; + $max_speed = ' +
' . $strava_som->swimpace( $activity_details->max_speed ) . '
+
' . $strava_som->get_swimpace_label() . '
+ '; break; default: $avg_title = ''; @@ -109,9 +133,8 @@ class WPStrava_ActivityRenderer { } if ( WPStrava::get_instance()->settings->hide_elevation ) { - $elevation = ''; $elevation_title = ''; - $elevation_label = ''; + $elevation = ''; } return ' @@ -127,22 +150,23 @@ class WPStrava_ActivityRenderer { - - ' . $strava_som->time( $activity_details->elapsed_time ) . ' - ' . $strava_som->time( $activity_details->moving_time ) . ' - ' . $strava_som->distance( $activity_details->distance ) . ' + + +
' . $strava_som->time( $activity_details->elapsed_time ) . '
+
' . $strava_som->get_time_label() . '
+ + +
' . $strava_som->time( $activity_details->moving_time ) . '
+
' . $strava_som->get_time_label() . '
+ + +
' . $strava_som->distance( $activity_details->distance ) . '
+
' . $strava_som->get_distance_label() . '
+ ' . $avg_speed . ' ' . $max_speed . ' ' . $elevation . ' - - ' . $strava_som->get_time_label() . ' - ' . $strava_som->get_time_label() . ' - ' . $strava_som->get_distance_label() . ' - ' . $speed_label . ' - ' . $speed_label . ' - ' . $elevation_label . ' - '; diff --git a/src/WPStrava/ActivityShortcode.php b/src/WPStrava/ActivityShortcode.php index 8350294..ecce5f3 100644 --- a/src/WPStrava/ActivityShortcode.php +++ b/src/WPStrava/ActivityShortcode.php @@ -31,6 +31,8 @@ class WPStrava_ActivityShortcode { add_shortcode( 'ride', array( $this, 'handler' ) ); // @deprecated 1.1 add_shortcode( 'activity', array( $this, 'handler' ) ); add_action( 'wp_footer', array( $this, 'print_scripts' ) ); + WPStrava_ActivityRenderer::load_shortcode_style_translations( array( 'ride', 'activity' ) ); + WPStrava_ActivityRenderer::load_style_translations(); } /** @@ -49,7 +51,7 @@ class WPStrava_ActivityShortcode { return __( 'The athlete_token parameter is deprecated as of WP-Strava version 2 and should be replaced with client_id.', 'wp-strava' ); } - $this->add_script = true; + $this->add_script = WPStrava_ActivityRenderer::has_shortcode( array( 'ride', 'activity' ) ); $renderer = new WPStrava_ActivityRenderer(); return $renderer->get_html( $atts ); diff --git a/src/WPStrava/Blocks/Activity.php b/src/WPStrava/Blocks/Activity.php index 3baa5e3..982fa2d 100644 --- a/src/WPStrava/Blocks/Activity.php +++ b/src/WPStrava/Blocks/Activity.php @@ -5,6 +5,10 @@ class WPStrava_Blocks_Activity implements WPStrava_Blocks_Interface { + public function __construct() { + WPStrava_ActivityRenderer::load_style_translations( array( 'wp-strava/activity' ) ); + } + /** * Register the wp-strava/activity block. * diff --git a/src/WPStrava/StyleTranslationRenderer.php b/src/WPStrava/StyleTranslationRenderer.php new file mode 100644 index 0000000..77b5df4 --- /dev/null +++ b/src/WPStrava/StyleTranslationRenderer.php @@ -0,0 +1,158 @@ + + * @since 2.4 + */ +abstract class WPStrava_StyleTranslationRenderer { + + /** + * Translations indexed by class. + * @var array + */ + private static $style_translations = array(); + + private static $shortcodes = array(); + + private static $blocks = array(); + + private static $has_shortcodes = array(); + + private static $has_blocks = array(); + + abstract public static function load_style_translations(); + + public static function load_block_style_translations( $block ) { + $block = ! is_array( $block ) ? array( $block ) : $block; + self::$blocks = array_unique( array_merge( self::$blocks, $block ) ); + self::add_filter(); + } + + public static function load_shortcode_style_translations( $shortcode ) { + $shortcode = ! is_array( $shortcode ) ? array( $shortcode ) : $shortcode; + self::$shortcodes = array_unique( array_merge( self::$shortcodes, $shortcode ) ); + self::add_filter(); + } + + /** + * Add translation text to column header, identified by class. + * + * @param [type] $class CSS class for table data (). + * @param [type] $translation Text to add to column. + * @author Justin Foell + * @since 2.4 + */ + public static function add_style_translation( $class, $translation ) { + self::$style_translations[ $class ] = $translation; + } + + public static function posts_results( $posts ) { + global $wp_query; + + if ( ! $wp_query->is_main_query() || $wp_query->is_admin() ) { + return $posts; + } + + if ( empty( $posts ) ) { + return $posts; + } + + if ( empty( self::$shortcodes ) && empty( self::$blocks ) ) { + return $posts; + } + + // This should only run once per request. + remove_filter( 'posts_results', array( __CLASS__, 'posts_results' ) ); + + foreach ( $posts as $post ) { + if ( self::has_shortcodes( $post ) || self::has_blocks( $post ) ) { + self::add_action(); + } + } + + return $posts; + } + + public static function has_shortcode( $shortcode ) { + $shortcode = ! is_array( $shortcode ) ? array( $shortcode ) : $shortcode; + return ! empty( array_intersect( self::$has_shortcodes, $shortcode ) ); + } + + public static function has_block( $block ) { + $block = ! is_array( $block ) ? array( $block ) : $block; + return ! empty( array_intersect( self::$has_blocks, $block ) ); + } + + + private static function add_filter() { + if ( ! has_filter( 'posts_results', array( __CLASS__, 'posts_results' ) ) ) { + add_filter( 'posts_results', array( __CLASS__, 'posts_results' ) ); + } + } + + private static function add_action() { + if ( ! has_action( 'wp_head', array( __CLASS__, 'print_style_translations' ) ) ) { + add_action( 'wp_head', array( __CLASS__, 'print_style_translations' ) ); + } + } + + private static function has_shortcodes( $post ) { + if ( empty( self::$shortcodes ) ) { + return false; + } + + $has_shortcode = false; + foreach ( self::$shortcodes as $shortcode ) { + if ( has_shortcode( $post->post_content, $shortcode ) ) { + if ( ! in_array( $shortcode, self::$has_shortcodes, true ) ) { + self::$has_shortcodes[] = $shortcode; + } + $has_shortcode = true; + } + } + + return $has_shortcode; + } + + private static function has_blocks( $post ) { + if ( empty( self::$blocks ) ) { + return false; + } + + $has_block = false; + foreach ( self::$blocks as $block ) { + if ( has_block( $block, $post ) ) { + if ( ! in_array( $block, self::$has_blocks, true ) ) { + self::$has_blocks[] = $block; + } + return true; + } + } + + return $has_block; + } + + /** + * Print translatable strings as "; + } +} diff --git a/src/WPStrava/StyleTranslations.php b/src/WPStrava/StyleTranslations.php deleted file mode 100644 index 3bab331..0000000 --- a/src/WPStrava/StyleTranslations.php +++ /dev/null @@ -1,50 +0,0 @@ - - * @since 2.4 - */ - public function hook() { - add_action( 'wp_head', array( $this, 'print_style_translations' ) ); - } - - /** - * Add translation text to column header, identified by class. - * - * @param [type] $class CSS class for table data (). - * @param [type] $translation Text to add to column. - * @author Justin Foell - * @since 2.4 - */ - public function add_style_translation( $class, $translation ) { - $this->style_translations[ $class ] = $translation; - } - /** - * Print translatable strings as "; - } - -}