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-27 11:17:53 -06:00
|
|
|
/**
|
|
|
|
|
* Whether or not to enqueue styles (if shortcode is present).
|
|
|
|
|
*
|
|
|
|
|
* @var boolean
|
|
|
|
|
* @author Justin Foell <justin@foell.org>
|
|
|
|
|
* @since 2.5.0
|
|
|
|
|
*/
|
|
|
|
|
private $add_script = false;
|
|
|
|
|
|
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-11-27 12:57:33 -06:00
|
|
|
'attributes' => array(
|
|
|
|
|
'url' => array(
|
|
|
|
|
'type' => 'string',
|
|
|
|
|
'default' => '',
|
|
|
|
|
),
|
|
|
|
|
'imageOnly' => array(
|
|
|
|
|
'type' => 'boolean',
|
|
|
|
|
'default' => false,
|
|
|
|
|
),
|
|
|
|
|
'displayMarkers' => array(
|
|
|
|
|
'type' => 'boolean',
|
|
|
|
|
'default' => false,
|
|
|
|
|
),
|
|
|
|
|
),
|
2020-02-28 13:39:42 -06:00
|
|
|
)
|
|
|
|
|
);
|
2020-11-27 11:17:53 -06:00
|
|
|
add_action( 'wp_footer', array( $this, 'print_scripts' ) );
|
2020-02-28 13:39:42 -06:00
|
|
|
}
|
|
|
|
|
|
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-11-27 11:17:53 -06:00
|
|
|
$this->add_script = true;
|
|
|
|
|
|
2020-03-27 16:39:57 -05:00
|
|
|
$matches = [];
|
2020-11-03 13:38:21 -06:00
|
|
|
preg_match( '/\/activities\/([0-9].*)$/', $attributes['url'], $matches );
|
2020-03-27 16:39:57 -05:00
|
|
|
if ( $matches[1] ) {
|
2020-06-08 15:18:04 -05:00
|
|
|
// 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();
|
2020-06-08 15:18:04 -05:00
|
|
|
return $renderer->get_html( $attributes );
|
2020-03-27 16:39:57 -05:00
|
|
|
}
|
|
|
|
|
return $content;
|
2020-02-28 13:39:42 -06:00
|
|
|
}
|
2020-11-27 11:17:53 -06:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Enqueue style if shortcode is being used.
|
|
|
|
|
*
|
|
|
|
|
* @author Justin Foell <justin@foell.org>
|
|
|
|
|
* @since 2.5.0
|
|
|
|
|
*/
|
|
|
|
|
public function print_scripts() {
|
|
|
|
|
if ( $this->add_script ) {
|
|
|
|
|
wp_enqueue_style( 'wp-strava-style' );
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-28 13:39:42 -06:00
|
|
|
}
|