PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 10.5.3
Jetpack – WP Security, Backup, Speed, & Growth v10.5.3
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-book.php
archiveorg-book.php
134 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Archive.org Shortcode
4 *
5 * Usage:
6 * [archiveorg-book goodytwoshoes00newyiala]
7 * [archiveorg-book https://www.archive.org/stream/goodytwoshoes00newyiala]
8 * [archiveorg id=goodytwoshoes00newyiala width=480 height=430]
9
10 * <iframe src="https://www.archive.org/stream/goodytwoshoes00newyiala?ui=embed#mode/1up" width="480px" height="430px" frameborder="0" ></iframe>
11 *
12 * @package automattic/jetpack
13 */
14
15 /**
16 * Get ID of requested archive.org book 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_book_id( $atts ) {
25 if ( isset( $atts[0] ) ) {
26 $atts[0] = trim( $atts[0], '=' );
27 if ( preg_match( '#archive.org/stream/(.+)/?$#i', $atts[0], $match ) ) {
28 $id = $match[1];
29 } else {
30 $id = $atts[0];
31 }
32 return $id;
33 }
34 return 0;
35 }
36
37 /**
38 * Convert an archive.org book 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 book
44 */
45 function jetpack_archiveorg_book_shortcode( $atts ) {
46 global $content_width;
47
48 if ( isset( $atts[0] ) && empty( $atts['id'] ) ) {
49 $atts['id'] = jetpack_shortcode_get_archiveorg_book_id( $atts );
50 }
51
52 $atts = shortcode_atts(
53 array(
54 'id' => '',
55 'width' => 480,
56 'height' => 430,
57 ),
58 $atts
59 );
60
61 if ( ! $atts['id'] ) {
62 return '<!-- error: missing archive.org book ID -->';
63 }
64
65 $id = $atts['id'];
66
67 if ( ! $atts['width'] ) {
68 $width = absint( $content_width );
69 } else {
70 $width = (int) $atts['width'];
71 }
72
73 if ( ! $atts['height'] ) {
74 $height = round( ( $width / 640 ) * 360 );
75 } else {
76 $height = (int) $atts['height'];
77 }
78
79 return sprintf(
80 '<div class="embed-archiveorg-book" style="text-align:center;"><iframe title="%s" src="%s" width="%s" height="%s" style="border:0;" webkitallowfullscreen="true" mozallowfullscreen="true" allowfullscreen></iframe></div>',
81 esc_attr__( 'Archive.org Book', 'jetpack' ),
82 esc_url( "https://archive.org/stream/{$id}?ui=embed#mode/1up" ),
83 esc_attr( $width ),
84 esc_attr( $height )
85 );
86 }
87
88 add_shortcode( 'archiveorg-book', 'jetpack_archiveorg_book_shortcode' );
89
90 /**
91 * Compose shortcode from archive.org book iframe.
92 *
93 * @since 4.5.0
94 *
95 * @param string $content Post content.
96 *
97 * @return mixed
98 */
99 function jetpack_archiveorg_book_embed_to_shortcode( $content ) {
100 if ( ! is_string( $content ) || false === stripos( $content, 'archive.org/stream/' ) ) {
101 return $content;
102 }
103
104 $regexp = '!<iframe\s+src=[\'"](http|https)://(www.archive|archive)\.org/stream/([^\'"]+)[\'"]((?:\s+\w+(=[\'"][^\'"]*[\'"])?)*)\s></iframe>!i';
105
106 if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) {
107 return $content;
108 }
109
110 foreach ( $matches as $match ) {
111 $url = explode( '?', $match[3] );
112 $id = $url[0];
113
114 $params = $match[4];
115
116 $params = wp_kses_hair( $params, array( 'http' ) );
117
118 $width = isset( $params['width'] ) ? absint( $params['width']['value'] ) : 0;
119 $height = isset( $params['height'] ) ? absint( $params['height']['value'] ) : 0;
120
121 $wh = '';
122 if ( $width && $height ) {
123 $wh = ' width=' . $width . ' height=' . $height;
124 }
125
126 $shortcode = '[archiveorg-book ' . $id . $wh . ']';
127 $content = str_replace( $match[0], $shortcode, $content );
128 }
129
130 return $content;
131 }
132
133 add_filter( 'pre_kses', 'jetpack_archiveorg_book_embed_to_shortcode' );
134