Açıklama Yok

gif.php 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * GIF Block.
  4. *
  5. * @since 7.0.0
  6. *
  7. * @package automattic/jetpack
  8. */
  9. namespace Automattic\Jetpack\Extensions\Gif;
  10. use Automattic\Jetpack\Blocks;
  11. use Jetpack_Gutenberg;
  12. const FEATURE_NAME = 'gif';
  13. const BLOCK_NAME = 'jetpack/' . FEATURE_NAME;
  14. /**
  15. * Registers the block for use in Gutenberg
  16. * This is done via an action so that we can disable
  17. * registration if we need to.
  18. */
  19. function register_block() {
  20. Blocks::jetpack_register_block(
  21. BLOCK_NAME,
  22. array( 'render_callback' => __NAMESPACE__ . '\render_block' )
  23. );
  24. }
  25. add_action( 'init', __NAMESPACE__ . '\register_block' );
  26. /**
  27. * Gif block registration/dependency declaration.
  28. *
  29. * @param array $attr - Array containing the gif block attributes.
  30. *
  31. * @return string
  32. */
  33. function render_block( $attr ) {
  34. $padding_top = isset( $attr['paddingTop'] ) ? $attr['paddingTop'] : 0;
  35. $style = 'padding-top:' . $padding_top;
  36. $giphy_url = isset( $attr['giphyUrl'] )
  37. ? Jetpack_Gutenberg::validate_block_embed_url( $attr['giphyUrl'], array( 'giphy.com' ) )
  38. : null;
  39. $search_text = isset( $attr['searchText'] ) ? $attr['searchText'] : '';
  40. $caption = isset( $attr['caption'] ) ? $attr['caption'] : null;
  41. if ( ! $giphy_url ) {
  42. return null;
  43. }
  44. $classes = Blocks::classes( FEATURE_NAME, $attr );
  45. $placeholder = sprintf( '<a href="%s">%s</a>', esc_url( $giphy_url ), esc_attr( $search_text ) );
  46. ob_start();
  47. ?>
  48. <div class="<?php echo esc_attr( $classes ); ?>">
  49. <figure>
  50. <?php if ( Blocks::is_amp_request() ) : ?>
  51. <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">
  52. <div placeholder>
  53. <?php echo wp_kses_post( $placeholder ); ?>
  54. </div>
  55. </amp-iframe>
  56. <?php else : ?>
  57. <div class="wp-block-jetpack-gif-wrapper" style="<?php echo esc_attr( $style ); ?>">
  58. <iframe src="<?php echo esc_url( $giphy_url ); ?>" title="<?php echo esc_attr( $search_text ); ?>"></iframe>
  59. </div>
  60. <?php endif; ?>
  61. <?php if ( $caption ) : ?>
  62. <figcaption class="wp-block-jetpack-gif-caption gallery-caption"><?php echo wp_kses_post( $caption ); ?></figcaption>
  63. <?php endif; ?>
  64. </figure>
  65. </div>
  66. <?php
  67. $html = ob_get_clean();
  68. Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
  69. return $html;
  70. }