From ab1d8f8515add0585462efa3cf4c75187049195f Mon Sep 17 00:00:00 2001 From: Justin Foell <630830+jrfoell@users.noreply.github.com> Date: Mon, 2 Nov 2020 16:43:15 -0600 Subject: [PATCH] Documentation and formatting --- css/wp-strava.css | 2 +- src/WPStrava/ActivityShortcode.php | 2 +- src/WPStrava/Blocks/Activity.php | 3 +- src/WPStrava/RouteShortcode.php | 2 +- src/WPStrava/SOM.php | 6 +- src/WPStrava/StyleTranslationRenderer.php | 210 +++++++++++++++------- 6 files changed, 157 insertions(+), 68 deletions(-) diff --git a/css/wp-strava.css b/css/wp-strava.css index 0b58c0d..53cfc3e 100755 --- a/css/wp-strava.css +++ b/css/wp-strava.css @@ -53,7 +53,7 @@ only screen and (max-width: 760px), td { /* Behave like a "row" */ border: none; - border-bottom: 1px solid #eee; + border-bottom: 1px solid #e7e7e7; position: relative; padding-left: 50%; } diff --git a/src/WPStrava/ActivityShortcode.php b/src/WPStrava/ActivityShortcode.php index ecce5f3..571d894 100644 --- a/src/WPStrava/ActivityShortcode.php +++ b/src/WPStrava/ActivityShortcode.php @@ -31,7 +31,7 @@ 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::register_shortcode_style_translations( array( 'ride', 'activity' ) ); WPStrava_ActivityRenderer::load_style_translations(); } diff --git a/src/WPStrava/Blocks/Activity.php b/src/WPStrava/Blocks/Activity.php index 982fa2d..f579e41 100644 --- a/src/WPStrava/Blocks/Activity.php +++ b/src/WPStrava/Blocks/Activity.php @@ -6,7 +6,8 @@ class WPStrava_Blocks_Activity implements WPStrava_Blocks_Interface { public function __construct() { - WPStrava_ActivityRenderer::load_style_translations( array( 'wp-strava/activity' ) ); + WPStrava_ActivityRenderer::register_block_style_translations( 'wp-strava/activity' ); + WPStrava_ActivityRenderer::load_style_translations(); } /** diff --git a/src/WPStrava/RouteShortcode.php b/src/WPStrava/RouteShortcode.php index 1c62cfd..f56f6d3 100644 --- a/src/WPStrava/RouteShortcode.php +++ b/src/WPStrava/RouteShortcode.php @@ -102,7 +102,7 @@ class WPStrava_RouteShortcode { /** * The the route details in in HTML table. * - * @param string $route_details route details from the route class. + * @param stdClass $route_details route details from the route class. * @param string $som System of measure (english/metric). * @return string HTML Table of route details. * @author Justin Foell diff --git a/src/WPStrava/SOM.php b/src/WPStrava/SOM.php index 11c4edb..0fc6e81 100644 --- a/src/WPStrava/SOM.php +++ b/src/WPStrava/SOM.php @@ -36,9 +36,9 @@ abstract class WPStrava_SOM { * @see https://stackoverflow.com/a/20870843/2146022 */ public function time( $seconds ) { - $zero = new DateTime( '@0' ); - $offset = new DateTime( "@{$seconds}" ); - $diff = $zero->diff( $offset ); + $zero = new DateTime( '@0' ); + $offset = new DateTime( "@{$seconds}" ); + $diff = $zero->diff( $offset ); return sprintf( '%02d:%02d:%02d', $diff->days * 24 + $diff->h, $diff->i, $diff->s ); } diff --git a/src/WPStrava/StyleTranslationRenderer.php b/src/WPStrava/StyleTranslationRenderer.php index 77b5df4..2a27d23 100644 --- a/src/WPStrava/StyleTranslationRenderer.php +++ b/src/WPStrava/StyleTranslationRenderer.php @@ -14,26 +14,62 @@ abstract class WPStrava_StyleTranslationRenderer { */ private static $style_translations = array(); + /** + * Shortcodes registered with translatable style strings. + * @var array + */ private static $shortcodes = array(); + /** + * Blocks registered with translatable style strings. + * @var array + */ private static $blocks = array(); + /** + * Shortcodes found in content of current page. + * @var array + */ private static $has_shortcodes = array(); + /** + * Blocks found in content of current page. + * @var array + */ private static $has_blocks = array(); + /** + * Implemented by concrete renderers to load column headers by calling self::add_style_translation(). + * + * @author Justin Foell + * @since 2.4 + */ 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 ) { + /** + * Register a shortcode that has translatable CSS strings. + * + * @param string|array $shortcode Shortcode (or array of shortcodes). + * @author Justin Foell + * @since 2.4 + */ + public static function register_shortcode_style_translations( $shortcode ) { $shortcode = ! is_array( $shortcode ) ? array( $shortcode ) : $shortcode; self::$shortcodes = array_unique( array_merge( self::$shortcodes, $shortcode ) ); - self::add_filter(); + self::add_content_filter(); + } + + /** + * Register a block that has translatable CSS strings. + * + * @param string|array $block Block name (or array of block names). + * @author Justin Foell + * @since 2.4 + */ + public static function register_block_style_translations( $block ) { + $block = ! is_array( $block ) ? array( $block ) : $block; + self::$blocks = array_unique( array_merge( self::$blocks, $block ) ); + self::add_content_filter(); } /** @@ -48,7 +84,15 @@ abstract class WPStrava_StyleTranslationRenderer { self::$style_translations[ $class ] = $translation; } - public static function posts_results( $posts ) { + /** + * Go through posts from The Loop looking for our shortcodes and blocks. + * + * @param array $posts Posts from the current page loop. + * @return array Posts array, unharmed. + * @author Justin Foell + * @since 2.4 + */ + public static function search_content_for_shortcodes_and_blocks( $posts ) { global $wp_query; if ( ! $wp_query->is_main_query() || $wp_query->is_admin() ) { @@ -64,76 +108,43 @@ abstract class WPStrava_StyleTranslationRenderer { } // This should only run once per request. - remove_filter( 'posts_results', array( __CLASS__, 'posts_results' ) ); + remove_filter( 'posts_results', array( __CLASS__, 'search_content_for_shortcodes_and_blocks' ) ); foreach ( $posts as $post ) { if ( self::has_shortcodes( $post ) || self::has_blocks( $post ) ) { - self::add_action(); + self::add_print_style_action(); } } return $posts; } + /** + * Current page has the shortcode specified. + * + * @param [type] $shortcode Shortcode. + * @return boolean True if shortcode was found in the_content. + * @author Justin Foell + * @since 2.4 + */ public static function has_shortcode( $shortcode ) { $shortcode = ! is_array( $shortcode ) ? array( $shortcode ) : $shortcode; return ! empty( array_intersect( self::$has_shortcodes, $shortcode ) ); } + /** + * Current page has the block specified. + * + * @param [type] $block Block name. + * @return boolean True if block was found in the_content. + * @author Justin Foell + * @since 2.4 + */ 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 "; + echo wp_kses( "", array( 'style' => array( 'id' => array() ) ) ); } + + /** + * Add filter to post_results content. + * + * @author Justin Foell + * @since 2.4 + */ + private static function add_content_filter() { + if ( ! has_filter( 'posts_results', array( __CLASS__, 'search_content_for_shortcodes_and_blocks' ) ) ) { + add_filter( 'posts_results', array( __CLASS__, 'search_content_for_shortcodes_and_blocks' ) ); + } + } + + /** + * Undocumented function + * + * @author Justin Foell + * @since 2.4 + */ + private static function add_print_style_action() { + if ( ! has_action( 'wp_head', array( __CLASS__, 'print_style_translations' ) ) ) { + add_action( 'wp_head', array( __CLASS__, 'print_style_translations' ) ); + } + } + + /** + * Post contains one of the registered shortcodes. + * + * @param \WP_Post $post + * @return boolean + * @author Justin Foell + * @since 2.4 + */ + 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; + } + + /** + * Post contains one of the registered blocks. + * + * @param \WP_Post $post + * @return boolean + * @author Justin Foell + * @since 2.4 + */ + 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; + } + $has_block = true; + } + } + + return $has_block; + } + }