Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 3
CRAP
n/a
0 / 0
jetpack_descript_enable_embeds
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
jetpack_shortcodereverse_descript
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
42
jetpack_descript_shortcode
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * Descript.com embed
4 *
5 * Example URL: https://share.descript.com/view/jUxUmel6GyN
6 * Example embed code: <iframe src="https://share.descript.com/embed/jUxUmel6GyN" width="640" height="360" frameborder="0" allowfullscreen></iframe>
7 *
8 * @package automattic/jetpack
9 */
10
11if ( ! defined( 'ABSPATH' ) ) {
12    exit( 0 );
13}
14
15if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
16    add_action( 'init', 'jetpack_descript_enable_embeds' );
17} else {
18    jetpack_descript_enable_embeds();
19}
20
21/**
22 * Register descript as oembed provider. Add filter to reverse iframes to shortcode. Register [descript] shortcode.
23 *
24 * @since 10.4
25 */
26function jetpack_descript_enable_embeds() {
27    // Support their oEmbed Endpoint.
28    wp_oembed_add_provider( '#https?://share.descript.com/(?:view|embed)/\w+#i', 'https://api.descript.com/v2/oembed', true );
29
30    if ( jetpack_shortcodes_should_hook_pre_kses() ) {
31        // Allow script to be filtered to short code (so direct copy+paste can be done).
32        add_filter( 'pre_kses', 'jetpack_shortcodereverse_descript' );
33    }
34
35    // Actually display the descript Embed.
36    add_shortcode( 'descript', 'jetpack_descript_shortcode' );
37}
38
39/**
40 * Compose shortcode based on Descript iframes.
41 *
42 * @since 10.4
43 *
44 * @param string $content Post content.
45 *
46 * @return mixed
47 */
48function jetpack_shortcodereverse_descript( $content ) {
49    if ( ! is_string( $content ) || false === stripos( $content, 'share.descript.com' ) ) {
50        return $content;
51    }
52
53    $regexp = '/<iframe (?:loading="lazy" )?src="https:\/\/share.descript.com\/embed\/(\w+)" width="(\d+)" height="(\d+)" frameborder="0" allowfullscreen(?:="")?><\/iframe>/i';
54
55    if ( preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) {
56        foreach ( $matches as $match ) {
57            // We need at least a id.
58            if ( isset( $match[1] ) ) {
59                $shortcode = sprintf(
60                    '[descript id="%1$s" width="%2$s" height="%3$s"]',
61                    esc_attr( $match[1] ),
62                    esc_attr( $match[2] ),
63                    esc_attr( $match[3] )
64                );
65                $content   = str_replace( $match[0], $shortcode, $content );
66            }
67        }
68    }
69
70    /** This action is documented in modules/widgets/social-media-icons.php */
71    do_action( 'jetpack_bump_stats_extras', 'html_to_shortcode', 'descript' );
72
73    return $content;
74}
75
76/**
77 * Parse shortcode arguments and render its output.
78 *
79 * @since 10.4
80 *
81 * @param array $atts Shortcode parameters.
82 *
83 * @return string
84 */
85function jetpack_descript_shortcode( $atts ) {
86    if ( ! empty( $atts['id'] ) ) {
87        $id = $atts['id'];
88    } else {
89        return '<!-- Missing descript id -->';
90    }
91
92    if ( ! empty( $atts['width'] ) ) {
93        $width = $atts['width'];
94    } else {
95        $width = '640';
96    }
97
98    if ( ! empty( $atts['height'] ) ) {
99        $height = $atts['height'];
100    } else {
101        $height = '480';
102    }
103
104    $params = array(
105        'id'     => esc_attr( $id ),
106        'width'  => (int) $width,
107        'height' => (int) $height,
108    );
109
110    $embed_url = sprintf(
111        'https://share.descript.com/view/%1$s',
112        esc_attr( $id )
113    );
114
115    $embed_code = wp_oembed_get( $embed_url, array_filter( $params ) );
116
117    // wrap the embed with wp-block-embed__wrapper, otherwise it would be aligned to the very left of the viewport.
118    return sprintf(
119        '<div class="wp-block-embed__wrapper">%1$s</div>',
120        $embed_code
121    );
122}