PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.5.1
Jetpack – WP Security, Backup, Speed, & Growth v7.5.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
161 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 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 = intval( $atts['width'] );
73 }
74
75 if ( ! $atts['height'] ) {
76 $height = round( ( $width / 640 ) * 360 );
77 } else {
78 $height = intval( $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 $url = esc_url( set_url_scheme( "https://archive.org/embed/{$id}{$autoplay}{$poster}" ) );
94
95 $html = "<div class='embed-archiveorg' style='text-align:center;'><iframe src='$url' width='$width' height='$height' style='border:0;' webkitallowfullscreen='true' mozallowfullscreen='true' allowfullscreen></iframe></div>";
96
97 return $html;
98 }
99
100 add_shortcode( 'archiveorg', 'jetpack_archiveorg_shortcode' );
101
102 /**
103 * Compose shortcode from archive.org iframe.
104 *
105 * @since 4.5.0
106 *
107 * @param string $content Post content.
108 *
109 * @return mixed
110 */
111 function jetpack_archiveorg_embed_to_shortcode( $content ) {
112 if ( ! is_string( $content ) || false === stripos( $content, 'archive.org/embed/' ) ) {
113 return $content;
114 }
115
116 $regexp = '!<iframe\s+src=[\'"]https?://archive\.org/embed/([^\'"]+)[\'"]((?:\s+\w+(=[\'"][^\'"]*[\'"])?)*)></iframe>!i';
117
118 if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) {
119 return $content;
120 }
121
122 foreach ( $matches as $match ) {
123 $url = explode( '&amp;', $match[1] );
124 $id = 'id=' . $url[0];
125
126 $autoplay = '';
127 $poster = '';
128 $url_count = count( $url );
129
130 for ( $ii = 1; $ii < $url_count; $ii++ ) {
131 if ( 'autoplay=1' === $url[ $ii ] ) {
132 $autoplay = ' autoplay="1"';
133 }
134
135 $map_matches = array();
136 if ( preg_match( '/^poster=(.+)$/', $url[ $ii ], $map_matches ) ) {
137 $poster = " poster=\"{$map_matches[1]}\"";
138 }
139 }
140
141 $params = $match[2];
142
143 $params = wp_kses_hair( $params, array( 'http' ) );
144
145 $width = isset( $params['width'] ) ? (int) $params['width']['value'] : 0;
146 $height = isset( $params['height'] ) ? (int) $params['height']['value'] : 0;
147
148 $wh = '';
149 if ( $width && $height ) {
150 $wh = ' width=' . $width . ' height=' . $height;
151 }
152
153 $shortcode = '[archiveorg ' . $id . $wh . $autoplay . $poster . ']';
154 $content = str_replace( $match[0], $shortcode, $content );
155 }
156
157 return $content;
158 }
159
160 add_filter( 'pre_kses', 'jetpack_archiveorg_embed_to_shortcode' );
161