Brak opisu

archiveorg-book.php 3.3KB

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