暂无描述

smartframe.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Smartframe.io embed
  4. *
  5. * Example URL: https://mikael-korpela.smartframe.io/p/mantymetsa_1630927773870/7673dc41a775fb845cc26acf24f1fe4?t=rql1c6dbpv2
  6. * Example embed code: <script src="https://embed.smartframe.io/6ae67829d1264ee0ea6071a788940eae.js" data-image-id="mantymetsa_1630927773870" data-width="100%" data-max-width="1412px"></script>
  7. *
  8. * @package automattic/jetpack
  9. */
  10. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  11. add_action( 'init', 'jetpack_smartframe_enable_embeds' );
  12. } else {
  13. jetpack_smartframe_enable_embeds();
  14. }
  15. /**
  16. * Register smartframe as oembed provider. Add filter to reverse iframes to shortcode. Register [smartframe] shortcode.
  17. *
  18. * @since 10.2.0
  19. */
  20. function jetpack_smartframe_enable_embeds() {
  21. // Support their oEmbed Endpoint.
  22. wp_oembed_add_provider( '#https?://(.*?)\.smartframe\.(io|net)/.*#i', 'https://oembed.smartframe.io/', true );
  23. // Allow script to be filtered to short code (so direct copy+paste can be done).
  24. add_filter( 'pre_kses', 'jetpack_shortcodereverse_smartframe' );
  25. // Actually display the smartframe Embed.
  26. add_shortcode( 'smartframe', 'jetpack_smartframe_shortcode' );
  27. }
  28. /**
  29. * Compose shortcode based on smartframe iframes.
  30. *
  31. * @since 10.2.0
  32. *
  33. * @param string $content Post content.
  34. *
  35. * @return mixed
  36. */
  37. function jetpack_shortcodereverse_smartframe( $content ) {
  38. if ( ! is_string( $content ) || false === stripos( $content, 'embed.smartframe' ) ) {
  39. return $content;
  40. }
  41. // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
  42. $regexp = '!<script\ssrc="https://embed\.smartframe\.(?:io|net)/(\w+)\.js"\sdata-image-id="(.*?)"(?:\sdata-width="(?:\d+(?:%|px))"\s)?(?:data-max-width="(\d+(%|px)))?"></script>!i';
  43. $regexp_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $regexp, ENT_NOQUOTES ) );
  44. foreach ( compact( 'regexp', 'regexp_ent' ) as $regexp ) {
  45. if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) {
  46. continue;
  47. }
  48. foreach ( $matches as $match ) {
  49. // We need at least a script ID and an image ID.
  50. if ( ! isset( $match[1], $match[2] ) ) {
  51. continue;
  52. }
  53. $shortcode = sprintf(
  54. '[smartframe script-id="%1$s" image-id="%2$s"%3$s]',
  55. esc_attr( $match[1] ),
  56. esc_attr( $match[2] ),
  57. ! empty( $match[3] ) ? ' max-width="' . esc_attr( $match[3] ) . '"' : ''
  58. );
  59. $content = str_replace( $match[0], $shortcode, $content );
  60. }
  61. }
  62. /** This action is documented in modules/widgets/social-media-icons.php */
  63. do_action( 'jetpack_bump_stats_extras', 'html_to_shortcode', 'smartframe' );
  64. return $content;
  65. }
  66. /**
  67. * Parse shortcode arguments and render its output.
  68. *
  69. * @since 10.2.0
  70. *
  71. * @param array $atts Shortcode parameters.
  72. *
  73. * @return string
  74. */
  75. function jetpack_smartframe_shortcode( $atts ) {
  76. if ( ! empty( $atts['image-id'] ) ) {
  77. $image_id = $atts['image-id'];
  78. } else {
  79. return '<!-- Missing smartframe image-id -->';
  80. }
  81. if ( ! empty( $atts['script-id'] ) ) {
  82. $script_id = $atts['script-id'];
  83. } else {
  84. return '<!-- Missing smartframe script-id -->';
  85. }
  86. $params = array(
  87. // ignore width for now, smartframe embed code has it "100%". % isn't allowed in oembed, making it 100px.
  88. // 'width' => isset( $atts['width'] ) ? (int) $atts['width'] : null,.
  89. 'max-width' => isset( $atts['max-width'] ) ? (int) $atts['max-width'] : null,
  90. );
  91. $embed_url = sprintf(
  92. 'https://imagecards.smartframe.io/%1$s/%2$s',
  93. esc_attr( $script_id ),
  94. esc_attr( $image_id )
  95. );
  96. // wrap the embed with wp-block-embed__wrapper, otherwise it would be aligned to the very left of the viewport.
  97. return sprintf(
  98. '<div class="wp-block-embed__wrapper">%1$s</div>',
  99. wp_oembed_get( $embed_url, array_filter( $params ) )
  100. );
  101. }