Documentation and formatting

This commit is contained in:
Justin Foell
2020-11-02 16:43:15 -06:00
parent aa9008822e
commit ab1d8f8515
6 changed files with 157 additions and 68 deletions
+1 -1
View File
@@ -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%;
}
+1 -1
View File
@@ -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();
}
+2 -1
View File
@@ -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();
}
/**
+1 -1
View File
@@ -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 <justin@foell.org>
+3 -3
View File
@@ -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 );
}
+149 -61
View File
@@ -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 <justin.foell.org>
* @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 <justin@foell.org>
* @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 <justin@foell.org>
* @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 <justin@foell.org>
* @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 <justin@foell.org>
* @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 <justin@foell.org>
* @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 <style> in <head>.
*
@@ -153,6 +164,83 @@ abstract class WPStrava_StyleTranslationRenderer {
$style .= ".{$class}:before { content: \"{$translation}\"; }\n";
}
$style .= '}';
echo "<style id='wp-strava-style-translations'>\n{$style}</style>";
echo wp_kses( "<style id='wp-strava-style-translations'>\n{$style}</style>", array( 'style' => array( 'id' => array() ) ) );
}
/**
* Add filter to post_results content.
*
* @author Justin Foell <justin@foell.org>
* @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 <justin@foell.org>
* @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 <justin@foell.org>
* @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 <justin@foell.org>
* @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;
}
}