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-book.php

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

129 lines 3.2 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 http://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
13 /**
14 * Get ID of requested archive.org book embed.
15 *
16 * @since 4.5.0
17 *
18 * @param $atts
19 *
20 * @return int|string
21 */
22 function jetpack_shortcode_get_archiveorg_book_id( $atts ) {
23 if ( isset( $atts[0] ) ) {
24 $atts[0] = trim( $atts[0], '=' );
25 if ( preg_match( '#archive.org/stream/(.+)/?$#i', $atts[0], $match ) ) {
26 $id = $match[1];
27 } else {
28 $id = $atts[0];
29 }
30 return $id;
31 }
32 return 0;
33 }
34
35 /**
36 * Convert an archive.org book 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 book
42 */
43 function jetpack_archiveorg_book_shortcode( $atts ) {
44 global $content_width;
45
46 if ( isset( $atts[0] ) && empty( $atts['id'] ) ) {
47 $atts['id'] = jetpack_shortcode_get_archiveorg_book_id( $atts );
48 }
49
50 $atts = shortcode_atts(
51 array(
52 'id' => '',
53 'width' => 480,
54 'height' => 430,
55 ),
56 $atts
57 );
58
59 if ( ! $atts['id'] ) {
60 return '<!-- error: missing archive.org book ID -->';
61 }
62
63 $id = $atts['id'];
64
65 if ( ! $atts['width'] ) {
66 $width = absint( $content_width );
67 } else {
68 $width = intval( $atts['width'] );
69 }
70
71 if ( ! $atts['height'] ) {
72 $height = round( ( $width / 640 ) * 360 );
73 } else {
74 $height = intval( $atts['height'] );
75 }
76
77 $url = esc_url( set_url_scheme( "http://archive.org/stream/{$id}?ui=embed#mode/1up" ) );
78
79 $html = "<div class='embed-archiveorg-book' style='text-align:center;'><iframe src='$url' width='$width' height='$height' style='border:0;' webkitallowfullscreen='true' mozallowfullscreen='true' allowfullscreen></iframe></div>";
80 return $html;
81 }
82
83 add_shortcode( 'archiveorg-book', 'jetpack_archiveorg_book_shortcode' );
84
85 /**
86 * Compose shortcode from archive.org book iframe.
87 *
88 * @since 4.5.0
89 *
90 * @param string $content
91 *
92 * @return mixed
93 */
94 function jetpack_archiveorg_book_embed_to_shortcode( $content ) {
95 if ( ! is_string( $content ) || false === stripos( $content, 'archive.org/stream/' ) ) {
96 return $content;
97 }
98
99 $regexp = '!<iframe\s+src=[\'"](http|https)://(www.archive|archive)\.org/stream/([^\'"]+)[\'"]((?:\s+\w+(=[\'"][^\'"]*[\'"])?)*)\s></iframe>!i';
100
101 if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) {
102 return $content;
103 }
104
105 foreach ( $matches as $match ) {
106 $url = explode( '?', $match[3] );
107 $id = $url[0];
108
109 $params = $match[4];
110
111 $params = wp_kses_hair( $params, array( 'http' ) );
112
113 $width = isset( $params['width'] ) ? absint( $params['width']['value'] ) : 0;
114 $height = isset( $params['height'] ) ? absint( $params['height']['value'] ) : 0;
115
116 $wh = '';
117 if ( $width && $height ) {
118 $wh = ' width=' . $width . ' height=' . $height;
119 }
120
121 $shortcode = '[archiveorg-book ' . $id . $wh . ']';
122 $content = str_replace( $match[0], $shortcode, $content );
123 }
124
125 return $content;
126 }
127
128 add_filter( 'pre_kses', 'jetpack_archiveorg_book_embed_to_shortcode' );
129