archiveorg.php 3.8 KB

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