PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3
16.3 16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 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 All 508 releases
← All changes | modules/shortcodes/smartframe.php +120 -0 16.2-beta → 16.3 View file →
@@ -1,0 +1,120 @@
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( 'ABSPATH' ) ) {
12 + exit( 0 );
13 +}
14 +
15 +if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
16 + add_action( 'init', 'jetpack_smartframe_enable_embeds' );
17 +} else {
18 + jetpack_smartframe_enable_embeds();
19 +}
20 +
21 +/**
22 + * Register smartframe as oembed provider. Add filter to reverse iframes to shortcode. Register [smartframe] shortcode.
23 + *
24 + * @since 10.2.0
25 + */
26 +function jetpack_smartframe_enable_embeds() {
27 + // Support their oEmbed Endpoint.
28 + wp_oembed_add_provider( '#https?://(.*?)\.smartframe\.(io|net)/.*#i', 'https://oembed.smartframe.io/', 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_smartframe' );
33 + }
34 +
35 + // Actually display the smartframe Embed.
36 + add_shortcode( 'smartframe', 'jetpack_smartframe_shortcode' );
37 +}
38 +
39 +/**
40 + * Compose shortcode based on smartframe iframes.
41 + *
42 + * @since 10.2.0
43 + *
44 + * @param string $content Post content.
45 + *
46 + * @return mixed
47 + */
48 +function jetpack_shortcodereverse_smartframe( $content ) {
49 + if ( ! is_string( $content ) || false === stripos( $content, 'embed.smartframe' ) ) {
50 + return $content;
51 + }
52 +
53 + // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
54 + $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';
55 + $regexp_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $regexp, ENT_NOQUOTES ) );
56 +
57 + foreach ( compact( 'regexp', 'regexp_ent' ) as $regexp ) {
58 + if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) {
59 + continue;
60 + }
61 +
62 + foreach ( $matches as $match ) {
63 + // We need at least a script ID and an image ID.
64 + if ( ! isset( $match[1] ) || ! isset( $match[2] ) ) {
65 + continue;
66 + }
67 + $shortcode = sprintf(
68 + '[smartframe script-id="%1$s" image-id="%2$s"%3$s]',
69 + esc_attr( $match[1] ),
70 + esc_attr( $match[2] ),
71 + ! empty( $match[3] ) ? ' max-width="' . esc_attr( $match[3] ) . '"' : ''
72 + );
73 + $content = str_replace( $match[0], $shortcode, $content );
74 + }
75 + }
76 + /** This action is documented in modules/widgets/social-media-icons.php */
77 + do_action( 'jetpack_bump_stats_extras', 'html_to_shortcode', 'smartframe' );
78 +
79 + return $content;
80 +}
81 +
82 +/**
83 + * Parse shortcode arguments and render its output.
84 + *
85 + * @since 10.2.0
86 + *
87 + * @param array $atts Shortcode parameters.
88 + *
89 + * @return string
90 + */
91 +function jetpack_smartframe_shortcode( $atts ) {
92 + if ( ! empty( $atts['image-id'] ) ) {
93 + $image_id = $atts['image-id'];
94 + } else {
95 + return '<!-- Missing smartframe image-id -->';
96 + }
97 + if ( ! empty( $atts['script-id'] ) ) {
98 + $script_id = $atts['script-id'];
99 + } else {
100 + return '<!-- Missing smartframe script-id -->';
101 + }
102 +
103 + $params = array(
104 + // ignore width for now, smartframe embed code has it "100%". % isn't allowed in oembed, making it 100px.
105 + // 'width' => isset( $atts['width'] ) ? (int) $atts['width'] : null,.
106 + 'max-width' => isset( $atts['max-width'] ) ? (int) $atts['max-width'] : null,
107 + );
108 +
109 + $embed_url = sprintf(
110 + 'https://imagecards.smartframe.io/%1$s/%2$s',
111 + esc_attr( $script_id ),
112 + esc_attr( $image_id )
113 + );
114 +
115 + // wrap the embed with wp-block-embed__wrapper, otherwise it would be aligned to the very left of the viewport.
116 + return sprintf(
117 + '<div class="wp-block-embed__wrapper">%1$s</div>',
118 + wp_oembed_get( $embed_url, array_filter( $params ) )
119 + );
120 +}