| 1 |
<?php |
| 2 |
/** |
| 3 |
* Gif block render implementation. |
| 4 |
* |
| 5 |
* Loaded lazily from gif.php only when the block is rendered, to keep |
| 6 |
* the render body out of the eager front-end PHP/opcache footprint. |
| 7 |
* |
| 8 |
* @package automattic/jetpack |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace Automattic\Jetpack\Extensions\Gif; |
| 12 |
|
| 13 |
use Automattic\Jetpack\Blocks; |
| 14 |
use Jetpack_Gutenberg; |
| 15 |
|
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
exit( 0 ); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Dynamic rendering of the block. |
| 22 |
* |
| 23 |
* @param array $attr - Array containing the gif block attributes. |
| 24 |
* |
| 25 |
* @return string |
| 26 |
*/ |
| 27 |
function render_implementation( $attr ) { |
| 28 |
$padding_top = $attr['paddingTop'] ?? 0; |
| 29 |
$style = 'padding-top:' . $padding_top; |
| 30 |
$giphy_url = isset( $attr['giphyUrl'] ) |
| 31 |
? Jetpack_Gutenberg::validate_block_embed_url( $attr['giphyUrl'], array( 'giphy.com' ) ) |
| 32 |
: null; |
| 33 |
$search_text = $attr['searchText'] ?? ''; |
| 34 |
$caption = $attr['caption'] ?? null; |
| 35 |
|
| 36 |
if ( ! $giphy_url ) { |
| 37 |
return null; |
| 38 |
} |
| 39 |
|
| 40 |
$classes = Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr ); |
| 41 |
|
| 42 |
$placeholder = sprintf( '<a href="%s">%s</a>', esc_url( $giphy_url ), esc_attr( $search_text ) ); |
| 43 |
|
| 44 |
ob_start(); |
| 45 |
?> |
| 46 |
<div class="<?php echo esc_attr( $classes ); ?>"> |
| 47 |
<figure> |
| 48 |
<?php if ( Blocks::is_amp_request() ) : ?> |
| 49 |
<amp-iframe src="<?php echo esc_url( $giphy_url ); ?>" width="100" height="<?php echo absint( $padding_top ); ?>" sandbox="allow-scripts allow-same-origin" layout="responsive"> |
| 50 |
<div placeholder> |
| 51 |
<?php echo wp_kses_post( $placeholder ); ?> |
| 52 |
</div> |
| 53 |
</amp-iframe> |
| 54 |
<?php else : ?> |
| 55 |
<div class="wp-block-jetpack-gif-wrapper" style="<?php echo esc_attr( $style ); ?>"> |
| 56 |
<iframe src="<?php echo esc_url( $giphy_url ); ?>" title="<?php echo esc_attr( $search_text ); ?>"></iframe> |
| 57 |
</div> |
| 58 |
<?php endif; ?> |
| 59 |
<?php if ( $caption ) : ?> |
| 60 |
<figcaption class="wp-block-jetpack-gif-caption gallery-caption"><?php echo wp_kses_post( $caption ); ?></figcaption> |
| 61 |
<?php endif; ?> |
| 62 |
</figure> |
| 63 |
</div> |
| 64 |
<?php |
| 65 |
$html = ob_get_clean(); |
| 66 |
|
| 67 |
Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); |
| 68 |
|
| 69 |
return $html; |
| 70 |
} |
| 71 |
|