暂无描述

archiveorg.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * Archive.org book shortcode.
  4. *
  5. * Usage:
  6. * [archiveorg Experime1940]
  7. * [archiveorg http://archive.org/details/Experime1940 poster=http://archive.org/images/map.png]
  8. * [archiveorg id=Experime1940 width=640 height=480 autoplay=1]
  9. * <iframe src="http://archive.org/embed/Experime1940&autoplay=1&poster=http://archive.org/images/map.png" width="640" height="480" frameborder="0" webkitallowfullscreen="true" mozallowfullscreen="true" allowfullscreen></iframe>
  10. *
  11. * @package automattic/jetpack
  12. */
  13. /**
  14. * Get ID of requested archive.org embed.
  15. *
  16. * @since 4.5.0
  17. *
  18. * @param array $atts Shortcode attributes.
  19. *
  20. * @return int|string
  21. */
  22. function jetpack_shortcode_get_archiveorg_id( $atts ) {
  23. if ( isset( $atts[0] ) ) {
  24. $atts[0] = trim( $atts[0], '=' );
  25. if ( preg_match( '#archive.org/(details|embed)/(.+)/?$#i', $atts[0], $match ) ) {
  26. $id = $match[2];
  27. } else {
  28. $id = $atts[0];
  29. }
  30. return $id;
  31. }
  32. return 0;
  33. }
  34. /**
  35. * Convert an archive.org shortcode into an embed code.
  36. *
  37. * @since 4.5.0
  38. *
  39. * @param array $atts An array of shortcode attributes.
  40. * @return string The embed code for the archive.org video.
  41. */
  42. function jetpack_archiveorg_shortcode( $atts ) {
  43. global $content_width;
  44. if ( isset( $atts[0] ) && empty( $atts['id'] ) ) {
  45. $atts['id'] = jetpack_shortcode_get_archiveorg_id( $atts );
  46. }
  47. $atts = shortcode_atts(
  48. array(
  49. 'id' => '',
  50. 'width' => 640,
  51. 'height' => 480,
  52. 'autoplay' => 0,
  53. 'poster' => '',
  54. ),
  55. $atts
  56. );
  57. if ( ! $atts['id'] ) {
  58. return '<!-- error: missing archive.org ID -->';
  59. }
  60. $id = $atts['id'];
  61. if ( ! $atts['width'] ) {
  62. $width = absint( $content_width );
  63. } else {
  64. $width = (int) $atts['width'];
  65. }
  66. if ( ! $atts['height'] ) {
  67. $height = round( ( $width / 640 ) * 360 );
  68. } else {
  69. $height = (int) $atts['height'];
  70. }
  71. if ( $atts['autoplay'] ) {
  72. $autoplay = '&autoplay=1';
  73. } else {
  74. $autoplay = '';
  75. }
  76. if ( $atts['poster'] ) {
  77. $poster = '&poster=' . $atts['poster'];
  78. } else {
  79. $poster = '';
  80. }
  81. return sprintf(
  82. '<div class="embed-archiveorg" style="text-align:center;"><iframe title="%s" src="%s" width="%s" height="%s" style="border:0;" webkitallowfullscreen="true" mozallowfullscreen="true" allowfullscreen></iframe></div>',
  83. esc_attr__( 'Archive.org', 'jetpack' ),
  84. esc_url( "https://archive.org/embed/{$id}{$autoplay}{$poster}" ),
  85. esc_attr( $width ),
  86. esc_attr( $height )
  87. );
  88. }
  89. add_shortcode( 'archiveorg', 'jetpack_archiveorg_shortcode' );
  90. /**
  91. * Compose shortcode from archive.org iframe.
  92. *
  93. * @since 4.5.0
  94. *
  95. * @param string $content Post content.
  96. *
  97. * @return mixed
  98. */
  99. function jetpack_archiveorg_embed_to_shortcode( $content ) {
  100. if ( ! is_string( $content ) || false === stripos( $content, 'archive.org/embed/' ) ) {
  101. return $content;
  102. }
  103. $regexp = '!<iframe\s+src=[\'"]https?://archive\.org/embed/([^\'"]+)[\'"]((?:\s+\w+(=[\'"][^\'"]*[\'"])?)*)></iframe>!i';
  104. if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) {
  105. return $content;
  106. }
  107. foreach ( $matches as $match ) {
  108. $url = explode( '&amp;', $match[1] );
  109. $id = 'id=' . $url[0];
  110. $autoplay = '';
  111. $poster = '';
  112. $url_count = count( $url );
  113. for ( $ii = 1; $ii < $url_count; $ii++ ) {
  114. if ( 'autoplay=1' === $url[ $ii ] ) {
  115. $autoplay = ' autoplay="1"';
  116. }
  117. $map_matches = array();
  118. if ( preg_match( '/^poster=(.+)$/', $url[ $ii ], $map_matches ) ) {
  119. $poster = " poster=\"{$map_matches[1]}\"";
  120. }
  121. }
  122. $params = $match[2];
  123. $params = wp_kses_hair( $params, array( 'http' ) );
  124. $width = isset( $params['width'] ) ? (int) $params['width']['value'] : 0;
  125. $height = isset( $params['height'] ) ? (int) $params['height']['value'] : 0;
  126. $wh = '';
  127. if ( $width && $height ) {
  128. $wh = ' width=' . $width . ' height=' . $height;
  129. }
  130. $shortcode = '[archiveorg ' . $id . $wh . $autoplay . $poster . ']';
  131. $content = str_replace( $match[0], $shortcode, $content );
  132. }
  133. return $content;
  134. }
  135. add_filter( 'pre_kses', 'jetpack_archiveorg_embed_to_shortcode' );