Completed
Push — try/user_token-auth-for-rest-a... ( 422660...ea5d0d )
by
unknown
08:47
created

modules/shortcodes/archiveorg.php (2 issues)

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
 * 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 View Code Duplication
function jetpack_shortcode_get_archiveorg_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...
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( array(
51
		'id'       => '',
52
		'width'    => 640,
53
		'height'   => 480,
54
		'autoplay' => 0,
55
		'poster'   => ''
56
	), $atts );
57
58
	if ( ! $atts['id'] ) {
59
		return '<!-- error: missing archive.org ID -->';
60
	}
61
62
	$id = $atts['id'];
63
64 View Code Duplication
	if ( ! $atts['width'] ) {
65
		$width = absint( $content_width );
66
	} else {
67
		$width = intval( $atts['width'] );
68
	}
69
70 View Code Duplication
	if ( ! $atts['height'] ) {
71
		$height = round( ( $width / 640 ) * 360 );
72
	} else {
73
		$height = intval( $atts['height'] );
74
	}
75
76
	if ( $atts['autoplay'] ) {
77
		$autoplay = '&autoplay=1';
78
	} else {
79
		$autoplay = '';
80
	}
81
82
	if ( $atts['poster'] ) {
83
		$poster = '&poster=' . $atts['poster'];
84
	} else {
85
		$poster = '';
86
	}
87
88
	$url = esc_url( set_url_scheme( "https://archive.org/embed/{$id}{$autoplay}{$poster}" ) );
89
90
	$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>";
91
92
	return $html;
93
}
94
95
add_shortcode( 'archiveorg', 'jetpack_archiveorg_shortcode' );
96
97
/**
98
 * Compose shortcode from archive.org iframe.
99
 *
100
 * @since 4.5.0
101
 *
102
 * @param string $content
103
 *
104
 * @return mixed
105
 */
106
function jetpack_archiveorg_embed_to_shortcode( $content ) {
107
	if ( ! is_string( $content ) || false === stripos( $content, 'archive.org/embed/' ) ) {
108
		return $content;
109
	}
110
111
	$regexp = '!<iframe\s+src=[\'"]https?://archive\.org/embed/([^\'"]+)[\'"]((?:\s+\w+(=[\'"][^\'"]*[\'"])?)*)></iframe>!i';
112
113
	if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) {
114
		return $content;
115
	}
116
117
	foreach ( $matches as $match ) {
118
		$url = explode( '&amp;', $match[1] );
119
		$id = 'id=' . $url[0];
120
121
		$autoplay = '';
122
		$poster = '';
123
		for ( $ii = 1; $ii < count( $url ); $ii++ ) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
124
			if ( 'autoplay=1' === $url[$ii] ) {
125
				$autoplay = ' autoplay="1"';
126
			}
127
128
			$map_matches = array();
129
			if ( preg_match( '/^poster=(.+)$/', $url[$ii], $map_matches ) ) {
130
				$poster = " poster=\"{$map_matches[1]}\"";
131
			}
132
		}
133
134
		$params = $match[2];
135
136
		$params = wp_kses_hair( $params, array( 'http' ) );
137
138
		$width = isset( $params['width'] ) ? (int) $params['width']['value'] : 0;
139
		$height = isset( $params['height'] ) ? (int) $params['height']['value'] : 0;
140
141
		$wh = '';
142
		if ( $width && $height ) {
143
			$wh = ' width=' . $width . ' height=' . $height;
144
		}
145
146
		$shortcode = '[archiveorg ' . $id . $wh . $autoplay . $poster . ']';
147
		$content = str_replace( $match[0], $shortcode, $content );
148
	}
149
150
	return $content;
151
}
152
153
add_filter( 'pre_kses', 'jetpack_archiveorg_embed_to_shortcode' );
154