slideshare.php 3.5 KB

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