Nessuna descrizione

scribd.php 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Scribd Shortcode
  4. *
  5. * [scribd id=DOCUMENT_ID key=DOCUMENT_KEY mode=MODE]
  6. * DOCUMENT_ID is an integer (also used as an object_id)
  7. * DOCUMENT_KEY is an alphanumeric hash ('-' character as well)
  8. * MODE can be 'list', 'book', 'slide', 'slideshow', or 'tile'
  9. *
  10. * [scribd id=39027960 key=key-3kaiwcjqhtipf25m8tw mode=list]
  11. *
  12. * @package automattic/jetpack
  13. */
  14. /**
  15. * Register Scribd shortcode.
  16. *
  17. * @param array $atts Shortcode attributes.
  18. */
  19. function scribd_shortcode_handler( $atts ) {
  20. $atts = shortcode_atts(
  21. array(
  22. 'id' => 0,
  23. 'key' => 0,
  24. 'mode' => '',
  25. ),
  26. $atts,
  27. 'scribd'
  28. );
  29. $modes = array( 'list', 'book', 'slide', 'slideshow', 'tile' );
  30. $atts['id'] = (int) $atts['id'];
  31. if ( preg_match( '/^[A-Za-z0-9-]+$/', $atts['key'], $m ) ) {
  32. $atts['key'] = $m[0];
  33. if ( ! in_array( $atts['mode'], $modes, true ) ) {
  34. $atts['mode'] = '';
  35. }
  36. return scribd_shortcode_markup( $atts );
  37. } else {
  38. return '';
  39. }
  40. }
  41. /**
  42. * Display the shortcode.
  43. *
  44. * @param array $atts Shortcode attributes.
  45. * @return string The rendered shortcode.
  46. */
  47. function scribd_shortcode_markup( $atts ) {
  48. $sandbox = class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request()
  49. ? 'sandbox="allow-popups allow-scripts allow-same-origin"'
  50. : '';
  51. $url = add_query_arg(
  52. array(
  53. 'start_page' => '1',
  54. 'view_mode' => esc_attr( $atts['mode'] ),
  55. 'access_key' => esc_attr( $atts['key'] ),
  56. ),
  57. esc_url(
  58. sprintf(
  59. 'https://www.scribd.com/embeds/%1$d/content',
  60. absint( $atts['id'] )
  61. )
  62. )
  63. );
  64. return sprintf(
  65. '<iframe class="scribd_iframe_embed" src="%1$s" %2$s data-auto-height="true" scrolling="no" id="scribd_%3$d" width="100%%" height="500" frameborder="0"></iframe>
  66. <div style="font-size:10px;text-align:center;width:100%%"><a href="https://www.scribd.com/doc/%3$d" rel="noopener noreferrer" target="_blank">%4$s</a></div>',
  67. $url,
  68. $sandbox,
  69. absint( $atts['id'] ),
  70. esc_html__( 'View this document on Scribd', 'jetpack' )
  71. );
  72. }
  73. add_shortcode( 'scribd', 'scribd_shortcode_handler' );
  74. /**
  75. * Scribd supports HTTPS, so use that endpoint to get HTTPS-compatible embeds.
  76. *
  77. * @param array $providers Array of oEmbed providers.
  78. */
  79. function scribd_https_oembed( $providers ) {
  80. if ( isset( $providers['#https?://(www\.)?scribd\.com/doc/.*#i'] ) ) {
  81. $providers['#https?://(www\.)?scribd\.com/doc/.*#i'][0] = 'https://www.scribd.com/services/oembed';
  82. }
  83. return $providers;
  84. }
  85. add_filter( 'oembed_providers', 'scribd_https_oembed' );