PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 10.2.3
Jetpack – WP Security, Backup, Speed, & Growth v10.2.3
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / shortcodes / smartframe.php

smartframe.php in Jetpack – WP Security, Backup, Speed, & Growth 10.2.3, at modules/shortcodes/smartframe.php

115 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Smartframe.io embed
4 *
5 * Example URL: https://mikael-korpela.smartframe.io/p/mantymetsa_1630927773870/7673dc41a775fb845cc26acf24f1fe4?t=rql1c6dbpv2
6 * Example embed code: <script src="https://embed.smartframe.io/6ae67829d1264ee0ea6071a788940eae.js" data-image-id="mantymetsa_1630927773870" data-width="100%" data-max-width="1412px"></script>
7 *
8 * @package automattic/jetpack
9 */
10
11 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
12 add_action( 'init', 'jetpack_smartframe_enable_embeds' );
13 } else {
14 jetpack_smartframe_enable_embeds();
15 }
16
17 /**
18 * Register smartframe as oembed provider. Add filter to reverse iframes to shortcode. Register [smartframe] shortcode.
19 *
20 * @since 10.2.0
21 */
22 function jetpack_smartframe_enable_embeds() {
23 // Support their oEmbed Endpoint.
24 wp_oembed_add_provider( '#https?://(.*?)\.smartframe\.(io|net)/.*#i', 'https://oembed.smartframe.io/', 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_smartframe' );
28
29 // Actually display the smartframe Embed.
30 add_shortcode( 'smartframe', 'jetpack_smartframe_shortcode' );
31 }
32
33 /**
34 * Compose shortcode based on smartframe iframes.
35 *
36 * @since 10.2.0
37 *
38 * @param string $content Post content.
39 *
40 * @return mixed
41 */
42 function jetpack_shortcodereverse_smartframe( $content ) {
43 if ( ! is_string( $content ) || false === stripos( $content, 'embed.smartframe' ) ) {
44 return $content;
45 }
46
47 // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
48 $regexp = '!<script\ssrc="https://embed\.smartframe\.(?:io|net)/(\w+)\.js"\sdata-image-id="(.*?)"(?:\sdata-width="(?:\d+(?:%|px))"\s)?(?:data-max-width="(\d+(%|px)))?"></script>!i';
49 $regexp_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $regexp, ENT_NOQUOTES ) );
50
51 foreach ( compact( 'regexp', 'regexp_ent' ) as $regexp ) {
52 if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) {
53 continue;
54 }
55
56 foreach ( $matches as $match ) {
57 // We need at least a script ID and an image ID.
58 if ( ! isset( $match[1], $match[2] ) ) {
59 continue;
60 }
61 $shortcode = sprintf(
62 '[smartframe script-id="%1$s" image-id="%2$s"%3$s]',
63 esc_attr( $match[1] ),
64 esc_attr( $match[2] ),
65 ! empty( $match[3] ) ? ' max-width="' . esc_attr( $match[3] ) . '"' : ''
66 );
67 $content = str_replace( $match[0], $shortcode, $content );
68 }
69 }
70 /** This action is documented in modules/widgets/social-media-icons.php */
71 do_action( 'jetpack_bump_stats_extras', 'html_to_shortcode', 'smartframe' );
72
73 return $content;
74 }
75
76 /**
77 * Parse shortcode arguments and render its output.
78 *
79 * @since 10.2.0
80 *
81 * @param array $atts Shortcode parameters.
82 *
83 * @return string
84 */
85 function jetpack_smartframe_shortcode( $atts ) {
86 if ( ! empty( $atts['image-id'] ) ) {
87 $image_id = $atts['image-id'];
88 } else {
89 return '<!-- Missing smartframe image-id -->';
90 }
91 if ( ! empty( $atts['script-id'] ) ) {
92 $script_id = $atts['script-id'];
93 } else {
94 return '<!-- Missing smartframe script-id -->';
95 }
96
97 $params = array(
98 // ignore width for now, smartframe embed code has it "100%". % isn't allowed in oembed, making it 100px.
99 // 'width' => isset( $atts['width'] ) ? (int) $atts['width'] : null,.
100 'max-width' => isset( $atts['max-width'] ) ? (int) $atts['max-width'] : null,
101 );
102
103 $embed_url = sprintf(
104 'https://imagecards.smartframe.io/%1$s/%2$s',
105 esc_attr( $script_id ),
106 esc_attr( $image_id )
107 );
108
109 // wrap the embed with wp-block-embed__wrapper, otherwise it would be aligned to the very left of the viewport.
110 return sprintf(
111 '<div class="wp-block-embed__wrapper">%1$s</div>',
112 wp_oembed_get( $embed_url, array_filter( $params ) )
113 );
114 }
115