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