PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 6.7
Jetpack – WP Security, Backup, Speed, & Growth v6.7
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 6.7, at modules/shortcodes/archiveorg.php

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