scribd.php
| 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 Jetpack |
| 13 | */ |
| 14 | |
| 15 | /** |
| 16 | * Register Scribd shortcode. |
| 17 | * |
| 18 | * @param array $atts Shortcode attributes. |
| 19 | */ |
| 20 | function scribd_shortcode_handler( $atts ) { |
| 21 | $atts = shortcode_atts( |
| 22 | array( |
| 23 | 'id' => 0, |
| 24 | 'key' => 0, |
| 25 | 'mode' => '', |
| 26 | ), |
| 27 | $atts, |
| 28 | 'scribd' |
| 29 | ); |
| 30 | |
| 31 | $modes = array( 'list', 'book', 'slide', 'slideshow', 'tile' ); |
| 32 | |
| 33 | $atts['id'] = (int) $atts['id']; |
| 34 | if ( preg_match( '/^[A-Za-z0-9-]+$/', $atts['key'], $m ) ) { |
| 35 | $atts['key'] = $m[0]; |
| 36 | |
| 37 | if ( ! in_array( $atts['mode'], $modes, true ) ) { |
| 38 | $atts['mode'] = ''; |
| 39 | } |
| 40 | |
| 41 | return scribd_shortcode_markup( $atts ); |
| 42 | } else { |
| 43 | return ''; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Display the shortcode. |
| 49 | * |
| 50 | * @param array $atts Shortcode attributes. |
| 51 | */ |
| 52 | function scribd_shortcode_markup( $atts ) { |
| 53 | $markup = <<<EOD |
| 54 | <iframe class="scribd_iframe_embed" src="//www.scribd.com/embeds/$atts[id]/content?start_page=1&view_mode=$atts[mode]&access_key=$atts[key]" data-auto-height="true" scrolling="no" id="scribd_$atts[id]" width="100%" height="500" frameborder="0"></iframe> |
| 55 | <div style="font-size:10px;text-align:center;width:100%"><a href="https://www.scribd.com/doc/$atts[id]" target="_blank">View this document on Scribd</a></div> |
| 56 | EOD; |
| 57 | |
| 58 | return $markup; |
| 59 | } |
| 60 | add_shortcode( 'scribd', 'scribd_shortcode_handler' ); |
| 61 | |
| 62 | /** |
| 63 | * Scribd supports HTTPS, so use that endpoint to get HTTPS-compatible embeds. |
| 64 | * |
| 65 | * @param array $providers Array of oEmbed providers. |
| 66 | */ |
| 67 | function scribd_https_oembed( $providers ) { |
| 68 | if ( isset( $providers['#https?://(www\.)?scribd\.com/doc/.*#i'] ) ) { |
| 69 | $providers['#https?://(www\.)?scribd\.com/doc/.*#i'][0] = 'https://www.scribd.com/services/oembed'; |
| 70 | } |
| 71 | |
| 72 | return $providers; |
| 73 | } |
| 74 | add_filter( 'oembed_providers', 'scribd_https_oembed' ); |
| 75 |