archiveorg-book.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Archive.org Shortcode
  4. *
  5. * Usage:
  6. * [archiveorg-book goodytwoshoes00newyiala]
  7. * [archiveorg-book http://www.archive.org/stream/goodytwoshoes00newyiala]
  8. * [archiveorg id=goodytwoshoes00newyiala width=480 height=430]
  9. *<iframe src='https://www.archive.org/stream/goodytwoshoes00newyiala?ui=embed#mode/1up' width='480px' height='430px' frameborder='0' ></iframe>
  10. */
  11. /**
  12. * Get ID of requested archive.org book embed.
  13. *
  14. * @since 4.5.0
  15. *
  16. * @param $atts
  17. *
  18. * @return int|string
  19. */
  20. function jetpack_shortcode_get_archiveorg_book_id( $atts ) {
  21. if ( isset( $atts[0] ) ) {
  22. $atts[0] = trim( $atts[0] , '=' );
  23. if ( preg_match( '#archive.org/stream/(.+)/?$#i', $atts[0], $match ) ) {
  24. $id = $match[1];
  25. } else {
  26. $id = $atts[0];
  27. }
  28. return $id;
  29. }
  30. return 0;
  31. }
  32. /**
  33. * Convert an archive.org book 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 book
  39. */
  40. function jetpack_archiveorg_book_shortcode( $atts ) {
  41. global $content_width;
  42. if ( isset( $atts[0] ) && empty( $atts['id'] ) ) {
  43. $atts['id'] = jetpack_shortcode_get_archiveorg_book_id( $atts );
  44. }
  45. $atts = shortcode_atts( array(
  46. 'id' => '',
  47. 'width' => 480,
  48. 'height' => 430,
  49. ), $atts );
  50. if ( ! $atts['id'] ) {
  51. return '<!-- error: missing archive.org book ID -->';
  52. }
  53. $id = $atts['id'];
  54. if ( ! $atts['width'] ) {
  55. $width = absint( $content_width );
  56. } else {
  57. $width = intval( $atts['width'] );
  58. }
  59. if ( ! $atts['height'] ) {
  60. $height = round( ( $width / 640 ) * 360 );
  61. } else {
  62. $height = intval( $atts['height'] );
  63. }
  64. $url = esc_url( set_url_scheme( "http://archive.org/stream/{$id}?ui=embed#mode/1up" ) );
  65. $html = "<div class='embed-archiveorg-book' style='text-align:center;'><iframe src='$url' width='$width' height='$height' style='border:0;' webkitallowfullscreen='true' mozallowfullscreen='true' allowfullscreen></iframe></div>";
  66. return $html;
  67. }
  68. add_shortcode( 'archiveorg-book', 'jetpack_archiveorg_book_shortcode' );
  69. /**
  70. * Compose shortcode from archive.org book iframe.
  71. *
  72. * @since 4.5.0
  73. *
  74. * @param string $content
  75. *
  76. * @return mixed
  77. */
  78. function jetpack_archiveorg_book_embed_to_shortcode( $content ) {
  79. if ( ! is_string( $content ) || false === stripos( $content, 'archive.org/stream/' ) ) {
  80. return $content;
  81. }
  82. $regexp = '!<iframe\s+src=[\'"](http|https)://(www.archive|archive)\.org/stream/([^\'"]+)[\'"]((?:\s+\w+(=[\'"][^\'"]*[\'"])?)*)\s></iframe>!i';
  83. if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) {
  84. return $content;
  85. }
  86. foreach ( $matches as $match ) {
  87. $url = explode( '?', $match[3] );
  88. $id = $url[0];
  89. $params = $match[4];
  90. $params = wp_kses_hair( $params, array( 'http' ) );
  91. $width = isset( $params['width'] ) ? absint( $params['width']['value'] ) : 0;
  92. $height = isset( $params['height'] ) ? absint( $params['height']['value'] ) : 0;
  93. $wh = '';
  94. if ( $width && $height ) {
  95. $wh = ' width=' . $width . ' height=' . $height;
  96. }
  97. $shortcode = '[archiveorg-book ' . $id . $wh . ']';
  98. $content = str_replace( $match[0], $shortcode, $content );
  99. }
  100. return $content;
  101. }
  102. add_filter( 'pre_kses', 'jetpack_archiveorg_book_embed_to_shortcode' );