Files
wp-strava/src/WPStrava/Blocks/Activity.php
T

61 lines
1.6 KiB
PHP
Raw Normal View History

2020-02-28 13:39:42 -06:00
<?php
/*
* Activity block.
*/
2020-03-27 16:39:57 -05:00
2020-02-28 13:39:42 -06:00
class WPStrava_Blocks_Activity implements WPStrava_Blocks_Interface {
2020-11-02 15:50:00 -06:00
public function __construct() {
2020-11-02 16:43:15 -06:00
WPStrava_ActivityRenderer::register_block_style_translations( 'wp-strava/activity' );
WPStrava_ActivityRenderer::load_style_translations();
2020-11-02 15:50:00 -06:00
}
2020-03-27 16:39:57 -05:00
/**
* Register the wp-strava/activity block.
*
* @author Justin Foell <justin@foell.org>
* @since 2.2.0
*/
2020-02-28 13:39:42 -06:00
public function register_block() {
register_block_type(
'wp-strava/activity',
array(
'style' => 'wp-strava-block',
'editor_style' => 'wp-strava-block-editor',
'editor_script' => 'wp-strava-block',
'render_callback' => array( $this, 'render_block' ),
)
);
}
2020-03-27 16:39:57 -05:00
/**
* Render for this block.
*
* @param array $attributes JSON attributes saved in the HTML comment for this block.
* @param string $content The content from JS save() for this block.
* @return string HTML for this block.
2020-03-27 16:56:12 -05:00
* @author Justin Foell <justin@foell.org>
2020-03-27 16:39:57 -05:00
* @since 2.2.0
*/
2020-02-28 13:39:42 -06:00
public function render_block( $attributes, $content ) {
2020-03-27 16:39:57 -05:00
if ( empty( $attributes['url'] ) ) {
return $content;
}
2020-02-28 13:39:42 -06:00
2020-03-27 16:39:57 -05:00
$matches = [];
preg_match( "/\/activities\/([0-9].*)$/", $attributes['url'], $matches );
if ( $matches[1] ) {
// Transform from block attributes to shortcode standard.
$attributes = array(
'image_only' => isset( $attributes['imageOnly'] ) ? $attributes['imageOnly'] : false,
'markers' => isset( $attributes['displayMarkers'] ) ? $attributes['displayMarkers'] : false,
'id' => $matches[1],
);
2020-03-27 16:39:57 -05:00
$renderer = new WPStrava_ActivityRenderer();
return $renderer->get_html( $attributes );
2020-03-27 16:39:57 -05:00
}
return $content;
2020-02-28 13:39:42 -06:00
}
}