Completed
Push — fix/sync_published_post_action ( 131f15...e65cb4 )
by
unknown
09:19 queued 01:28
created

modules/shortcodes/archiveorg-book.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/*
3
 * WARNING: This file is distributed verbatim in Jetpack.
4
 * There should be nothing WordPress.com specific in this file.
5
 *
6
 * @hide-in-jetpack
7
 */
8
9
/*
10
[archiveorg-book goodytwoshoes00newyiala]
11
[archiveorg-book http://www.archive.org/stream/goodytwoshoes00newyiala]
12
[archiveorg id=goodytwoshoes00newyiala width=480 height=430]
13
14
<iframe src='https://www.archive.org/stream/goodytwoshoes00newyiala?ui=embed#mode/1up' width='480px' height='430px' frameborder='0' ></iframe>
15
*/
16
17
/**
18
 * Get ID of requested archive.org book embed.
19
 *
20
 * @since 4.5.0
21
 *
22
 * @param $atts
23
 *
24
 * @return int|string
25
 */
26 View Code Duplication
function jetpack_shortcode_get_archiveorg_book_id( $atts ) {
0 ignored issues
show
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

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