Bez popisu

google-calendar.php 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Google Calendar Block.
  4. *
  5. * @since 8.3.0
  6. *
  7. * @package automattic/jetpack
  8. */
  9. namespace Automattic\Jetpack\Extensions\Google_Calendar;
  10. use Automattic\Jetpack\Blocks;
  11. use Jetpack_Gutenberg;
  12. const FEATURE_NAME = 'google-calendar';
  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(
  23. 'render_callback' => __NAMESPACE__ . '\load_assets',
  24. )
  25. );
  26. }
  27. add_action( 'init', __NAMESPACE__ . '\register_block' );
  28. /**
  29. * Google Calendar block registration/dependency declaration.
  30. *
  31. * @param array $attr Array containing the Google Calendar block attributes.
  32. * @return string
  33. */
  34. function load_assets( $attr ) {
  35. $height = isset( $attr['height'] ) ? $attr['height'] : '600';
  36. $url = isset( $attr['url'] )
  37. ? Jetpack_Gutenberg::validate_block_embed_url( $attr['url'], array( 'calendar.google.com' ) ) :
  38. '';
  39. $classes = Blocks::classes( FEATURE_NAME, $attr );
  40. Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
  41. if ( empty( $url ) ) {
  42. return '';
  43. }
  44. $sandbox = 'allow-scripts allow-same-origin allow-popups';
  45. if ( Blocks::is_amp_request() ) {
  46. $noscript_src = str_replace(
  47. '//calendar.google.com/calendar/embed',
  48. '//calendar.google.com/calendar/htmlembed',
  49. $url
  50. );
  51. $iframe = sprintf(
  52. '<amp-iframe src="%1$s" frameborder="0" scrolling="no" height="%2$d" layout="fixed-height" sandbox="%3$s">%4$s%5$s</amp-iframe>',
  53. esc_url( $url ),
  54. absint( $height ),
  55. esc_attr( $sandbox ),
  56. sprintf(
  57. '<a href="%s" placeholder>%s</a>',
  58. esc_url( $url ),
  59. esc_html__( 'Google Calendar', 'jetpack' )
  60. ),
  61. sprintf(
  62. '<noscript><iframe src="%1$s" frameborder="0" scrolling="no" sandbox="%2$s"></iframe></noscript>',
  63. esc_url( $noscript_src ),
  64. esc_attr( $sandbox )
  65. )
  66. );
  67. } else {
  68. $iframe = sprintf(
  69. '<iframe src="%1$s" frameborder="0" style="border:0" scrolling="no" height="%2$d" width="100%%" sandbox="%3$s"></iframe>',
  70. esc_url( $url ),
  71. absint( $height ),
  72. esc_attr( $sandbox )
  73. );
  74. }
  75. return sprintf( '<div class="%s">%s</div>', esc_attr( $classes ), $iframe );
  76. }