PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 13.0.1
Jetpack – WP Security, Backup, Speed, & Growth v13.0.1
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 14.4.2 All 500 releases
jetpack / modules / shortcodes / archiveorg.php
archiveorg.php
163 lines 3.9 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 /**
16 * Get ID of requested archive.org embed.
17 *
18 * @since 4.5.0
19 *
20 * @param array $atts Shortcode attributes.
21 *
22 * @return int|string
23 */
24 function jetpack_shortcode_get_archiveorg_id( $atts ) {
25 if ( isset( $atts[0] ) ) {
26 $atts[0] = trim( $atts[0], '=' );
27 if ( preg_match( '#archive.org/(details|embed)/(.+)/?$#i', $atts[0], $match ) ) {
28 $id = $match[2];
29 } else {
30 $id = $atts[0];
31 }
32 return $id;
33 }
34 return 0;
35 }
36
37 /**
38 * Convert an archive.org shortcode into an embed code.
39 *
40 * @since 4.5.0
41 *
42 * @param array $atts An array of shortcode attributes.
43 * @return string The embed code for the archive.org video.
44 */
45 function jetpack_archiveorg_shortcode( $atts ) {
46 global $content_width;
47
48 if ( isset( $atts[0] ) && empty( $atts['id'] ) ) {
49 $atts['id'] = jetpack_shortcode_get_archiveorg_id( $atts );
50 }
51
52 $atts = shortcode_atts(
53 array(
54 'id' => '',
55 'width' => 640,
56 'height' => 480,
57 'autoplay' => 0,
58 'poster' => '',
59 ),
60 $atts
61 );
62
63 if ( ! $atts['id'] ) {
64 return '<!-- error: missing archive.org ID -->';
65 }
66
67 $id = $atts['id'];
68
69 if ( ! $atts['width'] ) {
70 $width = absint( $content_width );
71 } else {
72 $width = (int) $atts['width'];
73 }
74
75 if ( ! $atts['height'] ) {
76 $height = round( ( $width / 640 ) * 360 );
77 } else {
78 $height = (int) $atts['height'];
79 }
80
81 if ( $atts['autoplay'] ) {
82 $autoplay = '&autoplay=1';
83 } else {
84 $autoplay = '';
85 }
86
87 if ( $atts['poster'] ) {
88 $poster = '&poster=' . $atts['poster'];
89 } else {
90 $poster = '';
91 }
92
93 return sprintf(
94 '<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>',
95 esc_attr__( 'Archive.org', 'jetpack' ),
96 esc_url( "https://archive.org/embed/{$id}{$autoplay}{$poster}" ),
97 esc_attr( $width ),
98 esc_attr( $height )
99 );
100 }
101
102 add_shortcode( 'archiveorg', 'jetpack_archiveorg_shortcode' );
103
104 /**
105 * Compose shortcode from archive.org iframe.
106 *
107 * @since 4.5.0
108 *
109 * @param string $content Post content.
110 *
111 * @return mixed
112 */
113 function jetpack_archiveorg_embed_to_shortcode( $content ) {
114 if ( ! is_string( $content ) || false === stripos( $content, 'archive.org/embed/' ) ) {
115 return $content;
116 }
117
118 $regexp = '!<iframe\s+src=[\'"]https?://archive\.org/embed/([^\'"]+)[\'"]((?:\s+\w+(=[\'"][^\'"]*[\'"])?)*)></iframe>!i';
119
120 if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) {
121 return $content;
122 }
123
124 foreach ( $matches as $match ) {
125 $url = explode( '&amp;', $match[1] );
126 $id = 'id=' . $url[0];
127
128 $autoplay = '';
129 $poster = '';
130 $url_count = count( $url );
131
132 for ( $ii = 1; $ii < $url_count; $ii++ ) {
133 if ( 'autoplay=1' === $url[ $ii ] ) {
134 $autoplay = ' autoplay="1"';
135 }
136
137 $map_matches = array();
138 if ( preg_match( '/^poster=(.+)$/', $url[ $ii ], $map_matches ) ) {
139 $poster = " poster=\"{$map_matches[1]}\"";
140 }
141 }
142
143 $params = $match[2];
144
145 $params = wp_kses_hair( $params, array( 'http' ) );
146
147 $width = isset( $params['width'] ) ? (int) $params['width']['value'] : 0;
148 $height = isset( $params['height'] ) ? (int) $params['height']['value'] : 0;
149
150 $wh = '';
151 if ( $width && $height ) {
152 $wh = ' width=' . $width . ' height=' . $height;
153 }
154
155 $shortcode = '[archiveorg ' . $id . $wh . $autoplay . $poster . ']';
156 $content = str_replace( $match[0], $shortcode, $content );
157 }
158
159 return $content;
160 }
161
162 add_filter( 'pre_kses', 'jetpack_archiveorg_embed_to_shortcode' );
163