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