説明なし

slideshare.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * Slideshare shortcode
  4. *
  5. * Formats:
  6. * Old style (still compatible): [slideshare id=5342235&doc=camprock-101002163655-phpapp01&w=300&h=200]
  7. * New style: [slideshare id=5342235&w=300&h=200&fb=0&mw=0&mh=0&sc=no]
  8. *
  9. * Legend:
  10. * id = Document ID provided by Slideshare
  11. * w = Width of iFrame (int)
  12. * h = Height of iFrame (int)
  13. * fb = iFrame frameborder (int)
  14. * mw = iFrame marginwidth (int)
  15. * mh = iFrame marginheight (int)
  16. * sc = iFrame Scrollbar (yes/no)
  17. * pro = Slideshare Pro (yes/no)
  18. * style = Inline CSS (string)
  19. *
  20. * @package automattic/jetpack
  21. */
  22. /**
  23. * Register and display shortcode.
  24. *
  25. * @param array $atts Shortcode attributes.
  26. */
  27. function slideshare_shortcode( $atts ) {
  28. global $content_width;
  29. $params = shortcode_new_to_old_params( $atts );
  30. parse_str( $params, $arguments );
  31. if ( empty( $arguments ) ) {
  32. return '<!-- SlideShare error: no arguments -->';
  33. }
  34. $attr = shortcode_atts(
  35. array(
  36. 'id' => '',
  37. 'w' => '',
  38. 'h' => '',
  39. 'fb' => '',
  40. 'mw' => '',
  41. 'mh' => '',
  42. 'sc' => '',
  43. 'pro' => '',
  44. 'style' => '',
  45. ),
  46. $arguments
  47. );
  48. // check that the Slideshare ID contains letters, numbers and query strings.
  49. $pattern = '/[^-_a-zA-Z0-9?=&]/';
  50. if ( empty( $attr['id'] ) || preg_match( $pattern, $attr['id'] ) ) {
  51. return '<!-- SlideShare error: id is missing or has illegal characters -->';
  52. }
  53. // check the width/height.
  54. $w = (int) $attr['w'];
  55. // If no width was specified (or uses the wrong format), and if we have a $content_width, use that.
  56. if ( empty( $w ) && ! empty( $content_width ) ) {
  57. $w = (int) $content_width;
  58. } elseif ( $w < 300 || $w > 1600 ) { // If width was specified, but is too small/large, set default value.
  59. $w = 425;
  60. } else {
  61. $w = (int) $w;
  62. }
  63. $h = ceil( $w * 348 / 425 ); // Note: user-supplied height is ignored.
  64. if ( ! empty( $attr['pro'] ) ) {
  65. $source = 'https://www.slideshare.net/slidesharepro/' . $attr['id'];
  66. } else {
  67. $source = 'https://www.slideshare.net/slideshow/embed_code/' . $attr['id'];
  68. }
  69. if ( isset( $attr['rel'] ) ) {
  70. $source = add_query_arg( 'rel', (int) $attr['rel'], $source );
  71. }
  72. if ( ! empty( $attr['startSlide'] ) ) {
  73. $source = add_query_arg( 'startSlide', (int) $attr['startSlide'], $source );
  74. }
  75. $player = sprintf( "<iframe src='%s' width='%d' height='%d'", esc_url( $source ), $w, $h );
  76. // check the frameborder.
  77. if ( ! empty( $attr['fb'] ) || '0' === $attr['fb'] ) {
  78. $player .= " frameborder='" . (int) $attr['fb'] . "'";
  79. }
  80. $is_amp = ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() );
  81. if ( ! $is_amp ) {
  82. // check the margin width; if not empty, cast as int.
  83. if ( ( ! empty( $attr['mw'] ) || '0' === $attr['mw'] ) ) {
  84. $player .= " marginwidth='" . (int) $attr['mw'] . "'";
  85. }
  86. // check the margin height, if not empty, cast as int.
  87. if ( ( ! empty( $attr['mh'] ) || '0' === $attr['mh'] ) ) {
  88. $player .= " marginheight='" . (int) $attr['mh'] . "'";
  89. }
  90. }
  91. if ( ! empty( $attr['style'] ) ) {
  92. $player .= " style='" . esc_attr( $attr['style'] ) . "'";
  93. }
  94. // check the scrollbar; cast as a lowercase string for comparison.
  95. if ( ! empty( $attr['sc'] ) ) {
  96. $sc = strtolower( $attr['sc'] );
  97. if ( in_array( $sc, array( 'yes', 'no' ), true ) ) {
  98. $player .= " scrolling='" . $sc . "'";
  99. }
  100. }
  101. $player .= ' sandbox="allow-popups allow-scripts allow-same-origin allow-presentation" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe>';
  102. /**
  103. * Filter the returned SlideShare shortcode.
  104. *
  105. * @module shortcodes
  106. *
  107. * @since 4.7.0
  108. *
  109. * @param string $player The iframe to return.
  110. * @param array $atts The attributes specified in the shortcode.
  111. */
  112. return apply_filters( 'jetpack_slideshare_shortcode', $player, $atts );
  113. }
  114. add_shortcode( 'slideshare', 'slideshare_shortcode' );