PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.2-beta
Jetpack – WP Security, Backup, Speed, & Growth v16.2-beta
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 / archiveorg.php

archiveorg.php in Jetpack – WP Security, Backup, Speed, & Growth 16.2-beta, at modules/shortcodes/archiveorg.php

194 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Archive.org book shortcode.
4 *
5 * Usage:
6 * [archiveorg Experime1940]
7 * [archiveorg http://archive.org/details/Experime1940 poster=http://archive.org/images/map.png]
8 * [archiveorg id=Experime1940 width=640 height=480 autoplay=1]
9
10 * <iframe src="http://archive.org/embed/Experime1940&autoplay=1&poster=http://archive.org/images/map.png" width="640" height="480" frameborder="0" webkitallowfullscreen="true" mozallowfullscreen="true" allowfullscreen></iframe>
11 *
12 * @package automattic/jetpack
13 */
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit( 0 );
17 }
18
19 /**
20 * Get ID of requested archive.org embed.
21 *
22 * @since 4.5.0
23 *
24 * @param array $atts Shortcode attributes.
25 *
26 * @return int|string
27 */
28 function jetpack_shortcode_get_archiveorg_id( $atts ) {
29 if ( isset( $atts[0] ) ) {
30 $atts[0] = trim( $atts[0], '=' );
31 if ( preg_match( '#archive.org/(details|embed)/(.+)/?$#i', $atts[0], $match ) ) {
32 $id = $match[2];
33 } else {
34 $id = $atts[0];
35 }
36 return $id;
37 }
38 return 0;
39 }
40
41 /**
42 * Convert an archive.org shortcode into an embed code.
43 *
44 * @since 4.5.0
45 *
46 * @param array $atts An array of shortcode attributes.
47 * @return string The embed code for the archive.org video.
48 */
49 function jetpack_archiveorg_shortcode( $atts ) {
50 global $content_width;
51
52 if ( isset( $atts[0] ) && empty( $atts['id'] ) ) {
53 $atts['id'] = jetpack_shortcode_get_archiveorg_id( $atts );
54 }
55
56 $atts = shortcode_atts(
57 array(
58 'id' => '',
59 'width' => 640,
60 'height' => 480,
61 'autoplay' => 0,
62 'poster' => '',
63 'playlist' => 0,
64 ),
65 $atts
66 );
67
68 if ( ! $atts['id'] ) {
69 return '<!-- error: missing archive.org ID -->';
70 }
71
72 $id = $atts['id'];
73
74 // Allow extra query parameters to be baked into the identifier, e.g. "myitem&playlist=1" or "myitem?playlist=1".
75 // In some environments a the_content filter encodes "&" to "&amp;" before do_shortcode runs, so the
76 // parser receives "myitem&amp;playlist=1" — normalize that back before splitting. The sibling function
77 // jetpack_archiveorg_embed_to_shortcode() splits on "&amp;" for the same reason.
78 $id = str_replace( '&amp;', '&', $id );
79 $id_extra_args = array();
80 if ( preg_match( '/^([^?&]*)[?&](.*)$/', $id, $id_match ) ) {
81 $id = $id_match[1];
82 wp_parse_str( $id_match[2], $id_extra_args );
83 }
84
85 // Re-check after the split — an identifier that's only a query string (e.g. "?playlist=1") leaves $id empty
86 // and would otherwise produce an item-less embed URL.
87 if ( '' === $id ) {
88 return '<!-- error: missing archive.org ID -->';
89 }
90
91 if ( ! $atts['width'] ) {
92 $width = absint( $content_width );
93 } else {
94 $width = (int) $atts['width'];
95 }
96
97 if ( ! $atts['height'] ) {
98 $height = round( ( $width / 640 ) * 360 );
99 } else {
100 $height = (int) $atts['height'];
101 }
102
103 $query_args = array();
104 if ( $atts['autoplay'] ) {
105 $query_args['autoplay'] = 1;
106 }
107 if ( $atts['poster'] ) {
108 $query_args['poster'] = $atts['poster'];
109 }
110 if ( $atts['playlist'] ) {
111 $query_args['playlist'] = 1;
112 }
113
114 // Explicit shortcode attributes take precedence over query parameters baked into the identifier.
115 $query_args = array_merge( $id_extra_args, $query_args );
116
117 $url = 'https://archive.org/embed/' . $id;
118 if ( ! empty( $query_args ) ) {
119 $url = add_query_arg( $query_args, $url );
120 }
121
122 return sprintf(
123 '<div class="embed-archiveorg" style="text-align:center;"><iframe title="%s" src="%s" width="%s" height="%s" style="border:0;" webkitallowfullscreen="true" mozallowfullscreen="true" allowfullscreen></iframe></div>',
124 esc_attr__( 'Archive.org', 'jetpack' ),
125 esc_url( $url ),
126 esc_attr( $width ),
127 esc_attr( $height )
128 );
129 }
130
131 add_shortcode( 'archiveorg', 'jetpack_archiveorg_shortcode' );
132
133 /**
134 * Compose shortcode from archive.org iframe.
135 *
136 * @since 4.5.0
137 *
138 * @param string $content Post content.
139 *
140 * @return mixed
141 */
142 function jetpack_archiveorg_embed_to_shortcode( $content ) {
143 if ( ! is_string( $content ) || false === stripos( $content, 'archive.org/embed/' ) ) {
144 return $content;
145 }
146
147 $regexp = '!<iframe\s+src=[\'"]https?://archive\.org/embed/([^\'"]+)[\'"]((?:\s+\w+(=[\'"][^\'"]*[\'"])?)*)></iframe>!i';
148
149 if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) {
150 return $content;
151 }
152
153 foreach ( $matches as $match ) {
154 $url = explode( '&amp;', $match[1] );
155 $id = 'id=' . $url[0];
156
157 $autoplay = '';
158 $poster = '';
159 $url_count = count( $url );
160
161 for ( $ii = 1; $ii < $url_count; $ii++ ) {
162 if ( 'autoplay=1' === $url[ $ii ] ) {
163 $autoplay = ' autoplay="1"';
164 }
165
166 $map_matches = array();
167 if ( preg_match( '/^poster=(.+)$/', $url[ $ii ], $map_matches ) ) {
168 $poster = " poster=\"{$map_matches[1]}\"";
169 }
170 }
171
172 $params = $match[2];
173
174 $params = wp_kses_hair( $params, array( 'http' ) );
175
176 $width = isset( $params['width'] ) ? (int) $params['width']['value'] : 0;
177 $height = isset( $params['height'] ) ? (int) $params['height']['value'] : 0;
178
179 $wh = '';
180 if ( $width && $height ) {
181 $wh = ' width=' . $width . ' height=' . $height;
182 }
183
184 $shortcode = '[archiveorg ' . $id . $wh . $autoplay . $poster . ']';
185 $content = str_replace( $match[0], $shortcode, $content );
186 }
187
188 return $content;
189 }
190
191 if ( jetpack_shortcodes_should_hook_pre_kses() ) {
192 add_filter( 'pre_kses', 'jetpack_archiveorg_embed_to_shortcode' );
193 }
194