Completed
Push — add/react-videopress-settings ( 1a2dcb...a2519f )
by
unknown
275:56 queued 263:52
created

blip.php ➔ blip_embed_to_shortcode()   D

Complexity

Conditions 9
Paths 4

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 19
nc 4
nop 1
dl 0
loc 29
rs 4.909
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Blip.tv embed code:
5
 * <embed src="http://blip.tv/play/g8sVgpfaCgI%2Em4v" type="application/x-shockwave-flash" width="480" height="255" allowscriptaccess="always" allowfullscreen="true"></embed>
6
 * Blip.tv shortcode is: [blip.tv url-or-something-else]
7
 * */
8
9
function blip_embed_to_shortcode( $content ) {
10
	if ( ! is_string( $content ) || false === stripos( $content, '/blip.tv/play/' ) ) {
11
		return $content;
12
	}
13
14
	$regexp = '!<embed((?:\s+\w+="[^"]*")*)\s+src="http(?:\:|&#0*58;)//(blip\.tv/play/[^"]*)"((?:\s+\w+="[^"]*")*)\s*(?:/>|>\s*</embed>)!';
15
	$regexp_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $regexp, ENT_NOQUOTES ) );
16
17
	foreach ( array( 'regexp', 'regexp_ent' ) as $reg ) {
18
		if ( ! preg_match_all( $$reg, $content, $matches, PREG_SET_ORDER ) ) {
19
			continue;
20
		}
21
22
		foreach ( $matches as $match ) {
23
			$src = 'http://' . html_entity_decode( $match[2] );
24
			$params = $match[1] . $match[3];
25
			if ( 'regexp_ent' == $reg ) {
26
				$src = html_entity_decode( $src );
27
				$params = html_entity_decode( $params );
28
			}
29
			$params = wp_kses_hair( $params, array( 'http' ) );
30
			if ( ! isset( $params['type'] ) || 'application/x-shockwave-flash' != $params['type']['value'] )
31
				continue;
32
33
			$content = str_replace( $match[0], "[blip.tv $src]", $content );
34
		}
35
	}
36
	return $content;
37
}
38
add_filter( 'pre_kses', 'blip_embed_to_shortcode' );
39
40
// [blip.tv ?posts_id=4060324&dest=-1]
41
// [blip.tv http://blip.tv/play/hpZTgffqCAI%2Em4v] // WLS
42
43
function blip_shortcode( $atts ) {
44
	if ( ! isset( $atts[0] ) )
45
		return '';
46
	$src = $atts[0];
47
48
	if ( preg_match( '/^\?posts_id=(\d+)&[^d]*dest=(-?\d+)$/', $src, $matches ) )
49
		return "<script type='text/javascript' src='http://blip.tv/syndication/write_player?skin=js&posts_id={$matches[1]}&cross_post_destination={$matches[2]}&view=full_js'></script>";
50
	elseif ( preg_match( '|^http://blip.tv/play/[.\w]+$|', urldecode( $src ) ) ) // WLS
51
		return "<embed src='$src' type='application/x-shockwave-flash' width='480' height='300' allowscriptaccess='never' allowfullscreen='true'></embed>";
52
53
54
	return "<!--blip.tv pattern not matched -->";
55
}
56
57
add_shortcode( 'blip.tv', 'blip_shortcode' );
58