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