Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
31.25% covered (danger)
31.25%
15 / 48
0.00% covered (danger)
0.00%
0 / 3
CRAP
n/a
0 / 0
scribd_shortcode_handler
88.24% covered (warning)
88.24%
15 / 17
0.00% covered (danger)
0.00%
0 / 1
3.01
scribd_shortcode_markup
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
12
scribd_https_oembed
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
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
15if ( ! defined( 'ABSPATH' ) ) {
16    exit( 0 );
17}
18
19/**
20 * Register Scribd shortcode.
21 *
22 * @param array $atts Shortcode attributes.
23 */
24function scribd_shortcode_handler( $atts ) {
25    $atts = shortcode_atts(
26        array(
27            'id'   => 0,
28            'key'  => 0,
29            'mode' => '',
30        ),
31        $atts,
32        'scribd'
33    );
34
35    $modes = array( 'list', 'book', 'slide', 'slideshow', 'tile' );
36
37    $atts['id'] = (int) $atts['id'];
38    if ( preg_match( '/^[A-Za-z0-9-]+$/', $atts['key'], $m ) ) {
39        $atts['key'] = $m[0];
40
41        if ( ! in_array( $atts['mode'], $modes, true ) ) {
42            $atts['mode'] = '';
43        }
44
45        return scribd_shortcode_markup( $atts );
46    } else {
47        return '';
48    }
49}
50
51/**
52 * Display the shortcode.
53 *
54 * @param array $atts Shortcode attributes.
55 * @return string The rendered shortcode.
56 */
57function scribd_shortcode_markup( $atts ) {
58    $sandbox = class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request()
59        ? 'sandbox="allow-popups allow-scripts allow-same-origin"'
60        : '';
61
62    $url = add_query_arg(
63        array(
64            'start_page' => '1',
65            'view_mode'  => esc_attr( $atts['mode'] ),
66            'access_key' => esc_attr( $atts['key'] ),
67        ),
68        esc_url(
69            sprintf(
70                'https://www.scribd.com/embeds/%1$d/content',
71                absint( $atts['id'] )
72            )
73        )
74    );
75
76    return sprintf(
77        '<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>
78        <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>',
79        $url,
80        $sandbox,
81        absint( $atts['id'] ),
82        esc_html__( 'View this document on Scribd', 'jetpack' )
83    );
84}
85add_shortcode( 'scribd', 'scribd_shortcode_handler' );
86
87/**
88 * Scribd supports HTTPS, so use that endpoint to get HTTPS-compatible embeds.
89 *
90 * @param array $providers Array of oEmbed providers.
91 */
92function scribd_https_oembed( $providers ) {
93    if ( isset( $providers['#https?://(www\.)?scribd\.com/doc/.*#i'] ) ) {
94        $providers['#https?://(www\.)?scribd\.com/doc/.*#i'][0] = 'https://www.scribd.com/services/oembed';
95    }
96
97    return $providers;
98}
99add_filter( 'oembed_providers', 'scribd_https_oembed' );