Completed
Push — fusion-sync/danroundhill/r2061... ( 6c7cf7...0f1caf )
by Jeremy
213:37 queued 206:26
created

kickstarter.php ➔ jetpack_kickstarter_shortcode()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * Kickstarter shortcode
4
 *
5
 * Usage:
6
 * [kickstarter url="https://www.kickstarter.com/projects/peaktoplateau/yak-wool-baselayers-from-tibet-to-the-world" width="480" height=""]
7
 *
8
 * @package Jetpack
9
 */
10
11
add_shortcode( 'kickstarter', 'jetpack_kickstarter_shortcode' );
12
add_filter( 'pre_kses', 'jetpack_kickstarter_embed_to_shortcode' );
13
14
/**
15
 * Parse shortcode arguments and render its output.
16
 *
17
 * @since 4.5.0
18
 *
19
 * @param array $atts Shortcode parameters.
20
 *
21
 * @return string
22
 */
23
function jetpack_kickstarter_shortcode( $atts ) {
24
	if ( empty( $atts['url'] ) ) {
25
		return '';
26
	}
27
28
	$url = esc_url_raw( $atts['url'] );
29
	if ( ! preg_match( '#^(www\.)?kickstarter\.com$#i', wp_parse_url( $url, PHP_URL_HOST ) ) ) {
0 ignored issues
show
Unused Code introduced by
The call to wp_parse_url() has too many arguments starting with PHP_URL_HOST.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
30
		return '<!-- Invalid Kickstarter URL -->';
31
	}
32
33
	global $wp_embed;
34
	return $wp_embed->shortcode( $atts, $url );
35
}
36
37
/**
38
 * Converts Kickstarter iframe embeds to a shortcode.
39
 *
40
 * EG: <iframe width="480" height="360" src="http://www.kickstarter.com/projects/deweymac/dewey-mac-kid-detective-book-make-diy-and-stem-spy/widget/video.html" frameborder="0" scrolling="no"> </iframe>
41
 *
42
 * @since 4.5.0
43
 *
44
 * @param string $content Entry content that possibly includes a Kickstarter embed.
45
 *
46
 * @return string
47
 */
48
function jetpack_kickstarter_embed_to_shortcode( $content ) {
49
	if ( ! is_string( $content ) || false === stripos( $content, 'www.kickstarter.com/projects' ) ) {
50
		return $content;
51
	}
52
53
	$regexp     = '!<iframe((?:\s+\w+=[\'"][^\'"]*[\'"])*)\s+src=[\'"](http://www\.kickstarter\.com/projects/[^/]+/[^/]+)/[^\'"]+[\'"]((?:\s+\w+=[\'"][^\'"]*[\'"])*)>[\s]*</iframe>!i';
54
	$regexp_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $regexp, ENT_NOQUOTES ) ); // phpcs:ignore
55
56
	foreach ( array( 'regexp', 'regexp_ent' ) as $reg ) {
57
		if ( ! preg_match_all( $$reg, $content, $matches, PREG_SET_ORDER ) ) {
58
			continue;
59
		}
60
61
		foreach ( $matches as $match ) {
62
			$url = esc_url( $match[2] );
63
64
			$params = $match[1] . $match[3];
65
66
			if ( 'regexp_ent' === $reg ) {
67
				$params = html_entity_decode( $params );
68
			}
69
70
			$params = wp_kses_hair( $params, array( 'http' ) );
71
72
			$width = isset( $params['width'] ) ? (int) $params['width']['value'] : 0;
73
74
			$shortcode = '[kickstarter url=' . $url . ( ( ! empty( $width ) ) ? " width=$width" : '' ) . ']';
75
			$content   = str_replace( $match[0], $shortcode, $content );
76
		}
77
	}
78
79
	return $content;
80
}
81